chiark / gitweb /
journal: move field index from file into journal object
[elogind.git] / src / pam-module.c
index a3ce2468d57ed6e1861121988b59b2c0e4d26f03..78f9b30d5bffa8db425508cf5360570571d72374 100644 (file)
@@ -163,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) {
-                        uid_t uid;
-
-                        r = parse_uid(s, &uid);
-                        free(s);
-
-                        if (r >= 0 && uid != (uint32_t) -1) {
-                                have_loginuid = true;
-                                pw = pam_modutil_getpwuid(handle, uid);
-                        }
-                }
-        }
-
-        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;
                 }
@@ -445,8 +427,8 @@ _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);
-        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);
@@ -529,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;
@@ -547,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) {