]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/pam_systemd.c
Merge pull request #14078 from poettering/cryptsetup-fixlets
[thirdparty/systemd.git] / src / login / pam_systemd.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8c6db833 2
00229fe4 3#include <endian.h>
8c6db833
LP
4#include <errno.h>
5#include <fcntl.h>
8c6db833 6#include <pwd.h>
8c6db833 7#include <security/_pam_macros.h>
8c6db833
LP
8#include <security/pam_ext.h>
9#include <security/pam_misc.h>
00229fe4
LP
10#include <security/pam_modules.h>
11#include <security/pam_modutil.h>
12#include <sys/file.h>
ca78ad1d
ZJS
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
8c6db833 16
b5efdb8a 17#include "alloc-util.h"
430f0182 18#include "audit-util.h"
00229fe4
LP
19#include "bus-common-errors.h"
20#include "bus-error.h"
47094ce0 21#include "bus-internal.h"
ffcfcb6b 22#include "bus-util.h"
fdb3deca 23#include "cgroup-setup.h"
4bbccb02 24#include "errno-util.h"
3ffd4af2 25#include "fd-util.h"
a5c32cff 26#include "fileio.h"
f97b34a6 27#include "format-util.h"
958b66ea 28#include "hostname-util.h"
00229fe4
LP
29#include "login-util.h"
30#include "macro.h"
6bedfcbb 31#include "parse-util.h"
e37e5ed3 32#include "path-util.h"
df0ff127 33#include "process-util.h"
00229fe4 34#include "socket-util.h"
055c08ef 35#include "stdio-util.h"
00229fe4
LP
36#include "strv.h"
37#include "terminal-util.h"
8c6db833 38
baae0358
LP
39static int parse_argv(
40 pam_handle_t *handle,
41 int argc, const char **argv,
42 const char **class,
49ebd11f 43 const char **type,
f5cb2820 44 const char **desktop,
baae0358 45 bool *debug) {
8c6db833
LP
46
47 unsigned i;
48
49 assert(argc >= 0);
50 assert(argc == 0 || argv);
51
49ebd11f 52 for (i = 0; i < (unsigned) argc; i++) {
d9608d40
LP
53 const char *p;
54
55 if ((p = startswith(argv[i], "class="))) {
485507b8 56 if (class)
d9608d40 57 *class = p;
485507b8 58
d9608d40 59 } else if ((p = startswith(argv[i], "type="))) {
49ebd11f 60 if (type)
d9608d40 61 *type = p;
49ebd11f 62
d9608d40 63 } else if ((p = startswith(argv[i], "desktop="))) {
f5cb2820 64 if (desktop)
d9608d40 65 *desktop = p;
f5cb2820 66
05a049cc
ZJS
67 } else if (streq(argv[i], "debug")) {
68 if (debug)
69 *debug = true;
fb6becb4 70
d9608d40 71 } else if ((p = startswith(argv[i], "debug="))) {
05a049cc 72 int k;
0e318cad 73
d9608d40 74 k = parse_boolean(p);
05a049cc 75 if (k < 0)
d9608d40 76 pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring: %s", p);
05a049cc 77 else if (debug)
0e318cad
MS
78 *debug = k;
79
05a049cc 80 } else
fb6becb4 81 pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]);
49ebd11f 82 }
8c6db833 83
8c6db833
LP
84 return 0;
85}
86
8c6db833
LP
87static int get_user_data(
88 pam_handle_t *handle,
89 const char **ret_username,
90 struct passwd **ret_pw) {
91
d90b9d27
LP
92 const char *username = NULL;
93 struct passwd *pw = NULL;
8c6db833
LP
94 int r;
95
96 assert(handle);
97 assert(ret_username);
98 assert(ret_pw);
99
baae0358
LP
100 r = pam_get_user(handle, &username, NULL);
101 if (r != PAM_SUCCESS) {
d9608d40 102 pam_syslog(handle, LOG_ERR, "Failed to get user name: %s", pam_strerror(handle, r));
baae0358
LP
103 return r;
104 }
d90b9d27 105
baae0358
LP
106 if (isempty(username)) {
107 pam_syslog(handle, LOG_ERR, "User name not valid.");
108 return PAM_AUTH_ERR;
8c6db833
LP
109 }
110
baae0358 111 pw = pam_modutil_getpwnam(handle, username);
d90b9d27 112 if (!pw) {
8c6db833
LP
113 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
114 return PAM_USER_UNKNOWN;
115 }
116
117 *ret_pw = pw;
8e24a4f8 118 *ret_username = username;
8c6db833
LP
119
120 return PAM_SUCCESS;
121}
122
ecd5f1a9
LP
123static bool display_is_local(const char *display) {
124 assert(display);
125
126 return
127 display[0] == ':' &&
128 display[1] >= '0' &&
129 display[1] <= '9';
130}
131
f7b8b5c4
LP
132static int socket_from_display(const char *display, char **path) {
133 size_t k;
134 char *f, *c;
135
136 assert(display);
137 assert(path);
138
139 if (!display_is_local(display))
140 return -EINVAL;
141
142 k = strspn(display+1, "0123456789");
143
144 f = new(char, STRLEN("/tmp/.X11-unix/X") + k + 1);
145 if (!f)
146 return -ENOMEM;
147
148 c = stpcpy(f, "/tmp/.X11-unix/X");
149 memcpy(c, display+1, k);
150 c[k] = 0;
151
152 *path = f;
153
154 return 0;
155}
156
4d6d6518 157static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
15a3e96f 158 union sockaddr_union sa = {};
baae0358
LP
159 _cleanup_free_ char *p = NULL, *tty = NULL;
160 _cleanup_close_ int fd = -1;
4d6d6518 161 struct ucred ucred;
15a3e96f 162 int v, r, salen;
4d6d6518
LP
163
164 assert(display);
4d6d6518
LP
165 assert(vtnr);
166
167 /* We deduce the X11 socket from the display name, then use
168 * SO_PEERCRED to determine the X11 server process, ask for
169 * the controlling tty of that and if it's a VC then we know
170 * the seat and the virtual terminal. Sounds ugly, is only
171 * semi-ugly. */
172
173 r = socket_from_display(display, &p);
174 if (r < 0)
175 return r;
15a3e96f
LP
176 salen = sockaddr_un_set_path(&sa.un, p);
177 if (salen < 0)
178 return salen;
4d6d6518
LP
179
180 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
b92bea5d 181 if (fd < 0)
4d6d6518 182 return -errno;
4d6d6518 183
15a3e96f 184 if (connect(fd, &sa.sa, salen) < 0)
4d6d6518 185 return -errno;
4d6d6518 186
eff05270 187 r = getpeercred(fd, &ucred);
4d6d6518 188 if (r < 0)
eff05270 189 return r;
4d6d6518
LP
190
191 r = get_ctty(ucred.pid, NULL, &tty);
192 if (r < 0)
193 return r;
194
195 v = vtnr_from_tty(tty);
4d6d6518
LP
196 if (v < 0)
197 return v;
198 else if (v == 0)
199 return -ENOENT;
200
fc7985ed
LP
201 if (seat)
202 *seat = "seat0";
4d6d6518
LP
203 *vtnr = (uint32_t) v;
204
205 return 0;
206}
207
00efd498
ZJS
208static int export_legacy_dbus_address(
209 pam_handle_t *handle,
210 uid_t uid,
211 const char *runtime) {
212
15ee6c20
ZJS
213 const char *s;
214 _cleanup_free_ char *t = NULL;
00efd498
ZJS
215 int r = PAM_BUF_ERR;
216
15ee6c20
ZJS
217 /* We need to export $DBUS_SESSION_BUS_ADDRESS because various applications will not connect
218 * correctly to the bus without it. This setting matches what dbus.socket does for the user
219 * session using 'systemctl --user set-environment'. We want to have the same configuration
220 * in processes started from the PAM session.
221 *
222 * The setting of the address is guarded by the access() check because it is also possible to compile
223 * dbus without --enable-user-session, in which case this socket is not used, and
224 * $DBUS_SESSION_BUS_ADDRESS should not be set. An alternative approach would to not do the access()
225 * check here, and let applications try on their own, by using "unix:path=%s/bus;autolaunch:". But we
226 * expect the socket to be present by the time we do this check, so we can just as well check once
227 * here. */
228
229 s = strjoina(runtime, "/bus");
230 if (access(s, F_OK) < 0)
231 return PAM_SUCCESS;
232
233 if (asprintf(&t, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0)
00efd498
ZJS
234 goto error;
235
15ee6c20 236 r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", t, 0);
00efd498
ZJS
237 if (r != PAM_SUCCESS)
238 goto error;
239
240 return PAM_SUCCESS;
241
242error:
243 pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
244 return r;
245}
246
22f93314
JS
247static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) {
248 uint64_t val;
249 int r;
250
251 if (isempty(limit))
252 return 0;
253
254 if (streq(limit, "infinity")) {
255 r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1);
256 if (r < 0) {
4bbccb02 257 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
22f93314
JS
258 return r;
259 }
260 } else {
f806dfd3 261 r = parse_permille(limit);
22f93314 262 if (r >= 0) {
f806dfd3 263 r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) r * UINT32_MAX) / 1000U));
22f93314 264 if (r < 0) {
4bbccb02 265 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
22f93314
JS
266 return r;
267 }
268 } else {
269 r = parse_size(limit, 1024, &val);
270 if (r >= 0) {
271 r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val);
272 if (r < 0) {
4bbccb02 273 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
22f93314
JS
274 return r;
275 }
276 } else
de5e9096 277 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.memory_max: %s, ignoring.", limit);
22f93314
JS
278 }
279 }
280
281 return 0;
282}
283
adc09af2
PW
284static int append_session_runtime_max_sec(pam_handle_t *handle, sd_bus_message *m, const char *limit) {
285 usec_t val;
286 int r;
287
288 /* No need to parse "infinity" here, it will be set by default later in scope_init() */
289 if (isempty(limit) || streq(limit, "infinity"))
290 return 0;
291
292 r = parse_sec(limit, &val);
293 if (r >= 0) {
294 r = sd_bus_message_append(m, "(sv)", "RuntimeMaxUSec", "t", (uint64_t) val);
295 if (r < 0) {
296 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
297 return r;
298 }
299 } else
300 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.runtime_max_sec: %s, ignoring.", limit);
301
302 return 0;
303}
304
5fdfbbd5 305static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) {
22f93314
JS
306 uint64_t val;
307 int r;
308
309 /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */
310 if (isempty(limit) || streq(limit, "infinity"))
311 return 0;
312
313 r = safe_atou64(limit, &val);
314 if (r >= 0) {
315 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val);
316 if (r < 0) {
4bbccb02 317 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
22f93314
JS
318 return r;
319 }
320 } else
de5e9096 321 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.tasks_max: %s, ignoring.", limit);
22f93314
JS
322
323 return 0;
324}
325
326static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) {
327 uint64_t val;
328 int r;
329
36a4dbae
LP
330 if (isempty(limit))
331 return 0;
332
333 r = cg_weight_parse(limit, &val);
334 if (r >= 0) {
335 r = sd_bus_message_append(m, "(sv)", field, "t", val);
336 if (r < 0) {
4bbccb02 337 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
36a4dbae
LP
338 return r;
339 }
340 } else if (streq(field, "CPUWeight"))
341 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit);
342 else
343 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight: %s, ignoring.", limit);
22f93314
JS
344
345 return 0;
346}
347
0ecc1c9d
LP
348static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) {
349 const char *v;
350
351 assert(handle);
352 assert(key);
353
5238e957 354 /* Looks for an environment variable, preferably in the environment block associated with the
83d4ab55
LP
355 * specified PAM handle, falling back to the process' block instead. Why check both? Because we want
356 * to permit configuration of session properties from unit files that invoke PAM services, so that
357 * PAM services don't have to be reworked to set systemd-specific properties, but these properties
358 * can still be set from the unit file Environment= block. */
0ecc1c9d
LP
359
360 v = pam_getenv(handle, key);
361 if (!isempty(v))
362 return v;
363
83d4ab55
LP
364 /* We use secure_getenv() here, since we might get loaded into su/sudo, which are SUID. Ideally
365 * they'd clean up the environment before invoking foreign code (such as PAM modules), but alas they
366 * currently don't (to be precise, they clean up the environment they pass to their children, but
367 * not their own environ[]). */
368 v = secure_getenv(key);
0ecc1c9d
LP
369 if (!isempty(v))
370 return v;
371
372 return fallback;
373}
374
d6baaa69
LP
375static int update_environment(pam_handle_t *handle, const char *key, const char *value) {
376 int r;
377
378 assert(handle);
379 assert(key);
380
381 /* Updates the environment, but only if there's actually a value set. Also, log about errors */
382
383 if (isempty(value))
384 return PAM_SUCCESS;
385
386 r = pam_misc_setenv(handle, key, value, 0);
387 if (r != PAM_SUCCESS)
d9608d40 388 pam_syslog(handle, LOG_ERR, "Failed to set environment variable %s: %s", key, pam_strerror(handle, r));
d6baaa69
LP
389
390 return r;
391}
392
b9217112
LP
393static bool validate_runtime_directory(pam_handle_t *handle, const char *path, uid_t uid) {
394 struct stat st;
395
dca81e28 396 assert(handle);
b9217112
LP
397 assert(path);
398
399 /* Just some extra paranoia: let's not set $XDG_RUNTIME_DIR if the directory we'd set it to isn't actually set
400 * up properly for us. */
401
402 if (lstat(path, &st) < 0) {
4bbccb02 403 pam_syslog(handle, LOG_ERR, "Failed to stat() runtime directory '%s': %s", path, strerror_safe(errno));
b9217112
LP
404 goto fail;
405 }
406
407 if (!S_ISDIR(st.st_mode)) {
408 pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not actually a directory.", path);
409 goto fail;
410 }
411
412 if (st.st_uid != uid) {
413 pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not owned by UID " UID_FMT ", as it should.", path, uid);
414 goto fail;
415 }
416
417 return true;
418
419fail:
420 pam_syslog(handle, LOG_WARNING, "Not setting $XDG_RUNTIME_DIR, as the directory is not in order.");
421 return false;
422}
423
98a28fef 424_public_ PAM_EXTERN int pam_sm_open_session(
8c6db833
LP
425 pam_handle_t *handle,
426 int flags,
427 int argc, const char **argv) {
428
4afd3348 429 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
22f93314 430 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
d1529c9e
LP
431 const char
432 *username, *id, *object_path, *runtime_path,
433 *service = NULL,
434 *tty = NULL, *display = NULL,
435 *remote_user = NULL, *remote_host = NULL,
436 *seat = NULL,
437 *type = NULL, *class = NULL,
f5cb2820 438 *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL,
adc09af2 439 *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL, *runtime_max_sec = NULL;
4afd3348 440 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
d1529c9e 441 int session_fd = -1, existing, r;
d1529c9e
LP
442 bool debug = false, remote;
443 struct passwd *pw;
baae0358
LP
444 uint32_t vtnr = 0;
445 uid_t original_uid;
8c6db833 446
ffcfcb6b 447 assert(handle);
98a28fef 448
79d860fe
MP
449 /* Make this a NOP on non-logind systems */
450 if (!logind_running())
8c6db833
LP
451 return PAM_SUCCESS;
452
e9fbc77c
LP
453 if (parse_argv(handle,
454 argc, argv,
fb6becb4 455 &class_pam,
49ebd11f 456 &type_pam,
f5cb2820 457 &desktop_pam,
5a330cda
ZJS
458 &debug) < 0)
459 return PAM_SESSION_ERR;
74fe1fe3 460
baae0358 461 if (debug)
3831838a 462 pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing");
baae0358 463
98a28fef 464 r = get_user_data(handle, &username, &pw);
42e66809 465 if (r != PAM_SUCCESS)
5a330cda 466 return r;
8c6db833 467
30b2c336
LP
468 /* Make sure we don't enter a loop by talking to
469 * systemd-logind when it is actually waiting for the
470 * background to finish start-up. If the service is
5c390a4a 471 * "systemd-user" we simply set XDG_RUNTIME_DIR and
30b2c336
LP
472 * leave. */
473
1798f5af 474 (void) pam_get_item(handle, PAM_SERVICE, (const void**) &service);
5c390a4a 475 if (streq_ptr(service, "systemd-user")) {
055c08ef 476 char rt[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)];
30b2c336 477
055c08ef 478 xsprintf(rt, "/run/user/"UID_FMT, pw->pw_uid);
b9217112
LP
479 if (validate_runtime_directory(handle, rt, pw->pw_uid)) {
480 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
481 if (r != PAM_SUCCESS) {
d9608d40 482 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir: %s", pam_strerror(handle, r));
b9217112
LP
483 return r;
484 }
30b2c336
LP
485 }
486
00efd498
ZJS
487 r = export_legacy_dbus_address(handle, pw->pw_uid, rt);
488 if (r != PAM_SUCCESS)
489 return r;
490
ffcfcb6b 491 return PAM_SUCCESS;
8c6db833
LP
492 }
493
ffcfcb6b 494 /* Otherwise, we ask logind to create a session for us */
8c6db833 495
1798f5af
LP
496 (void) pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
497 (void) pam_get_item(handle, PAM_TTY, (const void**) &tty);
498 (void) pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
499 (void) pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
a8573ccc 500
0ecc1c9d
LP
501 seat = getenv_harder(handle, "XDG_SEAT", NULL);
502 cvtnr = getenv_harder(handle, "XDG_VTNR", NULL);
503 type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam);
504 class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam);
f5cb2820 505 desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam);
a4cd87e9 506
ed18b08b 507 tty = strempty(tty);
98a28fef 508
ee8545b0 509 if (strchr(tty, ':')) {
3a736949
LP
510 /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things
511 * and don't pretend that an X display was a tty. */
ee8545b0
LP
512 if (isempty(display))
513 display = tty;
a4cd87e9 514 tty = NULL;
3a736949 515
1a4459d6 516 } else if (streq(tty, "cron")) {
3a736949
LP
517 /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but
518 * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set
519 * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked
520 * off processes.) */
0ad1271f 521 type = "unspecified";
49ebd11f 522 class = "background";
a4cd87e9 523 tty = NULL;
3a736949 524
0ad1271f 525 } else if (streq(tty, "ssh")) {
3a736949
LP
526 /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further
527 * details look for "PAM_TTY_KLUDGE" in the openssh sources). */
0ad1271f 528 type ="tty";
49ebd11f 529 class = "user";
3a736949
LP
530 tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually
531 * associated with a pty — won't be tracked by their tty in logind. This is because ssh
532 * does the PAM session registration early for new connections, and registers a pty only
533 * much later (this is because it doesn't know yet if it needs one at all, as whether to
534 * register a pty or not is negotiated much later in the protocol). */
535
c9ed61e7
LP
536 } else
537 /* Chop off leading /dev prefix that some clients specify, but others do not. */
538 tty = skip_dev_prefix(tty);
ee8545b0 539
8e7705e5 540 /* If this fails vtnr will be 0, that's intended */
4d6d6518 541 if (!isempty(cvtnr))
2ae4842b 542 (void) safe_atou32(cvtnr, &vtnr);
4d6d6518 543
92bd5ff3 544 if (!isempty(display) && !vtnr) {
fc7985ed 545 if (isempty(seat))
d487e2d6 546 (void) get_seat_from_display(display, &seat, &vtnr);
fc7985ed 547 else if (streq(seat, "seat0"))
d487e2d6 548 (void) get_seat_from_display(display, NULL, &vtnr);
fc7985ed 549 }
4d6d6518 550
49ebd11f 551 if (seat && !streq(seat, "seat0") && vtnr != 0) {
2675747f
LP
552 if (debug)
553 pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat);
d7353ef6
MM
554 vtnr = 0;
555 }
556
49ebd11f 557 if (isempty(type))
0ad1271f 558 type = !isempty(display) ? "x11" :
49ebd11f 559 !isempty(tty) ? "tty" : "unspecified";
98a28fef 560
55efac6c 561 if (isempty(class))
e2acb67b 562 class = streq(type, "unspecified") ? "background" : "user";
55efac6c 563
fecc80c1 564 remote = !isempty(remote_host) && !is_localhost(remote_host);
98a28fef 565
22f93314
JS
566 (void) pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max);
567 (void) pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max);
568 (void) pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight);
569 (void) pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight);
adc09af2 570 (void) pam_get_data(handle, "systemd.runtime_max_sec", (const void **)&runtime_max_sec);
22f93314 571
4d49b48c 572 /* Talk to logind over the message bus */
5a330cda 573
ffcfcb6b
ZJS
574 r = sd_bus_open_system(&bus);
575 if (r < 0) {
4bbccb02 576 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror_safe(r));
ffcfcb6b 577 return PAM_SESSION_ERR;
cc377381
LP
578 }
579
22f93314 580 if (debug) {
ce959314 581 pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
1fa2f38f 582 "uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
df0ff127 583 pw->pw_uid, getpid_cached(),
baae0358 584 strempty(service),
cda7ecb0 585 type, class, strempty(desktop),
a4cd87e9 586 strempty(seat), vtnr, strempty(tty), strempty(display),
baae0358 587 yes_no(remote), strempty(remote_user), strempty(remote_host));
22f93314 588 pam_syslog(handle, LOG_DEBUG, "Session limits: "
adc09af2
PW
589 "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s",
590 strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight), strna(runtime_max_sec));
22f93314
JS
591 }
592
593 r = sd_bus_message_new_method_call(
594 bus,
595 &m,
596 "org.freedesktop.login1",
597 "/org/freedesktop/login1",
598 "org.freedesktop.login1.Manager",
599 "CreateSession");
600 if (r < 0) {
4bbccb02 601 pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror_safe(r));
22f93314
JS
602 return PAM_SESSION_ERR;
603 }
604
605 r = sd_bus_message_append(m, "uusssssussbss",
606 (uint32_t) pw->pw_uid,
da0da5ec 607 0,
22f93314
JS
608 service,
609 type,
610 class,
611 desktop,
612 seat,
613 vtnr,
614 tty,
615 display,
616 remote,
617 remote_user,
618 remote_host);
619 if (r < 0) {
4bbccb02 620 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror_safe(r));
22f93314
JS
621 return PAM_SESSION_ERR;
622 }
623
624 r = sd_bus_message_open_container(m, 'a', "(sv)");
625 if (r < 0) {
4bbccb02 626 pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror_safe(r));
22f93314
JS
627 return PAM_SYSTEM_ERR;
628 }
629
630 r = append_session_memory_max(handle, m, memory_max);
631 if (r < 0)
632 return PAM_SESSION_ERR;
633
adc09af2
PW
634 r = append_session_runtime_max_sec(handle, m, runtime_max_sec);
635 if (r < 0)
636 return PAM_SESSION_ERR;
637
22f93314
JS
638 r = append_session_tasks_max(handle, m, tasks_max);
639 if (r < 0)
640 return PAM_SESSION_ERR;
641
642 r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight");
643 if (r < 0)
644 return PAM_SESSION_ERR;
645
646 r = append_session_cg_weight(handle, m, io_weight, "IOWeight");
647 if (r < 0)
648 return PAM_SESSION_ERR;
649
650 r = sd_bus_message_close_container(m);
651 if (r < 0) {
4bbccb02 652 pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror_safe(r));
22f93314
JS
653 return PAM_SYSTEM_ERR;
654 }
ce959314 655
22f93314 656 r = sd_bus_call(bus, m, 0, &error, &reply);
ffcfcb6b 657 if (r < 0) {
b80120c4 658 if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) {
2675747f 659 if (debug)
c7e93c4d 660 pam_syslog(handle, LOG_DEBUG, "Not creating session: %s", bus_error_message(&error, r));
b80120c4
DH
661 return PAM_SUCCESS;
662 } else {
663 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r));
8d46418e 664 return PAM_SESSION_ERR;
b80120c4 665 }
98a28fef 666 }
74fe1fe3 667
ffcfcb6b 668 r = sd_bus_message_read(reply,
baae0358 669 "soshusub",
ffcfcb6b
ZJS
670 &id,
671 &object_path,
672 &runtime_path,
673 &session_fd,
baae0358 674 &original_uid,
ffcfcb6b
ZJS
675 &seat,
676 &vtnr,
677 &existing);
678 if (r < 0) {
4bbccb02 679 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror_safe(r));
5a330cda 680 return PAM_SESSION_ERR;
98a28fef 681 }
74fe1fe3 682
ce959314
MS
683 if (debug)
684 pam_syslog(handle, LOG_DEBUG, "Reply from logind: "
baae0358
LP
685 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
686 id, object_path, runtime_path, session_fd, seat, vtnr, original_uid);
ce959314 687
d6baaa69
LP
688 r = update_environment(handle, "XDG_SESSION_ID", id);
689 if (r != PAM_SUCCESS)
5a330cda 690 return r;
8c6db833 691
baae0358
LP
692 if (original_uid == pw->pw_uid) {
693 /* Don't set $XDG_RUNTIME_DIR if the user we now
694 * authenticated for does not match the original user
695 * of the session. We do this in order not to result
696 * in privileged apps clobbering the runtime directory
697 * unnecessarily. */
698
b9217112
LP
699 if (validate_runtime_directory(handle, runtime_path, pw->pw_uid)) {
700 r = update_environment(handle, "XDG_RUNTIME_DIR", runtime_path);
701 if (r != PAM_SUCCESS)
702 return r;
703 }
00efd498
ZJS
704
705 r = export_legacy_dbus_address(handle, pw->pw_uid, runtime_path);
706 if (r != PAM_SUCCESS)
707 return r;
98a28fef 708 }
8c6db833 709
b2f74f07
LP
710 /* Most likely we got the session/type/class from environment variables, but might have gotten the data
711 * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this
712 * data is inherited into the session processes, and programs can rely on them to be initialized. */
713
714 r = update_environment(handle, "XDG_SESSION_TYPE", type);
715 if (r != PAM_SUCCESS)
716 return r;
717
718 r = update_environment(handle, "XDG_SESSION_CLASS", class);
719 if (r != PAM_SUCCESS)
720 return r;
721
722 r = update_environment(handle, "XDG_SESSION_DESKTOP", desktop);
723 if (r != PAM_SUCCESS)
724 return r;
725
d6baaa69
LP
726 r = update_environment(handle, "XDG_SEAT", seat);
727 if (r != PAM_SUCCESS)
728 return r;
bbc73283
LP
729
730 if (vtnr > 0) {
29d230f6 731 char buf[DECIMAL_STR_MAX(vtnr)];
baae0358 732 sprintf(buf, "%u", vtnr);
bbc73283 733
d6baaa69
LP
734 r = update_environment(handle, "XDG_VTNR", buf);
735 if (r != PAM_SUCCESS)
5a330cda 736 return r;
bbc73283
LP
737 }
738
77085881
LP
739 r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL);
740 if (r != PAM_SUCCESS) {
d9608d40 741 pam_syslog(handle, LOG_ERR, "Failed to install existing flag: %s", pam_strerror(handle, r));
5a330cda 742 return r;
77085881
LP
743 }
744
21c390cc 745 if (session_fd >= 0) {
85c08dc0 746 session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3);
5a330cda
ZJS
747 if (session_fd < 0) {
748 pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m");
749 return PAM_SESSION_ERR;
750 }
751
23e096cc 752 r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL);
21c390cc 753 if (r != PAM_SUCCESS) {
d9608d40 754 pam_syslog(handle, LOG_ERR, "Failed to install session fd: %s", pam_strerror(handle, r));
03e334a1 755 safe_close(session_fd);
5a330cda 756 return r;
21c390cc 757 }
98a28fef 758 }
8c6db833 759
ffcfcb6b 760 return PAM_SUCCESS;
98a28fef 761}
8c6db833 762
98a28fef
LP
763_public_ PAM_EXTERN int pam_sm_close_session(
764 pam_handle_t *handle,
765 int flags,
766 int argc, const char **argv) {
8c6db833 767
4afd3348
LP
768 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
769 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5f41d1f1 770 const void *existing = NULL;
75c8e3cf 771 const char *id;
75c8e3cf 772 int r;
8c6db833 773
ffcfcb6b 774 assert(handle);
75c8e3cf 775
77085881
LP
776 /* Only release session if it wasn't pre-existing when we
777 * tried to create it */
d487e2d6 778 (void) pam_get_data(handle, "systemd.existing", &existing);
77085881 779
75c8e3cf 780 id = pam_getenv(handle, "XDG_SESSION_ID");
77085881 781 if (id && !existing) {
75c8e3cf
LP
782
783 /* Before we go and close the FIFO we need to tell
784 * logind that this is a clean session shutdown, so
785 * that it doesn't just go and slaughter us
786 * immediately after closing the fd */
787
ffcfcb6b
ZJS
788 r = sd_bus_open_system(&bus);
789 if (r < 0) {
4bbccb02 790 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror_safe(r));
5f41d1f1 791 return PAM_SESSION_ERR;
75c8e3cf
LP
792 }
793
ffcfcb6b
ZJS
794 r = sd_bus_call_method(bus,
795 "org.freedesktop.login1",
796 "/org/freedesktop/login1",
797 "org.freedesktop.login1.Manager",
798 "ReleaseSession",
799 &error,
800 NULL,
801 "s",
802 id);
803 if (r < 0) {
5f41d1f1
LP
804 pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r));
805 return PAM_SESSION_ERR;
75c8e3cf
LP
806 }
807 }
808
5f41d1f1
LP
809 /* Note that we are knowingly leaking the FIFO fd here. This
810 * way, logind can watch us die. If we closed it here it would
811 * not have any clue when that is completed. Given that one
812 * cannot really have multiple PAM sessions open from the same
813 * process this means we will leak one FD at max. */
75c8e3cf 814
5f41d1f1 815 return PAM_SUCCESS;
8c6db833 816}