]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/pam-module.c
pam: use /proc/self/loginuid only if we have CAP_AUDIT_CONTROL
[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. */
224 if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
225 uint32_t u;
226 int r;
8c6db833 227
a838e6a1
LP
228 r = safe_atou32(s, &u);
229 free(s);
8c6db833 230
d90b9d27 231 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
2e225d65 232 *mode = SESSION_ID_AUDIT;
a838e6a1 233 return (uint64_t) u;
2e225d65 234 }
a838e6a1 235 }
8c6db833 236
a838e6a1
LP
237 /* Second attempt, use our own counter. */
238 if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
239 uint64_t counter;
240 ssize_t r;
8c6db833 241
a838e6a1 242 /* We do a bit of endianess swapping here, just to be
bb29785e
LP
243 * sure. /run should be machine specific anyway, and
244 * even mounted from tmpfs, so this byteswapping
245 * should really not be necessary. But then again, you
246 * never know, so let's avoid any risk. */
8c6db833 247
a838e6a1
LP
248 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
249 counter = 1;
250 else
251 counter = le64toh(counter) + 1;
8c6db833 252
a838e6a1
LP
253 if (lseek(fd, 0, SEEK_SET) == 0) {
254 uint64_t swapped = htole64(counter);
255
256 r = loop_write(fd, &swapped, sizeof(swapped), false);
257
258 if (r != sizeof(swapped))
259 r = -EIO;
260 } else
261 r = -errno;
8c6db833 262
a838e6a1
LP
263 close_nointr_nofail(fd);
264
2e225d65
LP
265 if (r >= 0) {
266 *mode = SESSION_ID_COUNTER;
a838e6a1 267 return counter;
2e225d65 268 }
a838e6a1
LP
269 }
270
2e225d65
LP
271 *mode = SESSION_ID_RANDOM;
272
a838e6a1
LP
273 /* Last attempt, pick a random value */
274 return (uint64_t) random_ull();
275}
1f73f0f1 276
8c6db833
LP
277static int get_user_data(
278 pam_handle_t *handle,
279 const char **ret_username,
280 struct passwd **ret_pw) {
281
d90b9d27
LP
282 const char *username = NULL;
283 struct passwd *pw = NULL;
8c6db833 284 int r;
d90b9d27
LP
285 bool have_loginuid = false;
286 char *s;
8c6db833
LP
287
288 assert(handle);
289 assert(ret_username);
290 assert(ret_pw);
291
ac123445
LP
292 if (have_effective_cap(CAP_AUDIT_CONTROL)) {
293 /* Only use audit login uid if we are executed with
294 * sufficient capabilities so that pam_loginuid could
295 * do its job. If we are lacking the CAP_AUDIT_CONTROL
296 * capabality we most likely are being run in a
297 * container and /proc/self/loginuid is useless since
298 * it probably contains a uid of the host system. */
d90b9d27 299
ac123445
LP
300 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
301 uint32_t u;
d90b9d27 302
ac123445
LP
303 r = safe_atou32(s, &u);
304 free(s);
305
306 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
307 have_loginuid = true;
308 pw = pam_modutil_getpwuid(handle, u);
309 }
d90b9d27 310 }
8c6db833
LP
311 }
312
d90b9d27
LP
313 if (!have_loginuid) {
314 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
315 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
316 return r;
317 }
318
319 if (!username || !*username) {
320 pam_syslog(handle, LOG_ERR, "User name not valid.");
321 return PAM_AUTH_ERR;
322 }
323
324 pw = pam_modutil_getpwnam(handle, username);
8c6db833
LP
325 }
326
d90b9d27 327 if (!pw) {
8c6db833
LP
328 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
329 return PAM_USER_UNKNOWN;
330 }
331
332 *ret_pw = pw;
d90b9d27 333 *ret_username = username ? username : pw->pw_name;
8c6db833
LP
334
335 return PAM_SUCCESS;
336}
337
74fe1fe3
LP
338static int create_user_group(
339 pam_handle_t *handle,
340 const char *controller,
341 const char *group,
342 struct passwd *pw,
343 bool attach,
344 bool remember) {
345
8c6db833
LP
346 int r;
347
348 assert(handle);
349 assert(group);
350
351 if (attach)
74fe1fe3 352 r = cg_create_and_attach(controller, group, 0);
8c6db833 353 else
74fe1fe3 354 r = cg_create(controller, group);
8c6db833
LP
355
356 if (r < 0) {
357 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
358 return PAM_SESSION_ERR;
359 }
360
672c48cc
LP
361 if (r > 0 && remember) {
362 /* Remember that it was us who created this group, and
363 * that hence we need to remove it too. This is a
364 * protection against removing the cgroup when run
365 * recursively. */
366 if ((r = pam_set_data(handle, "systemd.created", INT_TO_PTR(1), NULL)) != PAM_SUCCESS) {
367 pam_syslog(handle, LOG_ERR, "Failed to install created variable.");
368 return r;
369 }
370 }
371
74fe1fe3
LP
372 if ((r = cg_set_task_access(controller, group, 0644, pw->pw_uid, pw->pw_gid)) < 0 ||
373 (r = cg_set_group_access(controller, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
8c6db833
LP
374 pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
375 return PAM_SESSION_ERR;
376 }
377
378 return PAM_SUCCESS;
379}
380
b20c6be6
LP
381static int reset_group(
382 pam_handle_t *handle,
383 const char *controller) {
384
385 int r;
386
387 assert(handle);
388
389 if ((r = cg_attach(controller, "/", 0)) < 0) {
390 pam_syslog(handle, LOG_ERR, "Failed to reset cgroup for controller %s: %s", controller, strerror(-r));
391 return PAM_SESSION_ERR;
392 }
393
394 return PAM_SUCCESS;
395}
396
8c6db833
LP
397_public_ PAM_EXTERN int pam_sm_open_session(
398 pam_handle_t *handle,
399 int flags,
400 int argc, const char **argv) {
401
402 const char *username = NULL;
403 struct passwd *pw;
404 int r;
405 char *buf = NULL;
406 int lock_fd = -1;
407 bool create_session = true;
b20c6be6 408 char **controllers = NULL, **reset_controllers = NULL, **c;
1f73f0f1 409 char *cgroup_user_tree = NULL;
8c6db833
LP
410
411 assert(handle);
412
672c48cc 413 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
8c6db833 414
8c6db833
LP
415 /* Make this a NOP on non-systemd systems */
416 if (sd_booted() <= 0)
417 return PAM_SUCCESS;
418
e9fbc77c
LP
419 if (parse_argv(handle,
420 argc, argv,
421 &create_session, NULL, NULL,
422 &controllers, &reset_controllers,
423 NULL, NULL) < 0)
74fe1fe3
LP
424 return PAM_SESSION_ERR;
425
8c6db833
LP
426 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
427 goto finish;
428
1f73f0f1
LP
429 if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
430 pam_syslog(handle, LOG_ERR, "Failed to determine user cgroup tree: %s", strerror(-r));
431 r = PAM_SYSTEM_ERR;
432 goto finish;
433 }
434
8c6db833
LP
435 if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
436 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
437 r = PAM_SYSTEM_ERR;
438 goto finish;
439 }
440
441 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
442 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
443 r = PAM_SYSTEM_ERR;
444 goto finish;
445 }
446
bb29785e 447 /* Create /run/user/$USER */
8c6db833
LP
448 free(buf);
449 if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
450 r = PAM_BUF_ERR;
451 goto finish;
452 }
453
454 if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) {
455 pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m");
456 r = PAM_SYSTEM_ERR;
457 goto finish;
458 } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) {
459 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
460 goto finish;
461 }
462
463 free(buf);
464 buf = NULL;
465
466 if (create_session) {
a838e6a1 467 const char *id;
8c6db833
LP
468
469 /* Reuse or create XDG session ID */
a838e6a1 470 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
2e225d65 471 int mode;
a838e6a1 472
2e225d65 473 if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
8c6db833
LP
474 r = PAM_BUF_ERR;
475 goto finish;
476 }
477
2e225d65
LP
478 /* To avoid id clashes we add the session id
479 * source to our session ids. Note that the
480 * session id source might change during
481 * runtime, because a filesystem became
482 * writable or the system reconfigured. */
483 buf[strlen(buf)-1] =
484 mode != SESSION_ID_AUDIT ? (char) mode : 0;
485
a838e6a1
LP
486 if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) {
487 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
8c6db833
LP
488 goto finish;
489 }
490
a838e6a1
LP
491 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
492 pam_syslog(handle, LOG_ERR, "Failed to get session id.");
8c6db833
LP
493 r = PAM_SESSION_ERR;
494 goto finish;
495 }
496 }
497
1f73f0f1 498 r = asprintf(&buf, "%s/%s/%s", cgroup_user_tree, username, id);
8c6db833 499 } else
1f73f0f1 500 r = asprintf(&buf, "%s/%s/master", cgroup_user_tree, username);
8c6db833
LP
501
502 if (r < 0) {
503 r = PAM_BUF_ERR;
504 goto finish;
505 }
506
672c48cc
LP
507 pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
508
74fe1fe3 509 if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
8c6db833
LP
510 goto finish;
511
74fe1fe3
LP
512 /* The additional controllers don't really matter, so we
513 * ignore the return value */
514 STRV_FOREACH(c, controllers)
515 create_user_group(handle, *c, buf, pw, true, false);
516
b20c6be6
LP
517 STRV_FOREACH(c, reset_controllers)
518 reset_group(handle, *c);
519
8c6db833
LP
520 r = PAM_SUCCESS;
521
522finish:
523 free(buf);
524
525 if (lock_fd >= 0)
526 close_nointr_nofail(lock_fd);
527
74fe1fe3 528 strv_free(controllers);
b20c6be6 529 strv_free(reset_controllers);
74fe1fe3 530
1f73f0f1
LP
531 free(cgroup_user_tree);
532
8c6db833
LP
533 return r;
534}
535
536static int session_remains(pam_handle_t *handle, const char *user_path) {
35d2e7ec 537 int r;
8c6db833 538 bool remains = false;
35d2e7ec
LP
539 DIR *d;
540 char *subgroup;
8c6db833 541
35d2e7ec
LP
542 if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
543 return r;
8c6db833 544
35d2e7ec 545 while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
8c6db833 546
96a8cbfa 547 remains = !streq(subgroup, "master");
35d2e7ec 548 free(subgroup);
8c6db833 549
35d2e7ec
LP
550 if (remains)
551 break;
8c6db833
LP
552 }
553
35d2e7ec 554 closedir(d);
8c6db833 555
35d2e7ec
LP
556 if (r < 0)
557 return r;
8c6db833 558
35d2e7ec 559 return !!remains;
8c6db833
LP
560}
561
e9fbc77c
LP
562static bool check_user_lists(
563 pam_handle_t *handle,
564 uid_t uid,
565 char **kill_only_users,
566 char **kill_exclude_users) {
567
568 const char *name = NULL;
569 char **l;
570
571 assert(handle);
572
573 if (uid == 0)
574 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
575 else {
576 struct passwd *pw;
577
578 if ((pw = pam_modutil_getpwuid(handle, uid)))
579 name = pw->pw_name;
580 }
581
582 STRV_FOREACH(l, kill_exclude_users) {
583 uint32_t id;
584
585 if (safe_atou32(*l, &id) >= 0)
586 if ((uid_t) id == uid)
587 return false;
588
589 if (name && streq(name, *l))
590 return false;
591 }
592
593 if (strv_isempty(kill_only_users))
594 return true;
595
596 STRV_FOREACH(l, kill_only_users) {
597 uint32_t id;
598
599 if (safe_atou32(*l, &id) >= 0)
600 if ((uid_t) id == uid)
601 return true;
602
603 if (name && streq(name, *l))
604 return true;
605 }
606
607 return false;
608}
609
8c6db833
LP
610_public_ PAM_EXTERN int pam_sm_close_session(
611 pam_handle_t *handle,
612 int flags,
613 int argc, const char **argv) {
614
615 const char *username = NULL;
616 bool kill_session = false;
617 bool kill_user = false;
618 int lock_fd = -1, r;
619 char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
a838e6a1 620 const char *id;
8c6db833 621 struct passwd *pw;
672c48cc 622 const void *created = NULL;
e9fbc77c 623 char **controllers = NULL, **c, **kill_only_users = NULL, **kill_exclude_users = NULL;
1f73f0f1 624 char *cgroup_user_tree = NULL;
8c6db833
LP
625
626 assert(handle);
627
8c6db833
LP
628 /* Make this a NOP on non-systemd systems */
629 if (sd_booted() <= 0)
630 return PAM_SUCCESS;
631
e9fbc77c
LP
632 if (parse_argv(handle,
633 argc, argv,
634 NULL, &kill_session, &kill_user,
635 &controllers, NULL,
636 &kill_only_users, &kill_exclude_users) < 0)
74fe1fe3
LP
637 return PAM_SESSION_ERR;
638
8c6db833
LP
639 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
640 goto finish;
641
1f73f0f1
LP
642 if ((r = cg_get_user_path(&cgroup_user_tree)) < 0) {
643 pam_syslog(handle, LOG_ERR, "Failed to determine user cgroup tree: %s", strerror(-r));
644 r = PAM_SYSTEM_ERR;
645 goto finish;
646 }
647
8c6db833
LP
648 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
649 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
650 r = PAM_SYSTEM_ERR;
651 goto finish;
652 }
653
824a1d59 654 /* We are probably still in some session/user dir. Move ourselves out of the way as first step */
1f73f0f1 655 if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, cgroup_user_tree, 0)) < 0)
35d2e7ec
LP
656 pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
657
74fe1fe3 658 STRV_FOREACH(c, controllers)
1f73f0f1 659 if ((r = cg_attach(*c, cgroup_user_tree, 0)) < 0)
74fe1fe3
LP
660 pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r));
661
1f73f0f1 662 if (asprintf(&user_path, "%s/%s", cgroup_user_tree, username) < 0) {
8c6db833
LP
663 r = PAM_BUF_ERR;
664 goto finish;
665 }
666
672c48cc
LP
667 pam_get_data(handle, "systemd.created", &created);
668
669 if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) {
8c6db833 670
1f73f0f1
LP
671 if (asprintf(&session_path, "%s/%s/%s", cgroup_user_tree, username, id) < 0 ||
672 asprintf(&nosession_path, "%s/%s/master", cgroup_user_tree, username) < 0) {
8c6db833
LP
673 r = PAM_BUF_ERR;
674 goto finish;
675 }
676
e9fbc77c 677 if (kill_session && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
672c48cc
LP
678 pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
679
35d2e7ec
LP
680 /* Kill processes in session cgroup, and delete it */
681 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
8c6db833 682 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
35d2e7ec 683 } else {
672c48cc
LP
684 pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
685
824a1d59
LP
686 /* Migrate processes from session to user
687 * cgroup. First, try to create the user group
688 * in case it doesn't exist yet. Also, delete
689 * the session group. */
74fe1fe3 690 create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false);
8c6db833 691
35d2e7ec 692 if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
8c6db833
LP
693 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
694 }
74fe1fe3
LP
695
696 STRV_FOREACH(c, controllers) {
697 create_user_group(handle, *c, nosession_path, pw, false, false);
698
699 if ((r = cg_migrate_recursive(*c, session_path, nosession_path, false, true)) < 0)
700 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r));
701 }
8c6db833
LP
702 }
703
704 /* GC user tree */
c6c18be3 705 cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
8c6db833
LP
706
707 if ((r = session_remains(handle, user_path)) < 0)
708 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
709
710 /* Kill user processes not attached to any session */
e9fbc77c 711 if (kill_user && r == 0 && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
8c6db833 712
824a1d59 713 /* Kill user cgroup */
35d2e7ec 714 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
8c6db833
LP
715 pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
716 } else {
717
c6c18be3 718 if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
8c6db833
LP
719 pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
720
35d2e7ec
LP
721 /* Remove user cgroup */
722 if (r > 0) {
723 if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
724 pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
725
726 /* If we managed to find somebody, don't cleanup the cgroup. */
727 } else if (r == 0)
8c6db833
LP
728 r = -EBUSY;
729 }
730
74fe1fe3
LP
731 STRV_FOREACH(c, controllers)
732 cg_trim(*c, user_path, true);
733
8c6db833
LP
734 if (r >= 0) {
735 const char *runtime_dir;
736
8c6db833
LP
737 if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
738 if ((r = rm_rf(runtime_dir, false, true)) < 0)
739 pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r));
740 }
741
672c48cc
LP
742 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
743
8c6db833
LP
744 r = PAM_SUCCESS;
745
746finish:
747 if (lock_fd >= 0)
748 close_nointr_nofail(lock_fd);
749
750 free(session_path);
751 free(nosession_path);
752 free(user_path);
753
74fe1fe3 754 strv_free(controllers);
e9fbc77c
LP
755 strv_free(kill_exclude_users);
756 strv_free(kill_only_users);
74fe1fe3 757
1f73f0f1
LP
758 free(cgroup_user_tree);
759
8c6db833
LP
760 return r;
761}