2007-10-07 11:44:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Julianne F. Haugh nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2007-10-07 11:44:59 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
|
2007-10-07 11:44:02 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "rcsid.h"
|
2007-10-07 11:45:23 +00:00
|
|
|
RCSID (PKG_VER "$Id: lastlog.c,v 1.13 2003/12/17 12:52:25 kloczek Exp $")
|
2007-10-07 11:44:02 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#if HAVE_LASTLOG_H
|
|
|
|
#include <lastlog.h>
|
|
|
|
#else
|
|
|
|
#include "lastlog_.h"
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Needed for MkLinux DR1/2/2.1 - J.
|
|
|
|
*/
|
|
|
|
#ifndef LASTLOG_FILE
|
|
|
|
#define LASTLOG_FILE "/var/log/lastlog"
|
|
|
|
#endif
|
|
|
|
static FILE *lastlogfile; /* lastlog file stream */
|
2007-10-07 11:44:59 +00:00
|
|
|
static off_t user; /* one single user, specified on command line */
|
|
|
|
static int days; /* number of days to consider for print command */
|
2007-10-07 11:44:02 +00:00
|
|
|
static time_t seconds; /* that number of days in seconds */
|
|
|
|
|
|
|
|
static int uflg = 0; /* set if user is a valid user id */
|
|
|
|
static int tflg = 0; /* print is restricted to most recent days */
|
|
|
|
static struct lastlog lastlog; /* scratch structure to play with ... */
|
|
|
|
static struct stat statbuf; /* fstat buffer for file size */
|
|
|
|
static struct passwd *pwent;
|
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
static struct option const longopts[] = {
|
|
|
|
{"user", required_argument, 0, 'u'},
|
|
|
|
{"time", required_argument, 0, 't'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
2007-10-07 11:44:51 +00:00
|
|
|
};
|
|
|
|
|
2007-10-07 11:44:02 +00:00
|
|
|
#define NOW (time ((time_t *) 0))
|
|
|
|
|
|
|
|
/* local function prototypes */
|
2007-10-07 11:44:59 +00:00
|
|
|
static void print (void);
|
|
|
|
static void print_one (const struct passwd *);
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
int main (int argc, char **argv)
|
2007-10-07 11:44:02 +00:00
|
|
|
{
|
2007-10-07 11:44:59 +00:00
|
|
|
int c;
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
if ((lastlogfile = fopen (LASTLOG_FILE, "r")) == (FILE *) 0) {
|
2007-10-07 11:44:02 +00:00
|
|
|
perror (LASTLOG_FILE);
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 11:44:59 +00:00
|
|
|
while ((c =
|
|
|
|
getopt_long (argc, argv, "u:t:h", longopts, NULL)) != -1) {
|
2007-10-07 11:44:02 +00:00
|
|
|
switch (c) {
|
2007-10-07 11:44:59 +00:00
|
|
|
case 'u':
|
|
|
|
pwent = getpwnam (optarg);
|
|
|
|
if (!pwent) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("Unknown User: %s\n"), optarg);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
uflg++;
|
|
|
|
user = pwent->pw_uid;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
days = atoi (optarg);
|
|
|
|
seconds = days * DAY;
|
|
|
|
tflg++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
fprintf (stdout,
|
|
|
|
_
|
2007-10-07 11:45:23 +00:00
|
|
|
("Usage: %s [{-u|--login} login] [{-t|--time} days] [{-h|--help}]\n"),
|
2007-10-07 11:44:59 +00:00
|
|
|
argv[0]);
|
|
|
|
exit (0);
|
|
|
|
default:
|
|
|
|
fprintf (stdout,
|
|
|
|
_
|
2007-10-07 11:45:23 +00:00
|
|
|
("Usage: %s [{-u|--login} login] [{-t|--time} days] [{-h|--help}]\n"),
|
2007-10-07 11:44:59 +00:00
|
|
|
argv[0]);
|
|
|
|
exit (1);
|
2007-10-07 11:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
print ();
|
|
|
|
fclose (lastlogfile);
|
|
|
|
exit (0);
|
2007-10-07 11:44:59 +00:00
|
|
|
/*NOTREACHED*/}
|
2007-10-07 11:44:02 +00:00
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
static void print (void)
|
2007-10-07 11:44:02 +00:00
|
|
|
{
|
2007-10-07 11:44:59 +00:00
|
|
|
off_t offset;
|
2007-10-07 11:44:02 +00:00
|
|
|
|
|
|
|
if (uflg) {
|
2007-10-07 11:44:59 +00:00
|
|
|
offset = (unsigned long) user *sizeof lastlog;
|
|
|
|
|
2007-10-07 11:44:02 +00:00
|
|
|
if (fstat (fileno (lastlogfile), &statbuf)) {
|
2007-10-07 11:44:59 +00:00
|
|
|
perror (LASTLOG_FILE);
|
2007-10-07 11:44:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (offset >= statbuf.st_size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fseek (lastlogfile, offset, SEEK_SET);
|
|
|
|
if (fread ((char *) &lastlog, sizeof lastlog, 1,
|
2007-10-07 11:44:59 +00:00
|
|
|
lastlogfile) == 1)
|
2007-10-07 11:44:02 +00:00
|
|
|
print_one (pwent);
|
|
|
|
else
|
|
|
|
perror (LASTLOG_FILE);
|
|
|
|
} else {
|
|
|
|
setpwent ();
|
|
|
|
while ((pwent = getpwent ())) {
|
|
|
|
user = pwent->pw_uid;
|
2007-10-07 11:44:59 +00:00
|
|
|
offset = (unsigned long) user *sizeof lastlog;
|
|
|
|
|
2007-10-07 11:44:02 +00:00
|
|
|
fseek (lastlogfile, offset, SEEK_SET);
|
|
|
|
if (fread ((char *) &lastlog, sizeof lastlog, 1,
|
2007-10-07 11:44:59 +00:00
|
|
|
lastlogfile) != 1)
|
2007-10-07 11:44:02 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tflg && NOW - lastlog.ll_time > seconds)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
print_one (pwent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
static void print_one (const struct passwd *pw)
|
2007-10-07 11:44:02 +00:00
|
|
|
{
|
2007-10-07 11:44:59 +00:00
|
|
|
static int once;
|
|
|
|
char *cp;
|
|
|
|
struct tm *tm;
|
2007-10-07 11:45:23 +00:00
|
|
|
time_t ll_time;
|
2007-10-07 11:44:02 +00:00
|
|
|
#ifdef HAVE_STRFTIME
|
|
|
|
char ptime[80];
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
if (!pw)
|
2007-10-07 11:44:02 +00:00
|
|
|
return;
|
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
if (!once) {
|
2007-10-07 11:44:02 +00:00
|
|
|
#ifdef HAVE_LL_HOST
|
2007-10-07 11:44:59 +00:00
|
|
|
printf (_
|
|
|
|
("Username Port From Latest\n"));
|
2007-10-07 11:44:02 +00:00
|
|
|
#else
|
2007-10-07 11:44:59 +00:00
|
|
|
printf (_("Username Port Latest\n"));
|
2007-10-07 11:44:02 +00:00
|
|
|
#endif
|
|
|
|
once++;
|
|
|
|
}
|
2007-10-07 11:45:23 +00:00
|
|
|
ll_time = lastlog.ll_time;
|
|
|
|
tm = localtime (&ll_time);
|
2007-10-07 11:44:02 +00:00
|
|
|
#ifdef HAVE_STRFTIME
|
2007-10-07 11:44:59 +00:00
|
|
|
strftime (ptime, sizeof (ptime), "%a %b %e %H:%M:%S %z %Y", tm);
|
2007-10-07 11:44:02 +00:00
|
|
|
cp = ptime;
|
|
|
|
#else
|
|
|
|
cp = asctime (tm);
|
|
|
|
cp[24] = '\0';
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 11:44:59 +00:00
|
|
|
if (lastlog.ll_time == (time_t) 0)
|
2007-10-07 11:44:02 +00:00
|
|
|
cp = _("**Never logged in**\0");
|
|
|
|
|
|
|
|
#ifdef HAVE_LL_HOST
|
|
|
|
printf ("%-16s %-8.8s %-16.16s %s\n", pw->pw_name,
|
|
|
|
lastlog.ll_line, lastlog.ll_host, cp);
|
|
|
|
#else
|
2007-10-07 11:44:59 +00:00
|
|
|
printf ("%-16s\t%-8.8s %s\n", pw->pw_name, lastlog.ll_line, cp);
|
2007-10-07 11:44:02 +00:00
|
|
|
#endif
|
|
|
|
}
|