chiark / gitweb /
logind: make session type and class settable via the same ways
[elogind.git] / src / login / pam-module.c
index 89623aa765747e5bc93c0e2a3191dba3eb8f2b7d..9f0f7d186657f52f200c4cea736c6b3a06b8515c 100644 (file)
@@ -46,6 +46,7 @@ static int parse_argv(
                 pam_handle_t *handle,
                 int argc, const char **argv,
                 const char **class,
+                const char **type,
                 bool *debug) {
 
         unsigned i;
@@ -53,11 +54,15 @@ static int parse_argv(
         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;
@@ -73,6 +78,7 @@ static int parse_argv(
 
                 } else
                         pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]);
+        }
 
         return 0;
 }
@@ -164,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,
@@ -178,7 +212,7 @@ _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;
         bool debug = false, remote;
@@ -195,6 +229,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (parse_argv(handle,
                        argc, argv,
                        &class_pam,
+                       &type_pam,
                        &debug) < 0)
                 return PAM_SESSION_ERR;
 
@@ -232,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;
@@ -252,6 +291,18 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (isempty(cvtnr))
                 cvtnr = getenv("XDG_VTNR");
 
+        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);
 
@@ -268,14 +319,16 @@ _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 */
@@ -289,15 +342,15 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                         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";
 
@@ -386,6 +439,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, runtime_path);
+                if (r != PAM_SUCCESS)
+                        return r;
         }
 
         if (!isempty(seat)) {