1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
34 #include <libcgroup.h>
37 #include "cgroup-util.h"
39 #include "sd-daemon.h"
41 static int parse_argv(pam_handle_t *handle,
42 int argc, const char **argv,
50 assert(argc == 0 || argv);
52 for (i = 0; i < (unsigned) argc; i++) {
55 if (startswith(argv[i], "create-session=")) {
56 if ((k = parse_boolean(argv[i] + 15)) < 0) {
57 pam_syslog(handle, LOG_ERR, "Failed to parse create-session= argument.");
63 } else if (startswith(argv[i], "kill-session=")) {
64 if ((k = parse_boolean(argv[i] + 13)) < 0) {
65 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
72 } else if (startswith(argv[i], "kill-user=")) {
73 if ((k = parse_boolean(argv[i] + 10)) < 0) {
74 pam_syslog(handle, LOG_ERR, "Failed to parse kill-user= argument.");
81 pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
86 if (kill_session && *kill_session && kill_user)
92 static int open_file_and_lock(const char *fn) {
97 if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0)
100 /* The BSD socket semantics are a lot nicer than those of
101 * POSIX locks. Which is why we use flock() here. BSD locking
102 * does not work across NFS which however is not needed here
103 * as the filesystems in question should be local, and only
104 * locally accessible, and most likely even tmpfs. */
106 if (flock(fd, LOCK_EX) < 0)
113 SESSION_ID_AUDIT = 'a',
114 SESSION_ID_COUNTER = 'c',
115 SESSION_ID_RANDOM = 'r'
118 static uint64_t get_session_id(int *mode) {
124 /* First attempt: let's use the session ID of the audit
125 * system, if it is available. */
126 if (read_one_line_file("/proc/self/sessionid", &s) >= 0) {
130 r = safe_atou32(s, &u);
133 if (r >= 0 && u != (uint32_t) -1) {
134 *mode = SESSION_ID_AUDIT;
139 /* Second attempt, use our own counter. */
140 if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) {
144 /* We do a bit of endianess swapping here, just to be
145 * sure. /var should be machine specific anyway, and
146 * /var/run even mounted from tmpfs, so this
147 * byteswapping should really not be necessary. But
148 * then again, you never know, so let's avoid any
151 if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter))
154 counter = le64toh(counter) + 1;
156 if (lseek(fd, 0, SEEK_SET) == 0) {
157 uint64_t swapped = htole64(counter);
159 r = loop_write(fd, &swapped, sizeof(swapped), false);
161 if (r != sizeof(swapped))
166 close_nointr_nofail(fd);
169 *mode = SESSION_ID_COUNTER;
174 *mode = SESSION_ID_RANDOM;
176 /* Last attempt, pick a random value */
177 return (uint64_t) random_ull();
179 static int get_user_data(
180 pam_handle_t *handle,
181 const char **ret_username,
182 struct passwd **ret_pw) {
184 const char *username;
189 assert(ret_username);
192 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
193 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
197 if (!username || !*username) {
198 pam_syslog(handle, LOG_ERR, "User name not valid.");
202 if (!(pw = pam_modutil_getpwnam(handle, username))) {
203 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
204 return PAM_USER_UNKNOWN;
208 *ret_username = username;
213 static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach) {
220 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, group, 0);
222 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, group);
225 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
226 return PAM_SESSION_ERR;
229 if ((r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0 ||
230 (r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
231 pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
232 return PAM_SESSION_ERR;
238 _public_ PAM_EXTERN int pam_sm_open_session(
239 pam_handle_t *handle,
241 int argc, const char **argv) {
243 const char *username = NULL;
248 bool create_session = true;
252 pam_syslog(handle, LOG_INFO, "pam-systemd initializing");
254 if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0)
255 return PAM_SESSION_ERR;
257 /* Make this a NOP on non-systemd systems */
258 if (sd_booted() <= 0)
261 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
264 if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
265 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
270 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
271 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
276 /* Create /var/run/$USER */
278 if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
283 if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) {
284 pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m");
287 } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) {
288 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
295 if (create_session) {
298 /* Reuse or create XDG session ID */
299 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
302 if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
307 /* To avoid id clashes we add the session id
308 * source to our session ids. Note that the
309 * session id source might change during
310 * runtime, because a filesystem became
311 * writable or the system reconfigured. */
313 mode != SESSION_ID_AUDIT ? (char) mode : 0;
315 if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) {
316 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
320 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
321 pam_syslog(handle, LOG_ERR, "Failed to get session id.");
327 r = asprintf(&buf, "/user/%s/%s", username, id);
329 r = asprintf(&buf, "/user/%s/no-session", username);
336 if ((r = create_user_group(handle, buf, pw, true)) != PAM_SUCCESS)
345 close_nointr_nofail(lock_fd);
350 static int session_remains(pam_handle_t *handle, const char *user_path) {
352 bool remains = false;
356 if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
359 while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
361 remains = !streq(subgroup, "no-session");
376 _public_ PAM_EXTERN int pam_sm_close_session(
377 pam_handle_t *handle,
379 int argc, const char **argv) {
381 const char *username = NULL;
382 bool kill_session = false;
383 bool kill_user = false;
385 char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
391 if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user) < 0)
392 return PAM_SESSION_ERR;
394 /* Make this a NOP on non-systemd systems */
395 if (sd_booted() <= 0)
398 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
401 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
402 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
407 /* We are probably still in some session/no-session dir. Move ourselves out of the way as first step */
408 if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0)
409 pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
411 if (asprintf(&user_path, "/user/%s", username) < 0) {
416 if ((id = pam_getenv(handle, "XDG_SESSION_ID"))) {
418 if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 ||
419 asprintf(&nosession_path, "/user/%s/no-session", username) < 0) {
425 /* Kill processes in session cgroup, and delete it */
426 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
427 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
429 /* Migrate processes from session to
430 * no-session cgroup. First, try to create the
431 * no-session group in case it doesn't exist
432 * yet. Also, delete the session group. */
433 create_user_group(handle, nosession_path, pw, 0);
435 if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
436 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
441 cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
443 if ((r = session_remains(handle, user_path)) < 0)
444 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
446 /* Kill user processes not attached to any session */
447 if (kill_user && r == 0) {
449 /* Kill no-session cgroup */
450 if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
451 pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
454 if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
455 pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
457 /* Remove user cgroup */
459 if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
460 pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
462 /* If we managed to find somebody, don't cleanup the cgroup. */
468 const char *runtime_dir;
470 /* This will migrate us to the /user cgroup. */
472 if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
473 if ((r = rm_rf(runtime_dir, false, true)) < 0)
474 pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r));
481 close_nointr_nofail(lock_fd);
484 free(nosession_path);