Home | History | Annotate | Line # | Download | only in lastlogin
lastlogin.c revision 1.7
      1  1.7     elric /*	$NetBSD: lastlogin.c,v 1.7 2003/08/28 15:54:41 elric 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.7     elric __RCSID("$NetBSD: lastlogin.c,v 1.7 2003/08/28 15:54:41 elric 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.1      phil #include <errno.h>
     42  1.1      phil #include <pwd.h>
     43  1.1      phil #include <stdio.h>
     44  1.5      matt #include <stdlib.h>
     45  1.7     elric #include <string.h>
     46  1.3     lukem #include <time.h>
     47  1.3     lukem #include <utmp.h>
     48  1.4     perry #include <unistd.h>
     49  1.1      phil 
     50  1.7     elric struct output {
     51  1.7     elric 	char		 o_name[UT_NAMESIZE];
     52  1.7     elric 	char		 o_line[UT_LINESIZE];
     53  1.7     elric 	char		 o_host[UT_HOSTSIZE];
     54  1.7     elric 	time_t		 o_time;
     55  1.7     elric 	struct output	*next;
     56  1.7     elric };
     57  1.7     elric 
     58  1.1      phil static	char *logfile = _PATH_LASTLOG;
     59  1.1      phil 
     60  1.7     elric #define SORT_NONE	0x0000
     61  1.7     elric #define SORT_REVERSE	0x0001
     62  1.7     elric #define SORT_TIME	0x0002
     63  1.7     elric #define DOSORT(x)	((x) & (SORT_TIME))
     64  1.7     elric static	int sortlog = SORT_NONE;
     65  1.7     elric static	struct output *outstack = NULL;
     66  1.7     elric 
     67  1.3     lukem 	int	main __P((int, char **));
     68  1.7     elric static	int	comparelog __P((const void *, const void *));
     69  1.7     elric static	void	output __P((struct output *));
     70  1.7     elric static	void	process_entry __P((struct passwd *, struct lastlog *));
     71  1.7     elric static	void	push __P((struct output *));
     72  1.7     elric static	void	sortoutput __P((struct output *));
     73  1.3     lukem static	void	usage __P((void));
     74  1.1      phil 
     75  1.1      phil int
     76  1.1      phil main(argc, argv)
     77  1.1      phil 	int argc;
     78  1.1      phil 	char *argv[];
     79  1.1      phil {
     80  1.1      phil 	int	ch, i;
     81  1.1      phil 	FILE	*fp;
     82  1.1      phil 	struct passwd	*passwd;
     83  1.1      phil 	struct lastlog	last;
     84  1.1      phil 
     85  1.7     elric 	while ((ch = getopt(argc, argv, "rt")) != -1) {
     86  1.7     elric 		switch (ch) {
     87  1.7     elric 		case 'r':
     88  1.7     elric 			sortlog |= SORT_REVERSE;
     89  1.7     elric 			break;
     90  1.7     elric 		case 't':
     91  1.7     elric 			sortlog |= SORT_TIME;
     92  1.7     elric 			break;
     93  1.7     elric 		default:
     94  1.7     elric 			usage();
     95  1.7     elric 		}
     96  1.1      phil 	}
     97  1.7     elric 	argc -= optind;
     98  1.7     elric 	argv += optind;
     99  1.1      phil 
    100  1.1      phil 	fp = fopen(logfile, "r");
    101  1.1      phil 	if (fp == NULL)
    102  1.1      phil 		err(1, "%s", logfile);
    103  1.1      phil 
    104  1.1      phil 	setpassent(1);	/* Keep passwd file pointers open */
    105  1.1      phil 
    106  1.1      phil 	/* Process usernames given on the command line. */
    107  1.1      phil 	if (argc > 1) {
    108  1.1      phil 		long offset;
    109  1.1      phil 		for (i = 1; i < argc; ++i) {
    110  1.1      phil 			if ((passwd = getpwnam(argv[i])) == NULL) {
    111  1.1      phil 				warnx("user '%s' not found", argv[i]);
    112  1.1      phil 				continue;
    113  1.1      phil 			}
    114  1.1      phil 			/* Calculate the offset into the lastlog file. */
    115  1.1      phil 			offset = (long)(passwd->pw_uid * sizeof(last));
    116  1.1      phil 			if (fseek(fp, offset, SEEK_SET)) {
    117  1.1      phil 				warn("fseek error");
    118  1.1      phil 				continue;
    119  1.1      phil 			}
    120  1.1      phil 			if (fread(&last, sizeof(last), 1, fp) != 1) {
    121  1.1      phil 				warnx("fread error on '%s'", passwd->pw_name);
    122  1.1      phil 				clearerr(fp);
    123  1.1      phil 				continue;
    124  1.1      phil 			}
    125  1.7     elric 			process_entry(passwd, &last);
    126  1.1      phil 		}
    127  1.1      phil 	}
    128  1.1      phil 	/* Read all lastlog entries, looking for active ones */
    129  1.1      phil 	else {
    130  1.2  christos 		for (i = 0; fread(&last, sizeof(last), 1, fp) == 1; i++) {
    131  1.2  christos 			if (last.ll_time == 0)
    132  1.2  christos 				continue;
    133  1.2  christos 			if ((passwd = getpwuid(i)) != NULL)
    134  1.7     elric 				process_entry(passwd, &last);
    135  1.1      phil 		}
    136  1.1      phil 		if (ferror(fp))
    137  1.1      phil 			warnx("fread error");
    138  1.1      phil 	}
    139  1.1      phil 
    140  1.1      phil 	setpassent(0);	/* Close passwd file pointers */
    141  1.1      phil 
    142  1.7     elric 	if (DOSORT(sortlog))
    143  1.7     elric 		sortoutput(outstack);
    144  1.7     elric 
    145  1.1      phil 	fclose(fp);
    146  1.1      phil 	exit(0);
    147  1.1      phil }
    148  1.1      phil 
    149  1.7     elric static void
    150  1.7     elric process_entry(struct passwd *p, struct lastlog *l)
    151  1.7     elric {
    152  1.7     elric 	struct output	o;
    153  1.7     elric 
    154  1.7     elric 	strncpy(o.o_name, p->pw_name, UT_NAMESIZE);
    155  1.7     elric 	strncpy(o.o_line, l->ll_line, UT_LINESIZE);
    156  1.7     elric 	strncpy(o.o_host, l->ll_host, UT_HOSTSIZE);
    157  1.7     elric 	o.o_time = l->ll_time;
    158  1.7     elric 	o.next = NULL;
    159  1.7     elric 
    160  1.7     elric 	/*
    161  1.7     elric 	 * If we are sorting it, we need all the entries in memory so
    162  1.7     elric 	 * push the current entry onto a stack.  Otherwise, we can just
    163  1.7     elric 	 * output it.
    164  1.7     elric 	 */
    165  1.7     elric 	if (DOSORT(sortlog))
    166  1.7     elric 		push(&o);
    167  1.7     elric 	else
    168  1.7     elric 		output(&o);
    169  1.7     elric }
    170  1.7     elric 
    171  1.7     elric static void
    172  1.7     elric push(struct output *o)
    173  1.7     elric {
    174  1.7     elric 	struct output	*out;
    175  1.7     elric 
    176  1.7     elric 	out = malloc(sizeof(*out));
    177  1.7     elric 	if (!out)
    178  1.7     elric 		err(EXIT_FAILURE, "malloc failed");
    179  1.7     elric 	memcpy(out, o, sizeof(*out));
    180  1.7     elric 	out->next = NULL;
    181  1.7     elric 
    182  1.7     elric 	if (outstack) {
    183  1.7     elric 		out->next = outstack;
    184  1.7     elric 		outstack = out;
    185  1.7     elric 	} else {
    186  1.7     elric 		outstack = out;
    187  1.7     elric 	}
    188  1.7     elric }
    189  1.7     elric 
    190  1.7     elric static void
    191  1.7     elric sortoutput(struct output *o)
    192  1.7     elric {
    193  1.7     elric 	struct	output **outs;
    194  1.7     elric 	struct	output *tmpo;
    195  1.7     elric 	int	num;
    196  1.7     elric 	int	i;
    197  1.7     elric 
    198  1.7     elric 	/* count the number of entries to display */
    199  1.7     elric 	for (num=0, tmpo = o; tmpo; tmpo=tmpo->next, num++)
    200  1.7     elric 		;
    201  1.7     elric 
    202  1.7     elric 	outs = malloc(sizeof(*outs) * num);
    203  1.7     elric 	if (!outs)
    204  1.7     elric 		err(EXIT_FAILURE, "malloc failed");
    205  1.7     elric 	for (i=0, tmpo = o; i < num; tmpo=tmpo->next, i++)
    206  1.7     elric 		outs[i] = tmpo;
    207  1.7     elric 
    208  1.7     elric 	mergesort(outs, num, sizeof(*outs), comparelog);
    209  1.7     elric 
    210  1.7     elric 	for (i=0; i < num; i++)
    211  1.7     elric 		output(outs[i]);
    212  1.7     elric }
    213  1.7     elric 
    214  1.7     elric static int
    215  1.7     elric comparelog(const void *left, const void *right)
    216  1.7     elric {
    217  1.7     elric 	struct output *l = *(struct output **)left;
    218  1.7     elric 	struct output *r = *(struct output **)right;
    219  1.7     elric 	int order = (sortlog&SORT_REVERSE)?-1:1;
    220  1.7     elric 
    221  1.7     elric 	if (l->o_time < r->o_time)
    222  1.7     elric 		return 1 * order;
    223  1.7     elric 	if (l->o_time == r->o_time)
    224  1.7     elric 		return 0;
    225  1.7     elric 	return -1 * order;
    226  1.7     elric }
    227  1.7     elric 
    228  1.1      phil /* Duplicate the output of last(1) */
    229  1.1      phil static void
    230  1.7     elric output(struct output *o)
    231  1.1      phil {
    232  1.7     elric 
    233  1.1      phil 	printf("%-*.*s  %-*.*s %-*.*s   %s",
    234  1.7     elric 		UT_NAMESIZE, UT_NAMESIZE, o->o_name,
    235  1.7     elric 		UT_LINESIZE, UT_LINESIZE, o->o_line,
    236  1.7     elric 		UT_HOSTSIZE, UT_HOSTSIZE, o->o_host,
    237  1.7     elric 		(o->o_time) ? ctime(&(o->o_time)) : "Never logged in\n");
    238  1.1      phil }
    239  1.1      phil 
    240  1.1      phil static void
    241  1.1      phil usage()
    242  1.1      phil {
    243  1.7     elric 	fprintf(stderr, "usage: %s [-rt] [user ...]\n", getprogname());
    244  1.1      phil 	exit(1);
    245  1.1      phil }
    246