acpi: Narrow workaround for broken interrupt settings
[dragonfly.git] / libexec / utmp_update / utmp_update.c
blob6d4770896295e9005779216eaf185987822488af
1 /* $NetBSD: utmp_update.c,v 1.13 2015/04/26 08:56:19 mlelstv Exp $ */
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
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.
31 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
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 #include <stdarg.h>
48 #include <errno.h>
49 #include <syslog.h>
51 static __dead2 __printflike(2, 3) void
52 logerr(int e, const char *fmt, ...)
54 va_list sap, eap;
55 char *s = NULL;
57 if (e)
58 (void)asprintf(&s, "%s (%s)", fmt, strerror(e));
59 if (s)
60 fmt = s;
62 va_start(sap, fmt);
63 va_copy(eap, sap);
64 vsyslog(LOG_ERR, fmt, sap);
65 va_end(sap);
66 verrx(1, fmt, eap);
67 va_end(eap);
70 int
71 main(int argc, char *argv[])
73 struct utmpx *utx;
74 size_t len;
75 struct passwd *pwd;
76 struct stat st;
77 int fd;
78 int res;
79 uid_t euid, ruid;
80 char tty[MAXPATHLEN];
82 euid = geteuid();
83 ruid = getuid();
85 if (argc != 2) {
86 (void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
87 getprogname());
88 exit(1);
91 openlog(getprogname(), LOG_PID | LOG_NDELAY, LOG_AUTH);
92 if (seteuid(ruid) == -1)
93 logerr(errno, "Can't setuid %ld", (long)ruid);
95 len = strlen(argv[1]);
97 if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
98 logerr(0, "Bad argument size %zu", len);
100 if ((utx = malloc(len+1)) == NULL)
101 logerr(errno, "Can't allocate %zu", len+1);
103 res = strunvis((char *)utx, argv[1]);
104 if (res != (int)sizeof(*utx))
105 logerr(0, "Decoding error %s %d != %zu", argv[1], res,
106 sizeof(*utx));
108 switch (utx->ut_type) {
109 case USER_PROCESS:
110 case DEAD_PROCESS:
111 break;
112 default:
113 logerr(0, "Invalid utmpx type %hd", utx->ut_type);
116 if (ruid != 0) {
117 if ((pwd = getpwuid(ruid)) == NULL)
118 logerr(0, "User %ld does not exist in password"
119 " database", (long)ruid);
121 if (strcmp(pwd->pw_name, utx->ut_name) != 0)
122 logerr(0, "Current user `%s' does not match "
123 "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
126 (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
127 fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
128 if (fd != -1) {
129 if (fstat(fd, &st) == -1)
130 logerr(errno, "Cannot stat `%s'", tty);
131 if (ruid != 0 && st.st_uid != ruid)
132 logerr(0, "%s: Is not owned by you", tty);
133 if (!isatty(fd))
134 logerr(0, "%s: Not a tty device", tty);
135 (void)close(fd);
136 if (access(tty, W_OK|R_OK) == -1)
137 logerr(errno, "Can't access `%s'", tty);
138 } else {
139 struct utmpx utold, *utoldp;
140 pid_t ppid;
143 * A daemon like ftpd that does not use a tty line?
144 * We only allow it to kill its own existing entries
146 if (utx->ut_type != DEAD_PROCESS)
147 logerr(errno, "Cannot open `%s'", tty);
149 (void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
150 if ((utoldp = getutxline(&utold)) == NULL)
151 logerr(0, "Cannot find existing entry for `%s'",
152 utx->ut_line);
153 if (utoldp->ut_pid != (ppid = getppid()))
154 logerr(0, "Cannot modify entry for `%s' "
155 "utmpx pid %ld != parent %ld", tty,
156 (long)utoldp->ut_pid, (long)ppid);
159 if (seteuid(euid) == 1)
160 logerr(errno, "Can't setuid %ld", (long)euid);
161 if (pututxline(utx) == NULL)
162 logerr(errno, "Cannot update utmpx entry");
164 return 0;