-
Notifications
You must be signed in to change notification settings - Fork 327
/
UserSession.cpp
309 lines (266 loc) · 11 KB
/
UserSession.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
/*
* Session process wrapper
* Copyright (C) 2015 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
* Copyright (C) 2014 Martin Bříza <mbriza@redhat.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "Configuration.h"
#include "UserSession.h"
#include "HelperApp.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
namespace SDDM {
UserSession::UserSession(HelperApp *parent)
: QProcess(parent) {
}
UserSession::~UserSession() {
}
bool UserSession::start() {
QProcessEnvironment env = qobject_cast<HelperApp*>(parent())->session()->processEnvironment();
if (env.value(QStringLiteral("XDG_SESSION_CLASS")) == QLatin1String("greeter")) {
QProcess::start(m_path);
} else if (env.value(QStringLiteral("XDG_SESSION_TYPE")) == QLatin1String("x11")) {
const QString cmd = QStringLiteral("%1 \"%2\"").arg(mainConfig.X11.SessionCommand.get()).arg(m_path);
qInfo() << "Starting:" << cmd;
QProcess::start(cmd);
} else if (env.value(QStringLiteral("XDG_SESSION_TYPE")) == QLatin1String("wayland")) {
const QString cmd = QStringLiteral("%1 %2").arg(mainConfig.Wayland.SessionCommand.get()).arg(m_path);
qInfo() << "Starting:" << cmd;
QProcess::start(cmd);
} else {
qCritical() << "Unable to run user session: unknown session type";
}
return waitForStarted();
}
void UserSession::setPath(const QString& path) {
m_path = path;
}
QString UserSession::path() const {
return m_path;
}
void UserSession::setupChildProcess() {
// Session type
QString sessionType = processEnvironment().value(QStringLiteral("XDG_SESSION_TYPE"));
// For Wayland sessions we leak the VT into the session as stdin so
// that it stays open without races
if (sessionType == QLatin1String("wayland")) {
// open VT and get the fd
QString ttyString = QStringLiteral("/dev/tty%1").arg(processEnvironment().value(QStringLiteral("XDG_VTNR")));
int vtFd = ::open(qPrintable(ttyString), O_RDWR | O_NOCTTY);
// when this is true we'll take control of the tty
bool takeControl = false;
if (vtFd > 0) {
dup2(vtFd, STDIN_FILENO);
::close(vtFd);
takeControl = true;
} else {
int stdinFd = ::open("/dev/null", O_RDWR);
dup2(stdinFd, STDIN_FILENO);
::close(stdinFd);
}
// set this process as session leader
if (setsid() < 0) {
qCritical("Failed to set pid %lld as leader of the new session and process group: %s",
QCoreApplication::applicationPid(), strerror(errno));
exit(Auth::HELPER_OTHER_ERROR);
}
// take control of the tty
if (takeControl) {
if (ioctl(STDIN_FILENO, TIOCSCTTY) < 0) {
qCritical("Failed to take control of the tty: %s", strerror(errno));
exit(Auth::HELPER_OTHER_ERROR);
}
}
}
#ifdef Q_OS_LINUX
// enter Linux namespaces
for (const QString &ns: mainConfig.Namespaces.get()) {
qInfo() << "Entering namespace" << ns;
int fd = ::open(qPrintable(ns), O_RDONLY);
if (fd < 0) {
qCritical("open(%s) failed: %s", qPrintable(ns), strerror(errno));
exit(Auth::HELPER_OTHER_ERROR);
}
if (setns(fd, 0) != 0) {
qCritical("setns(open(%s), 0) failed: %s", qPrintable(ns), strerror(errno));
exit(Auth::HELPER_OTHER_ERROR);
}
::close(fd);
}
#endif
// switch user
const QByteArray username = qobject_cast<HelperApp*>(parent())->user().toLocal8Bit();
struct passwd pw;
struct passwd *rpw;
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1)
bufsize = 16384;
char *buffer = (char *)malloc(bufsize);
if (buffer == NULL)
exit(Auth::HELPER_OTHER_ERROR);
int err = getpwnam_r(username.constData(), &pw, buffer, bufsize, &rpw);
if (rpw == NULL) {
if (err == 0)
qCritical() << "getpwnam_r(" << username << ") username not found!";
else
qCritical() << "getpwnam_r(" << username << ") failed with error: " << strerror(err);
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
if (setgid(pw.pw_gid) != 0) {
qCritical() << "setgid(" << pw.pw_gid << ") failed for user: " << username;
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
#ifdef USE_PAM
// fetch ambient groups from PAM's environment;
// these are set by modules such as pam_groups.so
int n_pam_groups = getgroups(0, NULL);
gid_t *pam_groups = NULL;
if (n_pam_groups > 0) {
pam_groups = new gid_t[n_pam_groups];
if ((n_pam_groups = getgroups(n_pam_groups, pam_groups)) == -1) {
qCritical() << "getgroups() failed to fetch supplemental"
<< "PAM groups for user:" << username;
exit(Auth::HELPER_OTHER_ERROR);
}
} else {
n_pam_groups = 0;
}
// fetch session's user's groups
int n_user_groups = 0;
gid_t *user_groups = NULL;
if (-1 == getgrouplist(username.constData(), pw.pw_gid,
NULL, &n_user_groups)) {
user_groups = new gid_t[n_user_groups];
if ((n_user_groups = getgrouplist(username.constData(),
pw.pw_gid, user_groups,
&n_user_groups)) == -1 ) {
qCritical() << "getgrouplist(" << username << ", " << pw.pw_gid
<< ") failed";
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
}
// set groups to concatenation of PAM's ambient
// groups and the session's user's groups
int n_groups = n_pam_groups + n_user_groups;
if (n_groups > 0) {
gid_t *groups = new gid_t[n_groups];
memcpy(groups, pam_groups, (n_pam_groups * sizeof(gid_t)));
memcpy((groups + n_pam_groups), user_groups,
(n_user_groups * sizeof(gid_t)));
// setgroups(2) handles duplicate groups
if (setgroups(n_groups, groups) != 0) {
qCritical() << "setgroups() failed for user: " << username;
free(buffer);
exit (Auth::HELPER_OTHER_ERROR);
}
delete[] groups;
}
delete[] pam_groups;
delete[] user_groups;
#else
if (initgroups(pw.pw_name, pw.pw_gid) != 0) {
qCritical() << "initgroups(" << pw.pw_name << ", " << pw.pw_gid << ") failed for user: " << username;
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
#endif /* USE_PAM */
if (setuid(pw.pw_uid) != 0) {
qCritical() << "setuid(" << pw.pw_uid << ") failed for user: " << username;
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
if (chdir(pw.pw_dir) != 0) {
qCritical() << "chdir(" << pw.pw_dir << ") failed for user: " << username;
qCritical() << "verify directory exist and has sufficient permissions";
free(buffer);
exit(Auth::HELPER_OTHER_ERROR);
}
free(buffer);
//we cannot use setStandardError file as this code is run in the child process
//we want to redirect after we setuid so that the log file is owned by the user
// determine stderr log file based on session type
QString sessionLog = QStringLiteral("%1/%2")
.arg(QString::fromLocal8Bit(pw.pw_dir))
.arg(sessionType == QLatin1String("x11")
? mainConfig.X11.SessionLogFile.get()
: mainConfig.Wayland.SessionLogFile.get());
// create the path
QFileInfo finfo(sessionLog);
QDir().mkpath(finfo.absolutePath());
//swap the stderr pipe of this subprcess into a file
int fd = ::open(qPrintable(sessionLog), O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd >= 0)
{
dup2 (fd, STDERR_FILENO);
::close(fd);
} else {
qWarning() << "Could not open stderr to" << sessionLog;
}
//redirect any stdout to /dev/null
fd = ::open("/dev/null", O_WRONLY);
if (fd >= 0)
{
dup2 (fd, STDOUT_FILENO);
::close(fd);
} else {
qWarning() << "Could not redirect stdout";
}
// set X authority for X11 sessions only
if (sessionType != QLatin1String("x11"))
return;
QString cookie = qobject_cast<HelperApp*>(parent())->cookie();
if (!cookie.isEmpty()) {
QString file = processEnvironment().value(QStringLiteral("XAUTHORITY"));
QString display = processEnvironment().value(QStringLiteral("DISPLAY"));
qDebug() << "Adding cookie to" << file;
// create the path
QFileInfo finfo(file);
QDir().mkpath(finfo.absolutePath());
QFile file_handler(file);
file_handler.open(QIODevice::Append);
file_handler.close();
QString cmd = QStringLiteral("%1 -f %2 -q").arg(mainConfig.X11.XauthPath.get()).arg(file);
// execute xauth
FILE *fp = popen(qPrintable(cmd), "w");
// check file
if (!fp)
return;
fprintf(fp, "remove %s\n", qPrintable(display));
fprintf(fp, "add %s . %s\n", qPrintable(display), qPrintable(cookie));
fprintf(fp, "exit\n");
// close pipe
pclose(fp);
}
}
void UserSession::setCachedProcessId(qint64 pid) {
m_cachedProcessId = pid;
}
qint64 UserSession::cachedProcessId() {
return m_cachedProcessId;
}
}