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