From 3276642a489498cd6018a909ed7fb5acfdf4697f Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 4 Aug 2019 14:20:34 +0200
Subject: [PATCH 14/18] comsat(8): Sync with FreeBSD.

* Handle pts(4) device names, also catch fork() errors.

* Also switch to utmpx.
---
 libexec/Makefile        |   2 +-
 libexec/comsat/Makefile |   2 +-
 libexec/comsat/comsat.8 |   7 +--
 libexec/comsat/comsat.c | 129 ++++++++++++++++------------------------
 4 files changed, 55 insertions(+), 85 deletions(-)

diff --git a/libexec/Makefile b/libexec/Makefile
index ac7b76ebfa..0834b63450 100644
--- a/libexec/Makefile
+++ b/libexec/Makefile
@@ -3,6 +3,7 @@
 
 SUBDIR=	atrun \
 	bootpd \
+	comsat\
 	customcc \
 	dma \
 	fingerd \
@@ -29,7 +30,6 @@ SUBDIR=	atrun \
 	ypxfr
 
 _XXX_PORT_TO_UTMPX = \
-	comsat\
 	ftpd \
 	rpc.rusersd
 
diff --git a/libexec/comsat/Makefile b/libexec/comsat/Makefile
index a5fe059e4c..151c2edaea 100644
--- a/libexec/comsat/Makefile
+++ b/libexec/comsat/Makefile
@@ -1,5 +1,5 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/4/93
-# $FreeBSD: src/libexec/comsat/Makefile,v 1.6.2.1 2001/04/25 10:40:50 ru Exp $
+# $FreeBSD: head/libexec/comsat/Makefile 74814 2001-03-26 14:22:12Z ru $
 
 PROG=	comsat
 MAN=	comsat.8
diff --git a/libexec/comsat/comsat.8 b/libexec/comsat/comsat.8
index 84e9a99393..d7752320bb 100644
--- a/libexec/comsat/comsat.8
+++ b/libexec/comsat/comsat.8
@@ -26,10 +26,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)comsat.8	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD: src/libexec/comsat/comsat.8,v 1.14 2005/02/13 23:45:48 ru Exp $
-.\" $DragonFly: src/libexec/comsat/comsat.8,v 1.3 2006/02/17 19:33:31 swildner Exp $
+.\" $FreeBSD: head/libexec/comsat/comsat.8 262136 2014-02-17 22:27:32Z brueffer $
 .\"
-.Dd August 9, 2002
+.Dd March 9, 2014
 .Dt COMSAT 8
 .Os
 .Sh NAME
@@ -88,7 +87,7 @@ If
 omitted, standard mailbox assumed.
 .Sh FILES
 .Bl -tag -width ".Pa /var/mail/user" -compact
-.It Pa /var/run/utmp
+.It Pa /var/run/utmpx
 to find out who is logged on and on what terminals
 .It Pa /var/mail/user
 standard mailbox
diff --git a/libexec/comsat/comsat.c b/libexec/comsat/comsat.c
index 376455899f..85b8fff3f0 100644
--- a/libexec/comsat/comsat.c
+++ b/libexec/comsat/comsat.c
@@ -1,4 +1,6 @@
-/*
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
  * Copyright (c) 1980, 1993
  *	The Regents of the University of California.  All rights reserved.
  *
@@ -27,7 +29,7 @@
  * SUCH DAMAGE.
  *
  * @(#)comsat.c	8.1 (Berkeley) 6/4/93
- * $FreeBSD: src/libexec/comsat/comsat.c,v 1.17 2005/02/14 17:42:56 stefanf Exp $
+ * $FreeBSD: head/libexec/comsat/comsat.c 326025 2017-11-20 19:49:47Z pfg $
  */
 
 #include <sys/param.h>
@@ -52,25 +54,22 @@
 #include <sysexits.h>
 #include <syslog.h>
 #include <unistd.h>
+#include <utmpx.h>
 
