source: src-qt4/PCDM/src/pcdm-xprocess.cpp @ 2a1b10d

enter/10releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 2a1b10d was 2a1b10d, checked in by Ken Moore <ken@…>, 17 months ago

Restore running ~/.xprofile in a seperate "start" on the session process before starting the main process.

  • Property mode set to 100644
File size: 9.8 KB
Line 
1/* PCDM Login Manager:
2*  Written by Ken Moore (ken@pcbsd.org) 2012/2013
3*  Modified by Kris Moore (kris@pcbsd.org) 2013
4*  Copyright(c) 2013 by the PC-BSD Project
5*  Available under the 3-clause BSD license
6*/
7
8#include <sys/types.h>
9#include <sys/wait.h>
10#include <unistd.h>
11#include <pwd.h>
12#include <login_cap.h>
13#include <QMessageBox>
14
15/*
16Sub-classed QProcess for starting an XSession Process
17*/
18
19#include "pcdm-xprocess.h"
20
21XProcess::XProcess() : QProcess(0) {
22  //initialize the variables
23  xuser.clear();
24  xcmd.clear();
25  xhome.clear();
26  xpwd.clear();
27  xshell.clear();
28  pam_started = FALSE;
29  pam_session_open = FALSE;
30  //Setup the finished signal/slot
31  connect( this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCleanup()) );
32}
33
34XProcess::~XProcess(){
35  if( isRunning() ){
36    this->terminate();
37  }
38  this->close();
39}
40
41void XProcess::loginToXSession(QString username, QString password, QString desktop, QString lang){
42  //Setup the variables
43  xuser = username;
44  xpwd = password;
45  xhome = Backend::getUserHomeDir(xuser);
46  xcmd = Backend::getDesktopBinary(desktop);
47  xshell = Backend::getUserShell(xuser);
48  xde = desktop;
49  xlang = lang;
50  //Now start the login process
51  startXSession();
52}
53
54bool XProcess::isRunning(){
55  if(this->state() != QProcess::NotRunning){ return TRUE; }
56  else{ return FALSE; }
57}
58
59void XProcess::waitForSessionClosed(){
60  // CAUTION!!
61  // This function will pause the calling program to wait for the session to end!
62  if( isRunning() ){ this->waitForFinished(-1); }
63
64}
65
66/*
67 ========== SESSION STARTUP ==========
68*/
69
70bool XProcess::startXSession(){
71  disconnect(SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCleanup()) );
72  //Check that the necessary info to start the session is available
73  if( xuser.isEmpty() || xcmd.isEmpty() || xhome.isEmpty() || xde.isEmpty() ){
74    emit InvalidLogin();  //Make sure the GUI knows that it was a failure
75    return FALSE;
76  }
77  //Backend::log("Starting up Desktop environment ("+xcmd+") as user ("+xuser+")");
78 
79  //Check for PAM username/password validity
80  if( !pam_checkPW() ){ emit InvalidLogin(); pam_shutdown(); return FALSE; }
81
82
83  //Save the current user/desktop as the last login
84  Backend::saveLoginInfo(Backend::getDisplayNameFromUsername(xuser),xde);
85
86  // Get the users uid/gid information
87  struct passwd *pw;
88  int uid;
89  char *ok;
90
91  if (!(pw = getpwnam(xuser.toLatin1()))) {
92      uid = strtol(xuser.toLatin1(), &ok, 10);
93      if (!(pw = getpwuid(uid))) {
94          emit InvalidLogin();  //Make sure the GUI knows that it was a failure
95          return FALSE;
96      }
97  }
98
99  // Get the environment before we drop priv
100  this->setProcessEnvironment( QProcessEnvironment::systemEnvironment() ); //current environment
101  //Now allow this user access to the Xserver
102  QString xhostcmd = "xhost si:localuser:"+xuser;
103  system(xhostcmd.toUtf8());
104  //QWidget *wid = new QWidget();
105  if (setgid(pw->pw_gid) < 0) {
106      qDebug() << "setgid() failed!";
107      emit InvalidLogin();  //Make sure the GUI knows that it was a failure
108      return FALSE;
109  }
110
111  // Setup our other groups
112  if (initgroups(xuser.toLatin1(), pw->pw_gid) < 0) {
113      qDebug() << "initgroups() failed!";
114      emit InvalidLogin();  //Make sure the GUI knows that it was a failure
115      setgid(0);
116      return FALSE;
117  }
118
119  // Lets drop to user privs
120  if (setuid(pw->pw_uid) < 0) {
121      qDebug() << "setuid() failed!";
122      emit InvalidLogin();  //Make sure the GUI knows that it was a failure
123      return FALSE;
124  }
125  //Startup the PAM session
126  if( !pam_startSession() ){ pam_shutdown(); return FALSE; }
127  pam_session_open = TRUE; //flag that pam has an open session
128  QString cmd;
129  // Configure the DE startup command
130
131  //  - Add the DE startup command to the end
132  cmd.append("dbus-launch --exit-with-session "+xcmd);
133  //cmd.append(xcmd);
134  //cmd.append("; kill -l KILL"); //to clean up the session afterwards
135  //Backend::log("Startup command: "+cmd);
136  // Setup the process environment
137  setupSessionEnvironment();
138  //Log the DE startup outputs as well
139  this->setStandardOutputFile(xhome+"/.pcdm-startup.log",QIODevice::Truncate);
140  this->setStandardErrorFile(xhome+"/.pcdm-startup.err",QIODevice::Truncate);
141  // Startup the process(s)
142   //  - Setup to run the user's <home-dir>/.xprofile startup script
143  Backend::log("Starting session:");
144  if(QFile::exists(xhome+"/.xprofile")){
145    Backend::log(" --Run user ~/.xprofile");
146    //cmd.prepend("sh "+xhome+"/.xprofile; ");
147    QString xpro = "sh "+xhome+"/.xprofile";
148    //Make sure it has executable permissions
149    //QFile::setPermissions(xhome+"/.xprofile", QFile::permissions(xhome+"/.xprofile") | QFile::ExeOwner | QFile::ExeGroup | QFile::ExeOther);
150    this->start(xpro);
151    this->waitForFinished(3000);
152  }
153  connect( this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCleanup()) );
154  Backend::log(" --CMD: "+cmd);
155  this->start(cmd);
156  return TRUE;
157}
158
159void XProcess::slotCleanup(){
160  pam_shutdown(); //make sure that PAM shuts down properly
161  //Now remove this user's access to the Xserver
162  QString xhostcmd = "xhost -si:localuser:"+xuser;
163  system(xhostcmd.toUtf8());
164}
165
166void XProcess::setupSessionEnvironment(){
167  // Setup any specialized environment variables
168  QProcessEnvironment environ = this->processEnvironment();
169  // Check the current locale code
170  QString langCode = xlang;
171  if( langCode.toLower() == "c" ){ langCode = "en_US"; } // default to the US english (PCDM code default), LANG=C causes problems
172  if(!environ.value("MM_CHARSET").isEmpty() ){ langCode.append( "."+environ.value("MM_CHARSET") ); }
173  else{ langCode.append(".UTF-8"); }
174  // USER, HOME, and SHELL are set by the "su" login
175  environ.insert("LOGNAME",xuser); //Login name
176  environ.insert("USERNAME",xuser); // Username
177  environ.insert("USER",xuser); // Username
178  environ.insert("PATH",environ.value("PATH")+":"+xhome+"/bin"); // Append the user's home dir to the path
179  environ.insert("LANG",langCode); //Set the proper localized language
180  environ.insert("LC_ALL",langCode); //additional localization variable for some DE's
181  environ.insert("MAIL","/var/mail/"+xuser); //Set the mail variable
182  environ.insert("GROUP",xuser); //Set the proper group id
183  environ.insert("SHLVL","0"); //Set the proper shell level
184  environ.insert("HOME",xhome); //Set the users home directory
185  environ.insert("SHELL",xshell); //Set the user's default shell
186  environ.insert("PCDM_SESSION", Backend::getNLDesktopName(xde).toUpper() ); //List the desktop environment that is being started
187  this->setProcessEnvironment(environ);
188  this->setWorkingDirectory(xhome); //set the current directory to the user's home directory
189}
190
191//Stand-alone function to check a username/password combination for validity
192void XProcess::checkPW(QString user, QString pwd){
193  xuser = Backend::getUsernameFromDisplayname(user); 
194  xpwd = pwd;
195  bool ok = pam_checkPW();
196  pam_shutdown();
197  xuser.clear();
198  xpwd.clear();
199  if(ok){ emit ValidLogin(); }
200  else{ emit InvalidLogin(); }
201}
202
203/*
204 ========== PAM FUNCTIONS ==========
205*/
206static struct pam_conv pamc = { openpam_nullconv, NULL };
207
208bool XProcess::pam_checkPW(){
209 //Requires internal "xuser" and "xpwd" variables to be set
210       
211//Convert the inputs to C character arrays for use in PAM
212  QByteArray tmp = xuser.toUtf8();
213  char* cUser = tmp.data();
214  QByteArray tmp2 = xpwd.toUtf8();
215  char* cPassword = tmp2.data();
216  //initialize variables
217  bool result = FALSE;
218  int ret;
219  //Initialize PAM
220  ret = pam_start("login", cUser, &pamc, &pamh);
221  if( ret == PAM_SUCCESS ){
222    pam_started = TRUE; //flag that pam is started
223    //Place the user-supplied password into the structure
224    ret = pam_set_item(pamh, PAM_AUTHTOK, cPassword);
225    //Set the TTY
226    //ret = pam_set_item(pamh, PAM_TTY, "pcdm-terminal");
227    //Authenticate with PAM
228    ret = pam_authenticate(pamh,0);
229    if( ret == PAM_SUCCESS ){
230      //Check for valid, unexpired account and verify access restrictions
231      ret = pam_acct_mgmt(pamh,0);
232      if( ret == PAM_SUCCESS ){ result = TRUE; }
233   
234    }else{
235      pam_logFailure(ret);
236    }
237  }
238  //return verification result
239  return result;       
240}
241
242bool XProcess::pam_startSession(){
243  //This should only be run if pam_checkPW was successful
244  int ret = pam_open_session(pamh,0);
245  bool ok = FALSE;
246  if(ret == PAM_SUCCESS){ ok = TRUE; }
247  else{ pam_logFailure(ret); }
248 
249  return ok;
250}
251
252bool XProcess::pam_stopSession(){
253  //This should only be run if pam_startSession was successful
254  int ret = pam_close_session(pamh,0);
255  bool ok = FALSE;
256  if(ret == PAM_SUCCESS){ ok = TRUE; }
257  else{ pam_logFailure(ret); }
258 
259  return ok;
260}
261
262void XProcess::pam_logFailure(int ret){
263  //Interpret a PAM error message and log it
264  Backend::log("PAM Error: " + QString::number(ret));
265  switch( ret ){
266  case PAM_ABORT:
267    Backend::log(" - PAM abort error");
268    break;
269  case PAM_AUTHINFO_UNAVAIL:
270    Backend::log(" - Authentication info unavailable");
271    break;
272  case PAM_AUTH_ERR:
273    Backend::log(" - Authentication error");
274    break;
275  case PAM_BUF_ERR:
276    Backend::log(" - Buffer error");
277    break;
278  case PAM_CONV_ERR:
279    Backend::log(" - Conversion error");
280    break;
281  case PAM_CRED_INSUFFICIENT:
282    Backend::log(" - Credentials insufficient");
283    break;
284  case PAM_MAXTRIES:
285    Backend::log(" - Maximum number of tries exceeded");
286    break;
287  case PAM_PERM_DENIED:
288    Backend::log(" - Permission denied");
289    break;
290  case PAM_SERVICE_ERR:
291    Backend::log(" - Service error");
292    break;
293  case PAM_SYMBOL_ERR:
294    Backend::log(" - Symbol error");
295    break;
296  case PAM_SYSTEM_ERR:
297    Backend::log(" - System error");
298    break;
299  case PAM_USER_UNKNOWN:
300    Backend::log(" - Unknown user");
301    break;
302  default:
303    Backend::log(" - Unrecognized authentication error");
304  }
305       
306}
307
308void XProcess::pam_shutdown(){
309  if(pam_session_open){
310    pam_stopSession();
311    pam_session_open = FALSE;
312  }
313  if(pam_started){
314    pam_end(pamh,0);
315    pam_started = FALSE;
316  }
317}
Note: See TracBrowser for help on using the repository browser.