X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fpam-module.c;h=e65088638b6de4d99683f9252a8776f8f28ac916;hp=5f2df4b4391878bcbcf098c87144ca49b8ad90bc;hb=1dc995370987660ff045ff4d7cf512da0390cf96;hpb=824a1d590a0ec4d83baa51264a9913a702793230 diff --git a/src/pam-module.c b/src/pam-module.c index 5f2df4b43..e65088638 100644 --- a/src/pam-module.c +++ b/src/pam-module.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -32,15 +33,21 @@ #include #include "util.h" -#include "cgroup-util.h" #include "macro.h" #include "sd-daemon.h" +#include "strv.h" +#include "dbus-common.h" +#include "def.h" +#include "socket-util.h" static int parse_argv(pam_handle_t *handle, int argc, const char **argv, - bool *create_session, - bool *kill_session, - bool *kill_user) { + char ***controllers, + char ***reset_controllers, + bool *kill_processes, + char ***kill_only_users, + char ***kill_exclude_users, + bool *debug) { unsigned i; @@ -50,198 +57,278 @@ static int parse_argv(pam_handle_t *handle, for (i = 0; i < (unsigned) argc; i++) { int k; - if (startswith(argv[i], "create-session=")) { - if ((k = parse_boolean(argv[i] + 15)) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to parse create-session= argument."); + if (startswith(argv[i], "kill-session-processes=")) { + if ((k = parse_boolean(argv[i] + 23)) < 0) { + pam_syslog(handle, LOG_ERR, "Failed to parse kill-session-processes= argument."); return k; } - if (create_session) - *create_session = k; + if (kill_processes) + *kill_processes = k; + } else if (startswith(argv[i], "kill-session=")) { + /* As compatibility for old versions */ + if ((k = parse_boolean(argv[i] + 13)) < 0) { pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument."); return k; } - if (kill_session) - *kill_session = k; - - } else if (startswith(argv[i], "kill-user=")) { - if ((k = parse_boolean(argv[i] + 10)) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to parse kill-user= argument."); - return k; - } - - if (kill_user) - *kill_user = k; - } else { - pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]); - return -EINVAL; - } - } - - if (kill_session && *kill_session && kill_user) - *kill_user = true; + if (kill_processes) + *kill_processes = k; - return 0; -} + } else if (startswith(argv[i], "controllers=")) { -static int open_file_and_lock(const char *fn) { - int fd; + if (controllers) { + char **l; - assert(fn); + if (!(l = strv_split(argv[i] + 12, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } - if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0) - return -errno; + strv_free(*controllers); + *controllers = l; + } - /* The BSD socket semantics are a lot nicer than those of - * POSIX locks. Which is why we use flock() here. BSD locking - * does not work across NFS which however is not needed here - * as the filesystems in question should be local, and only - * locally accessible, and most likely even tmpfs. */ + } else if (startswith(argv[i], "reset-controllers=")) { - if (flock(fd, LOCK_EX) < 0) - return -errno; + if (reset_controllers) { + char **l; - return fd; -} + if (!(l = strv_split(argv[i] + 18, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } -enum { - SESSION_ID_AUDIT = 'a', - SESSION_ID_COUNTER = 'c', - SESSION_ID_RANDOM = 'r' -}; + strv_free(*reset_controllers); + *reset_controllers = l; + } -static uint64_t get_session_id(int *mode) { - char *s; - int fd; + } else if (startswith(argv[i], "kill-only-users=")) { - assert(mode); + if (kill_only_users) { + char **l; - /* First attempt: let's use the session ID of the audit - * system, if it is available. */ - if (read_one_line_file("/proc/self/sessionid", &s) >= 0) { - uint32_t u; - int r; + if (!(l = strv_split(argv[i] + 16, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } - r = safe_atou32(s, &u); - free(s); + strv_free(*kill_only_users); + *kill_only_users = l; + } - if (r >= 0 && u != (uint32_t) -1) { - *mode = SESSION_ID_AUDIT; - return (uint64_t) u; - } - } + } else if (startswith(argv[i], "kill-exclude-users=")) { - /* Second attempt, use our own counter. */ - if ((fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-session")) >= 0) { - uint64_t counter; - ssize_t r; + if (kill_exclude_users) { + char **l; - /* We do a bit of endianess swapping here, just to be - * sure. /var should be machine specific anyway, and - * /var/run even mounted from tmpfs, so this - * byteswapping should really not be necessary. But - * then again, you never know, so let's avoid any - * risk. */ + if (!(l = strv_split(argv[i] + 19, ","))) { + pam_syslog(handle, LOG_ERR, "Out of memory."); + return -ENOMEM; + } - if (loop_read(fd, &counter, sizeof(counter), false) != sizeof(counter)) - counter = 1; - else - counter = le64toh(counter) + 1; + strv_free(*kill_exclude_users); + *kill_exclude_users = l; + } - if (lseek(fd, 0, SEEK_SET) == 0) { - uint64_t swapped = htole64(counter); + } else if (startswith(argv[i], "debug=")) { + if ((k = parse_boolean(argv[i] + 6)) < 0) { + pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument."); + return k; + } - r = loop_write(fd, &swapped, sizeof(swapped), false); + if (debug) + *debug = k; - if (r != sizeof(swapped)) - r = -EIO; - } else - r = -errno; + } else if (startswith(argv[i], "create-session=") || + startswith(argv[i], "kill-user=")) { - close_nointr_nofail(fd); + pam_syslog(handle, LOG_WARNING, "Option %s not supported anymore, ignoring.", argv[i]); - if (r >= 0) { - *mode = SESSION_ID_COUNTER; - return counter; + } else { + pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]); + return -EINVAL; } } - *mode = SESSION_ID_RANDOM; - - /* Last attempt, pick a random value */ - return (uint64_t) random_ull(); + return 0; } + static int get_user_data( pam_handle_t *handle, const char **ret_username, struct passwd **ret_pw) { - const char *username; - struct passwd *pw; + const char *username = NULL; + struct passwd *pw = NULL; int r; + bool have_loginuid = false; + char *s; assert(handle); assert(ret_username); assert(ret_pw); - if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) { - pam_syslog(handle, LOG_ERR, "Failed to get user name."); - return r; + if (have_effective_cap(CAP_AUDIT_CONTROL) > 0) { + /* Only use audit login uid if we are executed with + * sufficient capabilities so that pam_loginuid could + * do its job. If we are lacking the CAP_AUDIT_CONTROL + * capabality we most likely are being run in a + * container and /proc/self/loginuid is useless since + * it probably contains a uid of the host system. */ + + if (read_one_line_file("/proc/self/loginuid", &s) >= 0) { + uid_t uid; + + r = parse_uid(s, &uid); + free(s); + + if (r >= 0 && uid != (uint32_t) -1) { + have_loginuid = true; + pw = pam_modutil_getpwuid(handle, uid); + } + } } - if (!username || !*username) { - pam_syslog(handle, LOG_ERR, "User name not valid."); - return PAM_AUTH_ERR; + if (!have_loginuid) { + if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to get user name."); + return r; + } + + if (!username || !*username) { + pam_syslog(handle, LOG_ERR, "User name not valid."); + return PAM_AUTH_ERR; + } + + pw = pam_modutil_getpwnam(handle, username); } - if (!(pw = pam_modutil_getpwnam(handle, username))) { + if (!pw) { pam_syslog(handle, LOG_ERR, "Failed to get user data."); return PAM_USER_UNKNOWN; } *ret_pw = pw; - *ret_username = username; + *ret_username = username ? username : pw->pw_name; return PAM_SUCCESS; } -static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach, bool remember) { - int r; +static bool check_user_lists( + pam_handle_t *handle, + uid_t uid, + char **kill_only_users, + char **kill_exclude_users) { + + const char *name = NULL; + char **l; assert(handle); - assert(group); - if (attach) - r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, group, 0); - else - r = cg_create(SYSTEMD_CGROUP_CONTROLLER, group); + if (uid == 0) + name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */ + else { + struct passwd *pw; - if (r < 0) { - pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r)); - return PAM_SESSION_ERR; + pw = pam_modutil_getpwuid(handle, uid); + if (pw) + name = pw->pw_name; } - if (r > 0 && remember) { - /* Remember that it was us who created this group, and - * that hence we need to remove it too. This is a - * protection against removing the cgroup when run - * recursively. */ - if ((r = pam_set_data(handle, "systemd.created", INT_TO_PTR(1), NULL)) != PAM_SUCCESS) { - pam_syslog(handle, LOG_ERR, "Failed to install created variable."); - return r; - } + STRV_FOREACH(l, kill_exclude_users) { + uid_t u; + + if (parse_uid(*l, &u) >= 0) + if (u == uid) + return false; + + if (name && streq(name, *l)) + return false; } - if ((r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0 || - (r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER, group, 0755, pw->pw_uid, pw->pw_gid)) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to change access modes: %s", strerror(-r)); - return PAM_SESSION_ERR; + if (strv_isempty(kill_only_users)) + return true; + + STRV_FOREACH(l, kill_only_users) { + uid_t u; + + if (parse_uid(*l, &u) >= 0) + if (u == uid) + return true; + + if (name && streq(name, *l)) + return true; } - return PAM_SUCCESS; + return false; +} + +static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { + char *p = NULL; + int r; + int fd; + union sockaddr_union sa; + struct ucred ucred; + socklen_t l; + char *tty; + int v; + + assert(display); + assert(seat); + assert(vtnr); + + /* We deduce the X11 socket from the display name, then use + * SO_PEERCRED to determine the X11 server process, ask for + * the controlling tty of that and if it's a VC then we know + * the seat and the virtual terminal. Sounds ugly, is only + * semi-ugly. */ + + r = socket_from_display(display, &p); + if (r < 0) + return r; + + fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); + if (fd < 0) { + free(p); + return -errno; + } + + zero(sa); + sa.un.sun_family = AF_UNIX; + strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1); + free(p); + + if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) { + close_nointr_nofail(fd); + return -errno; + } + + l = sizeof(ucred); + r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l); + close_nointr_nofail(fd); + + if (r < 0) + return -errno; + + r = get_ctty(ucred.pid, NULL, &tty); + if (r < 0) + return r; + + v = vtnr_from_tty(tty); + free(tty); + + if (v < 0) + return v; + else if (v == 0) + return -ENOENT; + + *seat = "seat0"; + *vtnr = (uint32_t) v; + + return 0; } _public_ PAM_EXTERN int pam_sm_open_session( @@ -249,258 +336,287 @@ _public_ PAM_EXTERN int pam_sm_open_session( int flags, int argc, const char **argv) { - const char *username = NULL; struct passwd *pw; + bool kill_processes = false, debug = false; + const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type, *cvtnr = NULL; + char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL; + DBusError error; + uint32_t uid, pid; + DBusMessageIter iter; + dbus_bool_t kp; + int session_fd = -1; + DBusConnection *bus = NULL; + DBusMessage *m = NULL, *reply = NULL; + dbus_bool_t remote; int r; - char *buf = NULL; - int lock_fd = -1; - bool create_session = true; + uint32_t vtnr = 0; assert(handle); - /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */ + dbus_error_init(&error); - if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0) - return PAM_SESSION_ERR; + /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */ /* Make this a NOP on non-systemd systems */ if (sd_booted() <= 0) return PAM_SUCCESS; - if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS) - goto finish; - - if (safe_mkdir(RUNTIME_DIR "/user", 0755, 0, 0) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to create runtime directory: %m"); - r = PAM_SYSTEM_ERR; + if (parse_argv(handle, + argc, argv, + &controllers, &reset_controllers, + &kill_processes, &kill_only_users, &kill_exclude_users, + &debug) < 0) { + r = PAM_SESSION_ERR; goto finish; } - if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m"); - r = PAM_SYSTEM_ERR; + r = get_user_data(handle, &username, &pw); + if (r != PAM_SUCCESS) goto finish; - } - /* Create /var/run/$USER */ - free(buf); - if (asprintf(&buf, RUNTIME_DIR "/user/%s", username) < 0) { - r = PAM_BUF_ERR; - goto finish; - } + /* Make sure we don't enter a loop by talking to + * systemd-logind when it is actually waiting for the + * background to finish start-up. If the service is + * "systemd-shared" we simply set XDG_RUNTIME_DIR and + * leave. */ - if (safe_mkdir(buf, 0700, pw->pw_uid, pw->pw_gid) < 0) { - pam_syslog(handle, LOG_WARNING, "Failed to create runtime directory: %m"); - r = PAM_SYSTEM_ERR; - goto finish; - } else if ((r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", buf, 0)) != PAM_SUCCESS) { - pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); - goto finish; - } + pam_get_item(handle, PAM_SERVICE, (const void**) &service); + if (streq_ptr(service, "systemd-shared")) { + char *p, *rt = NULL; - free(buf); - buf = NULL; - - if (create_session) { - const char *id; - - /* Reuse or create XDG session ID */ - if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) { - int mode; + if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) pw->pw_uid) < 0) { + r = PAM_BUF_ERR; + goto finish; + } - if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) { - r = PAM_BUF_ERR; - goto finish; - } + r = parse_env_file(p, NEWLINE, + "RUNTIME", &rt, + NULL); + free(p); - /* To avoid id clashes we add the session id - * source to our session ids. Note that the - * session id source might change during - * runtime, because a filesystem became - * writable or the system reconfigured. */ - buf[strlen(buf)-1] = - mode != SESSION_ID_AUDIT ? (char) mode : 0; + if (r < 0 && r != -ENOENT) { + r = PAM_SESSION_ERR; + free(rt); + goto finish; + } - if ((r = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) { - pam_syslog(handle, LOG_ERR, "Failed to set session id."); - goto finish; - } + if (rt) { + r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); + free(rt); - if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) { - pam_syslog(handle, LOG_ERR, "Failed to get session id."); - r = PAM_SESSION_ERR; + if (r != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); goto finish; } } - r = asprintf(&buf, "/user/%s/%s", username, id); - } else - r = asprintf(&buf, "/user/%s/user", username); - - if (r < 0) { - r = PAM_BUF_ERR; + r = PAM_SUCCESS; goto finish; } - pam_syslog(handle, LOG_INFO, "Moving new user session for %s into control group %s.", username, buf); - - if ((r = create_user_group(handle, buf, pw, true, true)) != PAM_SUCCESS) - goto finish; - - r = PAM_SUCCESS; + if (kill_processes) + kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users); -finish: - free(buf); - - if (lock_fd >= 0) - close_nointr_nofail(lock_fd); + dbus_connection_set_change_sigpipe(FALSE); - return r; -} - -static int session_remains(pam_handle_t *handle, const char *user_path) { - int r; - bool remains = false; - DIR *d; - char *subgroup; - - if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0) - return r; - - while ((r = cg_read_subgroup(d, &subgroup)) > 0) { - - remains = !streq(subgroup, "user"); - free(subgroup); - - if (remains) - break; + bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); + if (!bus) { + pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error)); + r = PAM_SESSION_ERR; + goto finish; } - closedir(d); - - if (r < 0) - return r; + m = dbus_message_new_method_call( + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "CreateSession"); - return !!remains; -} - -_public_ PAM_EXTERN int pam_sm_close_session( - pam_handle_t *handle, - int flags, - int argc, const char **argv) { + if (!m) { + pam_syslog(handle, LOG_ERR, "Could not allocate create session message."); + r = PAM_BUF_ERR; + goto finish; + } - const char *username = NULL; - bool kill_session = false; - bool kill_user = false; - int lock_fd = -1, r; - char *session_path = NULL, *nosession_path = NULL, *user_path = NULL; - const char *id; - struct passwd *pw; - const void *created = NULL; + uid = pw->pw_uid; + pid = getpid(); + + pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); + pam_get_item(handle, PAM_TTY, (const void**) &tty); + pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); + pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); + seat = pam_getenv(handle, "XDG_SEAT"); + cvtnr = pam_getenv(handle, "XDG_VTNR"); + + service = strempty(service); + tty = strempty(tty); + display = strempty(display); + remote_user = strempty(remote_user); + remote_host = strempty(remote_host); + seat = strempty(seat); + + if (strchr(tty, ':')) { + /* A tty with a colon is usually an X11 display, place + * there to show up in utmp. We rearrange things and + * don't pretend that an X display was a tty */ + + if (isempty(display)) + display = tty; + tty = ""; + } - assert(handle); + if (!isempty(cvtnr)) + safe_atou32(cvtnr, &vtnr); + + if (!isempty(display) && isempty(seat) && vtnr <= 0) + get_seat_from_display(display, &seat, &vtnr); + + type = !isempty(display) ? "x11" : + !isempty(tty) ? "tty" : "unspecified"; + + remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain"); + + if (!dbus_message_append_args(m, + DBUS_TYPE_UINT32, &uid, + DBUS_TYPE_UINT32, &pid, + DBUS_TYPE_STRING, &service, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &seat, + DBUS_TYPE_UINT32, &vtnr, + DBUS_TYPE_STRING, &tty, + DBUS_TYPE_STRING, &display, + DBUS_TYPE_BOOLEAN, &remote, + DBUS_TYPE_STRING, &remote_user, + DBUS_TYPE_STRING, &remote_host, + DBUS_TYPE_INVALID)) { + pam_syslog(handle, LOG_ERR, "Could not attach parameters to message."); + r = PAM_BUF_ERR; + goto finish; + } - if (parse_argv(handle, argc, argv, NULL, &kill_session, &kill_user) < 0) - return PAM_SESSION_ERR; + dbus_message_iter_init_append(m, &iter); - /* Make this a NOP on non-systemd systems */ - if (sd_booted() <= 0) - return PAM_SUCCESS; + r = bus_append_strv_iter(&iter, controllers); + if (r < 0) { + pam_syslog(handle, LOG_ERR, "Could not attach parameter to message."); + r = PAM_BUF_ERR; + goto finish; + } - if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS) + r = bus_append_strv_iter(&iter, reset_controllers); + if (r < 0) { + pam_syslog(handle, LOG_ERR, "Could not attach parameter to message."); + r = PAM_BUF_ERR; goto finish; + } - if ((lock_fd = open_file_and_lock(RUNTIME_DIR "/user/.pam-systemd-lock")) < 0) { - pam_syslog(handle, LOG_ERR, "Failed to lock runtime directory: %m"); - r = PAM_SYSTEM_ERR; + kp = kill_processes; + if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &kp)) { + pam_syslog(handle, LOG_ERR, "Could not attach parameter to message."); + r = PAM_BUF_ERR; goto finish; } - /* We are probably still in some session/user dir. Move ourselves out of the way as first step */ - if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r)); + reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error); + if (!reply) { + pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error)); + r = PAM_SESSION_ERR; + goto finish; + } - if (asprintf(&user_path, "/user/%s", username) < 0) { - r = PAM_BUF_ERR; + if (!dbus_message_get_args(reply, &error, + DBUS_TYPE_STRING, &id, + DBUS_TYPE_OBJECT_PATH, &object_path, + DBUS_TYPE_STRING, &runtime_path, + DBUS_TYPE_UNIX_FD, &session_fd, + DBUS_TYPE_STRING, &seat, + DBUS_TYPE_UINT32, &vtnr, + DBUS_TYPE_INVALID)) { + pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", bus_error_message(&error)); + r = PAM_SESSION_ERR; goto finish; } - pam_get_data(handle, "systemd.created", &created); + r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0); + if (r != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to set session id."); + goto finish; + } - if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) { + r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0); + if (r != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to set runtime dir."); + goto finish; + } - if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 || - asprintf(&nosession_path, "/user/%s/user", username) < 0) { - r = PAM_BUF_ERR; + if (!isempty(seat)) { + r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0); + if (r != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to set seat."); goto finish; } + } - if (kill_session) { - pam_syslog(handle, LOG_INFO, "Killing remaining processes of user session %s of %s.", id, username); - - /* Kill processes in session cgroup, and delete it */ - if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r)); - } else { - pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path); + if (vtnr > 0) { + char buf[11]; + snprintf(buf, sizeof(buf), "%u", vtnr); + char_array_0(buf); - /* Migrate processes from session to user - * cgroup. First, try to create the user group - * in case it doesn't exist yet. Also, delete - * the session group. */ - create_user_group(handle, nosession_path, pw, false, false); + r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0); + if (r != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number."); + goto finish; + } + } - if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r)); + if (session_fd >= 0) { + r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL); + if (r != PAM_SUCCESS) { + pam_syslog(handle, LOG_ERR, "Failed to install session fd."); + return r; } } - /* GC user tree */ - cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false); + session_fd = -1; - if ((r = session_remains(handle, user_path)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r)); + r = PAM_SUCCESS; - /* Kill user processes not attached to any session */ - if (kill_user && r == 0) { +finish: + strv_free(controllers); + strv_free(reset_controllers); + strv_free(kill_only_users); + strv_free(kill_exclude_users); - /* Kill user cgroup */ - if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r)); - } else { + dbus_error_free(&error); - if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r)); + if (bus) { + dbus_connection_close(bus); + dbus_connection_unref(bus); + } - /* Remove user cgroup */ - if (r > 0) { - if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r)); + if (m) + dbus_message_unref(m); - /* If we managed to find somebody, don't cleanup the cgroup. */ - } else if (r == 0) - r = -EBUSY; - } + if (reply) + dbus_message_unref(reply); - if (r >= 0) { - const char *runtime_dir; + if (session_fd >= 0) + close_nointr_nofail(session_fd); - if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR"))) - if ((r = rm_rf(runtime_dir, false, true)) < 0) - pam_syslog(handle, LOG_ERR, "Failed to remove runtime directory: %s", strerror(-r)); - } + return r; +} - /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */ +_public_ PAM_EXTERN int pam_sm_close_session( + pam_handle_t *handle, + int flags, + int argc, const char **argv) { - r = PAM_SUCCESS; + const void *p = NULL; -finish: - if (lock_fd >= 0) - close_nointr_nofail(lock_fd); + pam_get_data(handle, "systemd.session-fd", &p); - free(session_path); - free(nosession_path); - free(user_path); + if (p) + close_nointr(PTR_TO_INT(p) - 1); - return r; + return PAM_SUCCESS; }