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