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