chiark / gitweb /
journal: move field index from file into journal object
[elogind.git] / src / pam-module.c
index e1ad8c9bfade571dc1ea020c8bcb2d1c4910dd28..78f9b30d5bffa8db425508cf5360570571d72374 100644 (file)
@@ -50,8 +50,6 @@ static int parse_argv(pam_handle_t *handle,
                       bool *debug) {
 
         unsigned i;
-        bool reset_controller_set = false;
-        bool kill_exclude_users_set = false;
 
         assert(argc >= 0);
         assert(argc == 0 || argv);
@@ -59,9 +57,9 @@ static int parse_argv(pam_handle_t *handle,
         for (i = 0; i < (unsigned) argc; i++) {
                 int k;
 
-                if (startswith(argv[i], "kill-processes=")) {
-                        if ((k = parse_boolean(argv[i] + 15)) < 0) {
-                                pam_syslog(handle, LOG_ERR, "Failed to parse kill-processes= 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;
                         }
 
@@ -107,8 +105,6 @@ static int parse_argv(pam_handle_t *handle,
                                 *reset_controllers = l;
                         }
 
-                        reset_controller_set = true;
-
                 } else if (startswith(argv[i], "kill-only-users=")) {
 
                         if (kill_only_users) {
@@ -137,8 +133,6 @@ static int parse_argv(pam_handle_t *handle,
                                 *kill_exclude_users = l;
                         }
 
-                        kill_exclude_users_set = true;
-
                 } else if (startswith(argv[i], "debug=")) {
                         if ((k = parse_boolean(argv[i] + 6)) < 0) {
                                 pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
@@ -159,34 +153,6 @@ static int parse_argv(pam_handle_t *handle,
                 }
         }
 
-        if (!reset_controller_set && reset_controllers) {
-                char **l;
-
-                if (!(l = strv_new("cpu", NULL))) {
-                        pam_syslog(handle, LOG_ERR, "Out of memory");
-                        return -ENOMEM;
-                }
-
-                *reset_controllers = l;
-        }
-
-        if (controllers)
-                strv_remove(*controllers, SYSTEMD_CGROUP_CONTROLLER);
-
-        if (reset_controllers)
-                strv_remove(*reset_controllers, SYSTEMD_CGROUP_CONTROLLER);
-
-        if (!kill_exclude_users_set && kill_exclude_users) {
-                char **l;
-
-                if (!(l = strv_new("root", NULL))) {
-                        pam_syslog(handle, LOG_ERR, "Out of memory");
-                        return -ENOMEM;
-                }
-
-                *kill_exclude_users = l;
-        }
-
         return 0;
 }
 
@@ -197,42 +163,24 @@ static int get_user_data(
 
         const char *username = NULL;
         struct passwd *pw = NULL;
+        uid_t uid;
         int r;
-        bool have_loginuid = false;
-        char *s;
 
         assert(handle);
         assert(ret_username);
         assert(ret_pw);
 
-        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) {
-                        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 (!have_loginuid) {
-                if ((r = pam_get_user(handle, &username, NULL)) != PAM_SUCCESS) {
+        r = audit_loginuid_from_pid(0, &uid);
+        if (r >= 0)
+                pw = pam_modutil_getpwuid(handle, uid);
+        else {
+                r = pam_get_user(handle, &username, NULL);
+                if (r != PAM_SUCCESS) {
                         pam_syslog(handle, LOG_ERR, "Failed to get user name.");
                         return r;
                 }
 
-                if (!username || !*username) {
+                if (isempty(username)) {
                         pam_syslog(handle, LOG_ERR, "User name not valid.");
                         return PAM_AUTH_ERR;
                 }
@@ -273,10 +221,10 @@ static bool check_user_lists(
         }
 
         STRV_FOREACH(l, kill_exclude_users) {
-                uint32_t id;
+                uid_t u;
 
-                if (safe_atou32(*l, &id) >= 0)
-                        if ((uid_t) id == uid)
+                if (parse_uid(*l, &u) >= 0)
+                        if (u == uid)
                                 return false;
 
                 if (name && streq(name, *l))
@@ -287,10 +235,10 @@ static bool check_user_lists(
                 return true;
 
         STRV_FOREACH(l, kill_only_users) {
-                uint32_t id;
+                uid_t u;
 
-                if (safe_atou32(*l, &id) >= 0)
-                        if ((uid_t) id == uid)
+                if (parse_uid(*l, &u) >= 0)
+                        if (u == uid)
                                 return true;
 
                 if (name && streq(name, *l))
@@ -408,6 +356,46 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (r != PAM_SUCCESS)
                 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. */
+
+        pam_get_item(handle, PAM_SERVICE, (const void**) &service);
+        if (streq_ptr(service, "systemd-shared")) {
+                char *p, *rt = NULL;
+
+                if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) pw->pw_uid) < 0) {
+                        r = PAM_BUF_ERR;
+                        goto finish;
+                }
+
+                r = parse_env_file(p, NEWLINE,
+                                   "RUNTIME", &rt,
+                                   NULL);
+                free(p);
+
+                if (r < 0 && r != -ENOENT) {
+                        r = PAM_SESSION_ERR;
+                        free(rt);
+                        goto finish;
+                }
+
+                if (rt)  {
+                        r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
+                        free(rt);
+
+                        if (r != PAM_SUCCESS) {
+                                pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
+                                goto finish;
+                        }
+                }
+
+                r = PAM_SUCCESS;
+                goto finish;
+        }
+
         if (kill_processes)
                 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
 
@@ -435,13 +423,12 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         uid = pw->pw_uid;
         pid = getpid();
 
-        pam_get_item(handle, PAM_SERVICE, (const void**) &service);
         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, "LOGIN_SEAT");
-        cvtnr = pam_getenv(handle, "LOGIN_VTNR");
+        seat = pam_getenv(handle, "XDG_SEAT");
+        cvtnr = pam_getenv(handle, "XDG_VTNR");
 
         service = strempty(service);
         tty = strempty(tty);
@@ -524,6 +511,8 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                    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;
@@ -542,6 +531,26 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 goto finish;
         }
 
+        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 (vtnr > 0) {
+                char buf[11];
+                snprintf(buf, sizeof(buf), "%u", vtnr);
+                char_array_0(buf);
+
+                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 (session_fd >= 0) {
                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
                 if (r != PAM_SUCCESS) {