chiark / gitweb /
pam: always rely on loginuid instead of uid to determine cgroup and XDG_RUNTIME_DIR
[elogind.git] / src / pam-module.c
index 69b9b0ce78590dab1f410d3c12c027e70f1ae5b4..bc99684e110702587dd6398995081047c2ee066e 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
@@ -31,8 +31,6 @@
 #include <security/pam_ext.h>
 #include <security/pam_misc.h>
 
-#include <libcgroup.h>
-
 #include "util.h"
 #include "cgroup-util.h"
 #include "macro.h"
@@ -130,7 +128,7 @@ static uint64_t get_session_id(int *mode) {
                 r = safe_atou32(s, &u);
                 free(s);
 
-                if (r >= 0 && u != (uint32_t) -1) {
+                if (r >= 0 && u != (uint32_t) -1 && u > 0) {
                         *mode = SESSION_ID_AUDIT;
                         return (uint64_t) u;
                 }
@@ -181,36 +179,54 @@ static int get_user_data(
                 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 (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
+                uint32_t u;
+
+                r = safe_atou32(s, &u);
+                free(s);
+
+                if (r >= 0 && u != (uint32_t) -1 && u > 0) {
+                        have_loginuid = true;
+                        pw = pam_modutil_getpwuid(handle, u);
+                }
         }
 
-        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) {
+static int create_user_group(pam_handle_t *handle, const char *group, struct passwd *pw, bool attach, bool remember) {
         int r;
 
         assert(handle);
@@ -226,6 +242,17 @@ static int create_user_group(pam_handle_t *handle, const char *group, struct pas
                 return PAM_SESSION_ERR;
         }
 
+        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;
+                }
+        }
+
         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));
@@ -249,7 +276,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
         assert(handle);
 
-        pam_syslog(handle, LOG_INFO, "pam-systemd initializing");
+        /* pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); */
 
         if (parse_argv(handle, argc, argv, &create_session, NULL, NULL) < 0)
                 return PAM_SESSION_ERR;
@@ -326,14 +353,16 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
                 r = asprintf(&buf, "/user/%s/%s", username, id);
         } else
-                r = asprintf(&buf, "/user/%s/no-session", username);
+                r = asprintf(&buf, "/user/%s/master", username);
 
         if (r < 0) {
                 r = PAM_BUF_ERR;
                 goto finish;
         }
 
-        if ((r = create_user_group(handle, buf, pw, true)) != PAM_SUCCESS)
+        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;
@@ -358,7 +387,7 @@ static int session_remains(pam_handle_t *handle, const char *user_path) {
 
         while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
 
-                remains = !streq(subgroup, "no-session");
+                remains = !streq(subgroup, "master");
                 free(subgroup);
 
                 if (remains)
@@ -385,6 +414,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         char *session_path = NULL, *nosession_path = NULL, *user_path = NULL;
         const char *id;
         struct passwd *pw;
+        const void *created = NULL;
 
         assert(handle);
 
@@ -404,7 +434,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                 goto finish;
         }
 
-        /* We are probably still in some session/no-session dir. Move ourselves out of the way as first step */
+        /* 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));
 
@@ -413,24 +443,30 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                 goto finish;
         }
 
-        if ((id = pam_getenv(handle, "XDG_SESSION_ID"))) {
+        pam_get_data(handle, "systemd.created", &created);
+
+        if ((id = pam_getenv(handle, "XDG_SESSION_ID")) && created) {
 
                 if (asprintf(&session_path, "/user/%s/%s", username, id) < 0 ||
-                    asprintf(&nosession_path, "/user/%s/no-session", username) < 0) {
+                    asprintf(&nosession_path, "/user/%s/master", username) < 0) {
                         r = PAM_BUF_ERR;
                         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 {
-                        /* Migrate processes from session to
-                         * no-session cgroup. First, try to create the
-                         * no-session group in case it doesn't exist
-                         * yet. Also, delete the session group. */
-                        create_user_group(handle, nosession_path, pw, 0);
+                        pam_syslog(handle, LOG_INFO, "Moving remaining processes of user session %s of %s into control group %s.", id, username, nosession_path);
+
+                        /* 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);
 
                         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));
@@ -446,7 +482,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         /* Kill user processes not attached to any session */
         if (kill_user && r == 0) {
 
-                /* Kill no-session cgroup */
+                /* 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 {
@@ -467,13 +503,13 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         if (r >= 0) {
                 const char *runtime_dir;
 
-                /* This will migrate us to the /user cgroup. */
-
                 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));
         }
 
+        /* pam_syslog(handle, LOG_DEBUG, "pam-systemd done"); */
+
         r = PAM_SUCCESS;
 
 finish: