chiark / gitweb /
logind: move logind into its own subdirectory
[elogind.git] / src / pam-module.c
index dd05f93d426ade6ddeea76a5b0d0cc5b3868fca4..14e706b37430a52c2f03953f48fc36a159d2793e 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;
                 }
@@ -463,6 +445,10 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 if (isempty(display))
                         display = tty;
                 tty = "";
+        } else if (streq(tty, "cron")) {
+                /* cron has been setting PAM_TTY to "cron" for a very long time
+                 * and it cannot stop doing that for compatibility reasons. */
+                tty = "";
         }
 
         if (!isempty(cvtnr))
@@ -472,7 +458,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 get_seat_from_display(display, &seat, &vtnr);
 
         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");
 
@@ -517,6 +503,11 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 goto finish;
         }
 
+        if (debug)
+                pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
+                           "uid=%u pid=%u service=%s type=%s seat=%s vtnr=%u tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
+                           uid, pid, service, type, seat, vtnr, tty, display, yes_no(remote), remote_user, remote_host);
+
         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
         if (!reply) {
                 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error));
@@ -537,6 +528,11 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 goto finish;
         }
 
+        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);
+
         r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
         if (r != PAM_SUCCESS) {
                 pam_syslog(handle, LOG_ERR, "Failed to set session id.");