summaryrefslogtreecommitdiff
path: root/noncore/settings/usermanager/passwd.cpp
Unidiff
Diffstat (limited to 'noncore/settings/usermanager/passwd.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/usermanager/passwd.cpp271
1 files changed, 271 insertions, 0 deletions
diff --git a/noncore/settings/usermanager/passwd.cpp b/noncore/settings/usermanager/passwd.cpp
new file mode 100644
index 0000000..310cef8
--- a/dev/null
+++ b/noncore/settings/usermanager/passwd.cpp
@@ -0,0 +1,271 @@
1/***************************************************************************
2 * *
3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. *
7 * *
8 ***************************************************************************/
9
10#include "passwd.h"
11
12// Needed for crypt_make_salt();
13#include <sys/types.h>
14#include <unistd.h>
15#include <time.h>
16
17Passwd::Passwd() {
18}
19
20Passwd::~Passwd() {
21}
22
23// This function is taken from 'busybox'.
24int Passwd::i64c(int i)
25{
26 if (i <= 0)
27 return ('.');
28 if (i == 1)
29 return ('/');
30 if (i >= 2 && i < 12)
31 return ('0' - 2 + i);
32 if (i >= 12 && i < 38)
33 return ('A' - 12 + i);
34 if (i >= 38 && i < 63)
35 return ('a' - 38 + i);
36 return ('z');
37}
38
39// This function is taken from 'busybox'.
40char *Passwd::crypt_make_salt() {
41 time_t now;
42 static unsigned long x;
43 static char result[3];
44
45 time(&now);
46 x += now + getpid() + clock();
47 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
48 result[1] = i64c(((x >> 12) ^ x) & 077);
49 result[2] = '\0';
50 return result;
51}
52
53bool Passwd::open() {
54 int returnvalue=false;
55
56 QFile passwd_file("/etc/passwd");
57 QFile group_file("/etc/group");
58 passwdStringList.clear();
59 groupStringList.clear();
60 if((passwd_file.open(IO_ReadOnly))) {
61 if((group_file.open(IO_ReadOnly))) {
62 QTextStream ts_passwd(&passwd_file);
63 while(!(ts_passwd.eof())) {
64 passwdStringList << ts_passwd.readLine();
65 }
66 QTextStream ts_group(&group_file);
67 while(!(ts_group.eof())) {
68 groupStringList << ts_group.readLine();
69 }
70 returnvalue=true;
71 group_file.close();
72 }
73 passwd_file.close();
74 }
75 return returnvalue;
76}
77
78bool Passwd::close() {
79 int returnvalue=false;
80 QFile passwd_file("/etc/passwd");
81 QFile group_file("/etc/group");
82 if((passwd_file.open(IO_WriteOnly))) {
83 if((group_file.open(IO_WriteOnly))) {
84 QTextStream ts_passwd(&passwd_file);
85 for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
86 ts_passwd << (*it) + "\n";
87 }
88 QTextStream ts_group(&group_file);
89 for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
90 ts_group << (*it) + "\n";
91 }
92 returnvalue=true;
93 group_file.close();
94 }
95 passwd_file.close();
96 }
97 return returnvalue;
98}
99
100void Passwd::splitPasswdEntry(QString &userString) {
101 userdataStringList=QStringList::split(":",userString,true);
102 QStringList::Iterator it=userdataStringList.begin();
103 pw_name=(*it++);
104 pw_passwd=(*it++);
105 pw_uid=(*it++).toInt();
106 pw_gid=(*it++).toInt();
107 pw_gecos=(*it++);
108 pw_dir=(*it++);
109 pw_shell=(*it++);
110}
111
112void Passwd::splitGroupEntry(QString &groupString) {
113 groupdataStringList=QStringList::split(":",groupString,true);
114 QStringList::Iterator it=groupdataStringList.begin();
115 gr_name=(*it++);
116 it++;
117 gr_gid=(*it++).toInt();
118 gr_mem=QStringList::split(" ",(*it++));
119}
120
121bool Passwd::searchUser(QRegExp &userRegExp) {
122 QStringList tempStringList(passwdStringList.grep(userRegExp));
123 if((tempStringList.isEmpty())) {
124 return false;
125 } else {
126 userString=(*(tempStringList.begin()));
127 splitPasswdEntry(userString);
128 }
129 return true;
130}
131
132bool Passwd::findUser(const char *username) {
133 QRegExp userRegExp(QString("^%1\\:").arg(username));
134 return searchUser(userRegExp);
135}
136
137bool Passwd::findUser(int uid) {
138 QRegExp userRegExp(QString(":%1\\:").arg(uid));
139 return searchUser(userRegExp);
140}
141
142bool Passwd::addUser(QString pw_name, QString pw_passwd, int pw_uid, int pw_gid, QString pw_gecos,QString pw_dir, QString pw_shell, bool createGroup=true) {
143 QString tempString;
144 if((createGroup) && (!(findGroup(pw_gid)))) addGroup(pw_name,pw_gid);
145 pw_passwd = crypt(pw_passwd, crypt_make_salt());
146 tempString=pw_name+":"+pw_passwd+":"+QString::number(pw_uid)+":"+QString::number(pw_gid)+":"+pw_gecos+":"+pw_dir+":"+pw_shell;
147 passwdStringList.append(tempString);
148 // Make home dir.
149 QDir d;
150 if(!(d.exists(pw_dir))) {
151 d.mkdir(pw_dir);
152 }
153 return 1;
154}
155
156bool Passwd::updateUser(QString login) {
157 QRegExp userRegExp(QString("^%1\\:").arg(login));
158 for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
159 if(userRegExp.find((*it),0)!=-1) {
160 *it=QString(pw_name+":"+pw_passwd+":"+QString::number(pw_uid)+":"+QString::number(pw_gid)+":"+pw_gecos+":"+pw_dir+":"+pw_shell);
161 return true;
162 }
163 }
164 return false;
165}
166
167bool Passwd::deleteUser(QRegExp &userRegExp, bool delGroup) {
168 for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
169 if(userRegExp.find((*it),0)!=-1) {
170 splitPasswdEntry(*it);
171 if(delGroup) this->delGroup(pw_gid);
172 passwdStringList.remove(it);
173 return true;
174 }
175 }
176 return false;
177}
178
179bool Passwd::delUser(const char *username, bool delGroup) {
180 QRegExp userRegExp(QString("^%1\\:").arg(username));
181 return deleteUser(userRegExp,delGroup);
182}
183
184bool Passwd::delUser(int uid, bool delGroup) {
185 QRegExp userRegExp(QString(":%1\\:").arg(uid));
186 return deleteUser(userRegExp,delGroup);
187}
188
189bool Passwd::searchGroup(QRegExp &groupRegExp) {
190 QStringList tempStringList(groupStringList.grep(groupRegExp));
191 if((tempStringList.isEmpty())) {
192 return false;
193 } else {
194 groupString=(*(tempStringList.begin()));
195 splitGroupEntry(groupString);
196 }
197 return true;
198}
199
200bool Passwd::findGroup(const char *groupname) {
201 QRegExp groupRegExp(QString("^%1\\:").arg(groupname));
202 return searchGroup(groupRegExp);
203}
204
205bool Passwd::findGroup(int gid) {
206 QRegExp groupRegExp(QString(":%1\\:").arg(gid));
207 return searchGroup(groupRegExp);
208}
209
210bool Passwd::addGroup(QString gr_name, int gr_gid) {
211 QString tempString;
212 tempString=gr_name+":*:"+QString::number(gr_gid)+":";
213 groupStringList.append(tempString);
214 return 1;
215}
216
217bool Passwd::updateGroup(int gid) {
218 QRegExp groupRegExp(QString(":%1\\:").arg(QString::number(gid)));
219 for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
220 if(groupRegExp.find((*it),0)!=-1) {
221 *it=QString(gr_name+":*:"+QString::number(gr_gid)+":");
222 for(QStringList::Iterator member=gr_mem.begin(); member!=gr_mem.end(); ++member) {
223 *it+=*member;
224 *it+=" ";
225 }
226 return true;
227 }
228 }
229 return false;
230}
231
232bool Passwd::deleteGroup(QRegExp &groupRegExp) {
233 for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
234 if(groupRegExp.find((*it),0)!=-1) {
235 groupStringList.remove(it);
236 return true;
237 }
238 }
239 return false;
240}
241
242bool Passwd::delGroup(const char *groupname) {
243 QRegExp groupRegExp(QString("^%1\\:").arg(groupname));
244 return deleteGroup(groupRegExp);
245}
246
247bool Passwd::delGroup(int gid) {
248 QRegExp groupRegExp(QString(":%1\\:").arg(gid));
249 return deleteGroup(groupRegExp);
250}
251
252bool Passwd::addGroupMember(QString groupname, QString member) {
253 if(!(findGroup(groupname))) return false;
254 gr_mem << member;
255 if(!(updateGroup(gr_gid))) return false;
256 return true;
257}
258
259bool Passwd::delGroupMember(QString groupname, QString member) {
260 if(!(findGroup(groupname))) return false;
261 for(QStringList::Iterator it=gr_mem.begin(); it!=gr_mem.end(); ++it) {
262 if(*it==member) {
263 gr_mem.remove(it);
264 it=gr_mem.end();
265 }
266 }
267 if(!(updateGroup(gr_gid))) return false;
268 return true;
269}
270
271Passwd *accounts;