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