Home | History | Annotate | Line # | Download | only in last
want.c revision 1.11.12.1
      1 /*	$NetBSD: want.c,v 1.11.12.1 2012/03/15 08:50:41 sborrill 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 static time_t seentime;
     33 
     34 static void onintr(int);
     35 static int want(struct utmp *, int);
     36 static const char *gethost(struct utmp *, const char *, int);
     37 
     38 static const char *
     39 /*ARGSUSED*/
     40 gethost(struct utmp *ut, const char *host, int numeric)
     41 {
     42 #if FIRSTVALID == 0
     43 	return numeric ? "" : host;
     44 #else
     45 	if (numeric) {
     46 		static char buf[512];
     47 		buf[0] = '\0';
     48 		(void)sockaddr_snprintf(buf, sizeof(buf), "%a",
     49 		    (struct sockaddr *)&ut->ut_ss);
     50 		return buf;
     51 	} else
     52 		return host;
     53 #endif
     54 }
     55 
     56 #define NULTERM(what) \
     57 	if (check ## what) \
     58 		(void)strlcpy(what ## p = what ## buf, bp->ut_ ## what, \
     59 		    sizeof(what ## buf)); \
     60 	else \
     61 		what ## p = bp->ut_ ## what
     62 
     63 /*
     64  * wtmp --
     65  *	read through the wtmp file
     66  */
     67 void
     68 wtmp(const char *file, int namesz, int linesz, int hostsz, int numeric)
     69 {
     70 	struct utmp	*bp;		/* current structure */
     71 	TTY	*T;			/* tty list entry */
     72 	struct stat	stb;		/* stat of file for sz */
     73 	off_t	offset;
     74 	int	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(EXIT_FAILURE, "Cannot allocate utmp buffer");
     86 
     87 	crmsg = NULL;
     88 
     89 	if (!strcmp(file, "-")) {
     90 		wfd = STDIN_FILENO;
     91 		file = "<stdin>";
     92 	} else if ((wfd = open(file, O_RDONLY, 0)) < 0) {
     93 		err(EXIT_FAILURE, "%s", file);
     94 	}
     95 
     96 	if (lseek(wfd, 0, SEEK_CUR) < 0) {
     97 		const char *dir;
     98 		char *tfile;
     99 		int tempfd;
    100 		ssize_t tlen;
    101 
    102 		if (ESPIPE != errno) {
    103 			err(EXIT_FAILURE, "lseek");
    104 		}
    105 		dir = getenv("TMPDIR");
    106 		if (asprintf(&tfile, "%s/last.XXXXXX", dir ? dir : _PATH_TMP) == -1)
    107 			err(EXIT_FAILURE, "asprintf");
    108 		tempfd = mkstemp(tfile);
    109 		if (tempfd < 0) {
    110 			err(EXIT_FAILURE, "mkstemp");
    111 		}
    112 		unlink(tfile);
    113 		for (;;) {
    114 		   	tlen = read(wfd, buf, len);
    115 			if (tlen < 0) {
    116 				err(1, "%s: read", file);
    117 			}
    118 			if (tlen == 0) {
    119 				break;
    120 			}
    121 			if (write(tempfd, buf, tlen) != tlen) {
    122 				err(1, "%s: write", tfile);
    123 			}
    124 		}
    125 		wfd = tempfd;
    126 	}
    127 
    128 	if (fstat(wfd, &stb) == -1)
    129 		err(EXIT_FAILURE, "%s: fstat", file);
    130 	if (!S_ISREG(stb.st_mode))
    131 		errx(EXIT_FAILURE, "%s: Not a regular file", file);
    132 
    133 	seentime = stb.st_mtime;
    134 	(void)signal(SIGINT, onintr);
    135 	(void)signal(SIGQUIT, onintr);
    136 
    137 	offset = stb.st_size;
    138 	/* Ignore trailing garbage or partial record */
    139 	offset -= offset % (off_t) sizeof(*buf);
    140 
    141 	while (offset >= (off_t) sizeof(*buf)) {
    142 		ssize_t ret, i;
    143 		size_t size;
    144 
    145 		size = MIN(len, offset);
    146 		offset -= size; /* Always a multiple of sizeof(*buf) */
    147 		ret = pread(wfd, buf, size, offset);
    148 		if (ret < 0) {
    149 			err(EXIT_FAILURE, "%s: pread", file);
    150 		} else if ((size_t) ret < size) {
    151 			err(EXIT_FAILURE, "%s: Unexpected end of file", file);
    152 		}
    153 
    154 		for (i = ret / sizeof(*buf) - 1; i >= 0; i--) {
    155 			bp = &buf[i];
    156 
    157 			NULTERM(name);
    158 			NULTERM(line);
    159 			NULTERM(host);
    160 
    161 			seentime = bp->ut_timefld;
    162 
    163 			/*
    164 			 * if the terminal line is '~', the machine stopped.
    165 			 * see utmp(5) for more info.
    166 			 */
    167 			if (linep[0] == '~' && !linep[1]) {
    168 				/* everybody just logged out */
    169 				for (T = ttylist; T; T = T->next)
    170 					T->logout = -bp->ut_timefld;
    171 				currentout = -bp->ut_timefld;
    172 				crmsg = strncmp(namep, "shutdown",
    173 				    namesz) ? "crash" : "shutdown";
    174 				if (want(bp, NO)) {
    175 					ct = fmttime(bp->ut_timefld, fulltime);
    176 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
    177 					    namesz, namesz, namep,
    178 					    linesz, linesz, linep,
    179 					    hostsz, hostsz,
    180 					    gethost(bp, hostp, numeric), ct);
    181 					if (maxrec != -1 && !--maxrec)
    182 						return;
    183 				}
    184 				continue;
    185 			}
    186 			/*
    187 			 * if the line is '{' or '|', date got set; see
    188 			 * utmp(5) for more info.
    189 			 */
    190 			if ((linep[0] == '{' || linep[0] == '|') && !linep[1]) {
    191 				if (want(bp, NO)) {
    192 					ct = fmttime(bp->ut_timefld, fulltime);
    193 					printf("%-*.*s  %-*.*s %-*.*s %s\n",
    194 					    namesz, namesz, namep,
    195 					    linesz, linesz, linep,
    196 					    hostsz, hostsz,
    197 					    gethost(bp, hostp, numeric),
    198 					    ct);
    199 					if (maxrec && !--maxrec)
    200 						return;
    201 				}
    202 				continue;
    203 			}
    204 			/* find associated tty */
    205 			for (T = ttylist;; T = T->next) {
    206 				if (!T) {
    207 					/* add new one */
    208 					T = addtty(linep);
    209 					break;
    210 				}
    211 				if (!strncmp(T->tty, linep, LINESIZE))
    212 					break;
    213 			}
    214 			if (TYPE(bp) == SIGNATURE)
    215 				continue;
    216 			if (namep[0] && want(bp, YES)) {
    217 				ct = fmttime(bp->ut_timefld, fulltime);
    218 				printf("%-*.*s  %-*.*s %-*.*s %s ",
    219 				    namesz, namesz, namep,
    220 				    linesz, linesz, linep,
    221 				    hostsz, hostsz,
    222 				    gethost(bp, hostp, numeric),
    223 				    ct);
    224 				if (!T->logout)
    225 					puts("  still logged in");
    226 				else {
    227 					time_t	delta;			/* time difference */
    228 
    229 					if (T->logout < 0) {
    230 						T->logout = -T->logout;
    231 						printf("- %s", crmsg);
    232 					}
    233 					else
    234 						printf("- %s",
    235 						    fmttime(T->logout,
    236 						    fulltime | TIMEONLY));
    237 					delta = T->logout - bp->ut_timefld;
    238 					if (delta < SECSPERDAY)
    239 						printf("  (%s)\n",
    240 						    fmttime(delta,
    241 						    fulltime | TIMEONLY | GMT));
    242 					else
    243 						printf(" (%ld+%s)\n",
    244 						    delta / SECSPERDAY,
    245 						    fmttime(delta,
    246 						    fulltime | TIMEONLY | GMT));
    247 				}
    248 				if (maxrec != -1 && !--maxrec)
    249 					return;
    250 			}
    251 			T->logout = bp->ut_timefld;
    252 		}
    253 	}
    254 	fulltime = 1;	/* show full time */
    255 	crmsg = fmttime(seentime, FULLTIME);
    256 	if ((ct = strrchr(file, '/')) != NULL)
    257 		ct++;
    258 	printf("\n%s begins %s\n", ct ? ct : file, crmsg);
    259 }
    260 
    261 /*
    262  * want --
    263  *	see if want this entry
    264  */
    265 static int
    266 want(struct utmp *bp, int check)
    267 {
    268 	ARG *step;
    269 
    270 	if (check) {
    271 		/*
    272 		 * when uucp and ftp log in over a network, the entry in
    273 		 * the utmp file is the name plus their process id.  See
    274 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
    275 		 */
    276 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
    277 			bp->ut_line[3] = '\0';
    278 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
    279 			bp->ut_line[4] = '\0';
    280 	}
    281 	if (!arglist)
    282 		return (YES);
    283 
    284 	for (step = arglist; step; step = step->next)
    285 		switch(step->type) {
    286 		case HOST_TYPE:
    287 			if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
    288 				return (YES);
    289 			break;
    290 		case TTY_TYPE:
    291 			if (!strncmp(step->name, bp->ut_line, LINESIZE))
    292 				return (YES);
    293 			break;
    294 		case USER_TYPE:
    295 			if (!strncmp(step->name, bp->ut_name, NAMESIZE))
    296 				return (YES);
    297 			break;
    298 	}
    299 	return (NO);
    300 }
    301 
    302 /*
    303  * onintr --
    304  *	on interrupt, we inform the user how far we've gotten
    305  */
    306 static void
    307 onintr(int signo)
    308 {
    309 	/* FIXME: None of this is allowed in a signal handler */
    310 	printf("\ninterrupted %s\n", fmttime(seentime, FULLTIME));
    311 	if (signo == SIGINT) {
    312 		(void)raise_default_signal(signo);
    313 		exit(EXIT_FAILURE);
    314 	}
    315 	(void)fflush(stdout);		/* fix required for rsh */
    316 }
    317