1 /*        $NetBSD: utmp_update.c,v 1.14 2023/09/29 12:08:03 christos Exp $       */
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 
33 __RCSID("$NetBSD: utmp_update.c,v 1.14 2023/09/29 12:08:03 christos Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/stat.h>
38 
39 #include <stdio.h>
40 #include <vis.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <pwd.h>
44 #include <ctype.h>
45 #include <utmpx.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <paths.h>
50 #include <stdarg.h>
51 #include <errno.h>
52 #include <syslog.h>
53 
54 static __dead __printflike(2, 3) void
logerr(int e,const char * fmt,...)55 logerr(int e, const char *fmt, ...)
56 {
57           va_list sap, eap;
58           char *s = NULL;
59 
60           if (e)
61                     (void)asprintf(&s, "%s (%s)", fmt, strerror(e));
62           if (s)
63                     fmt = s;
64 
65           va_start(sap, fmt);
66           va_copy(eap, sap);
67           vsyslog(LOG_ERR, fmt, sap);
68           va_end(sap);
69           verrx(1, fmt, eap);
70           va_end(eap);
71 }
72 
73 int
main(int argc,char * argv[])74 main(int argc, char *argv[])
75 {
76           struct utmpx *utx;
77           size_t len;
78           struct passwd *pwd;
79           struct stat st;
80           int fd;
81           int res;
82           uid_t euid, ruid;
83           char tty[MAXPATHLEN];
84           const char *p, *ep;
85 
86           euid = geteuid();
87           ruid = getuid();
88 
89           if (argc != 2) {
90                     (void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
91                         getprogname());
92                     return 1;
93           }
94 
95           openlog(getprogname(), LOG_PID | LOG_NDELAY, LOG_AUTH);
96           if (seteuid(ruid) == -1)
97                     logerr(errno, "Can't setuid %ld", (long)ruid);
98 
99           len = strlen(argv[1]);
100 
101           if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
102                     logerr(0, "Bad argument size %zu", len);
103 
104           if ((utx = malloc(len+1)) == NULL)
105                     logerr(errno, "Can't allocate %zu", len+1);
106 
107           res = strunvis((char *)utx, argv[1]);
108           if (res != (int)sizeof(*utx))
109                     logerr(0, "Decoding error %s %d != %zu", argv[1], res,
110                         sizeof(*utx));
111 
112           switch (utx->ut_type) {
113           case USER_PROCESS:
114           case DEAD_PROCESS:
115                     break;
116           default:
117                     logerr(0, "Invalid utmpx type %d", (int)utx->ut_type);
118           }
119 
120           p = utx->ut_host;
121           ep = p + sizeof(utx->ut_host);
122           for (; p < ep && *p; p++)
123                     if (!isprint((unsigned char)*p))
124                               logerr(0, "Non-printable characters in hostname");
125 
126           if (ruid != 0) {
127                     if ((pwd = getpwuid(ruid)) == NULL)
128                               logerr(0, "User %ld does not exist in password"
129                                   " database", (long)ruid);
130 
131                     if (strcmp(pwd->pw_name, utx->ut_name) != 0)
132                               logerr(0, "Current user `%s' does not match "
133                                   "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
134           }
135 
136           (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
137           fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
138           if (fd != -1) {
139                     if (fstat(fd, &st) == -1)
140                               logerr(errno, "Cannot stat `%s'", tty);
141                     if (ruid != 0 && st.st_uid != ruid)
142                               logerr(0, "%s: Is not owned by you", tty);
143                     if (!isatty(fd))
144                               logerr(0, "%s: Not a tty device", tty);
145                     (void)close(fd);
146                     if (access(tty, W_OK|R_OK) == -1)
147                               logerr(errno, "Can't access `%s'", tty);
148           } else {
149                     struct utmpx utold, *utoldp;
150                     pid_t ppid;
151 
152                     /*
153                      * A daemon like ftpd that does not use a tty line?
154                      * We only allow it to kill its own existing entries
155                      */
156                     if (utx->ut_type != DEAD_PROCESS)
157                               logerr(errno, "Cannot open `%s'", tty);
158 
159                     (void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
160                     if ((utoldp = getutxline(&utold)) == NULL)
161                               logerr(0, "Cannot find existing entry for `%s'",
162                                   utx->ut_line);
163                     if (utoldp->ut_pid != (ppid = getppid()))
164                               logerr(0, "Cannot modify entry for `%s' "
165                                   "utmp pid %ld != parent %ld", tty,
166                                   (long)utoldp->ut_pid, (long)ppid);
167           }
168 
169           if (seteuid(euid) == 1)
170                     logerr(errno, "Can't setuid %ld", (long)euid);
171           if (pututxline(utx) == NULL)
172                     logerr(errno, "Cannot update utmp entry");
173 
174           return 0;
175 }
176