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/>.
27 #include <security/pam_modules.h>
28 #include <security/_pam_macros.h>
29 #include <security/pam_modutil.h>
30 #include <security/pam_ext.h>
31 #include <security/pam_misc.h>
33 #include <libcgroup.h>
36 #include "cgroup-util.h"
38 #include "sd-daemon.h"
40 static int parse_argv(pam_handle_t *handle,
41 int argc, const char **argv,
49 assert(argc == 0 || argv);
51 for (i = 0; i < (unsigned) argc; i++) {
54 if (startswith(argv[i], "create-session=")) {
55 if ((k = parse_boolean(argv[i] + 15)) < 0) {
56 pam_syslog(handle, LOG_ERR, "Failed to parse create-session= argument.");
62 } else if (startswith(argv[i], "kill-session=")) {
63 if ((k = parse_boolean(argv[i] + 13)) < 0) {
64 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
71 } else if (startswith(argv[i], "kill-user=")) {
72 if ((k = parse_boolean(argv[i] + 10)) < 0) {
73 pam_syslog(handle, LOG_ERR, "Failed to parse kill-user= argument.");
80 pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
85 if (kill_session && *kill_session && kill_user)
91 static int open_file_and_lock(const char *fn) {
96 if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0)
99 if (flock(fd, LOCK_EX) < 0)
105 static uint32_t combine32(unsigned long long u) {
109 for (i = 0; i < sizeof(u)/4; i ++)
110 r ^= (uint32_t) ((u >> (i*32)) & 0xFFFFFFFFULL);
115 static char *generate_session_cookie(void) {
118 unsigned long long r;
122 if (getmachineid_malloc(&machine) < 0)
123 if (!(machine = gethostname_malloc()))
127 u = now(CLOCK_REALTIME);
129 k = asprintf(&cookie, "%s-%lu-%lu",
131 (unsigned long) combine32(r),
132 (unsigned long) combine32((unsigned long long) u));
135 return k < 0 ? NULL : cookie;
138 static int get_user_data(
139 pam_handle_t *handle,
140 const char **ret_username,
141 struct passwd **ret_pw) {
143 const char *username;
148 assert(ret_username);
151 if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
152 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
156 if (!username || !*username) {
157 pam_syslog(handle, LOG_ERR, "User name not valid.");
161 if (!(pw = pam_modutil_getpwnam(handle, username))) {
162 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
163 return PAM_USER_UNKNOWN;
167 *ret_username = username;
172 static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach) {
179 r = cg_create_and_attach("name=systemd", group, 0);
181 r = cg_create("name=systemd", group);
184 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
185 return PAM_SESSION_ERR;
188 if ((r = cg_set_task_access("name=systemd", group, 0755, pw->pw_uid, pw->pw_gid)) < 0 ||
189 (r = cg_set_group_access("name=systemd", group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
190 pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r));
191 return PAM_SESSION_ERR;
197 _public_ PAM_EXTERN int pam_sm_open_session(
198 pam_handle_t *handle,
200 int argc, const char **argv) {
202 const char *username = NULL;
207 bool create_session = true;
211 pam_syslog(handle, LOG_INFO, "pam-systemd initializing");
213 if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0)
214 return PAM_SESSION_ERR;
216 /* Make this a NOP on non-systemd systems */
217 if (sd_booted() <= 0)
220 if ((r = cgroup_init()) != 0) {
221 pam_syslog(handle, LOG_ERR, "libcgroup initialization failed: %s", cgroup_strerror(r));
226 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
229 if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) {
230 pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m");
235 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
236 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
241 /* Create /var/run/$USER */
243 if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) {
248 if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) {
249 pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m");
252 } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) {
253 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
260 if (create_session) {
263 /* Reuse or create XDG session ID */
264 if (!(cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) {
265 if (!(buf = generate_session_cookie())) {
270 if ((r = pam_misc_setenv(handle, "XDG_SESSION_COOKIE", buf, 0)) != PAM_SUCCESS) {
271 pam_syslog(handle, LOG_ERR, "Failed to set cookie.");
275 if (!(cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) {
276 pam_syslog(handle, LOG_ERR, "Failed to get cookie.");
282 r = asprintf(&buf, "/user/%s/%s", username, cookie);
284 r = asprintf(&buf, "/user/%s/no-session", username);
291 if ((r = create_user_group(handle, buf, pw, true)) != PAM_SUCCESS)
300 close_nointr_nofail(lock_fd);
305 static int session_remains(pam_handle_t *handle, const char *user_path) {
306 struct cgroup_file_info info;
308 void *iterator = NULL;
309 bool remains = false;
313 r = cgroup_walk_tree_begin("name=systemd", user_path, 0, &iterator, &info, &level);
316 if (info.type != CGROUP_FILE_TYPE_DIR)
319 if (streq(info.path, ""))
322 if (streq(info.path, "no-session"))
330 r = cgroup_walk_tree_next(0, &iterator, &info, level);
336 else if (r == 0 || r == ECGEOF)
339 r = cg_translate_error(r, errno);
341 assert_se(cgroup_walk_tree_end(&iterator) == 0);
346 _public_ PAM_EXTERN int pam_sm_close_session(
347 pam_handle_t *handle,
349 int argc, const char **argv) {
351 const char *username = NULL;
352 bool kill_session = false;
353 bool kill_user = false;
355 char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
361 if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user) < 0)
362 return PAM_SESSION_ERR;
364 /* Make this a NOP on non-systemd systems */
365 if (sd_booted() <= 0)
368 if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
371 if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) {
372 pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m");
377 if (asprintf(&user_path, "/user/%s", username) < 0) {
382 if ((cookie = pam_getenv(handle, "XDG_SESSION_COOKIE"))) {
384 if (asprintf(&session_path, "/user/%s/%s", username, cookie) < 0 ||
385 asprintf(&nosession_path, "/user/%s/no-session", username) < 0) {
391 pam_syslog(handle, LOG_INFO, "KILLING ENTER");
393 /* Kill processes in session cgroup */
394 if ((r = cg_kill_recursive_and_wait("name=systemd", session_path)) < 0)
395 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
397 pam_syslog(handle, LOG_INFO, "KILLING EXIT");
400 /* Migrate processes from session to
401 * no-session cgroup. First, try to create the
402 * no-session group in case it doesn't exist
404 create_user_group(handle, nosession_path, pw, 0);
406 if ((r = cg_migrate_recursive("name=systemd", session_path, nosession_path, false)) < 0)
407 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
410 /* Delete session cgroup */
412 pam_syslog(handle, LOG_INFO, "Couldn't empty session cgroup, not deleting.");
414 if ((r = cg_delete("name=systemd", session_path)) < 0)
415 pam_syslog(handle, LOG_ERR, "Failed to delete session cgroup: %s", strerror(-r));
420 cg_trim("name=systemd", user_path, false);
422 if ((r = session_remains(handle, user_path)) < 0)
423 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
425 /* Kill user processes not attached to any session */
426 if (kill_user && r == 0) {
428 /* Kill no-session cgroup */
429 if ((r = cg_kill_recursive_and_wait("name=systemd", user_path)) < 0)
430 pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
433 if ((r = cg_is_empty_recursive("name=systemd", user_path, true)) < 0)
434 pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
436 /* If we managed to kill somebody, don't cleanup the cgroup. */
442 const char *runtime_dir;
444 /* Remove user cgroup */
445 if ((r = cg_delete("name=systemd", user_path)) < 0)
446 pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
448 /* This will migrate us to the /user cgroup. */
450 if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
451 if ((r = rm_rf(runtime_dir, false, true)) < 0)
452 pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r));
459 close_nointr_nofail(lock_fd);
462 free(nosession_path);