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