2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2009, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ident "$Id: lastlog.c 2851 2009-04-30 21:39:38Z nekral-guest $"
42 #include <sys/types.h>
46 #include "prototypes.h"
49 * Needed for MkLinux DR1/2/2.1 - J.
52 #define LASTLOG_FILE "/var/log/lastlog"
58 static FILE *lastlogfile; /* lastlog file stream */
59 static unsigned long umin; /* if uflg and has_umin, only display users with uid >= umin */
60 static bool has_umin = false;
61 static unsigned long umax; /* if uflg and has_umax, only display users with uid <= umax */
62 static bool has_umax = false;
63 static time_t seconds; /* that number of days in seconds */
64 static time_t inverse_seconds; /* that number of days in seconds */
65 static struct stat statbuf; /* fstat buffer for file size */
68 static bool uflg = false; /* print only an user of range of users */
69 static bool tflg = false; /* print is restricted to most recent days */
70 static bool bflg = false; /* print excludes most recent days */
72 #define NOW (time ((time_t *) 0))
74 static void usage (void)
76 fputs (_("Usage: lastlog [options]\n"
79 " -b, --before DAYS print only lastlog records older than DAYS\n"
80 " -h, --help display this help message and exit\n"
81 " -t, --time DAYS print only lastlog records more recent than DAYS\n"
82 " -u, --user LOGIN print lastlog record of the specified LOGIN\n"
87 static void print_one (/*@null@*/const struct passwd *pw)
89 static bool once = false;
105 offset = pw->pw_uid * sizeof (ll);
107 if (offset <= (statbuf.st_size - sizeof (ll))) {
108 /* fseeko errors are not really relevant for us. */
109 int err = fseeko (lastlogfile, offset, SEEK_SET);
111 /* lastlog is a sparse file. Even if no entries were
112 * entered for this user, which should be able to get the
113 * empty entry in this case.
115 if (fread ((char *) &ll, sizeof (ll), 1, lastlogfile) != 1) {
117 _("lastlog: Failed to get the entry for UID %lu\n"),
118 (unsigned long int)pw->pw_uid);
122 /* Outsize of the lastlog file.
123 * Behave as if there were a missing entry (same behavior
124 * as if we were reading an non existing entry in the
125 * sparse lastlog file).
127 memzero (&ll, sizeof (ll));
130 /* Filter out entries that do not match with the -t or -b options */
131 if (tflg && ((NOW - ll.ll_time) > seconds)) {
135 if (bflg && ((NOW - ll.ll_time) < inverse_seconds)) {
139 /* Print the header only once */
142 puts (_("Username Port From Latest"));
144 puts (_("Username Port Latest"));
149 ll_time = ll.ll_time;
150 tm = localtime (&ll_time);
152 strftime (ptime, sizeof (ptime), "%a %b %e %H:%M:%S %z %Y", tm);
159 if (ll.ll_time == (time_t) 0) {
160 cp = _("**Never logged in**\0");
164 printf ("%-16s %-8.8s %-16.16s %s\n",
165 pw->pw_name, ll.ll_line, ll.ll_host, cp);
167 printf ("%-16s\t%-8.8s %s\n",
168 pw->pw_name, ll.ll_line, cp);
172 static void print (void)
174 const struct passwd *pwent;
175 if (uflg && has_umin && has_umax && (umin == umax)) {
176 print_one (getpwuid ((uid_t)umin));
179 while ( (pwent = getpwent ()) != NULL ) {
181 && ( (has_umin && (pwent->pw_uid < (uid_t)umin))
182 || (has_umax && (pwent->pw_uid > (uid_t)umax)))) {
191 int main (int argc, char **argv)
193 (void) setlocale (LC_ALL, "");
194 (void) bindtextdomain (PACKAGE, LOCALEDIR);
195 (void) textdomain (PACKAGE);
199 static struct option const longopts[] = {
200 {"help", no_argument, NULL, 'h'},
201 {"time", required_argument, NULL, 't'},
202 {"before", required_argument, NULL, 'b'},
203 {"user", required_argument, NULL, 'u'},
204 {NULL, 0, NULL, '\0'}
207 while ((c = getopt_long (argc, argv, "ht:b:u:", longopts,
216 if (getulong (optarg, &days) == 0) {
218 _("%s: invalid numeric argument '%s'\n"),
222 seconds = (time_t) days * DAY;
228 unsigned long inverse_days;
229 if (getulong (optarg, &inverse_days) == 0) {
231 _("%s: invalid numeric argument '%s'\n"),
235 inverse_seconds = (time_t) inverse_days * DAY;
241 const struct passwd *pwent;
246 * - a numerical login ID
247 * - a range (-x, x-, x-y)
250 /* local, no need for xgetpwnam */
251 pwent = getpwnam (optarg);
253 umin = (unsigned long) pwent->pw_uid;
258 if (getrange (optarg,
260 &umax, &has_umax) == 0) {
262 _("lastlog: Unknown user or range: %s\n"),
276 _("lastlog: unexpected argument: %s\n"),
282 lastlogfile = fopen (LASTLOG_FILE, "r");
283 if (NULL == lastlogfile) {
284 perror (LASTLOG_FILE);
288 /* Get the lastlog size */
289 if (fstat (fileno (lastlogfile), &statbuf) != 0) {
291 _("lastlog: Cannot get the size of %s: %s\n"),
292 LASTLOG_FILE, strerror (errno));
298 (void) fclose (lastlogfile);