Home | History | Annotate | Line # | Download | only in last
want.c revision 1.5
      1 /*	$NetBSD: want.c,v 1.5 2005/03/04 17:11:19 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 static struct utmp *buf;
     32 
     33 static void onintr(int);
     34 static int want(struct utmp *, int);
     35 static const char *gethost(struct utmp *, const char *, int);
     36 
     37 static const char *
     38 /*ARGSUSED*/
     39 gethost(struct utmp *ut, const char *host, int numeric)
     40 {
     41 #if FIRSTVALID == 0
     42 	return numeric ? "" : host;
     43 #else
     44 	if (numeric) {
     45 		static char buf[512];
     46 		buf[0] = '\0';
     47 		(void)sockaddr_snprintf(buf, sizeof(buf), "%a",
     48 		    (struct sockaddr *)&ut->ut_ss);
     49 		return buf;
     50 	} else
     51 		return host;
     52 #endif
     53 }
     54 
     55 #define NULTERM(what) \
     56 	if (check ## what) \
     57 		(void)strlcpy(what ## p = what ## buf, bp->ut_ ## what, \
     58 		    sizeof(what ## buf)); \
     59 	else \
     60 		what ## p = bp->ut_ ## what
     61 
     62 /*
     63  * wtmp --
     64  *	read through the wtmp file
     65  */
     66 void
     67 wtmp(const char *file, int namesz, int linesz, int hostsz, int numeric)
     68 {
     69 	struct utmp	*bp;		/* current structure */
     70 	TTY	*T;			/* tty list entry */
     71 	struct stat	stb;		/* stat of file for sz */
     72 	time_t	delta;			/* time difference */
     73 	off_t	bl;
     74 	int	bytes, wfd;
     75 	char	*ct, *crmsg;
     76 	size_t  len = sizeof(*buf) * MAXUTMP;
     77 	char namebuf[sizeof(bp->ut_name) + 1], *namep;
     78 	char linebuf[sizeof(bp->ut_line) + 1], *linep;
     79 	char hostbuf[sizeof(bp->ut_host) + 1], *hostp;
     80 	int checkname = namesz > sizeof(bp->ut_name);
     81 	int checkline = linesz > sizeof(bp->ut_line);
     82 	int checkhost = hostsz > sizeof(bp->ut_host);
     83 
     84 	if ((buf = malloc(len)) == NULL)
     85 		err(1, "Cannot allocate utmp buffer");
     86 
     87 	crmsg = NULL;
     88 
     89 	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
     90 		err(1, "%s", file);
     91 	bl = (stb.st_size + len - 1) / len;
     92 
     93 	buf[FIRSTVALID].ut_timefld = time(NULL);
     94 	(void)signal(SIGINT, onintr);
     95 	(void)signal(SIGQUIT, onintr);
     96 
     97 	while (--bl >= 0) {
     98 		if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
     99 		    (bytes = read(wfd, buf, len)) == -1)
    100 			err(1, "%s", file);
    101 		for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
    102 			NULTERM(name);
    103 			NULTERM(line);
    104 			NULTERM(host);
    105 			/*
    106 			 * if the terminal line is '~', the machine stopped.
    107 			 * see utmp(5) for more info.
    108 			 */
    109 			if (linep[0] == '~' && !linep[1]) {
    110 				/* everybody just logged out */
    111 				for (T = ttylist; T; T = T->next)
    112 					T->logout = -bp->ut_timefld;
    113 				currentout = -bp->ut_timefld;
    114 				crmsg = strncmp(namep, "shutdown",
    115 				    namesz) ? "crash" : "shutdown";
    116 				if (want(bp, NO)) {
    117 					ct = fmttime(bp->ut_timefld, fulltime);
    118 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
    119 					    namesz, namesz, namep,
    120 					    linesz, linesz, linep,
    121 					    hostsz, hostsz,
    122 					    gethost(bp, hostp, numeric), ct);
    123 					if (maxrec != -1 && !--maxrec)
    124 						return;
    125 				}
    126 				continue;
    127 			}
    128 			/*
    129 			 * if the line is '{' or '|', date got set; see
    130 			 * utmp(5) for more info.
    131 			 */
    132 			if ((linep[0] == '{' || linep[0] == '|') && !linep[1]) {
    133 				if (want(bp, NO)) {
    134 					ct = fmttime(bp->ut_timefld, fulltime);
    135 				printf("%-*.*s  %-*.*s %-*.*s %s\n",
    136 				    namesz, namesz, namep,
    137 				    linesz, linesz, linep,
    138 				    hostsz, hostsz,
    139 				    gethost(bp, hostp, numeric),
    140 				    ct);
    141 					if (maxrec && !--maxrec)
    142 						return;
    143 				}
    144 				continue;
    145 			}
    146 			/* find associated tty */
    147 			for (T = ttylist;; T = T->next) {
    148 				if (!T) {
    149 					/* add new one */
    150 					T = addtty(linep);
    151 					break;
    152 				}
    153 				if (!strncmp(T->tty, linep, LINESIZE))
    154 					break;
    155 			}
    156 			if (TYPE(bp) == SIGNATURE)
    157 				continue;
    158 			if (namep[0] && want(bp, YES)) {
    159 				ct = fmttime(bp->ut_timefld, fulltime);
    160 				printf("%-*.*s  %-*.*s %-*.*s %s ",
    161 				    namesz, namesz, namep,
    162 				    linesz, linesz, linep,
    163 				    hostsz, hostsz,
    164 				    gethost(bp, hostp, numeric),
    165 				    ct);
    166 				if (!T->logout)
    167 					puts("  still logged in");
    168 				else {
    169 					if (T->logout < 0) {
    170 						T->logout = -T->logout;
    171 						printf("- %s", crmsg);
    172 					}
    173 					else
    174 						printf("- %s",
    175 						    fmttime(T->logout,
    176 						    fulltime | TIMEONLY));
    177 					delta = T->logout - bp->ut_timefld;
    178 					if (delta < SECSPERDAY)
    179 						printf("  (%s)\n",
    180 						    fmttime(delta,
    181 						    fulltime | TIMEONLY | GMT));
    182 					else
    183 						printf(" (%ld+%s)\n",
    184 						    delta / SECSPERDAY,
    185 						    fmttime(delta,
    186 						    fulltime | TIMEONLY | GMT));
    187 				}
    188 				if (maxrec != -1 && !--maxrec)
    189 					return;
    190 			}
    191 			T->logout = bp->ut_timefld;
    192 		}
    193 	}
    194 	fulltime = 1;	/* show full time */
    195 	crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
    196 	if ((ct = strrchr(file, '/')) != NULL)
    197 		ct++;
    198 	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
    199 }
    200 
    201 /*
    202  * want --
    203  *	see if want this entry
    204  */
    205 static int
    206 want(struct utmp *bp, int check)
    207 {
    208 	ARG *step;
    209 
    210 	if (check) {
    211 		/*
    212 		 * when uucp and ftp log in over a network, the entry in
    213 		 * the utmp file is the name plus their process id.  See
    214 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
    215 		 */
    216 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
    217 			bp->ut_line[3] = '\0';
    218 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
    219 			bp->ut_line[4] = '\0';
    220 	}
    221 	if (!arglist)
    222 		return (YES);
    223 
    224 	for (step = arglist; step; step = step->next)
    225 		switch(step->type) {
    226 		case HOST_TYPE:
    227 			if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
    228 				return (YES);
    229 			break;
    230 		case TTY_TYPE:
    231 			if (!strncmp(step->name, bp->ut_line, LINESIZE))
    232 				return (YES);
    233 			break;
    234 		case USER_TYPE:
    235 			if (!strncmp(step->name, bp->ut_name, NAMESIZE))
    236 				return (YES);
    237 			break;
    238 	}
    239 	return (NO);
    240 }
    241 
    242 /*
    243  * onintr --
    244  *	on interrupt, we inform the user how far we've gotten
    245  */
    246 static void
    247 onintr(int signo)
    248 {
    249 
    250 	printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
    251 	    FULLTIME));
    252 	if (signo == SIGINT)
    253 		exit(1);
    254 	(void)fflush(stdout);		/* fix required for rsh */
    255 }
    256