Home | History | Annotate | Line # | Download | only in utmp_update
      1  1.14  christos /*	$NetBSD: utmp_update.c,v 1.14 2023/09/29 12:08:03 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.14  christos __RCSID("$NetBSD: utmp_update.c,v 1.14 2023/09/29 12:08:03 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.14  christos #include <ctype.h>
     45   1.1  christos #include <utmpx.h>
     46   1.1  christos #include <stdlib.h>
     47   1.2  jmcneill #include <string.h>
     48   1.1  christos #include <unistd.h>
     49   1.5  christos #include <paths.h>
     50  1.10  christos #include <stdarg.h>
     51  1.10  christos #include <errno.h>
     52  1.10  christos #include <syslog.h>
     53   1.1  christos 
     54  1.11  christos static __dead __printflike(2, 3) void
     55  1.10  christos logerr(int e, const char *fmt, ...)
     56  1.10  christos {
     57  1.10  christos 	va_list sap, eap;
     58  1.10  christos 	char *s = NULL;
     59  1.10  christos 
     60  1.10  christos 	if (e)
     61  1.10  christos 		(void)asprintf(&s, "%s (%s)", fmt, strerror(e));
     62  1.10  christos 	if (s)
     63  1.10  christos 		fmt = s;
     64  1.10  christos 
     65  1.10  christos 	va_start(sap, fmt);
     66  1.10  christos 	va_copy(eap, sap);
     67  1.10  christos 	vsyslog(LOG_ERR, fmt, sap);
     68  1.10  christos 	va_end(sap);
     69  1.13   mlelstv 	verrx(1, fmt, eap);
     70  1.10  christos 	va_end(eap);
     71  1.10  christos }
     72   1.1  christos 
     73   1.1  christos int
     74   1.1  christos main(int argc, char *argv[])
     75   1.1  christos {
     76   1.1  christos 	struct utmpx *utx;
     77   1.1  christos 	size_t len;
     78   1.1  christos 	struct passwd *pwd;
     79   1.1  christos 	struct stat st;
     80   1.4    itojun 	int fd;
     81   1.9  christos 	int res;
     82   1.6  christos 	uid_t euid, ruid;
     83   1.5  christos 	char tty[MAXPATHLEN];
     84  1.14  christos 	const char *p, *ep;
     85   1.1  christos 
     86   1.4    itojun 	euid = geteuid();
     87   1.6  christos 	ruid = getuid();
     88   1.4    itojun 
     89   1.4    itojun 	if (argc != 2) {
     90   1.1  christos 		(void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
     91  1.10  christos 		    getprogname());
     92  1.10  christos 		return 1;
     93   1.1  christos 	}
     94   1.1  christos 
     95  1.10  christos 	openlog(getprogname(), LOG_PID | LOG_NDELAY, LOG_AUTH);
     96  1.10  christos 	if (seteuid(ruid) == -1)
     97  1.10  christos 		logerr(errno, "Can't setuid %ld", (long)ruid);
     98  1.10  christos 
     99   1.1  christos 	len = strlen(argv[1]);
    100   1.1  christos 
    101   1.1  christos 	if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
    102  1.10  christos 		logerr(0, "Bad argument size %zu", len);
    103   1.1  christos 
    104  1.12   mlelstv 	if ((utx = malloc(len+1)) == NULL)
    105  1.12   mlelstv 		logerr(errno, "Can't allocate %zu", len+1);
    106   1.1  christos 
    107   1.9  christos 	res = strunvis((char *)utx, argv[1]);
    108   1.9  christos 	if (res != (int)sizeof(*utx))
    109  1.10  christos 		logerr(0, "Decoding error %s %d != %zu", argv[1], res,
    110  1.10  christos 		    sizeof(*utx));
    111   1.1  christos 
    112   1.1  christos 	switch (utx->ut_type) {
    113   1.1  christos 	case USER_PROCESS:
    114   1.1  christos 	case DEAD_PROCESS:
    115   1.1  christos 		break;
    116   1.1  christos 	default:
    117  1.10  christos 		logerr(0, "Invalid utmpx type %d", (int)utx->ut_type);
    118   1.1  christos 	}
    119   1.1  christos 
    120  1.14  christos 	p = utx->ut_host;
    121  1.14  christos 	ep = p + sizeof(utx->ut_host);
    122  1.14  christos 	for (; p < ep && *p; p++)
    123  1.14  christos 		if (!isprint((unsigned char)*p))
    124  1.14  christos 			logerr(0, "Non-printable characters in hostname");
    125  1.14  christos 
    126   1.6  christos 	if (ruid != 0) {
    127   1.6  christos 		if ((pwd = getpwuid(ruid)) == NULL)
    128  1.10  christos 			logerr(0, "User %ld does not exist in password"
    129  1.10  christos 			    " database", (long)ruid);
    130   1.6  christos 
    131   1.6  christos 		if (strcmp(pwd->pw_name, utx->ut_name) != 0)
    132  1.10  christos 			logerr(0, "Current user `%s' does not match "
    133   1.6  christos 			    "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
    134   1.6  christos 	}
    135   1.1  christos 
    136   1.5  christos 	(void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
    137   1.7  christos 	fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
    138   1.6  christos 	if (fd != -1) {
    139   1.6  christos 		if (fstat(fd, &st) == -1)
    140  1.10  christos 			logerr(errno, "Cannot stat `%s'", tty);
    141   1.6  christos 		if (ruid != 0 && st.st_uid != ruid)
    142  1.10  christos 			logerr(0, "%s: Is not owned by you", tty);
    143   1.6  christos 		if (!isatty(fd))
    144  1.10  christos 			logerr(0, "%s: Not a tty device", tty);
    145   1.6  christos 		(void)close(fd);
    146   1.6  christos 		if (access(tty, W_OK|R_OK) == -1)
    147  1.10  christos 			logerr(errno, "Can't access `%s'", tty);
    148   1.6  christos 	} else {
    149   1.6  christos 		struct utmpx utold, *utoldp;
    150  1.10  christos 		pid_t ppid;
    151  1.10  christos 
    152   1.6  christos 		/*
    153   1.6  christos 		 * A daemon like ftpd that does not use a tty line?
    154   1.6  christos 		 * We only allow it to kill its own existing entries
    155   1.6  christos 		 */
    156   1.6  christos 		if (utx->ut_type != DEAD_PROCESS)
    157  1.10  christos 			logerr(errno, "Cannot open `%s'", tty);
    158   1.6  christos 
    159   1.6  christos 		(void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
    160   1.6  christos 		if ((utoldp = getutxline(&utold)) == NULL)
    161  1.10  christos 			logerr(0, "Cannot find existing entry for `%s'",
    162   1.6  christos 			    utx->ut_line);
    163  1.10  christos 		if (utoldp->ut_pid != (ppid = getppid()))
    164  1.10  christos 			logerr(0, "Cannot modify entry for `%s' "
    165  1.10  christos 			    "utmp pid %ld != parent %ld", tty,
    166  1.10  christos 			    (long)utoldp->ut_pid, (long)ppid);
    167   1.6  christos 	}
    168   1.1  christos 
    169  1.10  christos 	if (seteuid(euid) == 1)
    170  1.10  christos 		logerr(errno, "Can't setuid %ld", (long)euid);
    171   1.5  christos 	if (pututxline(utx) == NULL)
    172  1.10  christos 		logerr(errno, "Cannot update utmp entry");
    173   1.1  christos 
    174   1.1  christos 	return 0;
    175   1.1  christos }
    176