05ad79
From 70ed51ccc2e8e6c4976d422af960e4232210fb99 Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Mon, 16 Oct 2017 13:31:47 +0200
05ad79
Subject: [PATCH 134/135] logger: do not rely only getlogin(3) telling who ran
05ad79
 the command
05ad79
05ad79
The getlogin(3) is known not to always work, and when that happens it
05ad79
is reasonable to try determine user of name by looking process owner
05ad79
and passwd information.
05ad79
05ad79
Upstream: http://github.com/karelzak/util-linux/commit/019b97024fde3f07eaf541eef990762483369a11
05ad79
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1336432
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 misc-utils/logger.c | 25 ++++++++++++++++---------
05ad79
 1 file changed, 16 insertions(+), 9 deletions(-)
05ad79
05ad79
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
05ad79
index dfda01866..c1cec45e8 100644
05ad79
--- a/misc-utils/logger.c
05ad79
+++ b/misc-utils/logger.c
05ad79
@@ -49,6 +49,8 @@
05ad79
 #include <arpa/inet.h>
05ad79
 #include <netdb.h>
05ad79
 #include <getopt.h>
05ad79
+#include <sys/types.h>
05ad79
+#include <pwd.h>
05ad79
 
05ad79
 #include "c.h"
05ad79
 #include "closestream.h"
05ad79
@@ -183,9 +185,20 @@ inet_socket(const char *servername, const char *port, const int socket_type)
05ad79
 	return fd;
05ad79
 }
05ad79
 
05ad79
+static char const *xgetlogin(void)
05ad79
+{
05ad79
+	char const *cp;
05ad79
+	struct passwd *pw;
05ad79
+
05ad79
+	if (!(cp = getlogin()) || !*cp)
05ad79
+		cp = (pw = getpwuid(geteuid()))? pw->pw_name : "<someone>";
05ad79
+	return cp;
05ad79
+}
05ad79
+
05ad79
 static void
05ad79
 mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
05ad79
-       char *buf, pid[30], *cp, *tp;
05ad79
+       char *buf, pid[30], *tp;
05ad79
+       const char *cp;
05ad79
        time_t now;
05ad79
 
05ad79
        if (fd > -1) {
05ad79
@@ -193,13 +206,7 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
05ad79
                        snprintf (pid, sizeof(pid), "[%d]", getpid());
05ad79
 	       else
05ad79
 		       pid[0] = 0;
05ad79
-               if (tag)
05ad79
-		       cp = tag;
05ad79
-	       else {
05ad79
-		       cp = getlogin();
05ad79
-		       if (!cp)
05ad79
-			       cp = "<someone>";
05ad79
-	       }
05ad79
+	       cp = tag ? tag : xgetlogin();
05ad79
                (void)time(&now;;
05ad79
 	       tp = ctime(&now)+4;
05ad79
 
05ad79
@@ -334,7 +341,7 @@ main(int argc, char **argv) {
05ad79
 	else if (usock)
05ad79
 		LogSock = unix_socket(usock, socket_type);
05ad79
 	else
05ad79
-		openlog(tag ? tag : getlogin(), logflags, 0);
05ad79
+		openlog(tag ? tag : xgetlogin(), logflags, 0);
05ad79
 
05ad79
 	buf = xcalloc(1, max_message_size);
05ad79
 
05ad79
-- 
05ad79
2.13.6
05ad79