chiark / gitweb /
logind: make session type and class settable via the same ways
[elogind.git] / src / login / pam-module.c
index 1975d8038dd1eea3cb99ae32de9c97783d471f79..9f0f7d186657f52f200c4cea736c6b3a06b8515c 100644 (file)
 #include "fileio.h"
 #include "bus-error.h"
 
-static int parse_argv(pam_handle_t *handle,
-                      int argc, const char **argv,
-                      const char **class,
-                      bool *debug) {
+static int parse_argv(
+                pam_handle_t *handle,
+                int argc, const char **argv,
+                const char **class,
+                const char **type,
+                bool *debug) {
 
         unsigned i;
 
         assert(argc >= 0);
         assert(argc == 0 || argv);
 
-        for (i = 0; i < (unsigned) argc; i++)
+        for (i = 0; i < (unsigned) argc; i++) {
                 if (startswith(argv[i], "class=")) {
                         if (class)
                                 *class = argv[i] + 6;
 
+                } else if (startswith(argv[i], "type=")) {
+                        if (type)
+                                *type = argv[i] + 5;
+
                 } else if (streq(argv[i], "debug")) {
                         if (debug)
                                 *debug = true;
@@ -72,6 +78,7 @@ static int parse_argv(pam_handle_t *handle,
 
                 } else
                         pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]);
+        }
 
         return 0;
 }
