Home | History | Annotate | Line # | Download | only in last
last.c revision 1.31
      1 /*	$NetBSD: last.c,v 1.31 2007/10/05 07:27:42 lukem 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.31 2007/10/05 07:27:42 lukem Exp $");
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <paths.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(EXIT_FAILURE);
    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 if (optind < argc)
    173 					maxrec = atol(argv[optind] + 1);
    174 				else
    175 					usage();
    176 				if (!maxrec)
    177 					return 0;
    178 			}
    179 			break;
    180 		case 'f':
    181 			file = optarg;
    182 			if ('\0' == file[0])
    183 				usage();
    184 			break;
    185 		case 'H':
    186 			hostsize = atoi(optarg);
    187 			if (hostsize < 1)
    188 				usage();
    189 			break;
    190 		case 'h':
    191 			hostconv(optarg);
    192 			addarg(HOST_TYPE, optarg);
    193 			break;
    194 		case 'L':
    195 			linesize = atoi(optarg);
    196 			if (linesize < 1)
    197 				usage();
    198 			break;
    199 		case 'N':
    200 			namesize = atoi(optarg);
    201 			if (namesize < 1)
    202 				usage();
    203 			break;
    204 		case 'n':
    205 			numeric = 1;
    206 			break;
    207 		case 'T':
    208 			fulltime = 1;
    209 			break;
    210 		case 't':
    211 			addarg(TTY_TYPE, ttyconv(optarg));
    212 			break;
    213 		case 'x':
    214 			xflag = 1;
    215 			break;
    216 		case '?':
    217 		default:
    218 			usage();
    219 		}
    220 
    221 	if (argc) {
    222 		setlinebuf(stdout);
    223 		for (argv += optind; *argv; ++argv) {
    224 #define	COMPATIBILITY
    225 #ifdef	COMPATIBILITY
    226 			/* code to allow "last p5" to work */
    227 			addarg(TTY_TYPE, ttyconv(*argv));
    228 #endif
    229 			addarg(USER_TYPE, *argv);
    230 		}
    231 	}
    232 	if (file == NULL) {
    233 #ifdef SUPPORT_UTMPX
    234 		if (access(_PATH_WTMPX, R_OK) == 0)
    235 			file = _PATH_WTMPX;
    236 		else
    237 #endif
    238 #ifdef SUPPORT_UTMP
    239 		if (access(_PATH_WTMP, R_OK) == 0)
    240 			file = _PATH_WTMP;
    241 #endif
    242 		if (file == NULL)
    243 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    244 			errx(EXIT_FAILURE, "Cannot access `%s' or `%s'", _PATH_WTMPX,
    245 			    _PATH_WTMP);
    246 #elif defined(SUPPORT_UTMPX)
    247 			errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMPX);
    248 #elif defined(SUPPORT_UTMP)
    249 			errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMP);
    250 #else
    251 			errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
    252 #endif
    253 	}
    254 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
    255 	if (file[strlen(file) - 1] == 'x' || xflag)
    256 		wtmpx(file, namesize, linesize, hostsize, numeric);
    257 	else
    258 		wtmp(file, namesize, linesize, hostsize, numeric);
    259 #elif defined(SUPPORT_UTMPX)
    260 	wtmpx(file, namesize, linesize, hostsize, numeric);
    261 #elif defined(SUPPORT_UTMP)
    262 	wtmp(file, namesize, linesize, hostsize, numeric);
    263 #else
    264 	errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
    265 #endif
    266 	exit(EXIT_SUCCESS);
    267 }
    268 
    269 
    270 /*
    271  * addarg --
    272  *	add an entry to a linked list of arguments
    273  */
    274 static void
    275 addarg(int type, char *arg)
    276 {
    277 	ARG *cur;
    278 
    279 	if (!(cur = (ARG *)malloc(sizeof(ARG))))
    280 		err(EXIT_FAILURE, "malloc failure");
    281 	cur->next = arglist;
    282 	cur->type = type;
    283 	cur->name = arg;
    284 	arglist = cur;
    285 }
    286 
    287 /*
    288  * addtty --
    289  *	add an entry to a linked list of ttys
    290  */
    291 static TTY *
    292 addtty(const char *ttyname)
    293 {
    294 	TTY *cur;
    295 
    296 	if (!(cur = (TTY *)malloc(sizeof(TTY))))
    297 		err(EXIT_FAILURE, "malloc failure");
    298 	cur->next = ttylist;
    299 	cur->logout = currentout;
    300 	memmove(cur->tty, ttyname, sizeof(cur->tty));
    301 	return (ttylist = cur);
    302 }
    303 
    304 /*
    305  * hostconv --
    306  *	convert the hostname to search pattern; if the supplied host name
    307  *	has a domain attached that is the same as the current domain, rip
    308  *	off the domain suffix since that's what login(1) does.
    309  */
    310 static void
    311 hostconv(char *arg)
    312 {
    313 	static int first = 1;
    314 	static char *hostdot, name[MAXHOSTNAMELEN + 1];
    315 	char *argdot;
    316 
    317 	if (!(argdot = strchr(arg, '.')))
    318 		return;
    319 	if (first) {
    320 		first = 0;
    321 		if (gethostname(name, sizeof(name)))
    322 			err(EXIT_FAILURE, "gethostname");
    323 		name[sizeof(name) - 1] = '\0';
    324 		hostdot = strchr(name, '.');
    325 	}
    326 	if (hostdot && !strcasecmp(hostdot, argdot))
    327 		*argdot = '\0';
    328 }
    329 
    330 /*
    331  * ttyconv --
    332  *	convert tty to correct name.
    333  */
    334 static char *
    335 ttyconv(char *arg)
    336 {
    337 	char *mval;
    338 
    339 	if (!strcmp(arg, "co"))
    340 		return ("console");
    341 	/*
    342 	 * kludge -- we assume that all tty's end with
    343 	 * a two character suffix.
    344 	 */
    345 	if (strlen(arg) == 2) {
    346 		if (asprintf(&mval, "tty%s", arg) == -1)
    347 			err(EXIT_FAILURE, "malloc failure");
    348 		return (mval);
    349 	}
    350 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
    351 		return (&arg[sizeof(_PATH_DEV) - 1]);
    352 	return (arg);
    353 }
    354 
    355 /*
    356  * fmttime --
    357  *	return pointer to (static) formatted time string.
    358  */
    359 static char *
    360 fmttime(time_t t, int flags)
    361 {
    362 	struct tm *tm;
    363 	static char tbuf[TBUFLEN];
    364 
    365 	tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
    366 	strftime(tbuf, sizeof(tbuf),
    367 	    (flags & TIMEONLY)
    368 	     ? (flags & FULLTIME ? LTFMTS : TFMTS)
    369 	     : (flags & FULLTIME ? LTFMT : TFMT),
    370 	    tm);
    371 	return (tbuf);
    372 }
    373 
    374 #ifdef SUPPORT_UTMP
    375 #define TYPE(a)	0
    376 #define NAMESIZE UT_NAMESIZE
    377 #define LINESIZE UT_LINESIZE
    378 #define HOSTSIZE UT_HOSTSIZE
    379 #define ut_timefld ut_time
    380 #define FIRSTVALID 0
    381 #include "want.c"
    382 #undef TYPE /*(a)*/
    383 #undef NAMESIZE
    384 #undef LINESIZE
    385 #undef HOSTSIZE
    386 #undef ut_timefld
    387 #undef FIRSTVALID
    388 #endif
    389 
    390 #ifdef SUPPORT_UTMPX
    391 #define utmp utmpx
    392 #define want wantx
    393 #define wtmp wtmpx
    394 #define gethost gethostx
    395 #define buf bufx
    396 #define onintr onintrx
    397 #define TYPE(a) (a)->ut_type
    398 #define NAMESIZE UTX_USERSIZE
    399 #define LINESIZE UTX_LINESIZE
    400 #define HOSTSIZE UTX_HOSTSIZE
    401 #define ut_timefld ut_xtime
    402 #define FIRSTVALID 1
    403 #include "want.c"
    404 #endif
    405