last.c revision 1590
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1987, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
351590Srgrimesstatic char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1987, 1993, 1994\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
411590Srgrimesstatic char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
421590Srgrimes#endif /* not lint */
431590Srgrimes
441590Srgrimes#include <sys/param.h>
451590Srgrimes#include <sys/stat.h>
461590Srgrimes
471590Srgrimes#include <err.h>
481590Srgrimes#include <fcntl.h>
491590Srgrimes#include <paths.h>
501590Srgrimes#include <signal.h>
511590Srgrimes#include <stdio.h>
521590Srgrimes#include <stdlib.h>
531590Srgrimes#include <string.h>
541590Srgrimes#include <time.h>
551590Srgrimes#include <tzfile.h>
561590Srgrimes#include <unistd.h>
571590Srgrimes#include <utmp.h>
581590Srgrimes
591590Srgrimes#define	NO	0				/* false/no */
601590Srgrimes#define	YES	1				/* true/yes */
611590Srgrimes
621590Srgrimesstatic struct utmp	buf[1024];		/* utmp read buffer */
631590Srgrimes
641590Srgrimestypedef struct arg {
651590Srgrimes	char	*name;				/* argument */
661590Srgrimes#define	HOST_TYPE	-2
671590Srgrimes#define	TTY_TYPE	-3
681590Srgrimes#define	USER_TYPE	-4
691590Srgrimes	int	type;				/* type of arg */
701590Srgrimes	struct arg	*next;			/* linked list pointer */
711590Srgrimes} ARG;
721590SrgrimesARG	*arglist;				/* head of linked list */
731590Srgrimes
741590Srgrimestypedef struct ttytab {
751590Srgrimes	long	logout;				/* log out time */
761590Srgrimes	char	tty[UT_LINESIZE + 1];		/* terminal name */
771590Srgrimes	struct ttytab	*next;			/* linked list pointer */
781590Srgrimes} TTY;
791590SrgrimesTTY	*ttylist;				/* head of linked list */
801590Srgrimes
811590Srgrimesstatic long	currentout,			/* current logout value */
821590Srgrimes		maxrec;				/* records to display */
831590Srgrimesstatic char	*file = _PATH_WTMP;		/* wtmp file */
841590Srgrimes
851590Srgrimesvoid	 addarg __P((int, char *));
861590SrgrimesTTY	*addtty __P((char *));
871590Srgrimesvoid	 hostconv __P((char *));
881590Srgrimesvoid	 onintr __P((int));
891590Srgrimeschar	*ttyconv __P((char *));
901590Srgrimesint	 want __P((struct utmp *, int));
911590Srgrimesvoid	 wtmp __P((void));
921590Srgrimes
931590Srgrimesint
941590Srgrimesmain(argc, argv)
951590Srgrimes	int argc;
961590Srgrimes	char *argv[];
971590Srgrimes{
981590Srgrimes	extern int optind;
991590Srgrimes	extern char *optarg;
1001590Srgrimes	int ch;
1011590Srgrimes	char *p;
1021590Srgrimes
1031590Srgrimes	maxrec = -1;
1041590Srgrimes	while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != EOF)
1051590Srgrimes		switch (ch) {
1061590Srgrimes		case '0': case '1': case '2': case '3': case '4':
1071590Srgrimes		case '5': case '6': case '7': case '8': case '9':
1081590Srgrimes			/*
1091590Srgrimes			 * kludge: last was originally designed to take
1101590Srgrimes			 * a number after a dash.
1111590Srgrimes			 */
1121590Srgrimes			if (maxrec == -1) {
1131590Srgrimes				p = argv[optind - 1];
1141590Srgrimes				if (p[0] == '-' && p[1] == ch && !p[2])
1151590Srgrimes					maxrec = atol(++p);
1161590Srgrimes				else
1171590Srgrimes					maxrec = atol(argv[optind] + 1);
1181590Srgrimes				if (!maxrec)
1191590Srgrimes					exit(0);
1201590Srgrimes			}
1211590Srgrimes			break;
1221590Srgrimes		case 'f':
1231590Srgrimes			file = optarg;
1241590Srgrimes			break;
1251590Srgrimes		case 'h':
1261590Srgrimes			hostconv(optarg);
1271590Srgrimes			addarg(HOST_TYPE, optarg);
1281590Srgrimes			break;
1291590Srgrimes		case 't':
1301590Srgrimes			addarg(TTY_TYPE, ttyconv(optarg));
1311590Srgrimes			break;
1321590Srgrimes		case '?':
1331590Srgrimes		default:
1341590Srgrimes			(void)fprintf(stderr,
1351590Srgrimes	"usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n");
1361590Srgrimes			exit(1);
1371590Srgrimes		}
1381590Srgrimes
1391590Srgrimes	if (argc) {
1401590Srgrimes		setlinebuf(stdout);
1411590Srgrimes		for (argv += optind; *argv; ++argv) {
1421590Srgrimes#define	COMPATIBILITY
1431590Srgrimes#ifdef	COMPATIBILITY
1441590Srgrimes			/* code to allow "last p5" to work */
1451590Srgrimes			addarg(TTY_TYPE, ttyconv(*argv));
1461590Srgrimes#endif
1471590Srgrimes			addarg(USER_TYPE, *argv);
1481590Srgrimes		}
1491590Srgrimes	}
1501590Srgrimes	wtmp();
1511590Srgrimes	exit(0);
1521590Srgrimes}
1531590Srgrimes
1541590Srgrimes/*
1551590Srgrimes * wtmp --
1561590Srgrimes *	read through the wtmp file
1571590Srgrimes */
1581590Srgrimesvoid
1591590Srgrimeswtmp()
1601590Srgrimes{
1611590Srgrimes	struct utmp	*bp;			/* current structure */
1621590Srgrimes	TTY	*T;				/* tty list entry */
1631590Srgrimes	struct stat	stb;			/* stat of file for size */
1641590Srgrimes	long	bl, delta;			/* time difference */
1651590Srgrimes	int	bytes, wfd;
1661590Srgrimes	char	*ct, *crmsg;
1671590Srgrimes
1681590Srgrimes	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
1691590Srgrimes		err(1, "%s", file);
1701590Srgrimes	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
1711590Srgrimes
1721590Srgrimes	(void)time(&buf[0].ut_time);
1731590Srgrimes	(void)signal(SIGINT, onintr);
1741590Srgrimes	(void)signal(SIGQUIT, onintr);
1751590Srgrimes
1761590Srgrimes	while (--bl >= 0) {
1771590Srgrimes		if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
1781590Srgrimes		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
1791590Srgrimes			err(1, "%s", file);
1801590Srgrimes		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
1811590Srgrimes			/*
1821590Srgrimes			 * if the terminal line is '~', the machine stopped.
1831590Srgrimes			 * see utmp(5) for more info.
1841590Srgrimes			 */
1851590Srgrimes			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
1861590Srgrimes				/* everybody just logged out */
1871590Srgrimes				for (T = ttylist; T; T = T->next)
1881590Srgrimes					T->logout = -bp->ut_time;
1891590Srgrimes				currentout = -bp->ut_time;
1901590Srgrimes				crmsg = strncmp(bp->ut_name, "shutdown",
1911590Srgrimes				    UT_NAMESIZE) ? "crash" : "shutdown";
1921590Srgrimes				if (want(bp, NO)) {
1931590Srgrimes					ct = ctime(&bp->ut_time);
1941590Srgrimes				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n",
1951590Srgrimes					    UT_NAMESIZE, UT_NAMESIZE,
1961590Srgrimes					    bp->ut_name, UT_LINESIZE,
1971590Srgrimes					    UT_LINESIZE, bp->ut_line,
1981590Srgrimes					    UT_HOSTSIZE, UT_HOSTSIZE,
1991590Srgrimes					    bp->ut_host, ct, ct + 11);
2001590Srgrimes					if (maxrec != -1 && !--maxrec)
2011590Srgrimes						return;
2021590Srgrimes				}
2031590Srgrimes				continue;
2041590Srgrimes			}
2051590Srgrimes			/*
2061590Srgrimes			 * if the line is '{' or '|', date got set; see
2071590Srgrimes			 * utmp(5) for more info.
2081590Srgrimes			 */
2091590Srgrimes			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
2101590Srgrimes			    && !bp->ut_line[1]) {
2111590Srgrimes				if (want(bp, NO)) {
2121590Srgrimes					ct = ctime(&bp->ut_time);
2131590Srgrimes				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n",
2141590Srgrimes				    UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
2151590Srgrimes				    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
2161590Srgrimes				    UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
2171590Srgrimes				    ct, ct + 11);
2181590Srgrimes					if (maxrec && !--maxrec)
2191590Srgrimes						return;
2201590Srgrimes				}
2211590Srgrimes				continue;
2221590Srgrimes			}
2231590Srgrimes			/* find associated tty */
2241590Srgrimes			for (T = ttylist;; T = T->next) {
2251590Srgrimes				if (!T) {
2261590Srgrimes					/* add new one */
2271590Srgrimes					T = addtty(bp->ut_line);
2281590Srgrimes					break;
2291590Srgrimes				}
2301590Srgrimes				if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
2311590Srgrimes					break;
2321590Srgrimes			}
2331590Srgrimes			if (bp->ut_name[0] && want(bp, YES)) {
2341590Srgrimes				ct = ctime(&bp->ut_time);
2351590Srgrimes				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s ",
2361590Srgrimes				UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
2371590Srgrimes				UT_LINESIZE, UT_LINESIZE, bp->ut_line,
2381590Srgrimes				UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
2391590Srgrimes				ct, ct + 11);
2401590Srgrimes				if (!T->logout)
2411590Srgrimes					puts("  still logged in");
2421590Srgrimes				else {
2431590Srgrimes					if (T->logout < 0) {
2441590Srgrimes						T->logout = -T->logout;
2451590Srgrimes						printf("- %s", crmsg);
2461590Srgrimes					}
2471590Srgrimes					else
2481590Srgrimes						printf("- %5.5s",
2491590Srgrimes						    ctime(&T->logout)+11);
2501590Srgrimes					delta = T->logout - bp->ut_time;
2511590Srgrimes					if (delta < SECSPERDAY)
2521590Srgrimes						printf("  (%5.5s)\n",
2531590Srgrimes						    asctime(gmtime(&delta))+11);
2541590Srgrimes					else
2551590Srgrimes						printf(" (%ld+%5.5s)\n",
2561590Srgrimes						    delta / SECSPERDAY,
2571590Srgrimes						    asctime(gmtime(&delta))+11);
2581590Srgrimes				}
2591590Srgrimes				if (maxrec != -1 && !--maxrec)
2601590Srgrimes					return;
2611590Srgrimes			}
2621590Srgrimes			T->logout = bp->ut_time;
2631590Srgrimes		}
2641590Srgrimes	}
2651590Srgrimes	ct = ctime(&buf[0].ut_time);
2661590Srgrimes	printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11);
2671590Srgrimes}
2681590Srgrimes
2691590Srgrimes/*
2701590Srgrimes * want --
2711590Srgrimes *	see if want this entry
2721590Srgrimes */
2731590Srgrimesint
2741590Srgrimeswant(bp, check)
2751590Srgrimes	struct utmp *bp;
2761590Srgrimes	int check;
2771590Srgrimes{
2781590Srgrimes	ARG *step;
2791590Srgrimes
2801590Srgrimes	if (check)
2811590Srgrimes		/*
2821590Srgrimes		 * when uucp and ftp log in over a network, the entry in
2831590Srgrimes		 * the utmp file is the name plus their process id.  See
2841590Srgrimes		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
2851590Srgrimes		 */
2861590Srgrimes		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
2871590Srgrimes			bp->ut_line[3] = '\0';
2881590Srgrimes		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
2891590Srgrimes			bp->ut_line[4] = '\0';
2901590Srgrimes	if (!arglist)
2911590Srgrimes		return (YES);
2921590Srgrimes
2931590Srgrimes	for (step = arglist; step; step = step->next)
2941590Srgrimes		switch(step->type) {
2951590Srgrimes		case HOST_TYPE:
2961590Srgrimes			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
2971590Srgrimes				return (YES);
2981590Srgrimes			break;
2991590Srgrimes		case TTY_TYPE:
3001590Srgrimes			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
3011590Srgrimes				return (YES);
3021590Srgrimes			break;
3031590Srgrimes		case USER_TYPE:
3041590Srgrimes			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
3051590Srgrimes				return (YES);
3061590Srgrimes			break;
3071590Srgrimes	}
3081590Srgrimes	return (NO);
3091590Srgrimes}
3101590Srgrimes
3111590Srgrimes/*
3121590Srgrimes * addarg --
3131590Srgrimes *	add an entry to a linked list of arguments
3141590Srgrimes */
3151590Srgrimesvoid
3161590Srgrimesaddarg(type, arg)
3171590Srgrimes	int type;
3181590Srgrimes	char *arg;
3191590Srgrimes{
3201590Srgrimes	ARG *cur;
3211590Srgrimes
3221590Srgrimes	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
3231590Srgrimes		err(1, "malloc failure");
3241590Srgrimes	cur->next = arglist;
3251590Srgrimes	cur->type = type;
3261590Srgrimes	cur->name = arg;
3271590Srgrimes	arglist = cur;
3281590Srgrimes}
3291590Srgrimes
3301590Srgrimes/*
3311590Srgrimes * addtty --
3321590Srgrimes *	add an entry to a linked list of ttys
3331590Srgrimes */
3341590SrgrimesTTY *
3351590Srgrimesaddtty(ttyname)
3361590Srgrimes	char *ttyname;
3371590Srgrimes{
3381590Srgrimes	TTY *cur;
3391590Srgrimes
3401590Srgrimes	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
3411590Srgrimes		err(1, "malloc failure");
3421590Srgrimes	cur->next = ttylist;
3431590Srgrimes	cur->logout = currentout;
3441590Srgrimes	memmove(cur->tty, ttyname, UT_LINESIZE);
3451590Srgrimes	return (ttylist = cur);
3461590Srgrimes}
3471590Srgrimes
3481590Srgrimes/*
3491590Srgrimes * hostconv --
3501590Srgrimes *	convert the hostname to search pattern; if the supplied host name
3511590Srgrimes *	has a domain attached that is the same as the current domain, rip
3521590Srgrimes *	off the domain suffix since that's what login(1) does.
3531590Srgrimes */
3541590Srgrimesvoid
3551590Srgrimeshostconv(arg)
3561590Srgrimes	char *arg;
3571590Srgrimes{
3581590Srgrimes	static int first = 1;
3591590Srgrimes	static char *hostdot, name[MAXHOSTNAMELEN];
3601590Srgrimes	char *argdot;
3611590Srgrimes
3621590Srgrimes	if (!(argdot = strchr(arg, '.')))
3631590Srgrimes		return;
3641590Srgrimes	if (first) {
3651590Srgrimes		first = 0;
3661590Srgrimes		if (gethostname(name, sizeof(name)))
3671590Srgrimes			err(1, "gethostname");
3681590Srgrimes		hostdot = strchr(name, '.');
3691590Srgrimes	}
3701590Srgrimes	if (hostdot && !strcasecmp(hostdot, argdot))
3711590Srgrimes		*argdot = '\0';
3721590Srgrimes}
3731590Srgrimes
3741590Srgrimes/*
3751590Srgrimes * ttyconv --
3761590Srgrimes *	convert tty to correct name.
3771590Srgrimes */
3781590Srgrimeschar *
3791590Srgrimesttyconv(arg)
3801590Srgrimes	char *arg;
3811590Srgrimes{
3821590Srgrimes	char *mval;
3831590Srgrimes
3841590Srgrimes	/*
3851590Srgrimes	 * kludge -- we assume that all tty's end with
3861590Srgrimes	 * a two character suffix.
3871590Srgrimes	 */
3881590Srgrimes	if (strlen(arg) == 2) {
3891590Srgrimes		/* either 6 for "ttyxx" or 8 for "console" */
3901590Srgrimes		if (!(mval = malloc((u_int)8)))
3911590Srgrimes			err(1, "malloc failure");
3921590Srgrimes		if (!strcmp(arg, "co"))
3931590Srgrimes			(void)strcpy(mval, "console");
3941590Srgrimes		else {
3951590Srgrimes			(void)strcpy(mval, "tty");
3961590Srgrimes			(void)strcpy(mval + 3, arg);
3971590Srgrimes		}
3981590Srgrimes		return (mval);
3991590Srgrimes	}
4001590Srgrimes	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
4011590Srgrimes		return (arg + 5);
4021590Srgrimes	return (arg);
4031590Srgrimes}
4041590Srgrimes
4051590Srgrimes/*
4061590Srgrimes * onintr --
4071590Srgrimes *	on interrupt, we inform the user how far we've gotten
4081590Srgrimes */
4091590Srgrimesvoid
4101590Srgrimesonintr(signo)
4111590Srgrimes	int signo;
4121590Srgrimes{
4131590Srgrimes	char *ct;
4141590Srgrimes
4151590Srgrimes	ct = ctime(&buf[0].ut_time);
4161590Srgrimes	printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11);
4171590Srgrimes	if (signo == SIGINT)
4181590Srgrimes		exit(1);
4191590Srgrimes	(void)fflush(stdout);			/* fix required for rsh */
4201590Srgrimes}
421