-int	debug = 0;
+static int	debug = 0;
 #define	dsyslog	if (debug) syslog
 
 #define MAXIDLE	120
 
-char	hostname[MAXHOSTNAMELEN];
-struct	utmp *utmp = NULL;
-time_t	lastmsgtime;
-int	nutmp, uf;
+static char	hostname[MAXHOSTNAMELEN];
 
-void jkfprintf (FILE *, const char *, const char *, off_t);
-void mailfor (char *);
-void notify (struct utmp *, char *, off_t, int);
-void onalrm (int);
-void reapchildren (int);
+static void	jkfprintf(FILE *, char[], char[], off_t);
+static void	mailfor(char *);
+static void	notify(struct utmpx *, char[], off_t, int);
+static void	reapchildren(int);
 
 int
-main(int argc __unused, char **argv __unused)
+main(int argc __unused, char *argv[] __unused)
 {
 	struct sockaddr_in from;
 	socklen_t fromlen;
@@ -87,15 +86,7 @@ main(int argc __unused, char **argv __unused)
 		recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
 		exit(1);
 	}
-	if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
-		syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
-		recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
-		exit(1);
-	}
-	time(&lastmsgtime);
 	gethostname(hostname, sizeof(hostname));
-	onalrm(0);
-	signal(SIGALRM, onalrm);
 	signal(SIGTTOU, SIG_IGN);
 	signal(SIGCHLD, reapchildren);
 	for (;;) {
@@ -106,57 +97,28 @@ main(int argc __unused, char **argv __unused)
 			errno = 0;
 			continue;
 		}
-		if (!nutmp)		/* no one has logged in yet */
-			continue;
-		sigblock(sigmask(SIGALRM));
 		msgbuf[cc] = '\0';
-		time(&lastmsgtime);
 		mailfor(msgbuf);
 		sigsetmask(0L);
 	}
 }
 
-void
+static void
 reapchildren(int signo __unused)
 {
 	while (wait3(NULL, WNOHANG, NULL) > 0);
 }
 
-void
-onalrm(int signo __unused)
-{
-	static u_int utmpsize;		/* last malloced size for utmp */
-	static time_t utmpmtime;	/* last modification time for utmp */
-	struct stat statbf;
-
-	if (time(NULL) - lastmsgtime >= MAXIDLE)
-		exit(EX_OK);
-	alarm((u_int)15);
-	fstat(uf, &statbf);
-	if (statbf.st_mtime > utmpmtime) {
-		utmpmtime = statbf.st_mtime;
-		if (statbf.st_size > utmpsize) {
-			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
-			if ((utmp = realloc(utmp, utmpsize)) == NULL) {
-				syslog(LOG_ERR, "realloc: %m");
-				exit(1);
-			}
-		}
-		lseek(uf, (off_t)0, SEEK_SET);
-		nutmp = read(uf, utmp, (size_t)statbf.st_size)/sizeof(struct utmp);
-	}
-}
-
-void
+static void
 mailfor(char *name)
 {
-	struct utmp *utp = &utmp[nutmp];
+	struct utmpx *utp;
 	char *cp;
 	char *file;
 	off_t offset;
 	int folder;
-	char buf[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
-	char buf2[sizeof(_PATH_MAILDIR) + sizeof(utmp[0].ut_name) + 1];
+	char buf[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
+	char buf2[sizeof(_PATH_MAILDIR) + sizeof(utp->ut_user) + 1];
 
 	if (!(cp = strchr(name, '@')))
 		return;
@@ -166,62 +128,71 @@ mailfor(char *name)
 		file = name;
 	else
 		file = cp + 1;
-	sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utmp[0].ut_name),
+	sprintf(buf, "%s/%.*s", _PATH_MAILDIR, (int)sizeof(utp->ut_user),
 	    name);
 	if (*file != '/') {
 		sprintf(buf2, "%s/%.*s", _PATH_MAILDIR,
-		    (int)sizeof(utmp[0].ut_name), file);
+		    (int)sizeof(utp->ut_user), file);
 		file = buf2;
 	}
 	folder = strcmp(buf, file);
-	while (--utp >= utmp)
-		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
+	setutxent();
+	while ((utp = getutxent()) != NULL)
+		if (utp->ut_type == USER_PROCESS && !strcmp(utp->ut_user, name))
 			notify(utp, file, offset, folder);
+	endutxent();
 }
 
 static const char *cr;
 
