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]);
105 if (!controller_set && controllers) {
108 if (!(l = strv_new("cpu", NULL))) {
109 pam_syslog(handle, LOG_ERR, "Out of memory");
117 strv_remove(*controllers, "name=systemd");
119 if (kill_session && *kill_session && kill_user)
125 static int open_file_and_lock(const char *fn) {
130 if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0)
133 /* The BSD socket semantics are a lot nicer than those of
134 * POSIX locks. Which is why we use flock() here. BSD locking
135 * does not work across NFS which however is not needed here
136 * as the filesystems in question should be local, and only
137 * locally accessible, and most likely even tmpfs. */
139 if (flock(fd, LOCK_EX) < 0)
146 SESSION_ID_AUDIT = 'a',
147 SESSION_ID_COUNTER = 'c',
148 SESSION_ID_RANDOM = 'r'
151 static uint64_t get_session_id(int *mode) {
157 /* First attempt: let's use the session ID of the audit
158 * system, if it is available. */
159 if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
163 r = safe_atou32(s, &u);
166 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
167 *mode = SESSION_ID_AUDIT;
172 /* Second attempt, use our own counter. */
173 if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
177 /* We do a bit of endianess swapping here, just to be
178 * sure. /var should be machine specific anyway, and
179 * /var/run even mounted from tmpfs, so this
180 * byteswapping should really not be necessary. But
181 * then again, you never know, so let's avoid any
184 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
187 counter = le64toh(counter) + 1;
189 if (lseek(fd, 0, SEEK_SET) == 0) {
190 uint64_t swapped = htole64(counter);
192 r = loop_write(fd, &swapped, sizeof(swapped), false);
194 if (r != sizeof(swapped))
199 close_nointr_nofail(fd);
202 *mode = SESSION_ID_COUNTER;
207 *mode = SESSION_ID_RANDOM;
209 /* Last attempt, pick a random value */
210 return (uint64_t) random_ull();
212 static int get_user_data(
213 pam_handle_t *handle,
214 const char **ret_username,
215 struct passwd **ret_pw) {
217 const char *username = NULL;
218 struct passwd *pw = NULL;
220 bool have_loginuid = false;
224 assert(ret_username);
227 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
230 r = safe_atou32(s, &u);
233 if (r >= 0 && u != (uint32_t) -1 && u > 0) {
234 have_loginuid = true;
235 pw = pam_modutil_getpwuid(handle, u);
239 if (!have_loginuid) {
240 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
241 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
245 if (!username || !*username) {
246 pam_syslog(handle, LOG_ERR, "User name not valid.");
250 pw = pam_modutil_getpwnam(handle, username);
254 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
255 return PAM_USER_UNKNOWN;
259 *ret_username = username ? username : pw->pw_name;
264 static int create_user_group(
265 pam_handle_t *handle,
266 const char *controller,
278 r = cg_create_and_attach(controller, group, 0);
280 r = cg_create(controller, group);
283 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
284 return PAM_SESSION_ERR;
287 if (r > 0 && remember) {
288 /* Remember that it was us who created this group, and
289 * that hence we need to remove it too. This is a
290 * protection against removing the cgroup when run
292 if ((r = pam_set_data(handle, "systemd.created", INT_TO_PTR(1), NULL)) != PAM_SUCCESS) {
293 pam_syslog(handle, LOG_ERR, "Failed to install created variable.");
298 if ((r = cg_set_task_access(controller, group, 0644, pw->pw_uid, pw->pw_gid)) < 0 ||
299 (r = cg_set_group_access(controller, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
300 pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
301 return PAM_SESSION_ERR;
307 _public_ PAM_EXTERN int pam_sm_open_session(
308 pam_handle_t *handle,
310 int argc, const char **argv) {
312 const char *username = NULL;
317 bool create_session = true;
318 char **controllers = NULL, **c;
322 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
324 /* Make this a NOP on non-systemd systems */
325 if (sd_booted() <= 0)
328 if (parse_argv(handle, argc, argv, &create_session, NULL, NULL, &controllers) < 0)
329 return PAM_SESSION_ERR;
331 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
334 if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
335 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
340 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
341 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
346 /* Create /var/run/$USER */
348 if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
353 if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) {
354 pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m");
357 } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) {
358 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
365 if (create_session) {
368 /* Reuse or create XDG session ID */
369 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
372 if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
377 /* To avoid id clashes we add the session id
378 * source to our session ids. Note that the
379 * session id source might change during
380 * runtime, because a filesystem became
381 * writable or the system reconfigured. */
383 mode != SESSION_ID_AUDIT ? (char) mode : 0;
385 if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) {
386 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
390 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
391 pam_syslog(handle, LOG_ERR, "Failed to get session id.");
397 r = asprintf(&buf, "/user/%s/%s", username, id);
399 r = asprintf(&buf, "/user/%s/master", username);
406 pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf);
408 if ((r = create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, buf, pw, true, true)) != PAM_SUCCESS)
411 /* The additional controllers don't really matter, so we
412 * ignore the return value */
413 STRV_FOREACH(c, controllers)
414 create_user_group(handle, *c, buf, pw, true, false);
422 close_nointr_nofail(lock_fd);
424 strv_free(controllers);
429 static int session_remains(pam_handle_t *handle, const char *user_path) {
431 bool remains = false;
435 if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
438 while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
440 remains = !streq(subgroup, "master");
455 _public_ PAM_EXTERN int pam_sm_close_session(
456 pam_handle_t *handle,
458 int argc, const char **argv) {
460 const char *username = NULL;
461 bool kill_session = false;
462 bool kill_user = false;
464 char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
467 const void *created = NULL;
468 char **controllers = NULL, **c;
472 /* Make this a NOP on non-systemd systems */
473 if (sd_booted() <= 0)
476 if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user, &controllers) < 0)
477 return PAM_SESSION_ERR;
479 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
482 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
483 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
488 /* We are probably still in some session/user dir. Move ourselves out of the way as first step */
489 if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0)
490 pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
492 STRV_FOREACH(c, controllers)
493 if ((r = cg_attach(*c, "/user", 0)) < 0)
494 pam_syslog(handle, LOG_ERR, "Failed to move us away in %s hierarchy: %s", *c, strerror(-r));
496 if (asprintf(&user_path, "/user/%s", username) < 0) {
501 pam_get_data(handle, "systemd.created", &created);
503 if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) {
505 if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 ||
506 asprintf(&nosession_path, "/user/%s/master", username) < 0) {
512 pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username);
514 /* Kill processes in session cgroup, and delete it */
515 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
516 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
518 pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
520 /* Migrate processes from session to user
521 * cgroup. First, try to create the user group
522 * in case it doesn't exist yet. Also, delete
523 * the session group. */
524 create_user_group(handle, SYSTEMD_CGROUP_CONTROLLER, nosession_path, pw, false, false);
526 if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
527 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
530 STRV_FOREACH(c, controllers) {
531 create_user_group(handle, *c, nosession_path, pw, false, false);
533 if ((r = cg_migrate_recursive(*c, session_path, nosession_path, false, true)) < 0)
534 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup in hierarchy %s: %s", *c, strerror(-r));
539 cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
541 if ((r = session_remains(handle, user_path)) < 0)
542 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
544 /* Kill user processes not attached to any session */
545 if (kill_user && r == 0) {
547 /* Kill user cgroup */
548 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
549 pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
552 if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
553 pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
555 /* Remove user cgroup */
557 if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
558 pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
560 /* If we managed to find somebody, don't cleanup the cgroup. */
565 STRV_FOREACH(c, controllers)
566 cg_trim(*c, user_path, true);
569 const char *runtime_dir;
571 if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
572 if ((r = rm_rf(runtime_dir, false, true)) < 0)
573 pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r));
576 /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
582 close_nointr_nofail(lock_fd);
585 free(nosession_path);
588 strv_free(controllers);