From 58b8ce79f36a75c7f5e830e658095699ce187684 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 4 Aug 2019 17:41:22 +0200
Subject: [PATCH 21/22] ppp(8): Adjust for utmpx (taken from FreeBSD).

---
 usr.sbin/Makefile       |  2 +-
 usr.sbin/ppp/id.c       | 34 ++++++++++++----------------------
 usr.sbin/ppp/id.h       | 23 +++++------------------
 usr.sbin/ppp/physical.c | 26 +++++++++++++-------------
 4 files changed, 31 insertions(+), 54 deletions(-)

diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile
index 8d79724933..6bc92cc2ad 100644
--- a/usr.sbin/Makefile
+++ b/usr.sbin/Makefile
@@ -78,6 +78,7 @@ SUBDIR= 802_11 \
 	pfctl \
 	pflogd \
 	powerd \
+	ppp \
 	pppctl \
 	prefix \
 	procctl \
@@ -143,7 +144,6 @@ SUBDIR= 802_11 \
 
 _XXX_PORT_TO_UTMPX = \
 	ac \
-	ppp \
 	rwhod
 
 .if !defined(NO_LPR)
diff --git a/usr.sbin/ppp/id.c b/usr.sbin/ppp/id.c
index 4d82831fda..5a077ecdb3 100644
--- a/usr.sbin/ppp/id.c
+++ b/usr.sbin/ppp/id.c
@@ -49,6 +49,7 @@
 #else
 #include <libutil.h>
 #endif
+#include <utmpx.h>
 
 #include "log.h"
 #include "main.h"
@@ -201,36 +202,25 @@ ID0uu_unlock(const char *basettyname)
 }
 
 void
-ID0login(struct utmp *ut)
+ID0login(const struct utmpx *ut)
 {
   ID0set0();
-  if (logout(ut->ut_line)) {
-    log_Printf(LogID0, "logout(\"%s\")\n", ut->ut_line);
-    logwtmp(ut->ut_line, "", "");
-    log_Printf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut->ut_line);
-  }
-  login(ut);
-  log_Printf(LogID0, "login(\"%s\", \"%.*s\")\n",
-            ut->ut_line, (int)(sizeof ut->ut_name), ut->ut_name);
+  pututxline(ut);
+  log_Printf(LogID0, "pututxline(\"%.*s\", \"%.*s\", \"%.*s\", \"%.*s\")\n",
+      (int)sizeof ut->ut_id, ut->ut_id,
+      (int)sizeof ut->ut_user, ut->ut_user,
+      (int)sizeof ut->ut_line, ut->ut_line,
+      (int)sizeof ut->ut_host, ut->ut_host);
   ID0setuser();
 }
 
 void
-ID0logout(const char *device, int nologout)
+ID0logout(const struct utmpx *ut)
 {
-  struct utmp ut;
-  char ut_line[sizeof ut.ut_line + 1];
-
-  strncpy(ut_line, device, sizeof ut_line - 1);
-  ut_line[sizeof ut_line - 1] = '\0';
-
   ID0set0();
-  if (nologout || logout(ut_line)) {
-    log_Printf(LogID0, "logout(\"%s\")\n", ut_line);
-    logwtmp(ut_line, "", "");
-    log_Printf(LogID0, "logwtmp(\"%s\", \"\", \"\")\n", ut_line);
-  } else
-    log_Printf(LogERROR, "ID0logout: No longer logged in on %s\n", ut_line);
+  pututxline(ut);
+  log_Printf(LogID0, "pututxline(\"%.*s\")\n",
+      (int)sizeof ut->ut_id, ut->ut_id);
   ID0setuser();
 }
 
diff --git a/usr.sbin/ppp/id.h b/usr.sbin/ppp/id.h
index 9223c118b7..615e588416 100644
--- a/usr.sbin/ppp/id.h
+++ b/usr.sbin/ppp/id.h
@@ -24,11 +24,10 @@
  * SUCH DAMAGE.
  *
  * $FreeBSD: src/usr.sbin/ppp/id.h,v 1.13.2.1 2000/08/19 09:30:03 brian Exp $
- * $DragonFly: src/usr.sbin/ppp/id.h,v 1.3 2004/02/03 07:11:47 dillon Exp $
  */
 
 #ifndef NOSUID
-struct utmp;
+struct utmpx;
 struct sockaddr_un;
 
 extern void ID0init(void);
@@ -42,8 +41,8 @@ extern int ID0write(int, const void *, size_t);
 extern int ID0uu_lock(const char *);
 extern int ID0uu_lock_txfr(const char *, pid_t);
 extern int ID0uu_unlock(const char *);
