utmp_update.c revision 1.6 1 1.6 christos /* $NetBSD: utmp_update.c,v 1.6 2003/02/26 18:16:50 christos 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.5 christos #include <sys/cdefs.h>
39 1.1 christos
40 1.6 christos __RCSID("$NetBSD: utmp_update.c,v 1.6 2003/02/26 18:16:50 christos Exp $");
41 1.5 christos
42 1.5 christos #include <sys/types.h>
43 1.5 christos #include <sys/param.h>
44 1.1 christos #include <sys/stat.h>
45 1.1 christos
46 1.1 christos #include <stdio.h>
47 1.1 christos #include <vis.h>
48 1.1 christos #include <err.h>
49 1.4 itojun #include <fcntl.h>
50 1.1 christos #include <pwd.h>
51 1.1 christos #include <utmpx.h>
52 1.1 christos #include <stdlib.h>
53 1.2 jmcneill #include <string.h>
54 1.1 christos #include <unistd.h>
55 1.5 christos #include <paths.h>
56 1.1 christos
57 1.1 christos int main(int, char *[]);
58 1.1 christos
59 1.1 christos int
60 1.1 christos main(int argc, char *argv[])
61 1.1 christos {
62 1.1 christos struct utmpx *utx;
63 1.1 christos size_t len;
64 1.1 christos struct passwd *pwd;
65 1.1 christos struct stat st;
66 1.4 itojun int fd;
67 1.6 christos uid_t euid, ruid;
68 1.5 christos char tty[MAXPATHLEN];
69 1.1 christos
70 1.4 itojun euid = geteuid();
71 1.6 christos ruid = getuid();
72 1.6 christos if (seteuid(ruid) == -1)
73 1.4 itojun err(1, "seteuid");
74 1.4 itojun
75 1.4 itojun if (argc != 2) {
76 1.1 christos (void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
77 1.1 christos getprogname());
78 1.1 christos exit(1);
79 1.1 christos }
80 1.1 christos
81 1.1 christos len = strlen(argv[1]);
82 1.1 christos
83 1.1 christos if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
84 1.1 christos errx(1, "Bad argument");
85 1.1 christos
86 1.1 christos if ((utx = malloc(len)) == NULL)
87 1.1 christos err(1, NULL);
88 1.1 christos
89 1.1 christos if (strunvis((char *)utx, argv[1]) != sizeof(*utx))
90 1.1 christos errx(1, "Decoding error");
91 1.1 christos
92 1.1 christos switch (utx->ut_type) {
93 1.1 christos case USER_PROCESS:
94 1.1 christos case DEAD_PROCESS:
95 1.1 christos break;
96 1.1 christos default:
97 1.3 soren errx(1, "Invalid utmpx type %d", (int)utx->ut_type);
98 1.1 christos }
99 1.1 christos
100 1.6 christos if (ruid != 0) {
101 1.6 christos if ((pwd = getpwuid(ruid)) == NULL)
102 1.6 christos errx(1, "User %lu does not exist in password database",
103 1.6 christos (long)ruid);
104 1.6 christos
105 1.6 christos if (strcmp(pwd->pw_name, utx->ut_name) != 0)
106 1.6 christos errx(1, "Current user `%s' does not match "
107 1.6 christos "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
108 1.6 christos }
109 1.1 christos
110 1.5 christos (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
111 1.5 christos fd = open(tty, O_RDONLY, 0);
112 1.6 christos if (fd != -1) {
113 1.6 christos if (fstat(fd, &st) == -1)
114 1.6 christos err(1, "Cannot stat `%s'", tty);
115 1.6 christos if (ruid != 0 && st.st_uid != ruid)
116 1.6 christos errx(1, "%s: Is not owned by you", tty);
117 1.6 christos if (!isatty(fd))
118 1.6 christos errx(1, "%s: Not a tty device", tty);
119 1.6 christos (void)close(fd);
120 1.6 christos if (access(tty, W_OK|R_OK) == -1)
121 1.6 christos err(1, "%s", tty);
122 1.6 christos } else {
123 1.6 christos struct utmpx utold, *utoldp;
124 1.6 christos /*
125 1.6 christos * A daemon like ftpd that does not use a tty line?
126 1.6 christos * We only allow it to kill its own existing entries
127 1.6 christos */
128 1.6 christos if (utx->ut_type != DEAD_PROCESS)
129 1.6 christos err(1, "Cannot open `%s'", tty);
130 1.6 christos
131 1.6 christos (void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
132 1.6 christos if ((utoldp = getutxline(&utold)) == NULL)
133 1.6 christos err(1, "Cannot find existing entry for `%s'",
134 1.6 christos utx->ut_line);
135 1.6 christos if (utoldp->ut_pid != getppid())
136 1.6 christos err(1, "Cannot modify entry for `%s'", tty);
137 1.6 christos }
138 1.1 christos
139 1.4 itojun (void)seteuid(euid);
140 1.5 christos if (pututxline(utx) == NULL)
141 1.5 christos err(1, "Cannot update utmp entry");
142 1.1 christos
143 1.1 christos return 0;
144 1.1 christos }
145