]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/pam-module.c
dbus: don't hit assert when dumping properties
[thirdparty/systemd.git] / src / pam-module.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8c6db833
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/file.h>
25#include <pwd.h>
a838e6a1 26#include <endian.h>
ac123445 27#include <sys/capability.h>
8c6db833
LP
28
29#include <security/pam_modules.h>
30#include <security/_pam_macros.h>
31#include <security/pam_modutil.h>
32#include <security/pam_ext.h>
33#include <security/pam_misc.h>
34
8c6db833
LP
35#include "util.h"
36#include "cgroup-util.h"
37#include "macro.h"
38#include "sd-daemon.h"
74fe1fe3 39#include "strv.h"
8c6db833
LP
40
41static int parse_argv(pam_handle_t *handle,
42 int argc, const char **argv,
43 bool *create_session,
44 bool *kill_session,
74fe1fe3 45 bool *kill_user,
b20c6be6 46 char ***controllers,
e9fbc77c
LP
47 char ***reset_controllers,
48 char ***kill_only_users,
49 char ***kill_exclude_users) {
8c6db833
LP
50
51 unsigned i;
b20c6be6 52 bool reset_controller_set = false;
e9fbc77c 53 bool kill_exclude_users_set = false;
8c6db833
LP
54
55 assert(argc >= 0);
56 assert(argc == 0 || argv);
57
58 for (i = 0; i < (unsigned) argc; i++) {
59 int k;
60
61 if (startswith(argv[i], "create-session=")) {
62 if ((k = parse_boolean(argv[i] + 15)) < 0) {
63 pam_syslog(handle, LOG_ERR, "Failed to parse create-session= argument.");
64 return k;
65 }
66
67 if (create_session)
68 *create_session = k;
74fe1fe3 69
8c6db833
LP
70 } else if (startswith(argv[i], "kill-session=")) {
71 if ((k = parse_boolean(argv[i] + 13)) < 0) {
72 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
73 return k;
74 }
75
76 if (kill_session)
77 *kill_session = k;
78
79 } else if (startswith(argv[i], "kill-user=")) {
80 if ((k = parse_boolean(argv[i] + 10)) < 0) {
81 pam_syslog(handle, LOG_ERR, "Failed to parse kill-user= argument.");
82 return k;
83 }
84
85 if (kill_user)
86 *kill_user = k;
74fe1fe3
LP
87
88 } else if (startswith(argv[i], "controllers=")) {
89
90 if (controllers) {
91 char **l;
92
93 if (!(l = strv_split(argv[i] + 12, ","))) {
94 pam_syslog(handle, LOG_ERR, "Out of memory.");
95 return -ENOMEM;
96 }
97
98 strv_free(*controllers);
99 *controllers = l;
100 }
101
b20c6be6
LP
102 } else if (startswith(argv[i], "reset-controllers=")) {
103
104 if (reset_controllers) {
105 char **l;
106
107 if (!(l = strv_split(argv[i] + 18, ","))) {
108 pam_syslog(handle, LOG_ERR, "Out of memory.");
109 return -ENOMEM;
110 }
111
112 strv_free(*reset_controllers);
113 *reset_controllers = l;
114 }
115
116 reset_controller_set = true;
74fe1fe3 117
e9fbc77c
LP
118 } else if (startswith(argv[i], "kill-only-users=")) {
119
120 if (kill_only_users) {
121 char **l;
122
123 if (!(l = strv_split(argv[i] + 16, ","))) {
124 pam_syslog(handle, LOG_ERR, "Out of memory.");
125 return -ENOMEM;
126 }
127
128 strv_free(*kill_only_users);
129 *kill_only_users = l;
130 }
131
132 } else if (startswith(argv[i], "kill-exclude-users=")) {
133
134 if (kill_exclude_users) {
135 char **l;
136
137 if (!(l = strv_split(argv[i] + 19, ","))) {
138 pam_syslog(handle, LOG_ERR, "Out of memory.");
139 return -ENOMEM;
140 }
141
142 strv_free(*kill_exclude_users);
143 *kill_exclude_users = l;
144 }
145
146 kill_exclude_users_set = true;
147
8c6db833
LP
148 } else {
149 pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
150 return -EINVAL;
151 }
152 }
153
b20c6be6 154 if (!reset_controller_set && reset_controllers) {
74fe1fe3
LP
155 char **l;
156
157 if (!(l = strv_new("cpu", NULL))) {
158 pam_syslog(handle, LOG_ERR, "Out of memory");
159 return -ENOMEM;
160 }
161
b20c6be6 162 *reset_controllers = l;
74fe1fe3
LP
163 }
164
165 if (controllers)
166 strv_remove(*controllers, "name=systemd");
167
b20c6be6
LP
168 if (reset_controllers)
169 strv_remove(*reset_controllers, "name=systemd");
170
8c6db833
LP
171 if (kill_session && *kill_session && kill_user)
172 *kill_user = true;
173
e9fbc77c
LP
174 if (!kill_exclude_users_set && kill_exclude_users) {
175 char **l;
176
177 if (!(l = strv_new("root", NULL))) {
178 pam_syslog(handle, LOG_ERR, "Out of memory");
179 return -ENOMEM;
180 }
181
182 *kill_exclude_users = l;
183 }
184
8c6db833
LP
185 return 0;
186}
187
188static int open_file_and_lock(const char *fn) {
189 int fd;
190
191 assert(fn);
192
193 if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0)
194 return -errno;
195
2e225d65
LP
196 /* The BSD socket semantics are a lot nicer than those of
197 * POSIX locks. Which is why we use flock() here. BSD locking
198 * does not work across NFS which however is not needed here
199 * as the filesystems in question should be local, and only
200 * locally accessible, and most likely even tmpfs. */
201
90102b22 202 if (flock(fd, LOCK_EX) < 0) {
90102b22 203 close_nointr_nofail(fd);
1f73f0f1 204 return -errno;
90102b22 205 }
8c6db833
LP
206
207 return fd;
208}
209
2e225d65
LP
210enum {
211 SESSION_ID_AUDIT = 'a',
212 SESSION_ID_COUNTER = 'c',
213 SESSION_ID_RANDOM = 'r'
214};
215
216static uint64_t get_session_id(int *mode) {
a838e6a1
LP
217 char *s;
218 int fd;
8c6db833 219
2e225d65
LP
220 assert(mode);
221
a838e6a1
LP
222 /* First attempt: let's use the session ID of the audit
223 * system, if it is available. */
81481c99
LP
224 if (have_effective_cap(CAP_AUDIT_CONTROL) > 0)
225 if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
226 uint32_t u;
227 int r;
8c6db833 228
81481c99
LP
229 r = safe_atou32(s, &u);
230 free(s);
8c6db833 231
81481c99
LP
232 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
233 *mode = SESSION_ID_AUDIT;
234 return (uint64_t) u;
235 }
2e225d65 236 }
8c6db833 237
a838e6a1
LP
238 /* Second attempt, use our own counter. */
239 if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
240 uint64_t counter;
241 ssize_t r;
8c6db833 242
a838e6a1 243 /* We do a bit of endianess swapping here, just to be
bb29785e
LP
244 * sure. /run should be machine specific anyway, and
245 * even mounted from tmpfs, so this byteswapping
246 * should really not be necessary. But then again, you
247 * never know, so let's avoid any risk. */
8c6db833 248
a838e6a1
LP
249 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
250 counter = 1;
251 else
252 counter = le64toh(counter) + 1;
8c6db833 253
a838e6a1
LP
254 if (lseek(fd, 0, SEEK_SET) == 0) {
255 uint64_t swapped = htole64(counter);
256
257 r = loop_write(fd, &swapped, sizeof(swapped), false);
258
259 if (r != sizeof(swapped))
260 r = -EIO;
261 } else
262 r = -errno;
8c6db833 263
a838e6a1
LP
264 close_nointr_nofail(fd);
265
2e225d65
LP
266 if (r >= 0) {
267 *mode = SESSION_ID_COUNTER;
a838e6a1 268 return counter;
2e225d65 269 }
a838e6a1
LP
270 }
271
2e225d65
LP
272 *mode = SESSION_ID_RANDOM;
273
a838e6a1
LP
274 /* Last attempt, pick a random value */
275 return (uint64_t) random_ull();
276}
1f73f0f1 277
8c6db833
LP
278static int get_user_data(
279 pam_handle_t *handle,
280 const char **ret_username,
281 struct passwd **ret_pw) {
282
d90b9d27
LP
283 const char *username = NULL;
284 struct passwd *pw = NULL;
8c6db833 285 int r;
d90b9d27
LP
286 bool have_loginuid = false;
287 char *s;
8c6db833
LP
288
289 assert(handle);
290 assert(ret_username);
291 assert(ret_pw);
292
81481c99 293 if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) {
ac123445
LP
294 /* Only use audit login uid if we are executed with
295 * sufficient capabilities so that pam_loginuid could
296 * do its job. If we are lacking the CAP_AUDIT_CONTROL
297 * capabality we most likely are being run in a
298 * container and /proc/self/loginuid is useless since
299 * it probably contains a uid of the host system. */
d90b9d27 300
ac123445
LP
301 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
302 uint32_t u;
d90b9d27 303
ac123445
LP
304 r = safe_atou32(s, &u);
305 free(s);
306
307 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
308 have_loginuid = true;
309 pw = pam_modutil_getpwuid(handle, u);
310 }
d90b9d27 311 }
8c6db833
LP
312 }
313
d90b9d27
LP
314 if (!have_loginuid) {
315 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
316 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
317 return r;
318 }
319
320 if (!username || !*username) {
321 pam_syslog(handle, LOG_ERR, "User name not valid.");
322 return PAM_AUTH_ERR;
323 }
324
325 pw = pam_modutil_getpwnam(handle, username);
8c6db833
LP
326 }
327
d90b9d27 328 if (!pw) {
8c6db833
LP
329 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
330 return PAM_USER_UNKNOWN;
331 }
332
333 *ret_pw = pw;
d90b9d27 334 *ret_username = username ? username : pw->pw_name;
8c6db833
LP
335
336 return PAM_SUCCESS;
337}
338
74fe1fe3
LP
339static int create_user_group(
340 pam_handle_t *handle,
341 const char *controller,
342 const char *group,
343 struct passwd *pw,
344 bool attach,
345 bool remember) {
346
8c6db833
LP
347 int r;
348
349 assert(handle);
350 assert(group);
351
352 if (attach)
74fe1fe3 353 r = cg_create_and_attach(controller, group, 0);
8c6db833 354 else
74fe1fe3 355 r = cg_create(controller, group);
8c6db833
LP
356
357 if (r < 0) {
358 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
359 return PAM_SESSION_ERR;
360 }
361
672c48cc
LP
362 if (r > 0 && remember) {
363 /* Remember that it was us who created this group, and
364 * that hence we need to remove it too. This is a
365 * protection against removing the cgroup when run
366 * recursively. */
367 if ((r = pam_set_data(handle, "systemd.created", INT_TO_PTR(1), NULL)) != PAM_SUCCESS) {
368 pam_syslog(handle, LOG_ERR, "Failed to install created variable.");
369 return r;
370 }
371 }
372
74fe1fe3
LP
373 if ((r = cg_set_task_access(controller, group, 0644, pw->pw_uid, pw->pw_gid)) < 0 ||
374 (r = cg_set_group_access(controller, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
8c6db833
LP
375 pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
376 return PAM_SESSION_ERR;
377 }
378
379 return PAM_SUCCESS;
380}
381
b20c6be6
LP
382static int reset_group(
383 pam_handle_t *handle,
384 const char *controller) {
385
386 int r;
387
388 assert(handle);
389
390 if ((r = cg_attach(controller, "/", 0)) < 0) {
391 pam_syslog(handle, LOG_ERR, "Failed to reset cgroup for controller %s: %s", controller, strerror(-r));
392 return PAM_SESSION_ERR;
393 }
394
395 return PAM_SUCCESS;
396}
397
8c6db833
LP
398_public_ PAM_EXTERN int pam_sm_open_session(
399 pam_handle_t *handle,
400 int flags,
401 int argc, const char **argv) {
402
403 const char *username = NULL;
404 struct passwd *pw;
405 int r;
406 char *buf = NULL;
407 int lock_fd = -1;
408 bool create_session = true;
b20c6be6 409 char **controllers = NULL, **reset_controllers = NULL, **c;
1f73f0f1 410 char *cgroup_user_tree = NULL;
8c6db833
LP
411
412 assert(handle);
413
672c48cc 414 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
8c6db833 415
8c6db833
LP
416 /* Make this a NOP on non-systemd systems */
417 if (sd_booted() <= 0)
418 return PAM_SUCCESS;
419
e9fbc77c
LP
420 if (parse_argv(handle,
421 argc, argv,
422 &create_session, NULL, NULL,
423 &controllers, &reset_controllers,
424 NULL, NULL) < 0)
74fe1fe3
LP
425 return PAM_SESSION_ERR;
426
8c6db833
LP
427 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
428 goto finish;
429
1f73f0f1
LP
430 if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
431 pam_syslog(handle, LOG_ERR, "Failed to determine user cgroup tree: %s", strerror(-r));
432 r = PAM_SYSTEM_ERR;
433 goto finish;
434 }
435
8c6db833
LP
436 if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
437 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
438 r = PAM_SYSTEM_ERR;
439 goto finish;
440 }
441
442 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
443 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
444 r = PAM_SYSTEM_ERR;
445 goto finish;
446 }
447
bb29785e 448 /* Create /run/user/$USER */
8c6db833
LP
449 free(buf);
450 if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
451 r = PAM_BUF_ERR;
452 goto finish;
453 }
454
455 if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) {
456 pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m");
457 r = PAM_SYSTEM_ERR;
458 goto finish;
459 } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) {
460 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
461 goto finish;
462 }
463
464 free(buf);
465 buf = NULL;
466
467 if (create_session) {
a838e6a1 468 const char *id;
8c6db833
LP
469
470 /* Reuse or create XDG session ID */
a838e6a1 471 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
2e225d65 472 int mode;
a838e6a1 473
2e225d65 474 if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
8c6db833
LP
475 r = PAM_BUF_ERR;
476 goto finish;
477 }
478
2e225d65
LP
479 /* To avoid id clashes we add the session id
480 * source to our session ids. Note that the
481 * session id source might change during
482 * runtime, because a filesystem became
483 * writable or the system reconfigured. */
484 buf[strlen(buf)-1] =
485 mode != SESSION_ID_AUDIT ? (char) mode : 0;
486
a838e6a1
LP
487 if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) {
488 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
8c6db833
LP
489 goto finish;
490 }
491
a838e6a1
LP
492 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
493 pam_syslog(handle, LOG_ERR, "Failed to get session id.");
8c6db833
LP
494 r = PAM_SESSION_ERR;
495 goto finish;
496 }
497 }
498
1f73f0f1 499 r = asprintf(&buf, "%s/%s/%s", cgroup_user_tree, username, id);
8c6db833 500 } else
1f73f0f1 501 r = asprintf(&buf, "%s/%s/master", cgroup_user_tree, username);
8c6db833
LP
502
503 if (r < 0) {
504 r = PAM_BUF_ERR;
505 goto finish;
506 }
507
672c48cc
LP
508 pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
509
74fe1fe3 510 if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
8c6db833
LP
511 goto finish;
512
74fe1fe3
LP
513 /* The additional controllers don't really matter, so we
514 * ignore the return value */
515 STRV_FOREACH(c, controllers)
516 create_user_group(handle, *c, buf, pw, true, false);
517
b20c6be6
LP
518 STRV_FOREACH(c, reset_controllers)
519 reset_group(handle, *c);
520
8c6db833
LP
521 r = PAM_SUCCESS;
522
523finish:
524 free(buf);
525
526 if (lock_fd >= 0)
527 close_nointr_nofail(lock_fd);
528
74fe1fe3 529 strv_free(controllers);
b20c6be6 530 strv_free(reset_controllers);
74fe1fe3 531
1f73f0f1
LP
532 free(cgroup_user_tree);
533
8c6db833
LP
534 return r;
535}
536
537static int session_remains(pam_handle_t *handle, const char *user_path) {
35d2e7ec 538 int r;
8c6db833 539 bool remains = false;
35d2e7ec
LP
540 DIR *d;
541 char *subgroup;
8c6db833 542
35d2e7ec
LP
543 if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
544 return r;
8c6db833 545
35d2e7ec 546 while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
8c6db833 547
96a8cbfa 548 remains = !streq(subgroup, "master");
35d2e7ec 549 free(subgroup);
8c6db833 550
35d2e7ec
LP
551 if (remains)
552 break;
8c6db833
LP
553 }
554
35d2e7ec 555 closedir(d);
8c6db833 556
35d2e7ec
LP
557 if (r < 0)
558 return r;
8c6db833 559
35d2e7ec 560 return !!remains;
8c6db833
LP
561}
562
e9fbc77c
LP
563static bool check_user_lists(
564 pam_handle_t *handle,
565 uid_t uid,
566 char **kill_only_users,
567 char **kill_exclude_users) {
568
569 const char *name = NULL;
570 char **l;
571
572 assert(handle);
573
574 if (uid == 0)
575 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
576 else {
577 struct passwd *pw;
578
579 if ((pw = pam_modutil_getpwuid(handle, uid)))
580 name = pw->pw_name;
581 }
582
583 STRV_FOREACH(l, kill_exclude_users) {
584 uint32_t id;
585
586 if (safe_atou32(*l, &id) >= 0)
587 if ((uid_t) id == uid)
588 return false;
589
590 if (name && streq(name, *l))
591 return false;
592 }
593
594 if (strv_isempty(kill_only_users))
595 return true;
596
597 STRV_FOREACH(l, kill_only_users) {
598 uint32_t id;
599
600 if (safe_atou32(*l, &id) >= 0)
601 if ((uid_t) id == uid)
602 return true;
603
604 if (name && streq(name, *l))
605 return true;
606 }
607
608 return false;
609}
610
8c6db833
LP
611_public_ PAM_EXTERN int pam_sm_close_session(
612 pam_handle_t *handle,
613 int flags,
614 int argc, const char **argv) {
615
616 const char *username = NULL;
617 bool kill_session = false;
618 bool kill_user = false;
619 int lock_fd = -1, r;
620 char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
a838e6a1 621 const char *id;
8c6db833 622 struct passwd *pw;
672c48cc 623 const void *created = NULL;
e9fbc77c 624 char **controllers = NULL, **c, **kill_only_users = NULL, **kill_exclude_users = NULL;
1f73f0f1 625 char *cgroup_user_tree = NULL;
8c6db833
LP
626
627 assert(handle);
628
8c6db833
LP
629 /* Make this a NOP on non-systemd systems */
630 if (sd_booted() <= 0)
631 return PAM_SUCCESS;
632
e9fbc77c
LP
633 if (parse_argv(handle,
634 argc, argv,
635 NULL, &kill_session, &kill_user,
636 &controllers, NULL,
637 &kill_only_users, &kill_exclude_users) < 0)
74fe1fe3
LP
638 return PAM_SESSION_ERR;
639
8c6db833
LP
640 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
641 goto finish;
642
1f73f0f1
LP
643 if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
644 pam_syslog(handle, LOG_ERR, "Failed to determine user cgroup tree: %s", strerror(-r));
645 r = PAM_SYSTEM_ERR;
646 goto finish;
647 }
648
8c6db833
LP
649 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
650 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
651 r = PAM_SYSTEM_ERR;
652 goto finish;
653 }
654
824a1d59 655 /* We are probably still in some session/user dir. Move ourselves out of the way as first step */
1f73f0f1 656 if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, cgroup_user_tree, 0)) < 0)
35d2e7ec
LP
657 pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
658
74fe1fe3 659 STRV_FOREACH(c, controllers)
1f73f0f1 660 if ((r = cg_attach(*c, cgroup_user_tree, 0)) < 0)
74fe1fe3
LP
661 pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r));
662
1f73f0f1 663 if (asprintf(&user_path, "%s/%s", cgroup_user_tree, username) < 0) {
8c6db833
LP
664 r = PAM_BUF_ERR;
665 goto finish;
666 }
667
672c48cc
LP
668 pam_get_data(handle, "systemd.created", &created);
669
670 if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) {
8c6db833 671
1f73f0f1
LP
672 if (asprintf(&session_path, "%s/%s/%s", cgroup_user_tree, username, id) < 0 ||
673 asprintf(&nosession_path, "%s/%s/master", cgroup_user_tree, username) < 0) {
8c6db833
LP
674 r = PAM_BUF_ERR;
675 goto finish;
676 }
677
e9fbc77c 678 if (kill_session && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
672c48cc
LP
679 pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
680
35d2e7ec
LP
681 /* Kill processes in session cgroup, and delete it */
682 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
8c6db833 683 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
35d2e7ec 684 } else {
672c48cc
LP
685 pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
686
824a1d59
LP
687 /* Migrate processes from session to user
688 * cgroup. First, try to create the user group
689 * in case it doesn't exist yet. Also, delete
690 * the session group. */
74fe1fe3 691 create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false);
8c6db833 692
35d2e7ec 693 if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
8c6db833
LP
694 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
695 }
74fe1fe3
LP
696
697 STRV_FOREACH(c, controllers) {
698 create_user_group(handle, *c, nosession_path, pw, false, false);
699
700 if ((r = cg_migrate_recursive(*c, session_path, nosession_path, false, true)) < 0)
701 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r));
702 }
8c6db833
LP
703 }
704
705 /* GC user tree */
c6c18be3 706 cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
8c6db833
LP
707
708 if ((r = session_remains(handle, user_path)) < 0)
709 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
710
711 /* Kill user processes not attached to any session */
e9fbc77c 712 if (kill_user && r == 0 && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
8c6db833 713
824a1d59 714 /* Kill user cgroup */
35d2e7ec 715 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
8c6db833
LP
716 pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
717 } else {
718
c6c18be3 719 if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
8c6db833
LP
720 pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
721
35d2e7ec
LP
722 /* Remove user cgroup */
723 if (r > 0) {
724 if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
725 pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
726
727 /* If we managed to find somebody, don't cleanup the cgroup. */
728 } else if (r == 0)
8c6db833
LP
729 r = -EBUSY;
730 }
731
74fe1fe3
LP
732 STRV_FOREACH(c, controllers)
733 cg_trim(*c, user_path, true);
734
8c6db833
LP
735 if (r >= 0) {
736 const char *runtime_dir;
737
8c6db833
LP
738 if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
739 if ((r = rm_rf(runtime_dir, false, true)) < 0)
740 pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r));
741 }
742
672c48cc
LP
743 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
744
8c6db833
LP
745 r = PAM_SUCCESS;
746
747finish:
748 if (lock_fd >= 0)
749 close_nointr_nofail(lock_fd);
750
751 free(session_path);
752 free(nosession_path);
753 free(user_path);
754
74fe1fe3 755 strv_free(controllers);
e9fbc77c
LP
756 strv_free(kill_exclude_users);
757 strv_free(kill_only_users);
74fe1fe3 758
1f73f0f1
LP
759 free(cgroup_user_tree);
760
8c6db833
LP
761 return r;
762}