-extern void ID0login(struct utmp *);
-extern void ID0logout(const char *, int);
+extern void ID0login(const struct utmpx *);
+extern void ID0logout(const struct utmpx *);
 extern int ID0bind_un(int, const struct sockaddr_un *);
 extern int ID0connect_un(int, const struct sockaddr_un *);
 extern int ID0kill(pid_t, int);
@@ -65,20 +64,8 @@ extern int ID0NgMkSockNode(const char *, int *, int *);
 #define ID0uu_lock uu_lock
 #define ID0uu_lock_txfr uu_lock_txfr
 #define ID0uu_unlock uu_unlock
-#define ID0login(u)			\
-  do {					\
-    if (logout((u)->ut_line))		\
-      logwtmp((u)->ut_line, "", "");	\
-    login(u);				\
-  } while (0)
-#define ID0logout(dev, no)				\
-  do {							\
-    struct utmp ut;					\
-    strncpy(ut.ut_line, dev, sizeof ut.ut_line - 1);	\
-    ut.ut_line[sizeof ut.ut_line - 1] = '\0';		\
-    if (no || logout(ut.ut_line))			\
-      logwtmp(ut.ut_line, "", ""); 			\
-  } while (0)
+#define ID0login pututxline
+#define ID0logout pututxline
 #define ID0bind_un(s, n) bind(s, (const struct sockaddr *)(n), sizeof *(n))
 #define ID0connect_un(s, n) \
 	connect(s, (const struct sockaddr *)(n), sizeof *(n))
diff --git a/usr.sbin/ppp/physical.c b/usr.sbin/ppp/physical.c
index 5028ecca69..e7973b81e8 100644
--- a/usr.sbin/ppp/physical.c
+++ b/usr.sbin/ppp/physical.c
@@ -40,6 +40,7 @@
 #include <termios.h>
 #include <time.h>
 #include <unistd.h>
+#include <utmpx.h>
 #if defined(__OpenBSD__) || defined(__NetBSD__)
 #include <sys/ioctl.h>
 #include <util.h>
@@ -99,8 +100,6 @@
 #endif
 #include "tcpmss.h"
 
-#define PPPOTCPLINE "ppp"
-
 static int physical_DescriptorWrite(struct fdescriptor *, struct bundle *,
                                     const fd_set *);
 
@@ -322,6 +321,7 @@ physical_Close(struct physical *p)
 {
   int newsid;
   char fn[PATH_MAX];
+  struct utmpx ut;
 
   if (p->fd < 0)
     return;
@@ -333,12 +333,11 @@ physical_Close(struct physical *p)
 
   physical_StopDeviceTimer(p);
   if (p->Utmp) {
-    if (p->handler && (p->handler->type == TCP_DEVICE ||
-                       p->handler->type == UDP_DEVICE))
-      /* Careful - we logged in on line ``ppp'' with IP as our host */
-      ID0logout(PPPOTCPLINE, 1);
-    else
-      ID0logout(p->name.base, 0);
+    memset(&ut, 0, sizeof ut);
+    ut.ut_type = DEAD_PROCESS;
+    gettimeofday(&ut.ut_tv, NULL);
+    snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
+    ID0logout(&ut);
     p->Utmp = 0;
   }
   newsid = tcgetpgrp(p->fd) == getpgrp();
@@ -893,16 +892,17 @@ void
 physical_Login(struct physical *p, const char *name)
 {
   if (p->type == PHYS_DIRECT && *p->name.base && !p->Utmp) {
-    struct utmp ut;
+    struct utmpx ut;
     const char *connstr;
     char *colon;
 
     memset(&ut, 0, sizeof ut);
-    time(&ut.ut_time);
-    strncpy(ut.ut_name, name, sizeof ut.ut_name);
+    ut.ut_type = USER_PROCESS;
+    gettimeofday(&ut.ut_tv, NULL);
+    snprintf(ut.ut_id, sizeof ut.ut_id, "%xppp", (int)getpid());
+    strncpy(ut.ut_user, name, sizeof ut.ut_user);
     if (p->handler && (p->handler->type == TCP_DEVICE ||
                        p->handler->type == UDP_DEVICE)) {
-      strncpy(ut.ut_line, PPPOTCPLINE, sizeof ut.ut_line);
       strncpy(ut.ut_host, p->name.base, sizeof ut.ut_host);
       colon = memchr(ut.ut_host, ':', sizeof ut.ut_host);
       if (colon)
@@ -913,7 +913,7 @@ physical_Login(struct physical *p, const char *name)
       /* mgetty sets this to the connection speed */
       strncpy(ut.ut_host, connstr, sizeof ut.ut_host);
     ID0login(&ut);
-    p->Utmp = ut.ut_time;
+    p->Utmp = 1;
   }
 }
 
-- 
2.22.0

