daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
698723
From 96887ddecd1e4c36d8a32411ed515ddaf0f3a0e3 Mon Sep 17 00:00:00 2001
698723
From: Lennart Poettering <lennart@poettering.net>
698723
Date: Fri, 20 Jul 2018 11:27:55 +0200
698723
Subject: [PATCH] pam_systemd: simplify code which with we set environment
698723
 variables
698723
698723
Let's shorten things a bit by splitting out common code in a new
698723
function.
698723
698723
(cherry picked from commit d6baaa6978d3eb5b8e8497021c4ba576aee936a3)
698723
698723
Related: #1642460
698723
---
698723
 src/login/pam_systemd.c | 46 ++++++++++++++++++++++++-----------------
698723
 1 file changed, 27 insertions(+), 19 deletions(-)
698723
698723
diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c
698723
index 78ddb7d398..b2b62540bb 100644
698723
--- a/src/login/pam_systemd.c
698723
+++ b/src/login/pam_systemd.c
698723
@@ -301,6 +301,24 @@ static const char* getenv_harder(pam_handle_t *handle, const char *key, const ch
698723
         return fallback;
698723
 }
698723
 
698723
+static int update_environment(pam_handle_t *handle, const char *key, const char *value) {
698723
+        int r;
698723
+
698723
+        assert(handle);
698723
+        assert(key);
698723
+
698723
+        /* Updates the environment, but only if there's actually a value set. Also, log about errors */
698723
+
698723
+        if (isempty(value))
698723
+                return PAM_SUCCESS;
698723
+
698723
+        r = pam_misc_setenv(handle, key, value, 0);
698723
+        if (r != PAM_SUCCESS)
698723
+                pam_syslog(handle, LOG_ERR, "Failed to set environment variable %s.", key);
698723
+
698723
+        return r;
698723
+}
698723
+
698723
 _public_ PAM_EXTERN int pam_sm_open_session(
698723
                 pam_handle_t *handle,
698723
                 int flags,
698723
@@ -555,11 +573,9 @@ _public_ PAM_EXTERN int pam_sm_open_session(
698723
                            "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
698723
                            id, object_path, runtime_path, session_fd, seat, vtnr, original_uid);
698723
 
698723
-        r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
698723
-        if (r != PAM_SUCCESS) {
698723
-                pam_syslog(handle, LOG_ERR, "Failed to set session id.");
698723
+        r = update_environment(handle, "XDG_SESSION_ID", id);
698723
+        if (r != PAM_SUCCESS)
698723
                 return r;
698723
-        }
698723
 
698723
         if (original_uid == pw->pw_uid) {
698723
                 /* Don't set $XDG_RUNTIME_DIR if the user we now
698723
@@ -568,34 +584,26 @@ _public_ PAM_EXTERN int pam_sm_open_session(
698723
                  * in privileged apps clobbering the runtime directory
698723
                  * unnecessarily. */
698723
 
698723
-                r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
698723
-                if (r != PAM_SUCCESS) {
698723
-                        pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
698723
+                r = update_environment(handle, "XDG_RUNTIME_DIR", runtime_path);
698723
+                if (r != PAM_SUCCESS)
698723
                         return r;
698723
-                }
698723
 
698723
                 r = export_legacy_dbus_address(handle, pw->pw_uid, runtime_path);
698723
                 if (r != PAM_SUCCESS)
698723
                         return r;
698723
         }
698723
 
698723
-        if (!isempty(seat)) {
698723
-                r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
698723
-                if (r != PAM_SUCCESS) {
698723
-                        pam_syslog(handle, LOG_ERR, "Failed to set seat.");
698723
-                        return r;
698723
-                }
698723
-        }
698723
+        r = update_environment(handle, "XDG_SEAT", seat);
698723
+        if (r != PAM_SUCCESS)
698723
+                return r;
698723
 
698723
         if (vtnr > 0) {
698723
                 char buf[DECIMAL_STR_MAX(vtnr)];
698723
                 sprintf(buf, "%u", vtnr);
698723
 
698723
-                r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
698723
-                if (r != PAM_SUCCESS) {
698723
-                        pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
698723
+                r = update_environment(handle, "XDG_VTNR", buf);
698723
+                if (r != PAM_SUCCESS)
698723
                         return r;
698723
-                }
698723
         }
698723
 
698723
         r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL);