1 /* $NetBSD: utmp_update.c,v 1.13 2015/04/26 08:56:19 mlelstv Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
51 static __dead2
__printflike(2, 3) void
52 logerr(int e
, const char *fmt
, ...)
58 (void)asprintf(&s
, "%s (%s)", fmt
, strerror(e
));
64 vsyslog(LOG_ERR
, fmt
, sap
);
71 main(int argc
, char *argv
[])
86 (void)fprintf(stderr
, "Usage: %s <vis-utmpx-entry>\n",
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
,
108 switch (utx
->ut_type
) {
113 logerr(0, "Invalid utmpx type %hd", utx
->ut_type
);
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);
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
);
134 logerr(0, "%s: Not a tty device", tty
);
136 if (access(tty
, W_OK
|R_OK
) == -1)
137 logerr(errno
, "Can't access `%s'", tty
);
139 struct utmpx utold
, *utoldp
;
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'",
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");