last.c revision 11547
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 <unistd.h>
561590Srgrimes#include <utmp.h>
5711547Sdg#include <sys/queue.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
7411547SdgLIST_HEAD(ttylisthead, ttytab) ttylist;
7511547Sdg
7611547Sdgstruct ttytab {
771590Srgrimes	long	logout;				/* log out time */
781590Srgrimes	char	tty[UT_LINESIZE + 1];		/* terminal name */
7911547Sdg	LIST_ENTRY(ttytab) list;
8011547Sdg};
811590Srgrimes
821590Srgrimesstatic long	currentout,			/* current logout value */
831590Srgrimes		maxrec;				/* records to display */
841590Srgrimesstatic char	*file = _PATH_WTMP;		/* wtmp file */
851590Srgrimes
861590Srgrimesvoid	 addarg __P((int, char *));
871590Srgrimesvoid	 hostconv __P((char *));
881590Srgrimesvoid	 onintr __P((int));
891590Srgrimeschar	*ttyconv __P((char *));
9011547Sdgint	 want __P((struct utmp *));
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 */
16211547Sdg	struct ttytab	*tt;				/* ttylist 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
16811547Sdg	LIST_INIT(&ttylist);
16911547Sdg
1701590Srgrimes	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
1711590Srgrimes		err(1, "%s", file);
1721590Srgrimes	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
1731590Srgrimes
1741590Srgrimes	(void)time(&buf[0].ut_time);
1751590Srgrimes	(void)signal(SIGINT, onintr);
1761590Srgrimes	(void)signal(SIGQUIT, onintr);
1771590Srgrimes
1781590Srgrimes	while (--bl >= 0) {
1791590Srgrimes		if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
1801590Srgrimes		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
1811590Srgrimes			err(1, "%s", file);
1821590Srgrimes		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
1831590Srgrimes			/*
1841590Srgrimes			 * if the terminal line is '~', the machine stopped.
1851590Srgrimes			 * see utmp(5) for more info.
1861590Srgrimes			 */
1871590Srgrimes			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
1881590Srgrimes				/* everybody just logged out */
18911547Sdg				for (tt = ttylist.lh_first; tt; tt = tt->list.le_next) {
19011547Sdg					LIST_REMOVE(tt, list);
19111547Sdg					free(tt);
19211547Sdg				}
1931590Srgrimes				currentout = -bp->ut_time;
1941590Srgrimes				crmsg = strncmp(bp->ut_name, "shutdown",
1951590Srgrimes				    UT_NAMESIZE) ? "crash" : "shutdown";
19611547Sdg				if (want(bp)) {
1971590Srgrimes					ct = ctime(&bp->ut_time);
19811547Sdg					printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n",
1991590Srgrimes					    UT_NAMESIZE, UT_NAMESIZE,
2001590Srgrimes					    bp->ut_name, UT_LINESIZE,
2011590Srgrimes					    UT_LINESIZE, bp->ut_line,
2021590Srgrimes					    UT_HOSTSIZE, UT_HOSTSIZE,
2031590Srgrimes					    bp->ut_host, ct, ct + 11);
2041590Srgrimes					if (maxrec != -1 && !--maxrec)
2051590Srgrimes						return;
2061590Srgrimes				}
2071590Srgrimes				continue;
2081590Srgrimes			}
2091590Srgrimes			/*
2101590Srgrimes			 * if the line is '{' or '|', date got set; see
2111590Srgrimes			 * utmp(5) for more info.
2121590Srgrimes			 */
2131590Srgrimes			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
2141590Srgrimes			    && !bp->ut_line[1]) {
21511547Sdg				if (want(bp)) {
2161590Srgrimes					ct = ctime(&bp->ut_time);
21711547Sdg					printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n",
21811547Sdg					    UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
21911547Sdg					    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
22011547Sdg					    UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
22111547Sdg					    ct, ct + 11);
2221590Srgrimes					if (maxrec && !--maxrec)
2231590Srgrimes						return;
2241590Srgrimes				}
2251590Srgrimes				continue;
2261590Srgrimes			}
22711547Sdg			if (bp->ut_name[0] == '\0' || want(bp)) {
22811547Sdg				/* find associated tty */
22911547Sdg				for (tt = ttylist.lh_first; ; tt = tt->list.le_next) {
23011547Sdg					if (tt == NULL) {
23111547Sdg						/* add new one */
23211547Sdg						tt = malloc(sizeof(struct ttytab));
23311547Sdg						if (tt == NULL)
23411547Sdg							err(1, "malloc failure");
23511547Sdg						tt->logout = currentout;
23611547Sdg						strncpy(tt->tty, bp->ut_line, UT_LINESIZE);
23711547Sdg						LIST_INSERT_HEAD(&ttylist, tt, list);
23811547Sdg						break;
23911547Sdg					}
24011547Sdg					if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE))
24111547Sdg						break;
2421590Srgrimes				}
24311547Sdg				if (bp->ut_name[0]) {
24411547Sdg					/*
24511547Sdg					 * when uucp and ftp log in over a network, the entry in
24611547Sdg					 * the utmp file is the name plus their process id.  See
24711547Sdg					 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
24811547Sdg					 */
24911547Sdg					if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
25011547Sdg						bp->ut_line[3] = '\0';
25111547Sdg					else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
25211547Sdg						bp->ut_line[4] = '\0';
25311547Sdg					ct = ctime(&bp->ut_time);
25411547Sdg					printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s ",
25511547Sdg					    UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
25611547Sdg					    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
25711547Sdg					    UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
25811547Sdg					    ct, ct + 11);
25911547Sdg					if (!tt->logout)
26011547Sdg						puts("  still logged in");
26111547Sdg					else {
26211547Sdg						if (tt->logout < 0) {
26311547Sdg							tt->logout = -tt->logout;
26411547Sdg							printf("- %s", crmsg);
26511547Sdg						}
26611547Sdg						else
26711547Sdg							printf("- %5.5s",
26811547Sdg							    ctime(&tt->logout)+11);
26911547Sdg						delta = tt->logout - bp->ut_time;
27011547Sdg						if (delta < 86400)
27111547Sdg							printf("  (%5.5s)\n",
27211547Sdg							    asctime(gmtime(&delta))+11);
27311547Sdg						else
27411547Sdg							printf(" (%ld+%5.5s)\n",
27511547Sdg							    delta / 86400,
27611547Sdg							    asctime(gmtime(&delta))+11);
2771590Srgrimes					}
27811547Sdg					LIST_REMOVE(tt, list);
27911547Sdg					free(tt);
28011547Sdg					if (maxrec != -1 && !--maxrec)
28111547Sdg						return;
28211547Sdg				} else {
28311547Sdg					tt->logout = bp->ut_time;
2841590Srgrimes				}
2851590Srgrimes			}
2861590Srgrimes		}
2871590Srgrimes	}
2881590Srgrimes	ct = ctime(&buf[0].ut_time);
2891590Srgrimes	printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11);
2901590Srgrimes}
2911590Srgrimes
2921590Srgrimes/*
2931590Srgrimes * want --
2941590Srgrimes *	see if want this entry
2951590Srgrimes */
2961590Srgrimesint
29711547Sdgwant(bp)
2981590Srgrimes	struct utmp *bp;
2991590Srgrimes{
3001590Srgrimes	ARG *step;
3011590Srgrimes
3021590Srgrimes	if (!arglist)
3031590Srgrimes		return (YES);
3041590Srgrimes
3051590Srgrimes	for (step = arglist; step; step = step->next)
3061590Srgrimes		switch(step->type) {
3071590Srgrimes		case HOST_TYPE:
3081590Srgrimes			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
3091590Srgrimes				return (YES);
3101590Srgrimes			break;
3111590Srgrimes		case TTY_TYPE:
3121590Srgrimes			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
3131590Srgrimes				return (YES);
3141590Srgrimes			break;
3151590Srgrimes		case USER_TYPE:
3161590Srgrimes			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
3171590Srgrimes				return (YES);
3181590Srgrimes			break;
3191590Srgrimes	}
3201590Srgrimes	return (NO);
3211590Srgrimes}
3221590Srgrimes
3231590Srgrimes/*
3241590Srgrimes * addarg --
3251590Srgrimes *	add an entry to a linked list of arguments
3261590Srgrimes */
3271590Srgrimesvoid
3281590Srgrimesaddarg(type, arg)
3291590Srgrimes	int type;
3301590Srgrimes	char *arg;
3311590Srgrimes{
3321590Srgrimes	ARG *cur;
3331590Srgrimes
3341590Srgrimes	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
3351590Srgrimes		err(1, "malloc failure");
3361590Srgrimes	cur->next = arglist;
3371590Srgrimes	cur->type = type;
3381590Srgrimes	cur->name = arg;
3391590Srgrimes	arglist = cur;
3401590Srgrimes}
3411590Srgrimes
3421590Srgrimes/*
3431590Srgrimes * hostconv --
3441590Srgrimes *	convert the hostname to search pattern; if the supplied host name
3451590Srgrimes *	has a domain attached that is the same as the current domain, rip
3461590Srgrimes *	off the domain suffix since that's what login(1) does.
3471590Srgrimes */
3481590Srgrimesvoid
3491590Srgrimeshostconv(arg)
3501590Srgrimes	char *arg;
3511590Srgrimes{
3521590Srgrimes	static int first = 1;
3531590Srgrimes	static char *hostdot, name[MAXHOSTNAMELEN];
3541590Srgrimes	char *argdot;
3551590Srgrimes
3561590Srgrimes	if (!(argdot = strchr(arg, '.')))
3571590Srgrimes		return;
3581590Srgrimes	if (first) {
3591590Srgrimes		first = 0;
3601590Srgrimes		if (gethostname(name, sizeof(name)))
3611590Srgrimes			err(1, "gethostname");
3621590Srgrimes		hostdot = strchr(name, '.');
3631590Srgrimes	}
3641590Srgrimes	if (hostdot && !strcasecmp(hostdot, argdot))
3651590Srgrimes		*argdot = '\0';
3661590Srgrimes}
3671590Srgrimes
3681590Srgrimes/*
3691590Srgrimes * ttyconv --
3701590Srgrimes *	convert tty to correct name.
3711590Srgrimes */
3721590Srgrimeschar *
3731590Srgrimesttyconv(arg)
3741590Srgrimes	char *arg;
3751590Srgrimes{
3761590Srgrimes	char *mval;
3771590Srgrimes
3781590Srgrimes	/*
3791590Srgrimes	 * kludge -- we assume that all tty's end with
3801590Srgrimes	 * a two character suffix.
3811590Srgrimes	 */
3821590Srgrimes	if (strlen(arg) == 2) {
3831590Srgrimes		/* either 6 for "ttyxx" or 8 for "console" */
3841590Srgrimes		if (!(mval = malloc((u_int)8)))
3851590Srgrimes			err(1, "malloc failure");
3861590Srgrimes		if (!strcmp(arg, "co"))
3871590Srgrimes			(void)strcpy(mval, "console");
3881590Srgrimes		else {
3891590Srgrimes			(void)strcpy(mval, "tty");
3901590Srgrimes			(void)strcpy(mval + 3, arg);
3911590Srgrimes		}
3921590Srgrimes		return (mval);
3931590Srgrimes	}
3941590Srgrimes	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
3951590Srgrimes		return (arg + 5);
3961590Srgrimes	return (arg);
3971590Srgrimes}
3981590Srgrimes
3991590Srgrimes/*
4001590Srgrimes * onintr --
4011590Srgrimes *	on interrupt, we inform the user how far we've gotten
4021590Srgrimes */
4031590Srgrimesvoid
4041590Srgrimesonintr(signo)
4051590Srgrimes	int signo;
4061590Srgrimes{
4071590Srgrimes	char *ct;
4081590Srgrimes
4091590Srgrimes	ct = ctime(&buf[0].ut_time);
4101590Srgrimes	printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11);
4111590Srgrimes	if (signo == SIGINT)
4121590Srgrimes		exit(1);
4131590Srgrimes	(void)fflush(stdout);			/* fix required for rsh */
4141590Srgrimes}
415