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
|
|
|
|
* Copyright (c) 2007 - 2008, 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>
|
|
|
|
#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 */
|
2007-12-27 03:24:04 +05:30
|
|
|
static long umin; /* if uflg, only display users with uid >= umin */
|
|
|
|
static long umax; /* if uflg, only display users with uid <= umax */
|
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 */
|
|
|
|
|
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
|
|
|
static struct lastlog lastlog; /* scratch structure to play with ... */
|
|
|
|
static struct passwd *pwent;
|
|
|
|
|
|
|
|
#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);
|
2007-10-07 17:15:58 +05:30
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
static void print_one (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;
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
ll_time = lastlog.ll_time;
|
|
|
|
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
|
|
|
|
|
2008-06-10 00:45:27 +05:30
|
|
|
if (lastlog.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
|
|
|
|
printf ("%-16s %-8.8s %-16.16s %s\n", pw->pw_name,
|
|
|
|
lastlog.ll_line, lastlog.ll_host, cp);
|
|
|
|
#else
|
|
|
|
printf ("%-16s\t%-8.8s %s\n", pw->pw_name, lastlog.ll_line, cp);
|
|
|
|
#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
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
off_t offset;
|
2008-01-01 01:44:31 +05:30
|
|
|
uid_t user;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-27 03:26:47 +05:30
|
|
|
setpwent ();
|
Ensure that getpwent() is used in setpwent(), getpwent(),
endpwend() sequences (ditto for getgrent(), getspent(), and
getsgent()). The only real (minor) issue was in login, which kept
the passwd file open.
* libmisc/entry.c: Remove unneeded setspent() and endspent() (only
getspnam is called in the middle).
* libmisc/find_new_ids.c: Make sure to close the password and
group files with endpwent() and endgrent().
* libmisc/pwdcheck.c: Remove unneeded endspent() (only getspnam()
is called before).
* src/lastlog.c, src/passwd.c, src/groupmod.c, src/faillog.c,
src/groups.c: Make sure to close
the password file with endpwent().
* src/login.c: Remove unneeded setpwent() (only xgetpwnam is
called before).
* src/login.c, src/newgrp.c: Fix typos in comments.
2008-04-17 03:22:46 +05:30
|
|
|
while ( (pwent = getpwent ()) != NULL ) {
|
2007-12-27 03:26:47 +05:30
|
|
|
user = pwent->pw_uid;
|
2008-06-10 00:45:27 +05:30
|
|
|
if ( uflg
|
|
|
|
&& ( (umin != -1 && user < (uid_t)umin)
|
|
|
|
|| (umax != -1 && user > (uid_t)umax))) {
|
2007-12-27 03:26:47 +05:30
|
|
|
continue;
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2007-12-27 03:26:47 +05:30
|
|
|
offset = user * sizeof lastlog;
|
|
|
|
|
|
|
|
fseeko (lastlogfile, offset, SEEK_SET);
|
|
|
|
if (fread ((char *) &lastlog, sizeof lastlog, 1,
|
2008-06-10 00:45:27 +05:30
|
|
|
lastlogfile) != 1) {
|
2007-12-27 03:26:47 +05:30
|
|
|
continue;
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2007-12-27 03:26:47 +05:30
|
|
|
|
2008-06-10 00:45:27 +05:30
|
|
|
if (tflg && ((NOW - lastlog.ll_time) > seconds)) {
|
2007-12-27 03:26:47 +05:30
|
|
|
continue;
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2007-12-27 03:26:47 +05:30
|
|
|
|
2008-06-10 00:45:27 +05:30
|
|
|
if (bflg && ((NOW - lastlog.ll_time) < inverse_seconds)) {
|
2007-12-27 03:26:47 +05:30
|
|
|
continue;
|
2008-06-10 00:45:27 +05:30
|
|
|
}
|
2007-12-27 03:26:47 +05:30
|
|
|
|
|
|
|
print_one (pwent);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
Ensure that getpwent() is used in setpwent(), getpwent(),
endpwend() sequences (ditto for getgrent(), getspent(), and
getsgent()). The only real (minor) issue was in login, which kept
the passwd file open.
* libmisc/entry.c: Remove unneeded setspent() and endspent() (only
getspnam is called in the middle).
* libmisc/find_new_ids.c: Make sure to close the password and
group files with endpwent() and endgrent().
* libmisc/pwdcheck.c: Remove unneeded endspent() (only getspnam()
is called before).
* src/lastlog.c, src/passwd.c, src/groupmod.c, src/faillog.c,
src/groups.c: Make sure to close
the password file with endpwent().
* src/login.c: Remove unneeded setpwent() (only xgetpwnam is
called before).
* src/login.c, src/newgrp.c: Fix typos in comments.
2008-04-17 03:22:46 +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'}
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c =
|
2007-10-07 17:17:01 +05:30
|
|
|
getopt_long (argc, argv, "ht:b:u:", longopts,
|
2007-10-07 17:15:58 +05:30
|
|
|
NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
days = atoi (optarg);
|
|
|
|
seconds = 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':
|
|
|
|
inverse_days = atoi (optarg);
|
|
|
|
inverse_seconds = 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':
|
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;
|
* lib/prototypes.h, configure.in, libmisc/Makefile.am,
libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetpwuid.c,
libmisc/xgetgrnam.c, libmisc/xgetgrgid.c, libmisc/xgetspnam.c:
Added functions xgetpwnam(), xgetpwuid(), xgetgrnam(),
xgetgrgid(), and xgetspnam(). They allocate memory for the
returned structure and are more robust to successive calls. They
are implemented with the libc's getxxyyy_r() functions if
available.
* libmisc/limits.c, libmisc/entry.c, libmisc/chowntty.c,
libmisc/addgrps.c, libmisc/myname.c, libmisc/rlogin.c,
libmisc/pwdcheck.c, src/newgrp.c, src/login_nopam.c,
src/userdel.c, src/lastlog.c, src/grpck.c, src/gpasswd.c,
src/newusers.c, src/chpasswd.c, src/chfn.c, src/groupmems.c,
src/usermod.c, src/expiry.c, src/groupdel.c, src/chgpasswd.c,
src/su.c, src/useradd.c, src/groupmod.c, src/passwd.c, src/pwck.c,
src/groupadd.c, src/chage.c, src/login.c, src/suauth.c,
src/faillog.c, src/groups.c, src/chsh.c, src/id.c: Review all the
usage of one of the getpwnam(), getpwuid(), getgrnam(),
getgrgid(), and getspnam() functions. It was noticed on
http://bugs.debian.org/341230 that chfn and chsh use a passwd
structure after calling a pam function, which result in using
information from the passwd structure requested by pam, not the
original one. It is much easier to use the new xget... functions
to avoid these issues. I've checked which call to the original
get... functions could be left (reducing the scope of the
structure if possible), and I've left comments to ease future
reviews (e.g. /* local, no need for xgetpwnam */).
Note: the getpwent/getgrent calls should probably be checked also.
* src/groupdel.c, src/expiry.c: Fix typos in comments.
* src/groupmod.c: Re-indent.
* libmisc/Makefile.am, lib/groupmem.c, lib/groupio.c, lib/pwmem.c,
lib/pwio.c, lib/shadowmem.c, lib/shadowio.c: Move the __<xx>_dup
functions (used by the xget... functions) from the <xx>io.c files
to the new <xx>mem.c files. This avoid linking some utils against
the SELinux library.
2007-11-19 04:45:26 +05:30
|
|
|
pwent = xgetpwnam (optarg);
|
2007-12-27 03:24:04 +05:30
|
|
|
if (NULL != pwent) {
|
|
|
|
umin = pwent->pw_uid;
|
|
|
|
umax = umin;
|
|
|
|
} else {
|
|
|
|
char *endptr = NULL;
|
2008-01-01 20:08:47 +05:30
|
|
|
long int user;
|
|
|
|
user = strtol(optarg, &endptr, 10);
|
2007-12-27 03:24:04 +05:30
|
|
|
if (*optarg != '\0' && *endptr == '\0') {
|
|
|
|
if (user < 0) {
|
|
|
|
/* -<userid> */
|
|
|
|
umin = -1;
|
|
|
|
umax = -user;
|
|
|
|
} else {
|
|
|
|
/* <userid> */
|
|
|
|
umin = user;
|
|
|
|
umax = user;
|
|
|
|
}
|
|
|
|
} else if (endptr[0] == '-' && endptr[1] == '\0') {
|
|
|
|
/* <userid>- */
|
|
|
|
umin = user;
|
|
|
|
umax = -1;
|
|
|
|
} else if (*endptr == '-') {
|
|
|
|
/* <userid>-<userid> */
|
|
|
|
umin = user;
|
|
|
|
umax = atol(endptr+1);
|
|
|
|
} else {
|
|
|
|
fprintf (stderr,
|
|
|
|
_("Unknown user or range: %s\n"),
|
|
|
|
optarg);
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if ((lastlogfile = fopen (LASTLOG_FILE, "r")) == (FILE *) 0) {
|
|
|
|
perror (LASTLOG_FILE);
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
print ();
|
|
|
|
fclose (lastlogfile);
|
|
|
|
exit (0);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-10 00:45:27 +05:30
|
|
|
|