Home | History | Annotate | Line # | Download | only in last
last.c revision 1.27
      1 /*	$NetBSD: last.c,v 1.27 2005/03/14 13:34:57 christos 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 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT(
     35 "@(#) Copyright (c) 1987, 1993, 1994\n\
     36 	The Regents of the University of California.  All rights reserved.\n");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
     42 #endif
     43 __RCSID("$NetBSD: last.c,v 1.27 2005/03/14 13:34:57 christos Exp $");
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 
     49 #include <err.h>
     50 #include <fcntl.h>
     51 #include <paths.h>
     52 #include <signal.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <time.h>
     57 #include <tzfile.h>
     58 #include <unistd.h>
     59 #include <arpa/inet.h>
     60 #ifdef SUPPORT_UTMPX
     61 #include <utmpx.h>
     62 #endif
     63 #ifdef SUPPORT_UTMP
     64 #include <utmp.h>
     65 #endif
     66 #include <util.h>
     67 
     68 #ifndef UT_NAMESIZE
     69 #define UT_NAMESIZE 8
     70 #define UT_LINESIZE 8
     71 #define UT_HOSTSIZE 16
     72 #endif
     73 #ifndef SIGNATURE
     74 #define SIGNATURE -1
     75 #endif
     76 
     77 
     78 
     79 #define	NO	0			/* false/no */
     80 #define	YES	1			/* true/yes */
     81 
     82 #define	TBUFLEN	30			/* length of time string buffer */
     83 #define	TFMT	"%a %b %d %R"		/* strftime format string */
     84 #define	LTFMT	"%a %b %d %Y %T"	/* strftime long format string */
     85 #define	TFMTS	"%R"			/* strftime format string - time only */
     86 #define	LTFMTS	"%T"			/* strftime long format string - " */
     87 
     88 /* fmttime() flags */
     89 #define	FULLTIME	0x1		/* show year, seconds */
     90 #define	TIMEONLY	0x2		/* show time only, not date */
     91 #define	GMT		0x4		/* show time at GMT, for offsets only */
     92 
     93 #define MAXUTMP		1024
     94 
     95 typedef struct arg {
     96 	char	*name;			/* argument */
     97 #define	HOST_TYPE	-2
     98 #define	TTY_TYPE	-3
     99 #define	USER_TYPE	-4
    100 	int	type;			/* type of arg */
    101 	struct arg	*next;		/* linked list pointer */
    102 } ARG;
    103 static ARG	*arglist;		/* head of linked list */
    104 
    105 typedef struct ttytab {
    106 	time_t	logout;			/* log out time */
    107 	char	tty[128];		/* terminal name */
    108 	struct ttytab	*next;		/* linked list pointer */
    109 } TTY;
    110 static TTY	*ttylist;		/* head of linked list */
    111 
    112 static time_t	currentout;		/* current logout value */
    113 static long	maxrec;			/* records to display */
    114 static int	fulltime = 0;		/* Display seconds? */
    115 static int	xflag;			/* Assume file is wtmpx format */
    116 
    117 int	 main(int, char *[]);
    118 
    119 static void	 addarg(int, char *);
    120 static TTY	*addtty(const char *);
    121 static void	 hostconv(char *);
    122 static char	*ttyconv(char *);
    123 #ifdef SUPPORT_UTMPX
    124 static void	 wtmpx(const char *, int, int, int, int);
    125 #endif
    126 #ifdef SUPPORT_UTMP
    127 static void	 wtmp(const char *, int, int, int, int);
    128 #endif
    129 static char	*fmttime(time_t, int);
    130 static void	 usage(void);
    131 
    132 static
    133 void usage(void)
    134 {
    135 	(void)fprintf(stderr, "usage: %s [-#%s] [-nTx] [-f file]"
    136 	    " [-H hostsize] [-h host] [-L linesize]\n"
    137 	    "\t    [-N namesize] [-t tty] [user ...]\n", getprogname(),
    138 #ifdef NOTYET_SUPPORT_UTMPX
    139 	    "w"
    140 #else
    141 	    ""
    142 #endif
    143 	);
    144 	exit(1);
    145 }
    146 
    147 int
    148 main(int argc, char *argv[])
    149 {
    150 	int ch;
    151 	char *p;
    152 	char	*file = NULL;
    153 	int namesize = UT_NAMESIZE;
    154 	int linesize = UT_LINESIZE;
    155 	int hostsize = UT_HOSTSIZE;
    156 	int numeric = 0;
    157 
    158 	maxrec = -1;
    159 
    160 	while ((ch = getopt(argc, argv, "0123456789f:H:h:L:nN:Tt:x")) != -1)
    161 		switch (ch) {
    162 		case '0': case '1': case '2': case '3': case '4':
    163 		case '5': case '6': case '7': case '8': case '9':
    164 			/*
    165 			 * kludge: last was originally designed to take
    166 			 * a number after a dash.
    167 			 */
    168 			if (maxrec == -1) {
    169 				p = argv[optind - 1];
    170 				if (p[0] == '-' && p[1] == ch && !p[2])
    171 					maxrec = atol(++p);
    172 				else
    173 					maxrec = atol(argv[optind] + 1);
    174 				if (!maxrec)
    175 					exit(0);
    176 			}
    177 			break;
    178 		case 'f':
    179 			file = optarg;
    180 			break;
    181 		case 'H':
    182 			hostsize = atoi(optarg);
    183 			break;
    184 		case 'h':
    185 			hostconv(optarg);
    186 			addarg(HOST_TYPE, optarg);
    187 			break;
    188 		case 'L':
    189 			linesize = atoi(optarg);
    190 			break;
    191 		case 'N':
    192 			namesize = atoi(optarg);
    193 			break;
    194 		case 'n':
    195 			numeric = 1;
    196 			break;
    197 		case 'T':
    198 			fulltime = 1;
    199 			break;
    200 		case 't':
    201 			addarg(TTY_TYPE, ttyconv(optarg));
    202 			break;
    203 		case 'x':
    204 			xflag = 1;
    205 			break;
    206 		case '?':
    207 		default:
    208 			usage();
    209 		}
    210 
    211 	if (argc) {
    212 		setlinebuf(stdout);
    213 		for (argv += optind; *argv; ++argv) {
    214 #define	COMPATIBILITY
    215 #ifdef	COMPATIBILITY
    216 			/* code to allow "last p5" to work */
    217 			addarg(TTY_TYPE, ttyconv(*argv));
    218 #endif
    219 			addarg(USER_TYPE, *argv);
    220 		}
    221 	}
    222 	if (file == NULL) {
    223 #ifdef SUPPORT_UTMPX
    224 		if (access(_PATH_WTMPX, R_OK) == 0)
    225 			file = _PATH_WTMPX;
    226 		else
    227 #endif
    228 #ifdef SUPPORT_UTMP
    229 		if (access(_PATH_WTMP, R_OK) == 0)
    230 			file = _PATH_WTMP;
    231 #endif
    232 		if (file == NULL)
    233 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    234 			errx(1, "Cannot access `%s' or `%s'", _PATH_WTMPX,
    235 			    _PATH_WTMP);
    236 #elif defined(SUPPORT_UTMPX)
    237 			errx(1, "Cannot access `%s'", _PATH_WTMPX);
    238 #elif defined(SUPPORT_UTMP)
    239 			errx(1, "Cannot access `%s'", _PATH_WTMP);
    240 #else
    241 			errx(1, "No utmp or utmpx support compiled in.");
    242 #endif
    243 	}
    244 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    245 	if (file[strlen(file) - 1] == 'x' || xflag)
    246 		wtmpx(file, namesize, linesize, hostsize, numeric);
    247 	else
    248 		wtmp(file, namesize, linesize, hostsize, numeric);
    249 #elif defined(SUPPORT_UTMPX)
    250 	wtmpx(file, namesize, linesize, hostsize, numeric);
    251 #elif defined(SUPPORT_UTMP)
    252 	wtmp(file, namesize, linesize, hostsize, numeric);
    253 #else
    254 	errx(1, "No utmp or utmpx support compiled in.");
    255 #endif
    256 	exit(0);
    257 }
    258 
    259 
    260 /*
    261  * addarg --
    262  *	add an entry to a linked list of arguments
    263  */
    264 static void
    265 addarg(int type, char *arg)
    266 {
    267 	ARG *cur;
    268 
    269 	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
    270 		err(1, "malloc failure");
    271 	cur->next = arglist;
    272 	cur->type = type;
    273 	cur->name = arg;
    274 	arglist = cur;
    275 }
    276 
    277 /*
    278  * addtty --
    279  *	add an entry to a linked list of ttys
    280  */
    281 static TTY *
    282 addtty(const char *ttyname)
    283 {
    284 	TTY *cur;
    285 
    286 	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
    287 		err(1, "malloc failure");
    288 	cur->next = ttylist;
    289 	cur->logout = currentout;
    290 	memmove(cur->tty, ttyname, sizeof(cur->tty));
    291 	return (ttylist = cur);
    292 }
    293 
    294 /*
    295  * hostconv --
    296  *	convert the hostname to search pattern; if the supplied host name
    297  *	has a domain attached that is the same as the current domain, rip
    298  *	off the domain suffix since that's what login(1) does.
    299  */
    300 static void
    301 hostconv(char *arg)
    302 {
    303 	static int first = 1;
    304 	static char *hostdot, name[MAXHOSTNAMELEN + 1];
    305 	char *argdot;
    306 
    307 	if (!(argdot = strchr(arg, '.')))
    308 		return;
    309 	if (first) {
    310 		first = 0;
    311 		if (gethostname(name, sizeof(name)))
    312 			err(1, "gethostname");
    313 		name[sizeof(name) - 1] = '\0';
    314 		hostdot = strchr(name, '.');
    315 	}
    316 	if (hostdot && !strcasecmp(hostdot, argdot))
    317 		*argdot = '\0';
    318 }
    319 
    320 /*
    321  * ttyconv --
    322  *	convert tty to correct name.
    323  */
    324 static char *
    325 ttyconv(char *arg)
    326 {
    327 	char *mval;
    328 
    329 	/*
    330 	 * kludge -- we assume that all tty's end with
    331 	 * a two character suffix.
    332 	 */
    333 	if (strlen(arg) == 2) {
    334 		if (!strcmp(arg, "co"))
    335 			mval = strdup("console");
    336 		else
    337 			asprintf(&mval, "tty%s", arg);
    338 		if (!mval)
    339 			err(1, "malloc failure");
    340 		return (mval);
    341 	}
    342 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
    343 		return (arg + 5);
    344 	return (arg);
    345 }
    346 
    347 /*
    348  * fmttime --
    349  *	return pointer to (static) formatted time string.
    350  */
    351 static char *
    352 fmttime(time_t t, int flags)
    353 {
    354 	struct tm *tm;
    355 	static char tbuf[TBUFLEN];
    356 
    357 	tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
    358 	strftime(tbuf, sizeof(tbuf),
    359 	    (flags & TIMEONLY)
    360 	     ? (flags & FULLTIME ? LTFMTS : TFMTS)
    361 	     : (flags & FULLTIME ? LTFMT : TFMT),
    362 	    tm);
    363 	return (tbuf);
    364 }
    365 
    366 #ifdef SUPPORT_UTMP
    367 #define TYPE(a)	0
    368 #define NAMESIZE UT_NAMESIZE
    369 #define LINESIZE UT_LINESIZE
    370 #define HOSTSIZE UT_HOSTSIZE
    371 #define ut_timefld ut_time
    372 #define FIRSTVALID 0
    373 #include "want.c"
    374 #undef TYPE /*(a)*/
    375 #undef NAMESIZE
    376 #undef LINESIZE
    377 #undef HOSTSIZE
    378 #undef ut_timefld
    379 #undef FIRSTVALID
    380 #endif
    381 
    382 #ifdef SUPPORT_UTMPX
    383 #define utmp utmpx
    384 #define want wantx
    385 #define wtmp wtmpx
    386 #define gethost gethostx
    387 #define buf bufx
    388 #define onintr onintrx
    389 #define TYPE(a) (a)->ut_type
    390 #define NAMESIZE UTX_USERSIZE
    391 #define LINESIZE UTX_LINESIZE
    392 #define HOSTSIZE UTX_HOSTSIZE
    393 #define ut_timefld ut_xtime
    394 #define FIRSTVALID 1
    395 #include "want.c"
    396 #endif
    397