chiark / gitweb /
logind: call udev_device_get_is_initialized() to trigger lazy loading, as a temporary...
[elogind.git] / src / pam-module.c
index b742d649d24f6b274d738b1719b60725d764aecc..e1ad8c9bfade571dc1ea020c8bcb2d1c4910dd28 100644 (file)
@@ -38,6 +38,7 @@
 #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,
@@ -60,7 +61,7 @@ static int parse_argv(pam_handle_t *handle,
 
                 if (startswith(argv[i], "kill-processes=")) {
                         if ((k = parse_boolean(argv[i] + 15)) < 0) {
-                                pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
+                                pam_syslog(handle, LOG_ERR, "Failed to parse kill-processes= argument.");
                                 return k;
                         }
 
@@ -299,31 +300,96 @@ static bool check_user_lists(
         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,
                 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;
-        int r;
         DBusError error;
         uint32_t uid, pid;
         DBusMessageIter iter;
         dbus_bool_t kp;
-        const char *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type;
         int session_fd = -1;
         DBusConnection *bus = NULL;
         DBusMessage *m = NULL, *reply = NULL;
         dbus_bool_t remote;
+        int r;
+        uint32_t vtnr = 0;
 
         assert(handle);
 
         dbus_error_init(&error);
 
-        pam_syslog(handle, LOG_ERR, "pam-systemd initializing");
+        /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
 
         /* Make this a NOP on non-systemd systems */
         if (sd_booted() <= 0)
@@ -333,8 +399,10 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                        argc, argv,
                        &controllers, &reset_controllers,
                        &kill_processes, &kill_only_users, &kill_exclude_users,
-                       &debug) < 0)
-                return PAM_SESSION_ERR;
+                       &debug) < 0) {
+                r = PAM_SESSION_ERR;
+                goto finish;
+        }
 
         r = get_user_data(handle, &username, &pw);
         if (r != PAM_SUCCESS)
@@ -343,6 +411,8 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (kill_processes)
                 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
 
+        dbus_connection_set_change_sigpipe(FALSE);
+
         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));
@@ -370,18 +440,31 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         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);
-
-        if (isempty(tty))
-                service = "";
-        if (isempty(tty))
+        seat = pam_getenv(handle, "LOGIN_SEAT");
+        cvtnr = pam_getenv(handle, "LOGIN_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 = "";
-        if (isempty(display))
-                display = "";
-        if (isempty(remote_user))
-                remote_user = "";
-        if (isempty(remote_host))
-                remote_host = "";
-        seat = "";
+        }
+
+        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" : "other";
@@ -394,6 +477,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                       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,
@@ -458,10 +542,12 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 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 (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;
@@ -481,12 +567,12 @@ finish:
                 dbus_connection_unref(bus);
         }
 
-        if (reply)
-                dbus_message_unref(reply);
-
         if (m)
                 dbus_message_unref(m);
 
+        if (reply)
+                dbus_message_unref(reply);
+
         if (session_fd >= 0)
                 close_nointr_nofail(session_fd);