-void
-notify(struct utmp *utp, char *file, off_t offset, int folder)
+static void
+notify(struct utmpx *utp, char file[], off_t offset, int folder)
 {
 	FILE *tp;
 	struct stat stb;
 	struct termios tio;
-	char tty[20], name[sizeof(utmp[0].ut_name) + 1];
+	char tty[20];
+	const char *s = utp->ut_line;
 
-	snprintf(tty, sizeof(tty), "%s%.*s",
-	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
-	if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
+	if (strncmp(s, "pts/", 4) == 0)
+		s += 4;
+	if (strchr(s, '/')) {
 		/* A slash is an attempt to break security... */
-		syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
+		syslog(LOG_AUTH | LOG_NOTICE, "Unexpected `/' in `%s'",
+		    utp->ut_line);
 		return;
 	}
-	if (stat(tty, &stb) || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
-		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
+	snprintf(tty, sizeof(tty), "%s%.*s",
+	    _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
+	if (stat(tty, &stb) == -1 || !(stb.st_mode & (S_IXUSR | S_IXGRP))) {
+		dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_user, tty);
 		return;
 	}
-	dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
-	if (fork())
+	dsyslog(LOG_DEBUG, "notify %s on %s", utp->ut_user, tty);
+	switch (fork()) {
+	case -1:
+		syslog(LOG_NOTICE, "fork failed (%m)");
 		return;
-	signal(SIGALRM, SIG_DFL);
-	alarm((u_int)30);
+	case 0:
+		break;
+	default:
+		return;
+	}
 	if ((tp = fopen(tty, "w")) == NULL) {
 		dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
 		_exit(1);
 	}
 	tcgetattr(fileno(tp), &tio);
 	cr = ((tio.c_oflag & (OPOST|ONLCR)) == (OPOST|ONLCR)) ?  "\n" : "\n\r";
-	strncpy(name, utp->ut_name, sizeof(name) - 1);
-	name[sizeof(name) - 1] = '\0';
 	switch (stb.st_mode & (S_IXUSR | S_IXGRP)) {
 	case S_IXUSR:
 	case (S_IXUSR | S_IXGRP):
 		fprintf(tp, 
 		    "%s\007New mail for %s@%.*s\007 has arrived%s%s%s:%s----%s",
-		    cr, name, (int)sizeof(hostname), hostname,
+		    cr, utp->ut_user, (int)sizeof(hostname), hostname,
 		    folder ? cr : "", folder ? "to " : "", folder ? file : "",
 		    cr, cr);
-		jkfprintf(tp, name, file, offset);
+		jkfprintf(tp, utp->ut_user, file, offset);
 		break;
 	case S_IXGRP:
 		fprintf(tp, "\007");
@@ -236,8 +207,8 @@ notify(struct utmp *utp, char *file, off_t offset, int folder)
 	_exit(EX_OK);
 }
 
-void
-jkfprintf(FILE *tp, const char *user, const char *file, off_t offset)
+static void
+jkfprintf(FILE *tp, char user[], char file[], off_t offset)
 {
 	unsigned char *cp, ch;
 	FILE *fi;
@@ -252,7 +223,7 @@ jkfprintf(FILE *tp, const char *user, const char *file, off_t offset)
 	if ((fi = fopen(file, "r")) == NULL)
 		return;
 
-	fseeko(fi, offset, SEEK_SET);
+	fseeko(fi, offset, SEEK_CUR);
 	/*
 	 * Print the first 7 lines or 560 characters of the new mail
 	 * (whichever comes first).  Skip header crap other than
-- 
2.22.0

