chiark / gitweb /
b4130bbdd6e3e814ff9f6504cfb54073bfbb6206
[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 #include <sys/capability.h>
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
35 #include "util.h"
36 #include "cgroup-util.h"
37 #include "macro.h"
38 #include "sd-daemon.h"
39 #include "strv.h"
40
41 static int parse_argv(pam_handle_t *handle,
42                       int argc, const char **argv,
43                       bool *create_session,
44                       bool *kill_session,
45                       bool *kill_user,
46                       char ***controllers,
47                       char ***reset_controllers,
48                       char ***kill_only_users,
49                       char ***kill_exclude_users) {
50
51         unsigned i;
52         bool reset_controller_set = false;
53         bool kill_exclude_users_set = false;
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;
69
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;
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
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;
117
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
148                 } else {
149                         pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
150                         return -EINVAL;
151                 }
152         }
153
154         if (!reset_controller_set && reset_controllers) {
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
162                 *reset_controllers = l;
163         }
164
165         if (controllers)
166                 strv_remove(*controllers, "name=systemd");
167
168         if (reset_controllers)
169                 strv_remove(*reset_controllers, "name=systemd");
170
171         if (kill_session && *kill_session && kill_user)
172                 *kill_user = true;
173
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
185         return 0;
186 }
187
188 static 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
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
202         if (flock(fd, LOCK_EX) < 0) {
203                 close_nointr_nofail(fd);
204                 return -errno;
205         }
206
207         return fd;
208 }
209
210 enum {
211         SESSION_ID_AUDIT = 'a',
212         SESSION_ID_COUNTER = 'c',
213         SESSION_ID_RANDOM = 'r'
214 };
215
216 static uint64_t get_session_id(int *mode) {
217         char *s;
218         int fd;
219
220         assert(mode);
221
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;
227
228                 r = safe_atou32(s, &u);
229                 free(s);
230
231                 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
232                         *mode = SESSION_ID_AUDIT;
233                         return (uint64_t) u;
234                 }
235         }
236
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;
241
242                 /* We do a bit of endianess swapping here, just to be
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. */
247
248                 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
249                         counter = 1;
250                 else
251                         counter = le64toh(counter) + 1;
252
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;
262
263                 close_nointr_nofail(fd);
264
265                 if (r >= 0) {
266                         *mode = SESSION_ID_COUNTER;
267                         return counter;
268                 }
269         }
270
271         *mode = SESSION_ID_RANDOM;
272
273         /* Last attempt, pick a random value */
274         return (uint64_t) random_ull();
275 }
276
277 static int get_user_data(
278                 pam_handle_t *handle,
279                 const char **ret_username,
280                 struct passwd **ret_pw) {
281
282         const char *username = NULL;
283         struct passwd *pw = NULL;
284         int r;
285         bool have_loginuid = false;
286         char *s;
287
288         assert(handle);
289         assert(ret_username);
290         assert(ret_pw);
291
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. */
299
300                 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
301                         uint32_t u;
302
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                         }
310                 }
311         }
312
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);
325         }
326
327         if (!pw) {
328                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
329                 return PAM_USER_UNKNOWN;
330         }
331
332         *ret_pw = pw;
333         *ret_username = username ? username : pw->pw_name;
334
335         return PAM_SUCCESS;
336 }
337
338 static 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
346         int r;
347
348         assert(handle);
349         assert(group);
350
351         if (attach)
352                 r = cg_create_and_attach(controller, group, 0);
353         else
354                 r = cg_create(controller, group);
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
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
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) {
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
381 static 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
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;
408         char **controllers = NULL, **reset_controllers = NULL, **c;
409         char *cgroup_user_tree = NULL;
410
411         assert(handle);
412
413         /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
414
415         /* Make this a NOP on non-systemd systems */
416         if (sd_booted() <= 0)
417                 return PAM_SUCCESS;
418
419         if (parse_argv(handle,
420                        argc, argv,
421                        &create_session, NULL, NULL,
422                        &controllers, &reset_controllers,
423                        NULL, NULL) < 0)
424                 return PAM_SESSION_ERR;
425
426         if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
427                 goto finish;
428
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
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
447         /* Create /run/user/$USER */
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) {
467                 const char *id;
468
469                 /* Reuse or create XDG session ID */
470                 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
471                         int mode;
472
473                         if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
474                                 r = PAM_BUF_ERR;
475                                 goto finish;
476                         }
477
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
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.");
488                                 goto finish;
489                         }
490
491                         if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
492                                 pam_syslog(handle, LOG_ERR, "Failed to get session id.");
493                                 r = PAM_SESSION_ERR;
494                                 goto finish;
495                         }
496                 }
497
498                 r = asprintf(&buf, "%s/%s/%s", cgroup_user_tree, username, id);
499         } else
500                 r = asprintf(&buf, "%s/%s/master", cgroup_user_tree, username);
501
502         if (r < 0) {
503                 r = PAM_BUF_ERR;
504                 goto finish;
505         }
506
507         pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
508
509         if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
510                 goto finish;
511
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
517         STRV_FOREACH(c, reset_controllers)
518                 reset_group(handle, *c);
519
520         r = PAM_SUCCESS;
521
522 finish:
523         free(buf);
524
525         if (lock_fd >= 0)
526                 close_nointr_nofail(lock_fd);
527
528         strv_free(controllers);
529         strv_free(reset_controllers);
530
531         free(cgroup_user_tree);
532
533         return r;
534 }
535
536 static int session_remains(pam_handle_t *handle, const char *user_path) {
537         int r;
538         bool remains = false;
539         DIR *d;
540         char *subgroup;
541
542         if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
543                 return r;
544
545         while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
546
547                 remains = !streq(subgroup, "master");
548                 free(subgroup);
549
550                 if (remains)
551                         break;
552         }
553
554         closedir(d);
555
556         if (r < 0)
557                 return r;
558
559         return !!remains;
560 }
561
562 static 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
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;
620         const char *id;
621         struct passwd *pw;
622         const void *created = NULL;
623         char **controllers = NULL, **c, **kill_only_users = NULL, **kill_exclude_users = NULL;
624         char *cgroup_user_tree = NULL;
625
626         assert(handle);
627
628         /* Make this a NOP on non-systemd systems */
629         if (sd_booted() <= 0)
630                 return PAM_SUCCESS;
631
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)
637                 return PAM_SESSION_ERR;
638
639         if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
640                 goto finish;
641
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
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
654         /* We are probably still in some session/user dir. Move ourselves out of the way as first step */
655         if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, cgroup_user_tree, 0)) < 0)
656                 pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
657
658         STRV_FOREACH(c, controllers)
659                 if ((r = cg_attach(*c, cgroup_user_tree, 0)) < 0)
660                         pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r));
661
662         if (asprintf(&user_path, "%s/%s", cgroup_user_tree, username) < 0) {
663                 r = PAM_BUF_ERR;
664                 goto finish;
665         }
666
667         pam_get_data(handle, "systemd.created", &created);
668
669         if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) {
670
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) {
673                         r = PAM_BUF_ERR;
674                         goto finish;
675                 }
676
677                 if (kill_session && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users))  {
678                         pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
679
680                         /* Kill processes in session cgroup, and delete it */
681                         if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
682                                 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
683                 } else {
684                         pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
685
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. */
690                         create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false);
691
692                         if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
693                                 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
694                 }
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                 }
702         }
703
704         /* GC user tree */
705         cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
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 */
711         if (kill_user && r == 0 && check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users)) {
712
713                 /* Kill user cgroup */
714                 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
715                         pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
716         } else {
717
718                 if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
719                         pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
720
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)
728                         r = -EBUSY;
729         }
730
731         STRV_FOREACH(c, controllers)
732                 cg_trim(*c, user_path, true);
733
734         if (r >= 0) {
735                 const char *runtime_dir;
736
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
742         /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
743
744         r = PAM_SUCCESS;
745
746 finish:
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
754         strv_free(controllers);
755         strv_free(kill_exclude_users);
756         strv_free(kill_only_users);
757
758         free(cgroup_user_tree);
759
760         return r;
761 }