utmp_update.c revision 1.9 1 1.9 christos /* $NetBSD: utmp_update.c,v 1.9 2009/04/13 03:38:15 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 *
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.9 christos __RCSID("$NetBSD: utmp_update.c,v 1.9 2009/04/13 03:38:15 christos 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.1 christos
50 1.1 christos int main(int, char *[]);
51 1.1 christos
52 1.1 christos int
53 1.1 christos main(int argc, char *argv[])
54 1.1 christos {
55 1.1 christos struct utmpx *utx;
56 1.1 christos size_t len;
57 1.1 christos struct passwd *pwd;
58 1.1 christos struct stat st;
59 1.4 itojun int fd;
60 1.9 christos int res;
61 1.6 christos uid_t euid, ruid;
62 1.5 christos char tty[MAXPATHLEN];
63 1.1 christos
64 1.4 itojun euid = geteuid();
65 1.6 christos ruid = getuid();
66 1.6 christos if (seteuid(ruid) == -1)
67 1.4 itojun err(1, "seteuid");
68 1.4 itojun
69 1.4 itojun if (argc != 2) {
70 1.1 christos (void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
71 1.1 christos getprogname());
72 1.1 christos exit(1);
73 1.1 christos }
74 1.1 christos
75 1.1 christos len = strlen(argv[1]);
76 1.1 christos
77 1.1 christos if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
78 1.1 christos errx(1, "Bad argument");
79 1.1 christos
80 1.1 christos if ((utx = malloc(len)) == NULL)
81 1.1 christos err(1, NULL);
82 1.1 christos
83 1.9 christos res = strunvis((char *)utx, argv[1]);
84 1.9 christos if (res != (int)sizeof(*utx))
85 1.9 christos errx(1, "Decoding error %s %d != %zu", argv[1], res, sizeof(*utx));
86 1.1 christos
87 1.1 christos switch (utx->ut_type) {
88 1.1 christos case USER_PROCESS:
89 1.1 christos case DEAD_PROCESS:
90 1.1 christos break;
91 1.1 christos default:
92 1.3 soren errx(1, "Invalid utmpx type %d", (int)utx->ut_type);
93 1.1 christos }
94 1.1 christos
95 1.6 christos if (ruid != 0) {
96 1.6 christos if ((pwd = getpwuid(ruid)) == NULL)
97 1.6 christos errx(1, "User %lu does not exist in password database",
98 1.6 christos (long)ruid);
99 1.6 christos
100 1.6 christos if (strcmp(pwd->pw_name, utx->ut_name) != 0)
101 1.6 christos errx(1, "Current user `%s' does not match "
102 1.6 christos "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
103 1.6 christos }
104 1.1 christos
105 1.5 christos (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
106 1.7 christos fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
107 1.6 christos if (fd != -1) {
108 1.6 christos if (fstat(fd, &st) == -1)
109 1.6 christos err(1, "Cannot stat `%s'", tty);
110 1.6 christos if (ruid != 0 && st.st_uid != ruid)
111 1.6 christos errx(1, "%s: Is not owned by you", tty);
112 1.6 christos if (!isatty(fd))
113 1.6 christos errx(1, "%s: Not a tty device", tty);
114 1.6 christos (void)close(fd);
115 1.6 christos if (access(tty, W_OK|R_OK) == -1)
116 1.6 christos err(1, "%s", tty);
117 1.6 christos } else {
118 1.6 christos struct utmpx utold, *utoldp;
119 1.6 christos /*
120 1.6 christos * A daemon like ftpd that does not use a tty line?
121 1.6 christos * We only allow it to kill its own existing entries
122 1.6 christos */
123 1.6 christos if (utx->ut_type != DEAD_PROCESS)
124 1.6 christos err(1, "Cannot open `%s'", tty);
125 1.6 christos
126 1.6 christos (void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
127 1.6 christos if ((utoldp = getutxline(&utold)) == NULL)
128 1.6 christos err(1, "Cannot find existing entry for `%s'",
129 1.6 christos utx->ut_line);
130 1.6 christos if (utoldp->ut_pid != getppid())
131 1.6 christos err(1, "Cannot modify entry for `%s'", tty);
132 1.6 christos }
133 1.1 christos
134 1.4 itojun (void)seteuid(euid);
135 1.5 christos if (pututxline(utx) == NULL)
136 1.5 christos err(1, "Cannot update utmp entry");
137 1.1 christos
138 1.1 christos return 0;
139 1.1 christos }
140