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