2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 2000, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2000 - 2006, Tomasz Kłoczko
|
2009-03-14 03:50:20 +05:30
|
|
|
* Copyright (c) 2007 - 2009, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
* 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.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE 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.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <lastlog.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <pwd.h>
|
2007-10-07 17:17:01 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <time.h>
|
2009-03-14 03:50:20 +05:30
|
|
|
#include <assert.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "defines.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
#include "prototypes.h"
|
2008-06-10 00:45:27 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Needed for MkLinux DR1/2/2.1 - J.
|
|
|
|
*/
|
|
|
|
#ifndef LASTLOG_FILE
|
|
|
|
#define LASTLOG_FILE "/var/log/lastlog"
|
|
|
|
#endif
|
2008-06-10 00:45:27 +05:30
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
static FILE *lastlogfile; /* lastlog file stream */
|
2009-03-14 03:50:20 +05:30
|
|
|
static unsigned long umin; /* if uflg and has_umin, only display users with uid >= umin */
|
2008-06-15 02:36:53 +05:30
|
|
|
static bool has_umin = false;
|
2009-03-14 03:50:20 +05:30
|
|
|
static unsigned long umax; /* if uflg and has_umax, only display users with uid <= umax */
|
2008-06-15 02:36:53 +05:30
|
|
|
static bool has_umax = false;
|
2007-10-07 17:14:59 +05:30
|
|
|
static int days; /* number of days to consider for print command */
|
2007-10-07 17:14:02 +05:30
|
|
|
static time_t seconds; /* that number of days in seconds */
|
2007-10-07 17:17:01 +05:30
|
|
|
static int inverse_days; /* number of days to consider for print command */
|
|
|
|
static time_t inverse_seconds; /* that number of days in seconds */
|
2009-03-14 03:50:20 +05:30
|
|
|
static struct stat statbuf; /* fstat buffer for file size */
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-10 00:45:27 +05:30
|
|
|
static bool uflg = false; /* print only an user of range of users */
|
|
|
|
static bool tflg = false; /* print is restricted to most recent days */
|
|
|
|
static bool bflg = false; /* print excludes most recent days */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#define NOW (time ((time_t *) 0))
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
static void usage (void)
|
|
|
|
{
|
2008-01-25 02:12:12 +05:30
|
|
|
fputs (_("Usage: lastlog [options]\n"
|
2008-01-25 02:24:42 +05:30
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -b, --before DAYS print only lastlog records older than DAYS\n"
|
|
|
|
" -h, --help display this help message and exit\n"
|
|
|
|
" -t, --time DAYS print only lastlog records more recent than DAYS\n"
|
|
|
|
" -u, --user LOGIN print lastlog record of the specified LOGIN\n"
|
|
|
|
"\n"), stderr);
|
2009-04-23 16:51:01 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:15:58 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-23 16:51:01 +05:30
|
|
|
static void print_one (/*@null@*/const struct passwd *pw)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2008-06-10 00:45:27 +05:30
|
|
|
static bool once = false;
|
2007-10-07 17:15:58 +05:30
|
|
|
char *cp;
|
|
|
|
struct tm *tm;
|
|
|
|
time_t ll_time;
|
2009-03-14 03:50:20 +05:30
|
|
|
off_t offset;
|
|
|
|
struct lastlog ll;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
#ifdef HAVE_STRFTIME
|
|
|
|
char ptime[80];
|
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-10 00:45:27 +05:30
|
|
|
if (NULL == pw) {
|
2007-10-07 17:15:58 +05:30
|
|
|
return;
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
|
2009-03-14 03:50:20 +05:30
|
|
|
|
|
|
|
offset = pw->pw_uid * sizeof (ll);
|
|
|
|
|
|
|
|
if (offset <= (statbuf.st_size - sizeof (ll))) {
|
|
|
|
/* fseeko errors are not really relevant for us. */
|
|
|
|
assert ( fseeko (lastlogfile, offset, SEEK_SET) == 0 );
|
|
|
|
/* lastlog is a sparse file. Even if no entries were
|
|
|
|
* entered for this user, which should be able to get the
|
|
|
|
* empty entry in this case.
|
|
|
|
*/
|
|
|
|
if (fread ((char *) &ll, sizeof (ll), 1, lastlogfile) != 1) {
|
|
|
|
fprintf (stderr,
|
2009-04-23 16:51:01 +05:30
|
|
|
_("lastlog: Failed to get the entry for UID %lu\n"),
|
|
|
|
(unsigned long int)pw->pw_uid);
|
|
|
|
exit (EXIT_FAILURE);
|
2009-03-14 03:50:20 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Outsize of the lastlog file.
|
|
|
|
* Behave as if there were a missing entry (same behavior
|
|
|
|
* as if we were reading an non existing entry in the
|
|
|
|
* sparse lastlog file).
|
|
|
|
*/
|
|
|
|
memzero (&ll, sizeof (ll));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Filter out entries that do not match with the -t or -b options */
|
|
|
|
if (tflg && ((NOW - ll.ll_time) > seconds)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bflg && ((NOW - ll.ll_time) < inverse_seconds)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the header only once */
|
2007-10-07 17:15:58 +05:30
|
|
|
if (!once) {
|
|
|
|
#ifdef HAVE_LL_HOST
|
* libmisc/age.c, libmisc/yesno.c, src/lastlog.c, src/grpck.c,
src/chfn.c, src/passwd.c, src/chage.c, src/login.c, src/sulogin.c,
src/chsh.c: Fix call to puts (remove end of line, or use fputs).
* po/*.po: Unfuzzy PO files according to above change.
2008-02-03 21:58:03 +05:30
|
|
|
puts (_("Username Port From Latest"));
|
2007-10-07 17:15:58 +05:30
|
|
|
#else
|
* libmisc/age.c, libmisc/yesno.c, src/lastlog.c, src/grpck.c,
src/chfn.c, src/passwd.c, src/chage.c, src/login.c, src/sulogin.c,
src/chsh.c: Fix call to puts (remove end of line, or use fputs).
* po/*.po: Unfuzzy PO files according to above change.
2008-02-03 21:58:03 +05:30
|
|
|
puts (_("Username Port Latest"));
|
2007-10-07 17:15:58 +05:30
|
|
|
#endif
|
2008-06-10 00:45:27 +05:30
|
|
|
once = true;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2009-03-14 03:50:20 +05:30
|
|
|
|
|
|
|
ll_time = ll.ll_time;
|
2007-10-07 17:15:58 +05:30
|
|
|
tm = localtime (&ll_time);
|
|
|
|
#ifdef HAVE_STRFTIME
|
|
|
|
strftime (ptime, sizeof (ptime), "%a %b %e %H:%M:%S %z %Y", tm);
|
|
|
|
cp = ptime;
|
|
|
|
#else
|
|
|
|
cp = asctime (tm);
|
|
|
|
cp[24] = '\0';
|
|
|
|
#endif
|
|
|
|
|
2009-03-14 03:50:20 +05:30
|
|
|
if (ll.ll_time == (time_t) 0) {
|
2007-10-07 17:15:58 +05:30
|
|
|
cp = _("**Never logged in**\0");
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_LL_HOST
|
2009-03-14 03:50:20 +05:30
|
|
|
printf ("%-16s %-8.8s %-16.16s %s\n",
|
|
|
|
pw->pw_name, ll.ll_line, ll.ll_host, cp);
|
2007-10-07 17:15:58 +05:30
|
|
|
#else
|
2009-03-14 03:50:20 +05:30
|
|
|
printf ("%-16s\t%-8.8s %s\n",
|
|
|
|
pw->pw_name, ll.ll_line, cp);
|
2007-10-07 17:15:58 +05:30
|
|
|
#endif
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void print (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2009-04-23 16:51:01 +05:30
|
|
|
const struct passwd *pwent;
|
2009-03-14 03:50:20 +05:30
|
|
|
if (uflg && has_umin && has_umax && (umin == umax)) {
|
|
|
|
print_one (getpwuid ((uid_t)umin));
|
|
|
|
} else {
|
|
|
|
setpwent ();
|
|
|
|
while ( (pwent = getpwent ()) != NULL ) {
|
|
|
|
if ( uflg
|
|
|
|
&& ( (has_umin && (pwent->pw_uid < (uid_t)umin))
|
|
|
|
|| (has_umax && (pwent->pw_uid > (uid_t)umax)))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
print_one (pwent);
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2009-03-14 03:50:20 +05:30
|
|
|
endpwent ();
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
int main (int argc, char **argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2008-06-10 00:45:27 +05:30
|
|
|
(void) setlocale (LC_ALL, "");
|
|
|
|
(void) bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
(void) textdomain (PACKAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
{
|
|
|
|
int c;
|
|
|
|
static struct option const longopts[] = {
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"time", required_argument, NULL, 't'},
|
2007-10-07 17:17:01 +05:30
|
|
|
{"before", required_argument, NULL, 'b'},
|
2007-10-07 17:15:58 +05:30
|
|
|
{"user", required_argument, NULL, 'u'},
|
|
|
|
{NULL, 0, NULL, '\0'}
|
|
|
|
};
|
|
|
|
|
2009-03-14 03:50:20 +05:30
|
|
|
while ((c = getopt_long (argc, argv, "ht:b:u:", longopts,
|
|
|
|
NULL)) != -1) {
|
2007-10-07 17:15:58 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
case 't':
|
2008-06-15 02:36:53 +05:30
|
|
|
days = atoi (optarg); /* FIXME */
|
|
|
|
seconds = (time_t) days * DAY;
|
2008-06-10 00:45:27 +05:30
|
|
|
tflg = true;
|
2007-10-07 17:15:58 +05:30
|
|
|
break;
|
2007-10-07 17:17:01 +05:30
|
|
|
case 'b':
|
2008-06-15 02:36:53 +05:30
|
|
|
inverse_days = atoi (optarg); /* FIXME */
|
|
|
|
inverse_seconds = (time_t) inverse_days * DAY;
|
2008-06-10 00:45:27 +05:30
|
|
|
bflg = true;
|
2007-10-07 17:17:01 +05:30
|
|
|
break;
|
2007-10-07 17:15:58 +05:30
|
|
|
case 'u':
|
2009-04-23 16:51:01 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pwent;
|
2007-12-27 03:24:04 +05:30
|
|
|
/*
|
|
|
|
* The user can be:
|
|
|
|
* - a login name
|
|
|
|
* - numerical
|
|
|
|
* - a numerical login ID
|
|
|
|
* - a range (-x, x-, x-y)
|
|
|
|
*/
|
2008-06-10 00:45:27 +05:30
|
|
|
uflg = true;
|
2009-03-14 03:50:20 +05:30
|
|
|
/* local, no need for xgetpwnam */
|
|
|
|
pwent = getpwnam (optarg);
|
2007-12-27 03:24:04 +05:30
|
|
|
if (NULL != pwent) {
|
2008-06-15 02:36:53 +05:30
|
|
|
umin = (unsigned long) pwent->pw_uid;
|
|
|
|
has_umin = true;
|
2007-12-27 03:24:04 +05:30
|
|
|
umax = umin;
|
2008-06-15 02:36:53 +05:30
|
|
|
has_umax = true;
|
2007-12-27 03:24:04 +05:30
|
|
|
} else {
|
2008-06-15 02:36:53 +05:30
|
|
|
if (getrange (optarg,
|
|
|
|
&umin, &has_umin,
|
|
|
|
&umax, &has_umax) == 0) {
|
2007-12-27 03:24:04 +05:30
|
|
|
fprintf (stderr,
|
2008-06-15 02:36:53 +05:30
|
|
|
_("lastlog: Unknown user or range: %s\n"),
|
|
|
|
optarg);
|
2009-04-23 16:51:01 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2007-12-27 03:24:04 +05:30
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
}
|
|
|
|
break;
|
2009-04-23 16:51:01 +05:30
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:17:22 +05:30
|
|
|
if (argc > optind) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("lastlog: unexpected argument: %s\n"),
|
|
|
|
argv[optind]);
|
|
|
|
usage();
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2009-04-23 16:51:01 +05:30
|
|
|
lastlogfile = fopen (LASTLOG_FILE, "r");
|
|
|
|
if (NULL == lastlogfile) {
|
2007-10-07 17:15:58 +05:30
|
|
|
perror (LASTLOG_FILE);
|
2009-04-23 16:51:01 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:15:58 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-20 19:34:48 +05:30
|
|
|
/* Get the lastlog size */
|
2009-03-14 03:50:20 +05:30
|
|
|
if (fstat (fileno (lastlogfile), &statbuf) != 0) {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("lastlog: Cannot get the size of %s: %s\n"),
|
|
|
|
LASTLOG_FILE, strerror (errno));
|
2009-04-23 16:51:01 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2009-03-14 03:50:20 +05:30
|
|
|
}
|
|
|
|
|
2009-04-20 19:34:48 +05:30
|
|
|
print ();
|
|
|
|
|
2009-04-23 16:51:01 +05:30
|
|
|
(void) fclose (lastlogfile);
|
|
|
|
exit (EXIT_SUCCESS);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-10 00:45:27 +05:30
|
|
|
|