From e58d90ca35fde268e86c767e23ca3f74e8c5b349 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Wed, 19 Nov 2008 21:24:10 +0100
Subject: [PATCH] Changed acquisition of usernames in sieve command line tools.

---
 src/lib-sieve-tool/sieve-tool.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/lib-sieve-tool/sieve-tool.c b/src/lib-sieve-tool/sieve-tool.c
index 6c0274a78..b5f30766a 100644
--- a/src/lib-sieve-tool/sieve-tool.c
+++ b/src/lib-sieve-tool/sieve-tool.c
@@ -75,14 +75,25 @@ void sieve_tool_deinit(void)
 
 const char *sieve_tool_get_user(void)
 {
-	uid_t process_euid = geteuid();
-	struct passwd *pw = getpwuid(process_euid);
-	if (pw != NULL) {
-		return t_strdup(pw->pw_name);
-	} 
-		
-	i_fatal("couldn't lookup our username (uid=%s)", dec2str(process_euid));
-	return NULL;
+	const char *user;
+	uid_t process_euid;
+	struct passwd *pw;
+
+	user = getenv("USER");
+
+	if ( user == NULL || *user == '\0' ) {
+		process_euid = geteuid();
+
+		if ((pw = getpwuid(process_euid)) != NULL) {
+            user = t_strdup(pw->pw_name);
+        }
+	}
+
+	if ( user == NULL || *user == '\0' ) {
+		i_fatal("couldn't lookup our username (uid=%s)", dec2str(process_euid));
+	}
+	
+	return user;
 }
 
 void sieve_tool_get_envelope_data
-- 
GitLab