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