Subject: bin/1932: lastlogin accounting tool
To: None <gnats-bugs@gnats.netbsd.org>
From: John M Vinopal <banshee@gabriella.resort.com>
List: netbsd-bugs
Date: 01/11/1996 18:34:18
>Number:         1932
>Category:       bin
>Synopsis:       lastlogin program and man page
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Jan 11 21:50:07 1996
>Last-Modified:
>Originator:     John M Vinopal
>Organization:
>Release:        1.1
>Environment:
System: NetBSD gabriella.resort.com 1.1A NetBSD 1.1A (GABRIELLA-NCR) #2: Thu Jan 11 02:02:54 PST 1996 banshee@gabriella.resort.com:/usr/local/NetBSD/src/sys/arch/i386/compile/GABRIELLA-NCR i386


>Description:
	Contains a shar file with the lastlogin program suitable for
	examining the /var/log/lastlog file.
>How-To-Repeat:
>Fix:

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	lastlogin.c
#	lastlogin.8
#
echo x - lastlogin.c
sed 's/^X//' >lastlogin.c << 'END-of-lastlogin.c'
X/*
X * Copyright (c) 1996 John M. Vinopal
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X *    must display the following acknowledgement:
X *	This product includes software developed for the NetBSD Project
X *	by John M. Vinopal.
X * 4. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
X * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
X * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
X * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
X * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
X * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X */
X
X#include <sys/types.h>
X#include <utmp.h>
X#include <errno.h>
X#include <err.h>
X#include <pwd.h>
X#include <time.h>
X#include <stdio.h>
X#include <stdlib.h>
X
Xextern	char *__progname;
Xstatic	char *logfile = _PATH_LASTLOG;
X
Xstatic void output __P((struct passwd *, struct lastlog *));
Xstatic void usage __P((void));
X
Xint
Xmain(argc, argv)
X	int argc;
X	char *argv[];
X{
X	int	ch, i;
X	FILE	*fp;
X	struct passwd	*passwd;
X	struct lastlog	last;
X
X	while ((ch = getopt(argc, argv, "")) != -1) {
X		usage();
X	}
X
X	fp = fopen(logfile, "r");
X	if (fp == NULL)
X		err(1, "%s", logfile);
X
X	setpassent(1);	/* Keep passwd file pointers open */
X
X	/* Process usernames given on the command line. */
X	if (argc > 1) {
X		long offset;
X		for (i = 1; i < argc; ++i) {
X			if ((passwd = getpwnam(argv[i])) == NULL) {
X				warnx("user '%s' not found", argv[i]);
X				continue;
X			}
X			/* Calculate the offset into the lastlog file. */
X			offset = (long)(passwd->pw_uid * sizeof(last));
X			if (fseek(fp, offset, SEEK_SET)) {
X				warn("fseek error");
X				continue;
X			}
X			if (fread(&last, sizeof(last), 1, fp) != 1) {
X				warnx("fread error on '%s'", passwd->pw_name);
X				clearerr(fp);
X				continue;
X			}
X			output(passwd, &last);
X		}
X	}
X	/* Read all lastlog entries, looking for active ones */
X	else {
X		i = 0;
X		while (fread(&last, sizeof(last), 1, fp) == 1) {
X			passwd = getpwuid(i);
X			if (passwd != NULL && (last.ll_time != 0))
X				output(passwd, &last);
X			i++;
X		}
X		if (ferror(fp))
X			warnx("fread error");
X	}
X
X	setpassent(0);	/* Close passwd file pointers */
X
X	fclose(fp);
X	exit(0);
X}
X
X/* Duplicate the output of last(1) */
Xstatic void
Xoutput(p, l)
X	struct passwd *p;
X	struct lastlog *l;
X{
X	printf("%-*.*s  %-*.*s %-*.*s   %s",
X		UT_NAMESIZE, UT_NAMESIZE, p->pw_name,
X		UT_LINESIZE, UT_LINESIZE, l->ll_line,
X		UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host,
X		(l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n");
X}
X
Xstatic void
Xusage()
X{
X	fprintf(stderr, "usage: %s [user ...]\n", __progname);
X	exit(1);
X}
END-of-lastlogin.c
echo x - lastlogin.8
sed 's/^X//' >lastlogin.8 << 'END-of-lastlogin.8'
X.\"/*
X.\" * Copyright (c) 1996 John M. Vinopal
X.\" * All rights reserved.
X.\" *
X.\" * Redistribution and use in source and binary forms, with or without
X.\" * modification, are permitted provided that the following conditions
X.\" * are met:
X.\" * 1. Redistributions of source code must retain the above copyright
X.\" *    notice, this list of conditions and the following disclaimer.
X.\" * 2. Redistributions in binary form must reproduce the above copyright
X.\" *    notice, this list of conditions and the following disclaimer in the
X.\" *    documentation and/or other materials provided with the distribution.
X.\" * 3. All advertising materials mentioning features or use of this software
X.\" *    must display the following acknowledgement:
X.\" *	This product includes software developed for the NetBSD Project
X.\" *	by John M. Vinopal.
X.\" * 4. The name of the author may not be used to endorse or promote products
X.\" *    derived from this software without specific prior written permission.
X.\" *
X.\" * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X.\" * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X.\" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X.\" * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
X.\" * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
X.\" * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
X.\" * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
X.\" * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
X.\" * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X.\" * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X.\" * SUCH DAMAGE.
X.\" */
X.Dd January 11, 1996
X.Dt LASTLOGIN 8
X.Os NetBSD 1.1
X.Sh NAME
X.Nm lastlogin
X.Nd indicate last login time of users
X.Sh SYNOPSIS
X.Nm lastlogin
X.Op user ...
X.Sh DESCRIPTION
X.Nm Lastlogin
Xwill list the last login session of specified
X.Ar users ,
Xor for all users by default.  Each line of output contains
Xthe user name, the tty from which the session was conducted, any
Xhostname, and the start time for the session. 
X.Pp
XIf multiple
X.Ar users 
Xare given, the session information for each user is printed in
Xthe order given on the command line.  Otherwise, information 
Xfor all users is printed, sorted by uid.
X.Pp
X.Nm Lastlogin
Xdiffers from 
X.Nm last
Xin that it only prints information regarding the very last login session.
XThe last login database is never turned over or deleted in standard usage.
X.Sh FILES
X.Bl -tag -width /var/log/lastlog -compact
X.It Pa /var/log/lastlog
Xlast login database
X.El
X.Sh SEE ALSO
X.Xr last 1 ,
X.Xr lastlog 5 ,
X.Xr ac 8
END-of-lastlogin.8
exit

>Audit-Trail:
>Unformatted: