Home | History | Annotate | Line # | Download | only in last
want.c revision 1.7
      1 /*	$NetBSD: want.c,v 1.7 2006/12/27 18:03:26 pooka 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 (!strcmp(file, "-")) {
     90 		if (lseek(STDIN_FILENO, 0, SEEK_CUR) < 0) {
     91 			char tfile[] = _PATH_TMP "last.XXXXXX";
     92 			ssize_t len;
     93 
     94 			wfd = mkstemp(tfile);
     95 			if (wfd < 0) {
     96 				err(1, "mkstemp");
     97 			}
     98 			unlink(tfile);
     99 			for (;;) {
    100 			   	len = read(STDIN_FILENO, buf, sizeof(buf));
    101 				if (len < 0) {
    102 					err(1, "stdin: read");
    103 				}
    104 				if (len == 0) {
    105 					break;
    106 				}
    107 				len = write(wfd, buf, len);
    108 				if (len < 0) {
    109 					err(1, "%s: write", tfile);
    110 				}
    111 			}
    112 		} else {
    113 			wfd = STDIN_FILENO;
    114 		}
    115 		file = "<stdin>";
    116 	} else if ((wfd = open(file, O_RDONLY, 0)) < 0) {
    117 		err(1, "%s", file);
    118 	}
    119 
    120 	if (fstat(wfd, &stb) == -1)
    121 		err(1, "%s: fstat", file);
    122 	bl = (stb.st_size + len - 1) / len;
    123 
    124 	buf[FIRSTVALID].ut_timefld = time(NULL);
    125 	(void)signal(SIGINT, onintr);
    126 	(void)signal(SIGQUIT, onintr);
    127 
    128 	while (--bl >= 0) {
    129 		if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
    130 		    (bytes = read(wfd, buf, len)) == -1)
    131 			err(1, "%s", file);
    132 		for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
    133 			NULTERM(name);
    134 			NULTERM(line);
    135 			NULTERM(host);
    136 			/*
    137 			 * if the terminal line is '~', the machine stopped.
    138 			 * see utmp(5) for more info.
    139 			 */
    140 			if (linep[0] == '~' && !linep[1]) {
    141 				/* everybody just logged out */
    142 				for (T = ttylist; T; T = T->next)
    143 					T->logout = -bp->ut_timefld;
    144 				currentout = -bp->ut_timefld;
    145 				crmsg = strncmp(namep, "shutdown",
    146 				    namesz) ? "crash" : "shutdown";
    147 				if (want(bp, NO)) {
    148 					ct = fmttime(bp->ut_timefld, fulltime);
    149 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
    150 					    namesz, namesz, namep,
    151 					    linesz, linesz, linep,
    152 					    hostsz, hostsz,
    153 					    gethost(bp, hostp, numeric), ct);
    154 					if (maxrec != -1 && !--maxrec)
    155 						return;
    156 				}
    157 				continue;
    158 			}
    159 			/*
    160 			 * if the line is '{' or '|', date got set; see
    161 			 * utmp(5) for more info.
    162 			 */
    163 			if ((linep[0] == '{' || linep[0] == '|') && !linep[1]) {
    164 				if (want(bp, NO)) {
    165 					ct = fmttime(bp->ut_timefld, fulltime);
    166 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
    167 					    namesz, namesz, namep,
    168 					    linesz, linesz, linep,
    169 					    hostsz, hostsz,
    170 					    gethost(bp, hostp, numeric),
    171 					    ct);
    172 					if (maxrec && !--maxrec)
    173 						return;
    174 				}
    175 				continue;
    176 			}
    177 			/* find associated tty */
    178 			for (T = ttylist;; T = T->next) {
    179 				if (!T) {
    180 					/* add new one */
    181 					T = addtty(linep);
    182 					break;
    183 				}
    184 				if (!strncmp(T->tty, linep, LINESIZE))
    185 					break;
    186 			}
    187 			if (TYPE(bp) == SIGNATURE)
    188 				continue;
    189 			if (namep[0] && want(bp, YES)) {
    190 				ct = fmttime(bp->ut_timefld, fulltime);
    191 				printf("%-*.*s  %-*.*s %-*.*s %s ",
    192 				    namesz, namesz, namep,
    193 				    linesz, linesz, linep,
    194 				    hostsz, hostsz,
    195 				    gethost(bp, hostp, numeric),
    196 				    ct);
    197 				if (!T->logout)
    198 					puts("  still logged in");
    199 				else {
    200 					if (T->logout < 0) {
    201 						T->logout = -T->logout;
    202 						printf("- %s", crmsg);
    203 					}
    204 					else
    205 						printf("- %s",
    206 						    fmttime(T->logout,
    207 						    fulltime | TIMEONLY));
    208 					delta = T->logout - bp->ut_timefld;
    209 					if (delta < SECSPERDAY)
    210 						printf("  (%s)\n",
    211 						    fmttime(delta,
    212 						    fulltime | TIMEONLY | GMT));
    213 					else
    214 						printf(" (%ld+%s)\n",
    215 						    delta / SECSPERDAY,
    216 						    fmttime(delta,
    217 						    fulltime | TIMEONLY | GMT));
    218 				}
    219 				if (maxrec != -1 && !--maxrec)
    220 					return;
    221 			}
    222 			T->logout = bp->ut_timefld;
    223 		}
    224 	}
    225 	fulltime = 1;	/* show full time */
    226 	crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
    227 	if ((ct = strrchr(file, '/')) != NULL)
    228 		ct++;
    229 	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
    230 }
    231 
    232 /*
    233  * want --
    234  *	see if want this entry
    235  */
    236 static int
    237 want(struct utmp *bp, int check)
    238 {
    239 	ARG *step;
    240 
    241 	if (check) {
    242 		/*
    243 		 * when uucp and ftp log in over a network, the entry in
    244 		 * the utmp file is the name plus their process id.  See
    245 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
    246 		 */
    247 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
    248 			bp->ut_line[3] = '\0';
    249 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
    250 			bp->ut_line[4] = '\0';
    251 	}
    252 	if (!arglist)
    253 		return (YES);
    254 
    255 	for (step = arglist; step; step = step->next)
    256 		switch(step->type) {
    257 		case HOST_TYPE:
    258 			if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
    259 				return (YES);
    260 			break;
    261 		case TTY_TYPE:
    262 			if (!strncmp(step->name, bp->ut_line, LINESIZE))
    263 				return (YES);
    264 			break;
    265 		case USER_TYPE:
    266 			if (!strncmp(step->name, bp->ut_name, NAMESIZE))
    267 				return (YES);
    268 			break;
    269 	}
    270 	return (NO);
    271 }
    272 
    273 /*
    274  * onintr --
    275  *	on interrupt, we inform the user how far we've gotten
    276  */
    277 static void
    278 onintr(int signo)
    279 {
    280 
    281 	printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
    282 	    FULLTIME));
    283 	if (signo == SIGINT)
    284 		exit(1);
    285 	(void)fflush(stdout);		/* fix required for rsh */
    286 }
    287