The Pedigree Project  0.1
UserManager.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "UserManager.h"
21 #include "Group.h"
22 #include "User.h"
23 #include "modules/Module.h"
24 #include "modules/system/config/Config.h"
25 #include "pedigree/kernel/Log.h"
26 #include "pedigree/kernel/process/Process.h"
27 #include "pedigree/kernel/process/Thread.h"
28 #include "pedigree/kernel/processor/Processor.h"
29 #include "pedigree/kernel/processor/ProcessorInformation.h"
30 #include "pedigree/kernel/utilities/Iterator.h"
31 #include "pedigree/kernel/utilities/utility.h"
32 
33 UserManager UserManager::m_Instance;
34 
35 UserManager::UserManager() : m_Users(), m_Groups()
36 {
37 }
38 
39 UserManager::~UserManager()
40 {
41 }
42 
43 void UserManager::initialiseGroups()
44 {
45  Config::Result *pResult = Config::instance().query("select * from groups");
46  if (!pResult->succeeded())
47  ERROR("Error: " << pResult->errorMessage());
48 
49  for (size_t i = 0; i < pResult->rows(); i++)
50  {
51  uint32_t gid;
52  String name;
53 
54  gid = pResult->getNum(i, "gid") - 1;
55  name = pResult->getStr(i, "name");
56 
57  addGroup(gid, name);
58  }
59 
60  delete pResult;
61 }
62 
63 void UserManager::initialiseUsers()
64 {
65  Config::Result *pResult = Config::instance().query("select * from users");
66  if (!pResult->succeeded())
67  ERROR("Error: " << pResult->errorMessage());
68 
69  for (size_t i = 0; i < pResult->rows(); i++)
70  {
71  uint32_t uid;
72  String username, fullname, groupname, homedir, shell, password;
73 
74  uid = pResult->getNum(i, "uid") - 1;
75  username = pResult->getStr(i, "username");
76  fullname = pResult->getStr(i, "fullname");
77  groupname = pResult->getStr(i, "groupname");
78  homedir = pResult->getStr(i, "homedir");
79  shell = pResult->getStr(i, "shell");
80  password = pResult->getStr(i, "password");
81 
82  addUser(uid, username, fullname, groupname, homedir, shell, password);
83  }
84 
85  delete pResult;
86 }
87 
89 {
90  return m_Users.lookup(id);
91 }
92 
94 {
96  it != m_Users.end(); it++)
97  {
98  User *pU = it.value();
99  if (pU->getUsername() == name)
100  return pU;
101  }
102  return 0;
103 }
104 
106 {
107  return m_Groups.lookup(id);
108 }
109 
111 {
113  it != m_Groups.end(); it++)
114  {
115  Group *pG = it.value();
116  if (pG->getName() == name)
117  return pG;
118  }
119  return 0;
120 }
121 
122 void UserManager::addUser(
123  size_t uid, String username, String fullName, String group, String home,
124  String shell, String password)
125 {
126  // Check for duplicates.
127  if (getUser(uid))
128  {
129  WARNING(
130  "USERS: Not inserting user '" << username << "' with uid " << uid
131  << ": duplicate.");
132  return;
133  }
134 
135  // Check that the given group exists.
136  Group *pGroup = getGroup(group);
137  if (!pGroup)
138  {
139  WARNING(
140  "USERS: Not inserting user '" << username << "': group '" << group
141  << "' does not exist.");
142  return;
143  }
144 
145  NOTICE(
146  "USERS: Adding user {" << uid << ",'" << username << "','" << fullName
147  << "'}");
148  User *pUser =
149  new User(uid, username, fullName, pGroup, home, shell, password);
150  pGroup->join(pUser);
151  m_Users.insert(uid, pUser);
152 }
153 
154 void UserManager::addGroup(size_t gid, String name)
155 {
156  // Check for duplicates.
157  if (getGroup(gid))
158  {
159  WARNING(
160  "USERS: Not inserting group '" << name << "' with gid " << gid
161  << ": duplicate.");
162  return;
163  }
164 
165  NOTICE("USERS: Adding group {" << gid << ",'" << name << "'}");
166  Group *pGroup = new Group(gid, name);
167  m_Groups.insert(gid, pGroup);
168 }
169 
171 {
172 #ifdef INSTALLER
173  addGroup(0, String("root"));
174  addUser(
175  0, String("root"), String("root"), String("root"), String("/"),
176  String("/applications/bash"), String(""));
177 #else
178  initialiseGroups();
179  initialiseUsers();
180 #endif
181 
182  User *pUser = getUser(0);
183  if (!pUser)
184  {
185  FATAL("USERS: Unable to set default user (no such user for uid 0)");
186  return;
187  }
188  Process *pProcess =
189  Processor::information().getCurrentThread()->getParent();
190 
191  pProcess->setUser(pUser);
192  pProcess->setGroup(pUser->getDefaultGroup());
193 }
194 
195 static bool init()
196 {
197  // Initialise user/group configuration.
199 
200  return true;
201 }
202 
203 static void destroy()
204 {
205 }
206 
207 MODULE_INFO("users", &init, &destroy, "config");
Iterator end()
Definition: Tree.h:348
String getUsername()
Definition: User.h:71
size_t rows()
Returns the number of rows.
Tree< size_t, User * > m_Users
Definition: UserManager.h:67
void join(User *pUser)
Definition: Group.cc:32
User * getUser(size_t id)
Definition: UserManager.cc:88
Tree< size_t, Group * > m_Groups
Definition: UserManager.h:69
Definition: String.h:49
std::string getStr(size_t row, size_t col, size_t buffSz=256)
Returns the value in column &#39;col&#39; of the result, in string form.
static ProcessorInformation & information()
Definition: Processor.cc:45
Definition: User.h:32
void initialise()
Definition: UserManager.cc:170
Group * getGroup(size_t id)
Definition: UserManager.cc:105
#define WARNING(text)
Definition: Log.h:78
void insert(const K &key, const E &value)
Definition: Tree.h:173
size_t getNum(size_t row, size_t col)
Returns the value in column &#39;col&#39; of the result, in number form.
Result * query(const char *sql)
Iterator begin()
Definition: Tree.h:326
Group * getDefaultGroup()
Definition: User.h:81
#define NOTICE(text)
Definition: Log.h:74
bool succeeded()
Returns true if the result is valid, false if there was an error.
A key/value dictionary.
Definition: Tree.h:33
#define ERROR(text)
Definition: Log.h:82
static UserManager & instance()
Definition: UserManager.h:34
String getName()
Definition: Group.h:56
void setUser(User *pUser)
Definition: Process.h:194
#define FATAL(text)
Definition: Log.h:89
std::string errorMessage(size_t buffSz=256)
Returns the error message.
void setGroup(Group *pGroup)
Definition: Process.h:216
E lookup(const K &key) const
Definition: Tree.h:192
Definition: Group.h:32