lastlogin.c revision 78719
168723Sru/* 268723Sru * Copyright (c) 1996 John M. Vinopal 368723Sru * All rights reserved. 468723Sru * 568723Sru * Redistribution and use in source and binary forms, with or without 668723Sru * modification, are permitted provided that the following conditions 768723Sru * are met: 868723Sru * 1. Redistributions of source code must retain the above copyright 968723Sru * notice, this list of conditions and the following disclaimer. 1068723Sru * 2. Redistributions in binary form must reproduce the above copyright 1168723Sru * notice, this list of conditions and the following disclaimer in the 1268723Sru * documentation and/or other materials provided with the distribution. 1368723Sru * 3. All advertising materials mentioning features or use of this software 1468723Sru * must display the following acknowledgement: 1568723Sru * This product includes software developed for the NetBSD Project 1668723Sru * by John M. Vinopal. 1768723Sru * 4. The name of the author may not be used to endorse or promote products 1868723Sru * derived from this software without specific prior written permission. 1968723Sru * 2068723Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2168723Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2268723Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2368723Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2468723Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2568723Sru * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2668723Sru * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2768723Sru * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2868723Sru * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2968723Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3068723Sru * SUCH DAMAGE. 3168723Sru */ 3268723Sru 3368723Sru#include <sys/cdefs.h> 3468723Sru#ifndef lint 3568723Sru__RCSID("$FreeBSD: head/usr.sbin/lastlogin/lastlogin.c 78719 2001-06-24 20:05:30Z dd $"); 3668723Sru__RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $"); 3768723Sru#endif 3868723Sru 3968723Sru#include <err.h> 4068723Sru#include <pwd.h> 4168723Sru#include <stdio.h> 4278719Sdd#include <stdlib.h> 4368723Sru#include <time.h> 4468723Sru#include <utmp.h> 4568723Sru#include <unistd.h> 4668723Sru 4768723Srustatic const char *logfile = _PATH_LASTLOG; 4868723Sru 4968723Sru int main __P((int, char **)); 5068723Srustatic void output __P((struct passwd *, struct lastlog *)); 5168723Srustatic void usage __P((void)); 5268723Sru 5368723Sruint 5468723Srumain(argc, argv) 5568723Sru int argc; 5668723Sru char *argv[]; 5768723Sru{ 5868723Sru int ch, i; 5968723Sru FILE *fp; 6068723Sru struct passwd *passwd; 6168723Sru struct lastlog last; 6268723Sru 6368723Sru while ((ch = getopt(argc, argv, "")) != -1) { 6468723Sru usage(); 6568723Sru } 6668723Sru 6768723Sru fp = fopen(logfile, "r"); 6868723Sru if (fp == NULL) 6968723Sru err(1, "%s", logfile); 7068723Sru 7168723Sru setpassent(1); /* Keep passwd file pointers open */ 7268723Sru 7368723Sru /* Process usernames given on the command line. */ 7468723Sru if (argc > 1) { 7568723Sru long offset; 7668723Sru for (i = 1; i < argc; ++i) { 7768723Sru if ((passwd = getpwnam(argv[i])) == NULL) { 7868723Sru warnx("user '%s' not found", argv[i]); 7968723Sru continue; 8068723Sru } 8168723Sru /* Calculate the offset into the lastlog file. */ 8268723Sru offset = (long)(passwd->pw_uid * sizeof(last)); 8368723Sru if (fseek(fp, offset, SEEK_SET)) { 8468723Sru warn("fseek error"); 8568723Sru continue; 8668723Sru } 8768723Sru if (fread(&last, sizeof(last), 1, fp) != 1) { 8868723Sru warnx("fread error on '%s'", passwd->pw_name); 8968723Sru clearerr(fp); 9068723Sru continue; 9168723Sru } 9268723Sru output(passwd, &last); 9368723Sru } 9468723Sru } 9568723Sru /* Read all lastlog entries, looking for active ones */ 9668723Sru else { 9768723Sru for (i = 0; fread(&last, sizeof(last), 1, fp) == 1; i++) { 9868723Sru if (last.ll_time == 0) 9968723Sru continue; 10068723Sru if ((passwd = getpwuid((uid_t)i)) != NULL) 10168723Sru output(passwd, &last); 10268723Sru } 10368723Sru if (ferror(fp)) 10468723Sru warnx("fread error"); 10568723Sru } 10668723Sru 10768723Sru setpassent(0); /* Close passwd file pointers */ 10868723Sru 10968723Sru fclose(fp); 11068723Sru exit(0); 11168723Sru} 11268723Sru 11368723Sru/* Duplicate the output of last(1) */ 11468723Srustatic void 11568723Sruoutput(p, l) 11668723Sru struct passwd *p; 11768723Sru struct lastlog *l; 11868723Sru{ 11968723Sru printf("%-*.*s %-*.*s %-*.*s %s", 12068723Sru UT_NAMESIZE, UT_NAMESIZE, p->pw_name, 12168723Sru UT_LINESIZE, UT_LINESIZE, l->ll_line, 12268723Sru UT_HOSTSIZE, UT_HOSTSIZE, l->ll_host, 12368723Sru (l->ll_time) ? ctime(&(l->ll_time)) : "Never logged in\n"); 12468723Sru} 12568723Sru 12668723Srustatic void 12768723Sruusage() 12868723Sru{ 12969828Scharnier fprintf(stderr, "usage: lastlogin [user ...]\n"); 13068723Sru exit(1); 13168723Sru} 132