@@ -83,31 +90,24 @@ static int get_user_data(
 
         const char *username = NULL;
         struct passwd *pw = NULL;
-        uid_t uid;
         int r;
 
         assert(handle);
         assert(ret_username);
         assert(ret_pw);
 
-        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 (isempty(username)) {
-                        pam_syslog(handle, LOG_ERR, "User name not valid.");
-                        return PAM_AUTH_ERR;
-                }
+        r = pam_get_user(handle, &username, NULL);
+        if (r != PAM_SUCCESS) {
+                pam_syslog(handle, LOG_ERR, "Failed to get user name.");
+                return r;
+        }
 
-                pw = pam_modutil_getpwnam(handle, username);
+        if (isempty(username)) {
+                pam_syslog(handle, LOG_ERR, "User name not valid.");
+                return PAM_AUTH_ERR;
         }
 
+        pw = pam_modutil_getpwnam(handle, username);
         if (!pw) {
                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
                 return PAM_USER_UNKNOWN;
@@ -120,16 +120,13 @@ static int get_user_data(
 }
 
 static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
-        _cleanup_free_ char *p = NULL;
-        int r;
-        _cleanup_close_ int fd = -1;
         union sockaddr_union sa = {
                 .un.sun_family = AF_UNIX,
         };
+        _cleanup_free_ char *p = NULL, *tty = NULL;
+        _cleanup_close_ int fd = -1;
         struct ucred ucred;
-        socklen_t l;
-        _cleanup_free_ char *tty = NULL;
-        int v;
+        int v, r;
 
         assert(display);
         assert(vtnr);
@@ -152,10 +149,9 @@ static int get_seat_from_display(const char *display, const char **seat, uint32_
         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0)
                 return -errno;
 
-        l = sizeof(ucred);
-        r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
+        r = getpeercred(fd, &ucred);
         if (r < 0)
-                return -errno;
+                return r;
 
         r = get_ctty(ucred.pid, NULL, &tty);
         if (r < 0)
@@ -174,6 +170,34 @@ static int get_seat_from_display(const char *display, const char **seat, uint32_
         return 0;
 }
 
+static int export_legacy_dbus_address(
+                pam_handle_t *handle,
+                uid_t uid,
+                const char *runtime) {
+
+#ifdef ENABLE_KDBUS
+        _cleanup_free_ char *s = NULL;
+        int r;
+
+        /* skip export if kdbus is not active */
+        if (access("/dev/kdbus", F_OK) < 0)
+                return PAM_SUCCESS;
+
+        if (asprintf(&s, KERNEL_USER_BUS_FMT ";" UNIX_USER_BUS_FMT,
+                     (unsigned long) uid, runtime) < 0) {
+                pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
+                return PAM_BUF_ERR;
+        }
+
+        r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", s, 0);
+        if (r != PAM_SUCCESS) {
+                pam_syslog(handle, LOG_ERR, "Failed to set bus variable.");
+                return r;
+        }
+#endif
+        return PAM_SUCCESS;
+}
+
 _public_ PAM_EXTERN int pam_sm_open_session(
                 pam_handle_t *handle,
                 int flags,
@@ -188,18 +212,16 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 *remote_user = NULL, *remote_host = NULL,
                 *seat = NULL,
                 *type = NULL, *class = NULL,
-                *class_pam = NULL, *cvtnr = NULL;
+                *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL;
         _cleanup_bus_unref_ sd_bus *bus = NULL;
         int session_fd = -1, existing, r;
-        uint32_t uid, pid, vtnr = 0;
         bool debug = false, remote;
         struct passwd *pw;
+        uint32_t vtnr = 0;
+        uid_t original_uid;
 
         assert(handle);
 
-        if (debug)
-                pam_syslog(handle, LOG_INFO, "pam-systemd initializing");
-
         /* Make this a NOP on non-logind systems */
         if (!logind_running())
                 return PAM_SUCCESS;
@@ -207,9 +229,13 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (parse_argv(handle,
                        argc, argv,
                        &class_pam,
+                       &type_pam,
                        &debug) < 0)
                 return PAM_SESSION_ERR;
 
+        if (debug)
+                pam_syslog(handle, LOG_INFO, "pam-systemd initializing");
+
         r = get_user_data(handle, &username, &pw);
         if (r != PAM_SUCCESS) {
                 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
@@ -241,6 +267,10 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
                                 return r;
                         }
+
+                        r = export_legacy_dbus_address(handle, pw->pw_uid, rt);
+                        if (r != PAM_SUCCESS)
+                                return r;
                 }
 
                 return PAM_SUCCESS;
@@ -248,9 +278,6 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
         /* Otherwise, we ask logind to create a session for us */
 
-        uid = pw->pw_uid;
-        pid = getpid();
-
         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);
@@ -264,12 +291,20 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (isempty(cvtnr))
                 cvtnr = getenv("XDG_VTNR");
 
-        service = strempty(service);
+        type = pam_getenv(handle, "XDG_SESSION_TYPE");
+        if (isempty(type))
+                type = getenv("XDG_SESSION_TYPE");
+        if (isempty(type))
+                type = type_pam;
+
+        class = pam_getenv(handle, "XDG_SESSION_CLASS");
+        if (isempty(class))
+                class = getenv("XDG_SESSION_CLASS");
+        if (isempty(class))
+                class = class_pam;
+
         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,
@@ -284,42 +319,44 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 /* cron has been setting PAM_TTY to "cron" for a very
                  * long time and it probably shouldn't stop doing that
                  * for compatibility reasons. */
-                tty = "";
                 type = "unspecified";
+                class = "background";
+                tty = "";
         } else if (streq(tty, "ssh")) {
                 /* ssh has been setting PAM_TTY to "ssh" for a very
                  * long time and probably shouldn't stop doing that
                  * for compatibility reasons. */
-                tty = "";
                 type ="tty";
+                class = "user";
+                tty = "";
         }
 
         /* If this fails vtnr will be 0, that's intended */
         if (!isempty(cvtnr))
                 safe_atou32(cvtnr, &vtnr);
 
