lastlogin.c revision 1.15       1  1.15     joerg /*	$NetBSD: lastlogin.c,v 1.15 2011/08/31 13:31:29 joerg Exp $	*/
      2   1.1      phil /*
      3   1.1      phil  * Copyright (c) 1996 John M. Vinopal
      4   1.1      phil  * All rights reserved.
      5   1.1      phil  *
      6   1.1      phil  * Redistribution and use in source and binary forms, with or without
      7   1.1      phil  * modification, are permitted provided that the following conditions
      8   1.1      phil  * are met:
      9   1.1      phil  * 1. Redistributions of source code must retain the above copyright
     10   1.1      phil  *    notice, this list of conditions and the following disclaimer.
     11   1.1      phil  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      phil  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      phil  *    documentation and/or other materials provided with the distribution.
     14   1.1      phil  * 3. All advertising materials mentioning features or use of this software
     15   1.1      phil  *    must display the following acknowledgement:
     16   1.1      phil  *	This product includes software developed for the NetBSD Project
     17   1.1      phil  *	by John M. Vinopal.
     18   1.1      phil  * 4. The name of the author may not be used to endorse or promote products
     19   1.1      phil  *    derived from this software without specific prior written permission.
     20   1.1      phil  *
     21   1.1      phil  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1      phil  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1      phil  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1      phil  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1      phil  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26   1.1      phil  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27   1.1      phil  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28   1.1      phil  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29   1.1      phil  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1      phil  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1      phil  * SUCH DAMAGE.
     32   1.1      phil  */
     33   1.1      phil 
     34   1.3     lukem #include <sys/cdefs.h>
     35   1.3     lukem #ifndef lint
     36  1.15     joerg __RCSID("$NetBSD: lastlogin.c,v 1.15 2011/08/31 13:31:29 joerg Exp $");
     37   1.3     lukem #endif
     38   1.3     lukem 
     39   1.1      phil #include <sys/types.h>
     40   1.3     lukem #include <err.h>
     41   1.9  christos #include <db.h>
     42   1.1      phil #include <errno.h>
     43   1.1      phil #include <pwd.h>
     44   1.1      phil #include <stdio.h>
     45   1.5      matt #include <stdlib.h>
     46   1.7     elric #include <string.h>
     47   1.9  christos #include <fcntl.h>
     48   1.3     lukem #include <time.h>
     49   1.9  christos #include <arpa/inet.h>
     50   1.9  christos #include <sys/socket.h>
     51   1.9  christos #ifdef SUPPORT_UTMP
     52   1.3     lukem #include <utmp.h>
     53   1.9  christos #endif
     54   1.9  christos #ifdef SUPPORT_UTMPX
     55   1.9  christos #include <utmpx.h>
     56   1.9  christos #endif
     57   1.4     perry #include <unistd.h>
     58  1.11  christos #include <util.h>
     59   1.1      phil 
     60   1.7     elric struct output {
     61   1.9  christos 	struct timeval	 o_tv;
     62   1.9  christos 	struct sockaddr_storage o_ss;
     63   1.9  christos 	char		 o_name[64];
     64   1.9  christos 	char		 o_line[64];
     65   1.9  christos 	char		 o_host[256];
     66   1.7     elric 	struct output	*next;
     67   1.7     elric };
     68   1.7     elric 
     69   1.7     elric #define SORT_NONE	0x0000
     70   1.7     elric #define SORT_REVERSE	0x0001
     71   1.7     elric #define SORT_TIME	0x0002
     72   1.7     elric #define DOSORT(x)	((x) & (SORT_TIME))
     73   1.7     elric static	int sortlog = SORT_NONE;
     74   1.7     elric static	struct output *outstack = NULL;
     75   1.7     elric 
     76   1.9  christos static int numeric = 0;
     77   1.9  christos static size_t namelen = UT_NAMESIZE;
     78   1.9  christos static size_t linelen = UT_LINESIZE;
     79   1.9  christos static size_t hostlen = UT_HOSTSIZE;
     80   1.9  christos 
     81   1.9  christos static	int	comparelog(const void *, const void *);
     82   1.9  christos static	void	output(struct output *);
     83   1.9  christos #ifdef SUPPORT_UTMP
     84   1.9  christos static	void	process_entry(struct passwd *, struct lastlog *);
     85   1.9  christos static	void	dolastlog(const char *, int, char *[]);
     86   1.9  christos #endif
     87   1.9  christos #ifdef SUPPORT_UTMPX
     88   1.9  christos static	void	process_entryx(struct passwd *, struct lastlogx *);
     89   1.9  christos static	void	dolastlogx(const char *, int, char *[]);
     90   1.9  christos #endif
     91   1.9  christos static	void	push(struct output *);
     92   1.9  christos static	const char 	*gethost(struct output *);
     93   1.9  christos static	void	sortoutput(struct output *);
     94  1.15     joerg __dead static	void	usage(void);
     95   1.1      phil 
     96   1.1      phil int
     97  1.15     joerg main(int argc, char *argv[])
     98   1.1      phil {
     99  1.14     lukem 	const char *logfile =
    100  1.14     lukem #if defined(SUPPORT_UTMPX)
    101  1.14     lukem 	    _PATH_LASTLOGX;
    102  1.14     lukem #elif defined(SUPPORT_UTMP)
    103  1.14     lukem 	    _PATH_LASTLOG;
    104  1.14     lukem #else
    105  1.14     lukem 	#error "either SUPPORT_UTMP or SUPPORT_UTMPX must be defined"
    106  1.14     lukem #endif
    107   1.9  christos 	int	ch;
    108   1.9  christos 	size_t	len;
    109   1.1      phil 
    110   1.9  christos 	while ((ch = getopt(argc, argv, "f:H:L:nN:rt")) != -1) {
    111   1.7     elric 		switch (ch) {
    112   1.9  christos 		case 'H':
    113   1.9  christos 			hostlen = atoi(optarg);
    114   1.9  christos 			break;
    115   1.9  christos 		case 'f':
    116   1.9  christos 			logfile = optarg;
    117   1.9  christos 			break;
    118   1.9  christos 		case 'L':
    119   1.9  christos 			linelen = atoi(optarg);
    120   1.9  christos 			break;
    121   1.9  christos 		case 'n':
    122   1.9  christos 			numeric++;
    123   1.9  christos 			break;
    124   1.9  christos 		case 'N':
    125   1.9  christos 			namelen = atoi(optarg);
    126   1.9  christos 			break;
    127   1.7     elric 		case 'r':
    128   1.7     elric 			sortlog |= SORT_REVERSE;
    129   1.7     elric 			break;
    130   1.7     elric 		case 't':
    131   1.7     elric 			sortlog |= SORT_TIME;
    132   1.7     elric 			break;
    133   1.7     elric 		default:
    134   1.7     elric 			usage();
    135   1.7     elric 		}
    136   1.1      phil 	}
    137   1.7     elric 	argc -= optind;
    138   1.7     elric 	argv += optind;
    139   1.1      phil 
    140   1.9  christos 	len = strlen(logfile);
    141   1.9  christos 
    142   1.9  christos 	setpassent(1);	/* Keep passwd file pointers open */
    143   1.9  christos 
    144   1.9  christos #if defined(SUPPORT_UTMPX)
    145   1.9  christos 	if (len > 0 && logfile[len - 1] == 'x')
    146   1.9  christos 		dolastlogx(logfile, argc, argv);
    147   1.9  christos 	else
    148   1.9  christos #endif
    149   1.9  christos #if defined(SUPPORT_UTMP)
    150   1.9  christos 		dolastlog(logfile, argc, argv);
    151   1.9  christos #endif
    152   1.9  christos 
    153   1.9  christos 	setpassent(0);	/* Close passwd file pointers */
    154   1.9  christos 
    155  1.12    martin 	if (outstack && DOSORT(sortlog))
    156   1.9  christos 		sortoutput(outstack);
    157   1.9  christos 
    158   1.9  christos 	return 0;
    159   1.9  christos }
    160   1.9  christos 
    161   1.9  christos #ifdef SUPPORT_UTMP
    162   1.9  christos static void
    163   1.9  christos dolastlog(const char *logfile, int argc, char **argv)
    164   1.9  christos {
    165   1.9  christos 	int i;
    166   1.9  christos 	FILE *fp = fopen(logfile, "r");
    167   1.9  christos 	struct passwd	*passwd;
    168   1.9  christos 	struct lastlog l;
    169   1.9  christos 
    170   1.1      phil 	if (fp == NULL)
    171   1.1      phil 		err(1, "%s", logfile);
    172   1.1      phil 
    173   1.1      phil 	/* Process usernames given on the command line. */
    174   1.8      wulf 	if (argc > 0) {
    175   1.9  christos 		off_t offset;
    176   1.8      wulf 		for (i = 0; i < argc; i++) {
    177   1.1      phil 			if ((passwd = getpwnam(argv[i])) == NULL) {
    178   1.1      phil 				warnx("user '%s' not found", argv[i]);
    179   1.1      phil 				continue;
    180   1.1      phil 			}
    181   1.1      phil 			/* Calculate the offset into the lastlog file. */
    182   1.9  christos 			offset = passwd->pw_uid * sizeof(l);
    183   1.9  christos 			if (fseeko(fp, offset, SEEK_SET)) {
    184   1.1      phil 				warn("fseek error");
    185   1.1      phil 				continue;
    186   1.1      phil 			}
    187   1.9  christos 			if (fread(&l, sizeof(l), 1, fp) != 1) {
    188   1.1      phil 				warnx("fread error on '%s'", passwd->pw_name);
    189   1.1      phil 				clearerr(fp);
    190   1.1      phil 				continue;
    191   1.1      phil 			}
    192   1.9  christos 			process_entry(passwd, &l);
    193   1.1      phil 		}
    194   1.1      phil 	}
    195   1.1      phil 	/* Read all lastlog entries, looking for active ones */
    196   1.1      phil 	else {
    197   1.9  christos 		for (i = 0; fread(&l, sizeof(l), 1, fp) == 1; i++) {
    198   1.9  christos 			if (l.ll_time == 0)
    199   1.2  christos 				continue;
    200  1.13    atatat 			if ((passwd = getpwuid(i)) == NULL) {
    201  1.13    atatat 				static struct passwd p;
    202  1.13    atatat 				static char n[32];
    203  1.13    atatat 				snprintf(n, sizeof(n), "(%d)", i);
    204  1.13    atatat 				p.pw_uid = i;
    205  1.13    atatat 				p.pw_name = n;
    206  1.13    atatat 				passwd = &p;
    207  1.13    atatat 			}
    208  1.13    atatat 			process_entry(passwd, &l);
    209   1.1      phil 		}
    210   1.1      phil 		if (ferror(fp))
    211   1.1      phil 			warnx("fread error");
    212   1.1      phil 	}
    213   1.1      phil 
    214   1.9  christos 	(void)fclose(fp);
    215   1.9  christos }
    216   1.9  christos 
    217   1.9  christos static void
    218   1.9  christos process_entry(struct passwd *p, struct lastlog *l)
    219   1.9  christos {
    220   1.9  christos 	struct output	o;
    221   1.9  christos 
    222   1.9  christos 	(void)strlcpy(o.o_name, p->pw_name, sizeof(o.o_name));
    223   1.9  christos 	(void)strlcpy(o.o_line, l->ll_line, sizeof(l->ll_line));
    224   1.9  christos 	(void)strlcpy(o.o_host, l->ll_host, sizeof(l->ll_host));
    225   1.9  christos 	o.o_tv.tv_sec = l->ll_time;
    226   1.9  christos 	o.o_tv.tv_usec = 0;
    227   1.9  christos 	(void)memset(&o.o_ss, 0, sizeof(o.o_ss));
    228   1.9  christos 	o.next = NULL;
    229   1.1      phil 
    230   1.9  christos 	/*
    231   1.9  christos 	 * If we are sorting it, we need all the entries in memory so
    232   1.9  christos 	 * push the current entry onto a stack.  Otherwise, we can just
    233   1.9  christos 	 * output it.
    234   1.9  christos 	 */
    235   1.7     elric 	if (DOSORT(sortlog))
    236   1.9  christos 		push(&o);
    237   1.9  christos 	else
    238   1.9  christos 		output(&o);
    239   1.9  christos }
    240   1.9  christos #endif
    241   1.9  christos 
    242   1.9  christos #ifdef SUPPORT_UTMPX
    243   1.9  christos static void
    244   1.9  christos dolastlogx(const char *logfile, int argc, char **argv)
    245   1.9  christos {
    246   1.9  christos 	int i = 0;
    247   1.9  christos 	DB *db = dbopen(logfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
    248   1.9  christos 	DBT key, data;
    249   1.9  christos 	struct lastlogx l;
    250   1.9  christos 	struct passwd	*passwd;
    251   1.9  christos 
    252   1.9  christos 	if (db == NULL)
    253   1.9  christos 		err(1, "%s", logfile);
    254   1.9  christos 
    255   1.9  christos 	if (argc > 0) {
    256   1.9  christos 		for (i = 0; i < argc; i++) {
    257   1.9  christos 			if ((passwd = getpwnam(argv[i])) == NULL) {
    258   1.9  christos 				warnx("User `%s' not found", argv[i]);
    259   1.9  christos 				continue;
    260   1.9  christos 			}
    261   1.9  christos 			key.data = &passwd->pw_uid;
    262   1.9  christos 			key.size = sizeof(passwd->pw_uid);
    263   1.9  christos 
    264   1.9  christos 			switch ((*db->get)(db, &key, &data, 0)) {
    265   1.9  christos 			case 0:
    266   1.9  christos 				break;
    267   1.9  christos 			case 1:
    268   1.9  christos 				warnx("User `%s' not found", passwd->pw_name);
    269   1.9  christos 				continue;
    270   1.9  christos 			case -1:
    271   1.9  christos 				warn("Error looking up `%s'", passwd->pw_name);
    272   1.9  christos 				continue;
    273   1.9  christos 			default:
    274   1.9  christos 				abort();
    275   1.9  christos 			}
    276   1.9  christos 
    277   1.9  christos 			if (data.size != sizeof(l)) {
    278   1.9  christos 				errno = EFTYPE;
    279   1.9  christos 				err(1, "%s", logfile);
    280   1.9  christos 			}
    281   1.9  christos 			(void)memcpy(&l, data.data, sizeof(l));
    282   1.9  christos 
    283   1.9  christos 			process_entryx(passwd, &l);
    284   1.9  christos 		}
    285   1.9  christos 	}
    286   1.9  christos 	/* Read all lastlog entries, looking for active ones */
    287   1.9  christos 	else {
    288   1.9  christos 		switch ((*db->seq)(db, &key, &data, R_FIRST)) {
    289   1.9  christos 		case 0:
    290   1.9  christos 			break;
    291   1.9  christos 		case 1:
    292   1.9  christos 			warnx("No entries found");
    293   1.9  christos 			(*db->close)(db);
    294   1.9  christos 			return;
    295   1.9  christos 		case -1:
    296   1.9  christos 			warn("Error seeking to first entry");
    297   1.9  christos 			(*db->close)(db);
    298   1.9  christos 			return;
    299   1.9  christos 		default:
    300   1.9  christos 			abort();
    301   1.9  christos 		}
    302   1.9  christos 
    303   1.9  christos 		do {
    304   1.9  christos 			uid_t uid;
    305   1.9  christos 
    306   1.9  christos 			if (key.size != sizeof(uid) || data.size != sizeof(l)) {
    307   1.9  christos 				errno = EFTYPE;
    308   1.9  christos 				err(1, "%s", logfile);
    309   1.9  christos 			}
    310   1.9  christos 			(void)memcpy(&uid, key.data, sizeof(uid));
    311   1.9  christos 
    312   1.9  christos 			if ((passwd = getpwuid(uid)) == NULL) {
    313   1.9  christos 				warnx("Cannot find user for uid %lu",
    314   1.9  christos 				    (unsigned long)uid);
    315   1.9  christos 				continue;
    316   1.9  christos 			}
    317   1.9  christos 			(void)memcpy(&l, data.data, sizeof(l));
    318   1.9  christos 			process_entryx(passwd, &l);
    319   1.9  christos 		} while ((i = (*db->seq)(db, &key, &data, R_NEXT)) == 0);
    320   1.9  christos 
    321   1.9  christos 		switch (i) {
    322   1.9  christos 		case 1:
    323   1.9  christos 			break;
    324   1.9  christos 		case -1:
    325   1.9  christos 			warn("Error seeking to last entry");
    326   1.9  christos 			break;
    327   1.9  christos 		case 0:
    328   1.9  christos 		default:
    329   1.9  christos 			abort();
    330   1.9  christos 		}
    331   1.9  christos 	}
    332   1.7     elric 
    333   1.9  christos 	(*db->close)(db);
    334   1.1      phil }
    335   1.1      phil 
    336   1.7     elric static void
    337   1.9  christos process_entryx(struct passwd *p, struct lastlogx *l)
    338   1.7     elric {
    339   1.7     elric 	struct output	o;
    340   1.7     elric 
    341  1.13    atatat 	if (numeric > 1)
    342  1.13    atatat 		(void)snprintf(o.o_name, sizeof(o.o_name), "%d", p->pw_uid);
    343  1.13    atatat 	else
    344  1.13    atatat 		(void)strlcpy(o.o_name, p->pw_name, sizeof(o.o_name));
    345   1.9  christos 	(void)strlcpy(o.o_line, l->ll_line, sizeof(l->ll_line));
    346   1.9  christos 	(void)strlcpy(o.o_host, l->ll_host, sizeof(l->ll_host));
    347   1.9  christos 	(void)memcpy(&o.o_ss, &l->ll_ss, sizeof(o.o_ss));
    348   1.9  christos 	o.o_tv = l->ll_tv;
    349   1.7     elric 	o.next = NULL;
    350   1.7     elric 
    351   1.7     elric 	/*
    352   1.7     elric 	 * If we are sorting it, we need all the entries in memory so
    353   1.7     elric 	 * push the current entry onto a stack.  Otherwise, we can just
    354   1.7     elric 	 * output it.
    355   1.7     elric 	 */
    356   1.7     elric 	if (DOSORT(sortlog))
    357   1.7     elric 		push(&o);
    358   1.7     elric 	else
    359   1.7     elric 		output(&o);
    360   1.7     elric }
    361   1.9  christos #endif
    362   1.7     elric 
    363   1.7     elric static void
    364   1.7     elric push(struct output *o)
    365   1.7     elric {
    366   1.7     elric 	struct output	*out;
    367   1.7     elric 
    368   1.7     elric 	out = malloc(sizeof(*out));
    369   1.7     elric 	if (!out)
    370   1.7     elric 		err(EXIT_FAILURE, "malloc failed");
    371   1.9  christos 	(void)memcpy(out, o, sizeof(*out));
    372   1.7     elric 	out->next = NULL;
    373   1.7     elric 
    374   1.7     elric 	if (outstack) {
    375   1.7     elric 		out->next = outstack;
    376   1.7     elric 		outstack = out;
    377   1.7     elric 	} else {
    378   1.7     elric 		outstack = out;
    379   1.7     elric 	}
    380   1.7     elric }
    381   1.7     elric 
    382   1.7     elric static void
    383   1.7     elric sortoutput(struct output *o)
    384   1.7     elric {
    385   1.7     elric 	struct	output **outs;
    386   1.7     elric 	struct	output *tmpo;
    387   1.7     elric 	int	num;
    388   1.7     elric 	int	i;
    389   1.7     elric 
    390   1.7     elric 	/* count the number of entries to display */
    391   1.7     elric 	for (num=0, tmpo = o; tmpo; tmpo=tmpo->next, num++)
    392   1.7     elric 		;
    393   1.7     elric 
    394   1.7     elric 	outs = malloc(sizeof(*outs) * num);
    395   1.7     elric 	if (!outs)
    396   1.7     elric 		err(EXIT_FAILURE, "malloc failed");
    397   1.7     elric 	for (i=0, tmpo = o; i < num; tmpo=tmpo->next, i++)
    398   1.7     elric 		outs[i] = tmpo;
    399   1.7     elric 
    400   1.7     elric 	mergesort(outs, num, sizeof(*outs), comparelog);
    401   1.7     elric 
    402   1.7     elric 	for (i=0; i < num; i++)
    403   1.7     elric 		output(outs[i]);
    404   1.7     elric }
    405   1.7     elric 
    406   1.7     elric static int
    407   1.7     elric comparelog(const void *left, const void *right)
    408   1.7     elric {
    409  1.14     lukem 	const struct output *l = *(const struct output * const *)left;
    410  1.14     lukem 	const struct output *r = *(const struct output * const *)right;
    411   1.7     elric 	int order = (sortlog&SORT_REVERSE)?-1:1;
    412   1.7     elric 
    413   1.9  christos 	if (l->o_tv.tv_sec < r->o_tv.tv_sec)
    414   1.7     elric 		return 1 * order;
    415   1.9  christos 	if (l->o_tv.tv_sec == r->o_tv.tv_sec)
    416   1.7     elric 		return 0;
    417   1.7     elric 	return -1 * order;
    418   1.7     elric }
    419   1.7     elric 
    420   1.9  christos static const char *
    421   1.9  christos gethost(struct output *o)
    422   1.9  christos {
    423   1.9  christos 	if (!numeric)
    424   1.9  christos 		return o->o_host;
    425   1.9  christos 	else {
    426   1.9  christos 		static char buf[512];
    427  1.11  christos 		buf[0] = '\0';
    428  1.11  christos 		(void)sockaddr_snprintf(buf, sizeof(buf), "%a",
    429  1.11  christos 		    (struct sockaddr *)&o->o_ss);
    430   1.9  christos 		return buf;
    431   1.9  christos 	}
    432   1.9  christos }
    433   1.9  christos 
    434   1.1      phil /* Duplicate the output of last(1) */
    435   1.1      phil static void
    436   1.7     elric output(struct output *o)
    437   1.1      phil {
    438   1.9  christos 	time_t t = (time_t)o->o_tv.tv_sec;
    439   1.1      phil 	printf("%-*.*s  %-*.*s %-*.*s   %s",
    440  1.10        he 		(int)namelen, (int)namelen, o->o_name,
    441  1.10        he 		(int)linelen, (int)linelen, o->o_line,
    442  1.10        he 		(int)hostlen, (int)hostlen, gethost(o),
    443   1.9  christos 		t ? ctime(&t) : "Never logged in\n");
    444   1.1      phil }
    445   1.1      phil 
    446   1.1      phil static void
    447   1.9  christos usage(void)
    448   1.1      phil {
    449   1.9  christos 	(void)fprintf(stderr, "Usage: %s [-nrt] [-f <filename>] "
    450   1.9  christos 	    "[-H <hostlen>] [-L <linelen>] [-N <namelen>] [user ...]\n",
    451   1.9  christos 	    getprogname());
    452   1.1      phil 	exit(1);
    453   1.1      phil }
    454