Home | History | Annotate | Line # | Download | only in utmp_update
utmp_update.c revision 1.7
      1 /*	$NetBSD: utmp_update.c,v 1.7 2004/11/07 07:04:31 christos 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include <sys/cdefs.h>
     39 
     40 __RCSID("$NetBSD: utmp_update.c,v 1.7 2004/11/07 07:04:31 christos Exp $");
     41 
     42 #include <sys/types.h>
     43 #include <sys/param.h>
     44 #include <sys/stat.h>
     45 
     46 #include <stdio.h>
     47 #include <vis.h>
     48 #include <err.h>
     49 #include <fcntl.h>
     50 #include <pwd.h>
     51 #include <utmpx.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 #include <paths.h>
     56 
     57 int main(int, char *[]);
     58 
     59 int
     60 main(int argc, char *argv[])
     61 {
     62 	struct utmpx *utx;
     63 	size_t len;
     64 	struct passwd *pwd;
     65 	struct stat st;
     66 	int fd;
     67 	uid_t euid, ruid;
     68 	char tty[MAXPATHLEN];
     69 
     70 	euid = geteuid();
     71 	ruid = getuid();
     72 	if (seteuid(ruid) == -1)
     73 		err(1, "seteuid");
     74 
     75 	if (argc != 2) {
     76 		(void)fprintf(stderr, "Usage: %s <vis-utmpx-entry>\n",
     77 			getprogname());
     78 		exit(1);
     79 	}
     80 
     81 	len = strlen(argv[1]);
     82 
     83 	if (len > sizeof(*utx) * 4 + 1 || len < sizeof(*utx))
     84 		errx(1, "Bad argument");
     85 
     86 	if ((utx = malloc(len)) == NULL)
     87 		err(1, NULL);
     88 
     89 	if (strunvis((char *)utx, argv[1]) != sizeof(*utx))
     90 		errx(1, "Decoding error");
     91 
     92 	switch (utx->ut_type) {
     93 	case USER_PROCESS:
     94 	case DEAD_PROCESS:
     95 		break;
     96 	default:
     97 		errx(1, "Invalid utmpx type %d", (int)utx->ut_type);
     98 	}
     99 
    100 	if (ruid != 0) {
    101 		if ((pwd = getpwuid(ruid)) == NULL)
    102 			errx(1, "User %lu does not exist in password database",
    103 			    (long)ruid);
    104 
    105 		if (strcmp(pwd->pw_name, utx->ut_name) != 0)
    106 			errx(1, "Current user `%s' does not match "
    107 			    "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
    108 	}
    109 
    110 	(void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
    111 	fd = open(tty, O_RDONLY|O_NONBLOCK, 0);
    112 	if (fd != -1) {
    113 		if (fstat(fd, &st) == -1)
    114 			err(1, "Cannot stat `%s'", tty);
    115 		if (ruid != 0 && st.st_uid != ruid)
    116 			errx(1, "%s: Is not owned by you", tty);
    117 		if (!isatty(fd))
    118 			errx(1, "%s: Not a tty device", tty);
    119 		(void)close(fd);
    120 		if (access(tty, W_OK|R_OK) == -1)
    121 			err(1, "%s", tty);
    122 	} else {
    123 		struct utmpx utold, *utoldp;
    124 		/*
    125 		 * A daemon like ftpd that does not use a tty line?
    126 		 * We only allow it to kill its own existing entries
    127 		 */
    128 		if (utx->ut_type != DEAD_PROCESS)
    129 			err(1, "Cannot open `%s'", tty);
    130 
    131 		(void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
    132 		if ((utoldp = getutxline(&utold)) == NULL)
    133 			err(1, "Cannot find existing entry for `%s'",
    134 			    utx->ut_line);
    135 		if (utoldp->ut_pid != getppid())
    136 			err(1, "Cannot modify entry for `%s'", tty);
    137 	}
    138 
    139 	(void)seteuid(euid);
    140 	if (pututxline(utx) == NULL)
    141 		err(1, "Cannot update utmp entry");
    142 
    143 	return 0;
    144 }
    145