1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
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>
35 #include "cgroup-util.h"
37 #include "sd-daemon.h"
40 static int parse_argv(pam_handle_t *handle,
41 int argc, const char **argv,
45 char ***controllers) {
48 bool controller_set = false;
51 assert(argc == 0 || argv);
53 for (i = 0; i < (unsigned) argc; i++) {
56 if (startswith(argv[i], "create-session=")) {
57 if ((k = parse_boolean(argv[i] + 15)) < 0) {
58 pam_syslog(handle, LOG_ERR, "Failed to parse create-session= argument.");
65 } else if (startswith(argv[i], "kill-session=")) {
66 if ((k = parse_boolean(argv[i] + 13)) < 0) {
67 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
74 } else if (startswith(argv[i], "kill-user=")) {
75 if ((k = parse_boolean(argv[i] + 10)) < 0) {
76 pam_syslog(handle, LOG_ERR, "Failed to parse kill-user= argument.");
83 } else if (startswith(argv[i], "controllers=")) {
88 if (!(l = strv_split(argv[i] + 12, ","))) {
89 pam_syslog(handle, LOG_ERR, "Out of memory.");
93 strv_free(*controllers);
97 controller_set = true;
100 pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
106 if (!controller_set && controllers) {
109 if (!(l = strv_new("cpu", NULL))) {
110 pam_syslog(handle, LOG_ERR, "Out of memory");
119 strv_remove(*controllers, "name=systemd");
121 if (kill_session && *kill_session && kill_user)
127 static int open_file_and_lock(const char *fn) {
132 if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0)
135 /* The BSD socket semantics are a lot nicer than those of
136 * POSIX locks. Which is why we use flock() here. BSD locking
137 * does not work across NFS which however is not needed here
138 * as the filesystems in question should be local, and only
139 * locally accessible, and most likely even tmpfs. */
141 if (flock(fd, LOCK_EX) < 0)
148 SESSION_ID_AUDIT = 'a',
149 SESSION_ID_COUNTER = 'c',
150 SESSION_ID_RANDOM = 'r'
153 static uint64_t get_session_id(int *mode) {
159 /* First attempt: let's use the session ID of the audit
160 * system, if it is available. */
161 if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
165 r = safe_atou32(s, &u);
168 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
169 *mode = SESSION_ID_AUDIT;
174 /* Second attempt, use our own counter. */
175 if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
179 /* We do a bit of endianess swapping here, just to be
180 * sure. /var should be machine specific anyway, and
181 * /var/run even mounted from tmpfs, so this
182 * byteswapping should really not be necessary. But
183 * then again, you never know, so let's avoid any
186 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
189 counter = le64toh(counter) + 1;
191 if (lseek(fd, 0, SEEK_SET) == 0) {
192 uint64_t swapped = htole64(counter);
194 r = loop_write(fd, &swapped, sizeof(swapped), false);
196 if (r != sizeof(swapped))
201 close_nointr_nofail(fd);
204 *mode = SESSION_ID_COUNTER;
209 *mode = SESSION_ID_RANDOM;
211 /* Last attempt, pick a random value */
212 return (uint64_t) random_ull();
214 static int get_user_data(
215 pam_handle_t *handle,
216 const char **ret_username,
217 struct passwd **ret_pw) {
219 const char *username = NULL;
220 struct passwd *pw = NULL;
222 bool have_loginuid = false;
226 assert(ret_username);
229 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
232 r = safe_atou32(s, &u);
235 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
236 have_loginuid = true;
237 pw = pam_modutil_getpwuid(handle, u);
241 if (!have_loginuid) {
242 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
243 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
247 if (!username || !*username) {
248 pam_syslog(handle, LOG_ERR, "User name not valid.");
252 pw = pam_modutil_getpwnam(handle, username);
256 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
257 return PAM_USER_UNKNOWN;
261 *ret_username = username ? username : pw->pw_name;
266 static int create_user_group(
267 pam_handle_t *handle,
268 const char *controller,
280 r = cg_create_and_attach(controller, group, 0);
282 r = cg_create(controller, group);
285 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
286 return PAM_SESSION_ERR;
289 if (r > 0 && remember) {
290 /* Remember that it was us who created this group, and
291 * that hence we need to remove it too. This is a
292 * protection against removing the cgroup when run
294 if ((r = pam_set_data(handle, "systemd.created", INT_TO_PTR(1), NULL)) != PAM_SUCCESS) {
295 pam_syslog(handle, LOG_ERR, "Failed to install created variable.");
300 if ((r = cg_set_task_access(controller, group, 0644, pw->pw_uid, pw->pw_gid)) < 0 ||
301 (r = cg_set_group_access(controller, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
302 pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
303 return PAM_SESSION_ERR;
309 _public_ PAM_EXTERN int pam_sm_open_session(
310 pam_handle_t *handle,
312 int argc, const char **argv) {
314 const char *username = NULL;
319 bool create_session = true;
320 char **controllers = NULL, **c;
324 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
326 /* Make this a NOP on non-systemd systems */
327 if (sd_booted() <= 0)
330 if (parse_argv(handle, argc, argv, &create_session, NULL, NULL, &controllers) < 0)
331 return PAM_SESSION_ERR;
333 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
336 if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
337 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
342 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
343 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
348 /* Create /var/run/$USER */
350 if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
355 if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) {
356 pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m");
359 } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) {
360 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
367 if (create_session) {
370 /* Reuse or create XDG session ID */
371 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
374 if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
379 /* To avoid id clashes we add the session id
380 * source to our session ids. Note that the
381 * session id source might change during
382 * runtime, because a filesystem became
383 * writable or the system reconfigured. */
385 mode != SESSION_ID_AUDIT ? (char) mode : 0;
387 if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) {
388 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
392 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
393 pam_syslog(handle, LOG_ERR, "Failed to get session id.");
399 r = asprintf(&buf, "/user/%s/%s", username, id);
401 r = asprintf(&buf, "/user/%s/master", username);
408 pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
410 if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
413 /* The additional controllers don't really matter, so we
414 * ignore the return value */
415 STRV_FOREACH(c, controllers)
416 create_user_group(handle, *c, buf, pw, true, false);
424 close_nointr_nofail(lock_fd);
426 strv_free(controllers);
431 static int session_remains(pam_handle_t *handle, const char *user_path) {
433 bool remains = false;
437 if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
440 while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
442 remains = !streq(subgroup, "master");
457 _public_ PAM_EXTERN int pam_sm_close_session(
458 pam_handle_t *handle,
460 int argc, const char **argv) {
462 const char *username = NULL;
463 bool kill_session = false;
464 bool kill_user = false;
466 char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
469 const void *created = NULL;
470 char **controllers = NULL, **c;
474 /* Make this a NOP on non-systemd systems */
475 if (sd_booted() <= 0)
478 if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user, &controllers) < 0)
479 return PAM_SESSION_ERR;
481 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
484 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
485 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
490 /* We are probably still in some session/user dir. Move ourselves out of the way as first step */
491 if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0)
492 pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
494 STRV_FOREACH(c, controllers)
495 if ((r = cg_attach(*c, "/user", 0)) < 0)
496 pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r));
498 if (asprintf(&user_path, "/user/%s", username) < 0) {
503 pam_get_data(handle, "systemd.created", &created);
505 if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) {
507 if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 ||
508 asprintf(&nosession_path, "/user/%s/master", username) < 0) {
514 pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
516 /* Kill processes in session cgroup, and delete it */
517 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
518 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
520 pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
522 /* Migrate processes from session to user
523 * cgroup. First, try to create the user group
524 * in case it doesn't exist yet. Also, delete
525 * the session group. */
526 create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false);
528 if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
529 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
532 STRV_FOREACH(c, controllers) {
533 create_user_group(handle, *c, nosession_path, pw, false, false);
535 if ((r = cg_migrate_recursive(*c, session_path, nosession_path, false, true)) < 0)
536 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r));
541 cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
543 if ((r = session_remains(handle, user_path)) < 0)
544 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
546 /* Kill user processes not attached to any session */
547 if (kill_user && r == 0) {
549 /* Kill user cgroup */
550 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
551 pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
554 if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
555 pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
557 /* Remove user cgroup */
559 if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
560 pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
562 /* If we managed to find somebody, don't cleanup the cgroup. */
567 STRV_FOREACH(c, controllers)
568 cg_trim(*c, user_path, true);
571 const char *runtime_dir;
573 if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
574 if ((r = rm_rf(runtime_dir, false, true)) < 0)
575 pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r));
578 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
584 close_nointr_nofail(lock_fd);
587 free(nosession_path);
590 strv_free(controllers);