chiark / gitweb /
execute: use right logger socket
[elogind.git] / src / pam-module.c
index 08de006f687852580d68aab55d5e54f4541d1525..1441987fdc39e7c944f167d383b2027448612d0b 100644 (file)
@@ -97,16 +97,30 @@ static int open_file_and_lock(const char *fn) {
         if ((fd = open(fn, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW|O_CREAT, 0600)) < 0)
                 return -errno;
 
+        /* 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. */
+
         if (flock(fd, LOCK_EX) < 0)
                 return -errno;
 
         return fd;
 }
 
-static uint64_t get_session_id(void) {
+enum {
+        SESSION_ID_AUDIT = 'a',
+        SESSION_ID_COUNTER = 'c',
+        SESSION_ID_RANDOM = 'r'
+};
+
+static uint64_t get_session_id(int *mode) {
         char *s;
         int fd;
 
+        assert(mode);
+
         /* 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) {
@@ -116,8 +130,10 @@ static uint64_t get_session_id(void) {
                 r = safe_atou32(s, &u);
                 free(s);
 
-                if (r >= 0 && u != (uint32_t) -1)
+                if (r >= 0 && u != (uint32_t) -1) {
+                        *mode = SESSION_ID_AUDIT;
                         return (uint64_t) u;
+                }
         }
 
         /* Second attempt, use our own counter. */
@@ -149,10 +165,14 @@ static uint64_t get_session_id(void) {
 
                 close_nointr_nofail(fd);
 
-                if (r >= 0)
+                if (r >= 0) {
+                        *mode = SESSION_ID_COUNTER;
                         return counter;
+                }
         }
 
+        *mode = SESSION_ID_RANDOM;
+
         /* Last attempt, pick a random value */
         return (uint64_t) random_ull();
 }
@@ -197,17 +217,17 @@ static int create_user_group(pam_handle_t *handle, const char *group, struct pas
         assert(group);
 
         if (attach)
-                r = cg_create_and_attach("name=systemd", group, 0);
+                r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, group, 0);
         else
-                r = cg_create("name=systemd", group);
+                r = cg_create(SYSTEMD_CGROUP_CONTROLLER, group);
 
         if (r < 0) {
                 pam_syslog(handle, LOG_ERR, "Failed to create cgroup: %s", strerror(-r));
                 return PAM_SESSION_ERR;
         }
 
-        if ((r = cg_set_task_access("name=systemd", group, 0755, pw->pw_uid, pw->pw_gid)) < 0 ||
-            (r = cg_set_group_access("name=systemd", group, 0755, pw->pw_uid, pw->pw_gid)) < 0) {
+        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;
         }
@@ -238,8 +258,8 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (sd_booted() <= 0)
                 return PAM_SUCCESS;
 
-        if ((r = cgroup_init()) != 0) {
-                pam_syslog(handle, LOG_ERR, "libcgroup initialization failed: %s", cgroup_strerror(r));
+        if ((r = cg_init()) < 0) {
+                pam_syslog(handle, LOG_ERR, "libcgroup initialization failed: %s", strerror(-r));
                 r = PAM_SESSION_ERR;
                 goto finish;
         }
@@ -283,12 +303,21 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
                 /* Reuse or create XDG session ID */
                 if (!(id = pam_getenv(handle, "XDG_SESSION_ID"))) {
+                        int mode;
 
-                        if (asprintf(&buf, "%llu", (unsigned long long) get_session_id()) < 0) {
+                        if (asprintf(&buf, "%llux", (unsigned long long) get_session_id(&mode)) < 0) {
                                 r = PAM_BUF_ERR;
                                 goto finish;
                         }
 
+                        /* 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 = pam_misc_setenv(handle, "XDG_SESSION_ID", buf, 0)) != PAM_SUCCESS) {
                                 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
                                 goto finish;
@@ -332,7 +361,7 @@ static int session_remains(pam_handle_t *handle, const char *user_path) {
 
         zero(info);
 
-        r = cgroup_walk_tree_begin("name=systemd", user_path, 0, &iterator, &info, &level);
+        r = cgroup_walk_tree_begin(SYSTEMD_CGROUP_CONTROLLER, user_path, 0, &iterator, &info, &level);
         while (r == 0) {
 
                 if (info.type != CGROUP_FILE_TYPE_DIR)
@@ -411,7 +440,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
 
                 if (kill_session)  {
                         /* Kill processes in session cgroup */
-                        if ((r = cg_kill_recursive_and_wait("name=systemd", session_path)) < 0)
+                        if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path)) < 0)
                                 pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
 
                 } else  {
@@ -421,7 +450,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                          * yet. */
                         create_user_group(handle, nosession_path, pw, 0);
 
-                        if ((r = cg_migrate_recursive("name=systemd", session_path, nosession_path, false)) < 0)
+                        if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false)) < 0)
                                 pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
                 }
 
@@ -429,13 +458,13 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                 if (r < 0)
                         pam_syslog(handle, LOG_INFO, "Couldn't empty session cgroup, not deleting.");
                 else {
-                        if ((r = cg_delete("name=systemd", session_path)) < 0)
+                        if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, session_path)) < 0)
                                 pam_syslog(handle, LOG_ERR, "Failed to delete session cgroup: %s", strerror(-r));
                 }
         }
 
         /* GC user tree */
-        cg_trim("name=systemd", user_path, false);
+        cg_trim(SYSTEMD_CGROUP_CONTROLLER, user_path, false);
 
         if ((r = session_remains(handle, user_path)) < 0)
                 pam_syslog(handle, LOG_ERR, "Failed to determine whether a session remains: %s", strerror(-r));
@@ -444,11 +473,11 @@ _public_ PAM_EXTERN int pam_sm_close_session(
         if (kill_user && r == 0) {
 
                 /* Kill no-session cgroup */
-                if ((r = cg_kill_recursive_and_wait("name=systemd", user_path)) < 0)
+                if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
                         pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
         } else {
 
-                if ((r = cg_is_empty_recursive("name=systemd", user_path, true)) < 0)
+                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 we managed to kill somebody, don't cleanup the cgroup. */
@@ -460,7 +489,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                 const char *runtime_dir;
 
                 /* Remove user cgroup */
-                if ((r = cg_delete("name=systemd", user_path)) < 0)
+                if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
                         pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
 
                 /* This will migrate us to the /user cgroup. */