-        if (!isempty(display) && vtnr <= 0) {
+        if (!isempty(display) && !vtnr) {
                 if (isempty(seat))
                         get_seat_from_display(display, &seat, &vtnr);
                 else if (streq(seat, "seat0"))
                         get_seat_from_display(display, NULL, &vtnr);
         }
 
-        if (!type)
+        if (seat && !streq(seat, "seat0") && vtnr != 0) {
+                pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %d for %s which is not seat0", vtnr, seat);
+                vtnr = 0;
+        }
+
+        if (isempty(type))
                 type = !isempty(display) ? "x11" :
-                        !isempty(tty) ? "tty" : "unspecified";
+                           !isempty(tty) ? "tty" : "unspecified";
 
-        class = pam_getenv(handle, "XDG_SESSION_CLASS");
-        if (isempty(class))
-                class = getenv("XDG_SESSION_CLASS");
-        if (isempty(class))
-                class = class_pam;
         if (isempty(class))
                 class = streq(type, "unspecified") ? "background" : "user";
 
         remote = !isempty(remote_host) &&
-                !streq(remote_host, "localhost") &&
-                !streq(remote_host, "localhost.localdomain");
+                !streq_ptr(remote_host, "localhost") &&
+                !streq_ptr(remote_host, "localhost.localdomain");
 
         /* Talk to logind over the message bus */
 
@@ -332,7 +369,11 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (debug)
                 pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
                            "uid=%u pid=%u service=%s type=%s class=%s seat=%s vtnr=%u tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
-                           uid, pid, service, type, class, seat, vtnr, tty, display, yes_no(remote), remote_user, remote_host);
+                           pw->pw_uid, getpid(),
+                           strempty(service),
+                           type, class,
+                           strempty(seat), vtnr, tty, strempty(display),
+                           yes_no(remote), strempty(remote_user), strempty(remote_host));
 
         r = sd_bus_call_method(bus,
                                "org.freedesktop.login1",
@@ -342,18 +383,18 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                                &error,
                                &reply,
                                "uussssussbssa(sv)",
-                               uid,
-                               pid,
-                               service,
+                               (uint32_t) pw->pw_uid,
+                               (uint32_t) getpid(),
+                               strempty(service),
                                type,
                                class,
-                               seat,
+                               strempty(seat),
                                vtnr,
                                tty,
-                               display,
+                               strempty(display),
                                remote,
-                               remote_user,
-                               remote_host,
+                               strempty(remote_user),
+                               strempty(remote_host),
                                0);
         if (r < 0) {
                 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r));
@@ -361,11 +402,12 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         }
 
         r = sd_bus_message_read(reply,
-                                "soshsub",
+                                "soshusub",
                                 &id,
                                 &object_path,
                                 &runtime_path,
                                 &session_fd,
+                                &original_uid,
                                 &seat,
                                 &vtnr,
                                 &existing);
@@ -376,8 +418,8 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
         if (debug)
                 pam_syslog(handle, LOG_DEBUG, "Reply from logind: "
-                           "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u",
-                           id, object_path, runtime_path, session_fd, seat, vtnr);
+                           "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
+                           id, object_path, runtime_path, session_fd, seat, vtnr, original_uid);
 
         r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
         if (r != PAM_SUCCESS) {
@@ -385,10 +427,22 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 return r;
         }
 
-        r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
-        if (r != PAM_SUCCESS) {
-                pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
-                return r;
+        if (original_uid == pw->pw_uid) {
+                /* Don't set $XDG_RUNTIME_DIR if the user we now
+                 * authenticated for does not match the original user
+                 * of the session. We do this in order not to result
+                 * in privileged apps clobbering the runtime directory
+                 * unnecessarily. */
+
+                r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
+                if (r != PAM_SUCCESS) {
+                        pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
+                        return r;
+                }
+
+                r = export_legacy_dbus_address(handle, pw->pw_uid, runtime_path);
+                if (r != PAM_SUCCESS)
+                        return r;
         }
 
         if (!isempty(seat)) {
@@ -401,7 +455,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
 
         if (vtnr > 0) {
                 char buf[DECIMAL_STR_MAX(vtnr)];
-                snprintf(buf, sizeof(buf), "%u", vtnr);
+                sprintf(buf, "%u", vtnr);
 
                 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
                 if (r != PAM_SUCCESS) {
@@ -440,7 +494,6 @@ _public_ PAM_EXTERN int pam_sm_close_session(
                 int argc, const char **argv) {
 
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_bus_unref_ sd_bus *bus = NULL;
         const void *p = NULL, *existing = NULL;
         const char *id;