utmp_update.c revision 1.11 1 /* $NetBSD: utmp_update.c,v 1.11 2011/09/17 14:25:43 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.11 2011/09/17 14:25:43 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 <utmpx.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <paths.h>
49 #include <stdarg.h>
50 #include <errno.h>
51 #include <syslog.h>
52
53 static __dead __printflike(2, 3) void
54 logerr(int e, const char *fmt, ...)
55 {
56 va_list sap, eap;
57 char *s = NULL;
58
59 if (e)
60 (void)asprintf(&s, "%s (%s)", fmt, strerror(e));
61 if (s)
62 fmt = s;
63
64 va_start(sap, fmt);
65 va_copy(eap, sap);
66 vsyslog(LOG_ERR, fmt, sap);
67 va_end(sap);
68 errx(1, fmt, eap);
69 va_end(eap);
70 }
71
72 int
73 main(int argc, char *argv[])
74 {
75 struct utmpx *utx;
76 size_t len;
77 struct passwd *pwd;
78 struct stat st;
79 int fd;
80 int res;
81 uid_t euid, ruid;
82 char tty[MAXPATHLEN];
83
84 euid = geteuid();
85 ruid = getuid();
86
87 if (argc != 2) {
88 (void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
89 getprogname());
90 return 1;
91 }
92
93 openlog(getprogname(), LOG_PID | LOG_NDELAY, LOG_AUTH);
94 if (seteuid(ruid) == -1)
95 logerr(errno, "Can't setuid %ld", (long)ruid);
96
97 len = strlen(argv[1]);
98
99 if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
100 logerr(0, "Bad argument size %zu", len);
101
102 if ((utx = malloc(len)) == NULL)
103 logerr(errno, "Can't allocate %zu", len);
104
105 res = strunvis((char *)utx, argv[1]);
106 if (res != (int)sizeof(*utx))
107 logerr(0, "Decoding error %s %d != %zu", argv[1], res,
108 sizeof(*utx));
109
110 switch (utx->ut_type) {
111 case USER_PROCESS:
112 case DEAD_PROCESS:
113 break;
114 default:
115 logerr(0, "Invalid utmpx type %d", (int)utx->ut_type);
116 }
117
118 if (ruid != 0) {
119 if ((pwd = getpwuid(ruid)) == NULL)
120 logerr(0, "User %ld does not exist in password"
121 " database", (long)ruid);
122
123 if (strcmp(pwd->pw_name, utx->ut_name) != 0)
124 logerr(0, "Current user `%s' does not match "
125 "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
126 }
127
128 (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
129 fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
130 if (fd != -1) {
131 if (fstat(fd, &st) == -1)
132 logerr(errno, "Cannot stat `%s'", tty);
133 if (ruid != 0 && st.st_uid != ruid)
134 logerr(0, "%s: Is not owned by you", tty);
135 if (!isatty(fd))
136 logerr(0, "%s: Not a tty device", tty);
137 (void)close(fd);
138 if (access(tty, W_OK|R_OK) == -1)
139 logerr(errno, "Can't access `%s'", tty);
140 } else {
141 struct utmpx utold, *utoldp;
142 pid_t ppid;
143
144 /*
145 * A daemon like ftpd that does not use a tty line?
146 * We only allow it to kill its own existing entries
147 */
148 if (utx->ut_type != DEAD_PROCESS)
149 logerr(errno, "Cannot open `%s'", tty);
150
151 (void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
152 if ((utoldp = getutxline(&utold)) == NULL)
153 logerr(0, "Cannot find existing entry for `%s'",
154 utx->ut_line);
155 if (utoldp->ut_pid != (ppid = getppid()))
156 logerr(0, "Cannot modify entry for `%s' "
157 "utmp pid %ld != parent %ld", tty,
158 (long)utoldp->ut_pid, (long)ppid);
159 }
160
161 if (seteuid(euid) == 1)
162 logerr(errno, "Can't setuid %ld", (long)euid);
163 if (pututxline(utx) == NULL)
164 logerr(errno, "Cannot update utmp entry");
165
166 return 0;
167 }
168