summaryrefslogtreecommitdiff
path: root/noncore/settings/usermanager/passwd.cpp
blob: 0a2bfbacda57cbf0696ad36b3e79d170151e2175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "passwd.h"

// Needed for crypt_make_salt();
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

Passwd::Passwd() {
}

Passwd::~Passwd() {
}

// This function is taken from 'busybox'.
int Passwd::i64c(int i)
{
	if (i <= 0)
		return ('.');
	if (i == 1)
		return ('/');
	if (i >= 2 && i < 12)
		return ('0' - 2 + i);
	if (i >= 12 && i < 38)
		return ('A' - 12 + i);
	if (i >= 38 && i < 63)
		return ('a' - 38 + i);
	return ('z');
}

// This function is taken from 'busybox'.
char *Passwd::crypt_make_salt() {
	time_t now;
	static unsigned long x;
	static char result[3];

	time(&now);
	x += now + getpid() + clock();
	result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
	result[1] = i64c(((x >> 12) ^ x) & 077);
	result[2] = '\0';
	return result;
}

//  opens the files /etc/passwd & /etc/group and loads the contents into passwdStringList & groupStringList
bool Passwd::open() {
	int returnvalue=false;
	
	QFile passwd_file("/etc/passwd");
	QFile group_file("/etc/group");
	passwdStringList.clear();
	groupStringList.clear();
	if((passwd_file.open(IO_ReadOnly))) {
		if((group_file.open(IO_ReadOnly))) {
			QTextStream ts_passwd(&passwd_file);
			while(!(ts_passwd.eof())) {
				passwdStringList << ts_passwd.readLine();
			}
			QTextStream ts_group(&group_file);
			while(!(ts_group.eof())) {
				groupStringList << ts_group.readLine();
			}
			returnvalue=true;
			group_file.close();
		}
		passwd_file.close();
	}
	return returnvalue;
}

// Writes back the contents of passwdStringList to /etc/passwd & groupStringList to /etc/group
bool Passwd::close() {
	int returnvalue=false;
	QFile passwd_file("/etc/passwd");
	QFile group_file("/etc/group");
	if((passwd_file.open(IO_WriteOnly))) {
		if((group_file.open(IO_WriteOnly))) {
			QTextStream ts_passwd(&passwd_file);
			for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
				ts_passwd << (*it) + "\n";
			}
			QTextStream ts_group(&group_file);
			for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
				ts_group << (*it) + "\n";
			}
			returnvalue=true;
			group_file.close();
		}
		passwd_file.close();
	}
	return returnvalue;
}

// Splits a "passwd" line into the components and stores them in the pw_* variables.
void Passwd::splitPasswdEntry(QString &userString) {
	userdataStringList=QStringList::split(":",userString,true);
	QStringList::Iterator it=userdataStringList.begin();
	pw_name=(*it++);
	pw_passwd=(*it++);
	pw_uid=(*it++).toInt();
	pw_gid=(*it++).toInt();
	pw_gecos=(*it++);
	pw_dir=(*it++);
	pw_shell=(*it++);
}

// Splits a "group" line into the components and stores them in the gr_* variables.
void Passwd::splitGroupEntry(QString &groupString) {
	groupdataStringList=QStringList::split(":",groupString,true);
	QStringList::Iterator it=groupdataStringList.begin();
	gr_name=(*it++);
	it++;
	gr_gid=(*it++).toInt();
	gr_mem=QStringList::split(" ",(*it++));
}

// Find a user in the passwdStringList. Return true if found and also fill the pw_* variables.
bool Passwd::searchUser(QRegExp &userRegExp) {
	QStringList tempStringList(passwdStringList.grep(userRegExp));
	if((tempStringList.isEmpty())) {
		return false;
	} else {
		userString=(*(tempStringList.begin()));
		splitPasswdEntry(userString);
	}
	return true;
}

// Find a user by login.
bool Passwd::findUser(const char *username) {
	QRegExp userRegExp(QString("^%1\\:").arg(username));
	return searchUser(userRegExp);
}

// Find a user by uid.
bool Passwd::findUser(int uid) {
	QRegExp userRegExp(QString(":%1\\:").arg(uid));
	return searchUser(userRegExp);
}

// Add a user to the passwdStringList, create home directory, and optionally create a group for the user.
bool 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) {
	QString tempString;
	if((createGroup) && (!(findGroup(pw_gid)))) addGroup(pw_name,pw_gid);
	pw_passwd = crypt(pw_passwd, crypt_make_salt());
	tempString=pw_name+":"+pw_passwd+":"+QString::number(pw_uid)+":"+QString::number(pw_gid)+":"+pw_gecos+":"+pw_dir+":"+pw_shell;
	passwdStringList.append(tempString);
	// Make home dir.
	QDir d;
	if(!(d.exists(pw_dir))) {
		d.mkdir(pw_dir);
		chown(pw_dir,pw_uid,pw_gid);
		chmod(pw_dir,S_IRUSR|S_IWUSR|S_IXUSR);
	}
	return 1;
}

