Home | History | Annotate | Line # | Download | only in last
want.c revision 1.8
      1 /*	$NetBSD: want.c,v 1.8 2007/01/06 14:00:36 cbiere 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 	off_t	offset;
     73 	int	wfd;
     74 	char	*ct, *crmsg;
     75 	size_t  len = sizeof(*buf) * MAXUTMP;
     76 	char namebuf[sizeof(bp->ut_name) + 1], *namep;
     77 	char linebuf[sizeof(bp->ut_line) + 1], *linep;
     78 	char hostbuf[sizeof(bp->ut_host) + 1], *hostp;
     79 	int checkname = namesz > sizeof(bp->ut_name);
     80 	int checkline = linesz > sizeof(bp->ut_line);
     81 	int checkhost = hostsz > sizeof(bp->ut_host);
     82 
     83 	if ((buf = malloc(len)) == NULL)
     84 		err(EXIT_FAILURE, "Cannot allocate utmp buffer");
     85 
     86 	crmsg = NULL;
     87 
     88 	if (!strcmp(file, "-")) {
     89 		wfd = STDIN_FILENO;
     90 		file = "<stdin>";
     91 	} else if ((wfd = open(file, O_RDONLY, 0)) < 0) {
     92 		err(EXIT_FAILURE, "%s", file);
     93 	}
     94 
     95 	if (lseek(wfd, 0, SEEK_CUR) < 0) {
     96 		char tfile[] = _PATH_TMP "last.XXXXXX";
     97 		int tempfd;
     98 		ssize_t tlen;
     99 
    100 		if (ESPIPE != errno) {
    101 			err(EXIT_FAILURE, "lseek");
    102 		}
    103 		tempfd = mkstemp(tfile);
    104 		if (tempfd < 0) {
    105 			err(EXIT_FAILURE, "mkstemp");
    106 		}
    107 		unlink(tfile);
    108 		for (;;) {
    109 		   	tlen = read(wfd, buf, len);
    110 			if (tlen < 0) {
    111 				err(1, "%s: read", file);
    112 			}
    113 			if (tlen == 0) {
    114 				break;
    115 			}
    116 			if (write(tempfd, buf, tlen) != tlen) {
    117 				err(1, "%s: write", tfile);
    118 			}
    119 		}
    120 		wfd = tempfd;
    121 	}
    122 
    123 	if (fstat(wfd, &stb) == -1)
    124 		err(EXIT_FAILURE, "%s: fstat", file);
    125 	if (!S_ISREG(stb.st_mode))
    126 		errx(EXIT_FAILURE, "%s: Not a regular file", file);
    127 
    128 	buf[FIRSTVALID].ut_timefld = time(NULL);
    129 	(void)signal(SIGINT, onintr);
    130 	(void)signal(SIGQUIT, onintr);
    131 
    132 	offset = stb.st_size;
    133 	/* Ignore trailing garbage or partial record */
    134 	offset -= offset % (off_t) sizeof(*buf);
    135 
    136 	while (offset >= (off_t) sizeof(*buf)) {
    137 		ssize_t ret;
    138 		size_t size;
    139 
    140 		size = MIN(len, offset);
    141 		offset -= size; /* Always a multiple of sizeof(*buf) */
    142 		ret = pread(wfd, buf, size, offset);
    143 		if (ret < 0) {
    144 			err(EXIT_FAILURE, "%s: pread", file);
    145 		} else if ((size_t) ret < size) {
    146 			err(EXIT_FAILURE, "%s: Unexpected end of file", file);
    147 		}
    148 
    149 		for (bp = &buf[ret / sizeof(*buf) - 1]; bp >= buf; --bp) {
    150 			NULTERM(name);
    151 			NULTERM(line);
    152 			NULTERM(host);
    153 			/*
    154 			 * if the terminal line is '~', the machine stopped.
    155 			 * see utmp(5) for more info.
    156 			 */
    157 			if (linep[0] == '~' && !linep[1]) {
    158 				/* everybody just logged out */
    159 				for (T = ttylist; T; T = T->next)
    160 					T->logout = -bp->ut_timefld;
    161 				currentout = -bp->ut_timefld;
    162 				crmsg = strncmp(namep, "shutdown",
    163 				    namesz) ? "crash" : "shutdown";
    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), ct);
    171 					if (maxrec != -1 && !--maxrec)
    172 						return;
    173 				}
    174 				continue;
    175 			}
    176 			/*
    177 			 * if the line is '{' or '|', date got set; see
    178 			 * utmp(5) for more info.
    179 			 */
    180 			if ((linep[0] == '{' || linep[0] == '|') && !linep[1]) {
    181 				if (want(bp, NO)) {
    182 					ct = fmttime(bp->ut_timefld, fulltime);
    183 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
    184 					    namesz, namesz, namep,
    185 					    linesz, linesz, linep,
    186 					    hostsz, hostsz,
    187 					    gethost(bp, hostp, numeric),
    188 					    ct);
    189 					if (maxrec && !--maxrec)
    190 						return;
    191 				}
    192 				continue;
    193 			}
    194 			/* find associated tty */
    195 			for (T = ttylist;; T = T->next) {
    196 				if (!T) {
    197 					/* add new one */
    198 					T = addtty(linep);
    199 					break;
    200 				}
    201 				if (!strncmp(T->tty, linep, LINESIZE))
    202 					break;
    203 			}
    204 			if (TYPE(bp) == SIGNATURE)
    205 				continue;
    206 			if (namep[0] && want(bp, YES)) {
    207 				ct = fmttime(bp->ut_timefld, fulltime);
    208 				printf("%-*.*s  %-*.*s %-*.*s %s ",
    209 				    namesz, namesz, namep,
    210 				    linesz, linesz, linep,
    211 				    hostsz, hostsz,
    212 				    gethost(bp, hostp, numeric),
    213 				    ct);
    214 				if (!T->logout)
    215 					puts("  still logged in");
    216 				else {
    217 					time_t	delta;			/* time difference */
    218 
    219 					if (T->logout < 0) {
    220 						T->logout = -T->logout;
    221 						printf("- %s", crmsg);
    222 					}
    223 					else
    224 						printf("- %s",
    225 						    fmttime(T->logout,
    226 						    fulltime | TIMEONLY));
    227 					delta = T->logout - bp->ut_timefld;
    228 					if (delta < SECSPERDAY)
    229 						printf("  (%s)\n",
    230 						    fmttime(delta,
    231 						    fulltime | TIMEONLY | GMT));
    232 					else
    233 						printf(" (%ld+%s)\n",
    234 						    delta / SECSPERDAY,
    235 						    fmttime(delta,
    236 						    fulltime | TIMEONLY | GMT));
    237 				}
    238 				if (maxrec != -1 && !--maxrec)
    239 					return;
    240 			}
    241 			T->logout = bp->ut_timefld;
    242 		}
    243 	}
    244 	fulltime = 1;	/* show full time */
    245 	crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
    246 	if ((ct = strrchr(file, '/')) != NULL)
    247 		ct++;
    248 	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
    249 }
    250 
    251 /*
    252  * want --
    253  *	see if want this entry
    254  */
    255 static int
    256 want(struct utmp *bp, int check)
    257 {
    258 	ARG *step;
    259 
    260 	if (check) {
    261 		/*
    262 		 * when uucp and ftp log in over a network, the entry in
    263 		 * the utmp file is the name plus their process id.  See
    264 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
    265 		 */
    266 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
    267 			bp->ut_line[3] = '\0';
    268 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
    269 			bp->ut_line[4] = '\0';
    270 	}
    271 	if (!arglist)
    272 		return (YES);
    273 
    274 	for (step = arglist; step; step = step->next)
    275 		switch(step->type) {
    276 		case HOST_TYPE:
    277 			if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
    278 				return (YES);
    279 			break;
    280 		case TTY_TYPE:
    281 			if (!strncmp(step->name, bp->ut_line, LINESIZE))
    282 				return (YES);
    283 			break;
    284 		case USER_TYPE:
    285 			if (!strncmp(step->name, bp->ut_name, NAMESIZE))
    286 				return (YES);
    287 			break;
    288 	}
    289 	return (NO);
    290 }
    291 
    292 /*
    293  * onintr --
    294  *	on interrupt, we inform the user how far we've gotten
    295  */
    296 static void
    297 onintr(int signo)
    298 {
    299 	/* FIXME: None of this is allowed in a signal handler */
    300 	printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
    301 	    FULLTIME));
    302 	if (signo == SIGINT)
    303 		exit(EXIT_FAILURE);
    304 	(void)fflush(stdout);		/* fix required for rsh */
    305 }
    306