daandemeyer / rpms / systemd

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