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