This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
pcdm-xprocess.cpp
428 lines (371 loc) · 12.5 KB
/
pcdm-xprocess.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* PCDM Login Manager:
* Written by Ken Moore (ken@pcbsd.org) 2012/2013
* Modified by Kris Moore (kris@pcbsd.org) 2013
* Copyright(c) 2013 by the PC-BSD Project
* Available under the 3-clause BSD license
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pwd.h>
#include <login_cap.h>
#include <QMessageBox>
/*
Sub-classed QProcess for starting an XSession Process
*/
#include "pcdm-xprocess.h"
XProcess::XProcess() : QProcess(0) {
//initialize the variables
xuser.clear();
xcmd.clear();
xhome.clear();
xpwd.clear();
pam_started = FALSE;
pam_session_open = FALSE;
//Setup the finished signal/slot
connect( this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCleanup(int, QProcess::ExitStatus)) );
}
XProcess::~XProcess(){
if( isRunning() ){
this->terminate();
}
this->close();
}
void XProcess::loginToXSession(QString username, QString password, QString desktop){
//Setup the variables
xuser = username;
xpwd = password;
xhome = Backend::getUserHomeDir(xuser);
xcmd = Backend::getDesktopBinary(desktop);
xde = desktop;
//Now start the login process
startXSession();
}
bool XProcess::isRunning(){
if(this->state() != QProcess::NotRunning){ return TRUE; }
else{ return FALSE; }
}
void XProcess::waitForSessionClosed(){
// CAUTION!!
// This function will pause the calling program to wait for the session to end!
if( isRunning() ){ this->waitForFinished(-1); }
}
/*
========== SESSION STARTUP ==========
*/
bool XProcess::startXSession(){
//Check that the necessary info to start the session is available
if( xuser.isEmpty() || xcmd.isEmpty() || xhome.isEmpty() || xde.isEmpty() ){
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return FALSE;
}
//Backend::log("Starting up Desktop environment ("+xcmd+") as user ("+xuser+")");
//Check for PAM username/password validity
if( !pam_checkPW() ){ emit InvalidLogin(); pam_shutdown(); return FALSE; }
//Save the current user/desktop as the last login
Backend::saveLoginInfo(Backend::getDisplayNameFromUsername(xuser),xde);
// Get the users uid/gid information
struct passwd *pw;
int uid;
char *ok;
if (!(pw = getpwnam(xuser.toLatin1()))) {
uid = strtol(xuser.toLatin1(), &ok, 10);
if (!(pw = getpwuid(uid))) {
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return FALSE;
}
}
// Get the environment before we drop priv
QProcessEnvironment environ = QProcessEnvironment::systemEnvironment(); //current environment
QWidget *wid = new QWidget();
if (setgid(pw->pw_gid) < 0) {
qDebug() << "setgid() failed!";
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return FALSE;
}
// Setup our other groups
if (initgroups(xuser.toLatin1(), pw->pw_gid) < 0) {
qDebug() << "initgroups() failed!";
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
setgid(0);
return FALSE;
}
// Lets drop to user privs
if (setuid(pw->pw_uid) < 0) {
qDebug() << "setuid() failed!";
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return FALSE;
}
//Startup the PAM session
if( !pam_startSession() ){ pam_shutdown(); return FALSE; }
pam_session_open = TRUE; //flag that pam has an open session
QString cmd;
// Configure the DE startup command
// - Setup to run the user's <home-dir>/.xprofile startup script
if(QFile::exists(xhome+"/.xprofile")){
//cmd.append(". "+xhome+"/.xprofile; "); //make sure to start it in parallel
}
// - Add the DE startup command to the end
//cmd.append("dbus-launch --exit-with-session "+xcmd);
cmd.append(xcmd);
//cmd.append("; kill -l KILL"); //to clean up the session afterwards
// Get the current locale code
QLocale mylocale;
QString langCode = mylocale.name();
//Backend::log("Startup command: "+cmd);
// Setup the process environment
// Setup any specialized environment variables
// USER, HOME, and SHELL are set by the "su" login
environ.insert("LOGNAME",xuser); //Login name
environ.insert("USERNAME",xuser); // Username
environ.insert("PATH",environ.value("PATH")+":"+xhome+"/bin"); // Append the user's home dir to the path
if( langCode.toLower() == "c" ){} // do nothing extra to it
else if(!environ.value("MM_CHARSET").isEmpty() ){ langCode.append( "."+environ.value("MM_CHARSET") ); }
else{ langCode.append(".UTF-8"); }
environ.insert("LANG",langCode); //Set the proper localized language
environ.insert("MAIL","/var/mail/"+xuser); //Set the mail variable
environ.insert("GROUP",xuser); //Set the proper group id
environ.insert("SHLVL","0"); //Set the proper shell level
environ.insert("HOME",xhome); //Set the users home directory
this->setProcessEnvironment(environ);
this->setWorkingDirectory(xhome); //set the current directory to the user's home directory
//Log the DE startup outputs as well
this->setStandardOutputFile(xhome+"/.pcdm-startup.log",QIODevice::Truncate);
this->setStandardErrorFile(xhome+"/.pcdm-startup.err",QIODevice::Truncate);
// Startup the process
QMessageBox::warning(wid, "My Application", "CMD: " + cmd, QMessageBox::Ok, QMessageBox::Ok);
this->start(cmd);
return TRUE;
}
void XProcess::slotCleanup(int exitCode, QProcess::ExitStatus status){
pam_shutdown(); //make sure that PAM shuts down properly
}
//Start the desktop in the current process with C functions
void XProcess::startDesktop(){
//Check for PAM username/password validity
if( !pam_checkPW() ){ qDebug() << "Invalid username/password"; pam_shutdown(); return; }
//Startup the PAM session
if( !pam_startSession() ){ qDebug() << "Unable to open PAM session"; pam_shutdown(); return; }
pam_session_open = TRUE; //flag that pam has an open session
//Save the current user/desktop as the last login
Backend::saveLoginInfo(Backend::getDisplayNameFromUsername(xuser),xde);
// We will now fork off, so the child can drop root perms and do its thing
int pid = fork();
if(pid < 0){
Backend::log("Error: Could not fork for user permissions");
return;
}else if( pid !=0 ){
//Parent (calling) process
int status;
sleep(2);
waitpid(pid,&status,0); //wait for the child (session) to finish
// Child is all done, lets close down the pam session and cleanup
pam_shutdown();
exit(0);
}
// Get the users uid/gid information
struct passwd *pw;
int uid;
char *ok;
if (!(pw = getpwnam(xuser.toLatin1()))) {
uid = strtol(xuser.toLatin1(), &ok, 10);
if (!(pw = getpwuid(uid))) {
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return;
}
}
if (setgid(pw->pw_gid) < 0) {
qDebug() << "setgid() failed!";
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return;
}
// Setup our other groups
if (initgroups(xuser.toLatin1(), pw->pw_gid) < 0) {
qDebug() << "initgroups() failed!";
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
setgid(0);
return;
}
// Lets drop to user privs
if (setuid(pw->pw_uid) < 0) {
qDebug() << "setuid() failed!";
emit InvalidLogin(); //Make sure the GUI knows that it was a failure
return;
}
QString cmd;
// Configure the DE startup command
// - Setup to run the user's <home-dir>/.xprofile startup script
if(QFile::exists(xhome+"/.xprofile")){
cmd.append(". "+xhome+"/.xprofile; "); //make sure to start it in parallel
}
// - Add the DE startup command to the end
cmd.append("dbus-launch --exit-with-session "+xcmd);
// Get the current locale code
QLocale mylocale;
QString langCode = mylocale.name();
//Alternate way of starting a process using c library functions
//setup the environment variables
setenv("LOGNAME",xuser.toUtf8(),1);
setenv("USERNAME",xuser.toUtf8(),1);
QString pth = QString(getenv("PATH"))+":"+xhome+"/bin";
setenv("PATH",pth.toUtf8(),1);
if(langCode.toLower()=="c"){}
else if(QString(getenv("MM_CHARSET")).isEmpty() ){ langCode.append("."+QString(getenv("MM_CHARSET"))); }
else{ langCode.append(".UTF-8"); }
setenv("LANG",langCode.toUtf8(),1);
setenv("MAIL",QString("/var/mail/"+xuser).toUtf8(),1);
setenv("GROUP",xuser.toUtf8(),1);
setenv("HOME",xhome.toUtf8(),1);
setenv("SHELL",pw->pw_shell,1);
setenv("SHLVL","0",1);
chdir(xhome.toUtf8()); //move to home dir
//Now start the process
system(cmd.toLatin1());
}
void XProcess::setupDesktop(QString user, QString pwd, QString desktop){
//Setup internal variables
xuser = Backend::getUsernameFromDisplayname(user);
xpwd = pwd;
xhome = Backend::getUserHomeDir(xuser);
xcmd = Backend::getDesktopBinary(desktop);
xde = desktop;
//Also check password validity
bool ok = pam_checkPW();
pam_shutdown();
if(ok){ emit ValidLogin(); }
else{ emit InvalidLogin(); }
}
//Stand-alone function to check a username/password combination for validity
void XProcess::checkPW(QString user, QString pwd){
xuser = Backend::getUsernameFromDisplayname(user);
xpwd = pwd;
bool ok = pam_checkPW();
pam_shutdown();
xuser.clear();
xpwd.clear();
if(ok){ emit ValidLogin(); }
else{ emit InvalidLogin(); }
}
/*
========== PAM FUNCTIONS ==========
*/
static struct pam_conv pamc = { openpam_nullconv, NULL };
bool XProcess::pam_checkPW(){
//Requires internal "xuser" and "xpwd" variables to be set
//Convert the inputs to C character arrays for use in PAM
QByteArray tmp = xuser.toUtf8();
char* cUser = tmp.data();
QByteArray tmp2 = xpwd.toUtf8();
char* cPassword = tmp2.data();
//initialize variables
bool result = FALSE;
int ret;
//Initialize PAM
ret = pam_start("login", cUser, &pamc, &pamh);
if( ret == PAM_SUCCESS ){
pam_started = TRUE; //flag that pam is started
//Place the user-supplied password into the structure
ret = pam_set_item(pamh, PAM_AUTHTOK, cPassword);
//Set the TTY
//ret = pam_set_item(pamh, PAM_TTY, "pcdm-terminal");
//Authenticate with PAM
ret = pam_authenticate(pamh,0);
if( ret == PAM_SUCCESS ){
//Check for valid, unexpired account and verify access restrictions
ret = pam_acct_mgmt(pamh,0);
if( ret == PAM_SUCCESS ){ result = TRUE; }
}else{
pam_logFailure(ret);
}
}
//return verification result
return result;
}
bool XProcess::pam_startSession(){
//This should only be run if pam_checkPW was successful
int ret = pam_open_session(pamh,0);
bool ok = FALSE;
if(ret == PAM_SUCCESS){ ok = TRUE; }
else{ pam_logFailure(ret); }
return ok;
}
bool XProcess::pam_stopSession(){
//This should only be run if pam_startSession was successful
int ret = pam_close_session(pamh,0);
bool ok = FALSE;
if(ret == PAM_SUCCESS){ ok = TRUE; }
else{ pam_logFailure(ret); }
return ok;
}
void XProcess::pam_logFailure(int ret){
//Interpret a PAM error message and log it
Backend::log("PAM Error: " + QString::number(ret));
switch( ret ){
case PAM_ABORT:
Backend::log(" - PAM abort error");
break;
case PAM_AUTHINFO_UNAVAIL:
Backend::log(" - Authentication info unavailable");
break;
case PAM_AUTH_ERR:
Backend::log(" - Authentication error");
break;
case PAM_BUF_ERR:
Backend::log(" - Buffer error");
break;
case PAM_CONV_ERR:
Backend::log(" - Conversion error");
break;
case PAM_CRED_INSUFFICIENT:
Backend::log(" - Credentials insufficient");
break;
case PAM_MAXTRIES:
Backend::log(" - Maximum number of tries exceeded");
break;
case PAM_PERM_DENIED:
Backend::log(" - Permission denied");
break;
case PAM_SERVICE_ERR:
Backend::log(" - Service error");
break;
case PAM_SYMBOL_ERR:
Backend::log(" - Symbol error");
break;
case PAM_SYSTEM_ERR:
Backend::log(" - System error");
break;
case PAM_USER_UNKNOWN:
Backend::log(" - Unknown user");
break;
default:
Backend::log(" - Unrecognized authentication error");
}
}
void XProcess::pam_shutdown(){
if(pam_session_open){
pam_stopSession();
pam_session_open = FALSE;
}
if(pam_started){
pam_end(pamh,0);
pam_started = FALSE;
}
}
// -------------------------------------------
// XAUTHORITY FUNCTIONS
// -------------------------------------------
void XProcess::generateXAuth(){
//determine the filename
XAUTHFILENAME = XauFileName();
//generate the structure
XAUTH = new Xauth;
XAUTH->
//open the file (as new)
FILE *XFILE = fopen( XAUTHFILENAME, "w+" );
//Write the authorization file
XauWriteAuth( XFILE, XAUTH );
//Now close the file
fclose(XFILE);
}
void XProcess::revokeXAuth(){
}