From d892a4453363292eb130abc4acfb418091e2f447 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 6 Aug 2019 23:18:05 +0200
Subject: [PATCH 28/28] Sync utmp_update(8) with NetBSD.

---
 libexec/utmp_update/Makefile      |  2 +-
 libexec/utmp_update/utmp_update.8 |  2 +-
 libexec/utmp_update/utmp_update.c | 74 ++++++++++++++++++++++---------
 3 files changed, 54 insertions(+), 24 deletions(-)

diff --git a/libexec/utmp_update/Makefile b/libexec/utmp_update/Makefile
index ac9f33f6c2..956214a85a 100644
--- a/libexec/utmp_update/Makefile
+++ b/libexec/utmp_update/Makefile
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.2 2002/09/18 06:21:15 lukem Exp $
+#	$NetBSD: Makefile,v 1.4 2011/09/17 01:50:54 christos Exp $
 
 PROG=	utmp_update
 MAN=	utmp_update.8
diff --git a/libexec/utmp_update/utmp_update.8 b/libexec/utmp_update/utmp_update.8
index afa7c7ab65..8421cbd183 100644
--- a/libexec/utmp_update/utmp_update.8
+++ b/libexec/utmp_update/utmp_update.8
@@ -1,4 +1,4 @@
-.\"	$NetBSD: utmp_update.8,v 1.1 2002/12/16 22:45:15 wiz Exp $
+.\"	$NetBSD: utmp_update.8,v 1.2 2008/04/30 13:10:52 martin Exp $
 .\"
 .\" Copyright (c) 2002 The NetBSD Foundation, Inc.
 .\" All rights reserved.
diff --git a/libexec/utmp_update/utmp_update.c b/libexec/utmp_update/utmp_update.c
index f56659ff13..f893c58e3a 100644
--- a/libexec/utmp_update/utmp_update.c
+++ b/libexec/utmp_update/utmp_update.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: utmp_update.c,v 1.8 2008/04/28 20:23:04 martin Exp $	 */
+/*	$NetBSD: utmp_update.c,v 1.13 2015/04/26 08:56:19 mlelstv Exp $	 */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -44,6 +44,28 @@
 #include <string.h>
 #include <unistd.h>
 #include <paths.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <syslog.h>
+
+static __dead2 __printflike(2, 3) void
+logerr(int e, const char *fmt, ...)
+{
+	va_list sap, eap;
+	char *s = NULL;
+	
+	if (e)
+		(void)asprintf(&s, "%s (%s)", fmt, strerror(e));
+	if (s)
+		fmt = s;
+
+	va_start(sap, fmt);
+	va_copy(eap, sap);
+	vsyslog(LOG_ERR, fmt, sap);
+	va_end(sap);
+	verrx(1, fmt, eap);
+	va_end(eap);
+}
 
 int
 main(int argc, char *argv[])
@@ -59,42 +81,45 @@ main(int argc, char *argv[])
 
 	euid = geteuid();
 	ruid = getuid();
-	if (seteuid(ruid) == -1)
-		err(1, "seteuid");
 
 	if (argc != 2) {
 		(void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
-			getprogname());
+		    getprogname());
 		exit(1);
 	}
 
+	openlog(getprogname(), LOG_PID | LOG_NDELAY, LOG_AUTH);
+	if (seteuid(ruid) == -1)
+		logerr(errno, "Can't setuid %ld", (long)ruid);
+
 	len = strlen(argv[1]);
 
 	if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
-		errx(1, "Bad argument");
+		logerr(0, "Bad argument size %zu", len);
 
-	if ((utx = malloc(len)) == NULL)
-		err(1, NULL);
+	if ((utx = malloc(len+1)) == NULL)
+		logerr(errno, "Can't allocate %zu", len+1);
 
 	res = strunvis((char *)utx, argv[1]);
 	if (res != (int)sizeof(*utx))
-		errx(1, "Decoding error %s %d != %zu", argv[1], res, sizeof(*utx));
+		logerr(0, "Decoding error %s %d != %zu", argv[1], res,
+		    sizeof(*utx));
 
 	switch (utx->ut_type) {
 	case USER_PROCESS:
 	case DEAD_PROCESS:
 		break;
 	default:
-		errx(1, "Invalid utmpx type %hd", utx->ut_type);
+		logerr(0, "Invalid utmpx type %hd", utx->ut_type);
 	}
 
 	if (ruid != 0) {
 		if ((pwd = getpwuid(ruid)) == NULL)
-			errx(1, "User %lu does not exist in password database",
-			    (long)ruid);
+			logerr(0, "User %ld does not exist in password"
+			    " database", (long)ruid);
 
 		if (strcmp(pwd->pw_name, utx->ut_name) != 0)
-			errx(1, "Current user `%s' does not match "
+			logerr(0, "Current user `%s' does not match "
 			    "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
 	}
 
@@ -102,34 +127,39 @@ main(int argc, char *argv[])
 	fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
 	if (fd != -1) {
 		if (fstat(fd, &st) == -1)
-			err(1, "Cannot stat `%s'", tty);
+			logerr(errno, "Cannot stat `%s'", tty);
 		if (ruid != 0 && st.st_uid != ruid)
-			errx(1, "%s: Is not owned by you", tty);
+			logerr(0, "%s: Is not owned by you", tty);
 		if (!isatty(fd))
-			errx(1, "%s: Not a tty device", tty);
+			logerr(0, "%s: Not a tty device", tty);
 		(void)close(fd);
 		if (access(tty, W_OK|R_OK) == -1)
-			err(1, "%s", tty);
+			logerr(errno, "Can't access `%s'", tty);
 	} else {
 		struct utmpx utold, *utoldp;
+		pid_t ppid;
+
 		/*
 		 * A daemon like ftpd that does not use a tty line? 
 		 * We only allow it to kill its own existing entries 
 		 */
 		if (utx->ut_type != DEAD_PROCESS)
-			err(1, "Cannot open `%s'", tty);
+			logerr(errno, "Cannot open `%s'", tty);
 
 		(void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
 		if ((utoldp = getutxline(&utold)) == NULL)
-			err(1, "Cannot find existing entry for `%s'",
+			logerr(0, "Cannot find existing entry for `%s'",
 			    utx->ut_line);
-		if (utoldp->ut_pid != getppid())
-			err(1, "Cannot modify entry for `%s'", tty);
+		if (utoldp->ut_pid != (ppid = getppid()))
+			logerr(0, "Cannot modify entry for `%s' "
+			    "utmp pid %ld != parent %ld", tty,
+			    (long)utoldp->ut_pid, (long)ppid);
 	}
 
-	(void)seteuid(euid);
+	if (seteuid(euid) == 1)
+		logerr(errno, "Can't setuid %ld", (long)euid);
 	if (pututxline(utx) == NULL)
-		err(1, "Cannot update utmp entry");
+		logerr(errno, "Cannot update utmp entry");
 
 	return 0;
 }
-- 
2.22.0