// Update info for a user in passwdStringList, take info from the pw_* fields.
bool Passwd::updateUser(QString login) {
	QRegExp userRegExp(QString("^%1\\:").arg(login));
	for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
		if(userRegExp.find((*it),0)!=-1) {
			*it=QString(pw_name+":"+pw_passwd+":"+QString::number(pw_uid)+":"+QString::number(pw_gid)+":"+pw_gecos+":"+pw_dir+":"+pw_shell);
			return true;
		}
	}
	return false;
}

// Delete a user from passwdStringList.
bool Passwd::deleteUser(QRegExp &userRegExp, bool delGroup) {
	for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
		if(userRegExp.find((*it),0)!=-1) {
			splitPasswdEntry(*it);
			if(delGroup) this->delGroup(pw_gid);
			passwdStringList.remove(it);
			return true;
		}
	}
	return false;
}

// Delete a user by login, and optionally also delete group.
bool Passwd::delUser(const char *username, bool delGroup) {
	QRegExp userRegExp(QString("^%1\\:").arg(username));
	return deleteUser(userRegExp,delGroup);
}

// Delete a user by uid, and optionally also delete group.
bool Passwd::delUser(int uid, bool delGroup) {
	QRegExp userRegExp(QString(":%1\\:").arg(uid));
	return deleteUser(userRegExp,delGroup);
}

// Locate a group in the groupStringList, fill out the gr_* variables and return "true" if found.
bool Passwd::searchGroup(QRegExp &groupRegExp) {
	QStringList tempStringList(groupStringList.grep(groupRegExp));
	if((tempStringList.isEmpty())) {
		return false;
	} else {
		groupString=(*(tempStringList.begin()));
		splitGroupEntry(groupString);
	}
	return true;
}

// Find a group by groupname.
bool Passwd::findGroup(const char *groupname) {
	QRegExp groupRegExp(QString("^%1\\:").arg(groupname));
	return searchGroup(groupRegExp);
}

// Find a group by gid.
bool Passwd::findGroup(int gid) {
	QRegExp groupRegExp(QString(":%1\\:").arg(gid));
	return searchGroup(groupRegExp);
}

// Add a group to groupStringList
bool Passwd::addGroup(QString gr_name, int gr_gid) {
	QString tempString;
	tempString=gr_name+":*:"+QString::number(gr_gid)+":";
	groupStringList.append(tempString);
	return 1;
}

// Update fields for a group in groupStringList, take info from the gr_* variables.
bool Passwd::updateGroup(int gid) {
	QRegExp groupRegExp(QString(":%1\\:").arg(QString::number(gid)));
	for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
		if(groupRegExp.find((*it),0)!=-1) {
			*it=QString(gr_name+":*:"+QString::number(gr_gid)+":");
			for(QStringList::Iterator member=gr_mem.begin(); member!=gr_mem.end(); ++member) {
				*it+=*member;
				*it+=" ";
			}
			return true;
		}
	}
	return false;
}

// Delete a group from groupStringList.
bool Passwd::deleteGroup(QRegExp &groupRegExp) {
	for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
		if(groupRegExp.find((*it),0)!=-1) {
			groupStringList.remove(it);
			return true;
		}
	}
	return false;
}

// Delete a group by groupname.
bool Passwd::delGroup(const char *groupname) {
	QRegExp groupRegExp(QString("^%1\\:").arg(groupname));
	return deleteGroup(groupRegExp);
}

// Delete a group by gid.
bool Passwd::delGroup(int gid) {
	QRegExp groupRegExp(QString(":%1\\:").arg(gid));
	return deleteGroup(groupRegExp);
}

// Add a user as a member to a group in groupStringList.
bool Passwd::addGroupMember(QString groupname, QString member) {
	if(!(findGroup(groupname))) return false;
	gr_mem << member;
	if(!(updateGroup(gr_gid))) return false;
	return true;
}

// Delete a user as a groupmember from a group in groupStringList.
bool Passwd::delGroupMember(QString groupname, QString member) {
	if(!(findGroup(groupname))) return false;
	for(QStringList::Iterator it=gr_mem.begin(); it!=gr_mem.end(); ++it) {
		if(*it==member) {
			gr_mem.remove(it);
			it=gr_mem.end();
		}
	}
	if(!(updateGroup(gr_gid))) return false;
	return true;
}

// Global Object 
Passwd *accounts;