dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0134-logger-do-not-rely-only-getlogin-3-telling-who-ran-t.patch

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