chiark / gitweb /
pam-module: use the correct session type "unspecified"
[elogind.git] / src / pam-module.c
index dc7c00166e6ca35f1f313de3df236a9c57d93a9b..e65088638b6de4d99683f9252a8776f8f28ac916 100644 (file)
@@ -38,6 +38,7 @@
 #include "strv.h"
 #include "dbus-common.h"
 #include "def.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,
 
 static int parse_argv(pam_handle_t *handle,
                       int argc, const char **argv,
@@ -49,8 +50,6 @@ static int parse_argv(pam_handle_t *handle,
                       bool *debug) {
 
         unsigned i;
                       bool *debug) {
 
         unsigned i;
-        bool reset_controller_set = false;
-        bool kill_exclude_users_set = false;
 
         assert(argc >= 0);
         assert(argc == 0 || argv);
 
         assert(argc >= 0);
         assert(argc == 0 || argv);
@@ -58,9 +57,9 @@ static int parse_argv(pam_handle_t *handle,
         for (i = 0; i < (unsigned) argc; i++) {
                 int k;
 
         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;
                         }
 
                                 return k;
                         }
 
@@ -106,8 +105,6 @@ static int parse_argv(pam_handle_t *handle,
                                 *reset_controllers = l;
                         }
 
                                 *reset_controllers = l;
                         }
 
-                        reset_controller_set = true;
-
                 } else if (startswith(argv[i], "kill-only-users=")) {
 
                         if (kill_only_users) {
                 } else if (startswith(argv[i], "kill-only-users=")) {
 
                         if (kill_only_users) {
@@ -136,8 +133,6 @@ static int parse_argv(pam_handle_t *handle,
                                 *kill_exclude_users = l;
                         }
 
                                 *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.");
                 } else if (startswith(argv[i], "debug=")) {
                         if ((k = parse_boolean(argv[i] + 6)) < 0) {
                                 pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
@@ -158,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;
 }
 
         return 0;
 }
 
@@ -213,14 +180,14 @@ static int get_user_data(
                  * it probably contains a uid of the host system. */
 
                 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
                  * it probably contains a uid of the host system. */
 
                 if (read_one_line_file("/proc/self/loginuid", &s) >= 0) {
-                        uint32_t u;
+                        uid_t uid;
 
 
-                        r = safe_atou32(s, &u);
+                        r = parse_uid(s, &uid);
                         free(s);
 
                         free(s);
 
-                        if (r >= 0 && u != (uint32_t) -1 && u > 0) {
+                        if (r >= 0 && uid != (uint32_t) -1) {
                                 have_loginuid = true;
                                 have_loginuid = true;
-                                pw = pam_modutil_getpwuid(handle, u);
+                                pw = pam_modutil_getpwuid(handle, uid);
                         }
                 }
         }
                         }
                 }
         }
@@ -272,10 +239,10 @@ static bool check_user_lists(
         }
 
         STRV_FOREACH(l, kill_exclude_users) {
         }
 
         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))
                                 return false;
 
                 if (name && streq(name, *l))
@@ -286,10 +253,10 @@ static bool check_user_lists(
                 return true;
 
         STRV_FOREACH(l, kill_only_users) {
                 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))
                                 return true;
 
                 if (name && streq(name, *l))
@@ -299,6 +266,71 @@ static bool check_user_lists(
         return false;
 }
 
         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(
                 pam_handle_t *handle,
                 int flags,
 _public_ PAM_EXTERN int pam_sm_open_session(
                 pam_handle_t *handle,
                 int flags,
@@ -306,7 +338,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
         struct passwd *pw;
         bool kill_processes = false, debug = false;
 
         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;
+        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;
         char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL;
         DBusError error;
         uint32_t uid, pid;
@@ -317,6 +349,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         DBusMessage *m = NULL, *reply = NULL;
         dbus_bool_t remote;
         int r;
         DBusMessage *m = NULL, *reply = NULL;
         dbus_bool_t remote;
         int r;
+        uint32_t vtnr = 0;
 
         assert(handle);
 
 
         assert(handle);
 
@@ -341,6 +374,46 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (r != PAM_SUCCESS)
                 goto finish;
 
         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);
 
         if (kill_processes)
                 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
 
@@ -368,12 +441,12 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         uid = pw->pw_uid;
         pid = getpid();
 
         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, "XDG_SEAT");
         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);
 
         service = strempty(service);
         tty = strempty(tty);
@@ -382,8 +455,24 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         remote_host = strempty(remote_host);
         seat = strempty(seat);
 
         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 = "";
+        }
+
+        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" :
         type = !isempty(display) ? "x11" :
-                   !isempty(tty) ? "tty" : "other";
+                   !isempty(tty) ? "tty" : "unspecified";
 
         remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain");
 
 
         remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain");
 
@@ -393,6 +482,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                       DBUS_TYPE_STRING, &service,
                                       DBUS_TYPE_STRING, &type,
                                       DBUS_TYPE_STRING, &seat,
                                       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, &tty,
                                       DBUS_TYPE_STRING, &display,
                                       DBUS_TYPE_BOOLEAN, &remote,
@@ -439,6 +529,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_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;
                                    DBUS_TYPE_INVALID)) {
                 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", bus_error_message(&error));
                 r = PAM_SESSION_ERR;
@@ -457,10 +549,32 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 goto finish;
         }
 
                 goto finish;
         }
 
-        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;
+        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) {
+                        pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
+                        return r;
+                }
         }
 
         session_fd = -1;
         }
 
         session_fd = -1;