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