]> granicus.if.org Git - shadow/blob - src/lastlog.c
* src/usermod.c (process_flags): Report usage if no options are
[shadow] / src / lastlog.c
1 /*
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
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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.
31  */
32
33 #include <config.h>
34
35 #ident "$Id$"
36
37 #include <getopt.h>
38 #include <lastlog.h>
39 #include <pwd.h>
40 #include <stdio.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <time.h>
44 #include <assert.h>
45 #include "defines.h"
46 #include "prototypes.h"
47
48 /*
49  * Needed for MkLinux DR1/2/2.1 - J.
50  */
51 #ifndef LASTLOG_FILE
52 #define LASTLOG_FILE "/var/log/lastlog"
53 #endif
54
55 /*
56  * Global variables
57  */
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 */
66
67
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 */
71
72 #define NOW     (time ((time_t *) 0))
73
74 static void usage (int status)
75 {
76         fputs (_("Usage: lastlog [options]\n"
77                  "\n"
78                  "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"
83                  "\n"), (EXIT_SUCCESS != status) ? stderr : stdout);
84         exit (status);
85 }
86
87 static void print_one (/*@null@*/const struct passwd *pw)
88 {
89         static bool once = false;
90         char *cp;
91         struct tm *tm;
92         time_t ll_time;
93         off_t offset;
94         struct lastlog ll;
95
96 #ifdef HAVE_STRFTIME
97         char ptime[80];
98 #endif
99
100         if (NULL == pw) {
101                 return;
102         }
103
104
105         offset = (off_t) pw->pw_uid * sizeof (ll);
106         if (offset + sizeof (ll) <= statbuf.st_size) {
107                 /* fseeko errors are not really relevant for us. */
108                 int err = fseeko (lastlogfile, offset, SEEK_SET);
109                 assert (0 == err);
110                 /* lastlog is a sparse file. Even if no entries were
111                  * entered for this user, which should be able to get the
112                  * empty entry in this case.
113                  */
114                 if (fread ((char *) &ll, sizeof (ll), 1, lastlogfile) != 1) {
115                         fprintf (stderr,
116                                  _("lastlog: Failed to get the entry for UID %lu\n"),
117                                  (unsigned long int)pw->pw_uid);
118                         exit (EXIT_FAILURE);
119                 }
120         } else {
121                 /* Outsize of the lastlog file.
122                  * Behave as if there were a missing entry (same behavior
123                  * as if we were reading an non existing entry in the
124                  * sparse lastlog file).
125                  */
126                 memzero (&ll, sizeof (ll));
127         }
128
129         /* Filter out entries that do not match with the -t or -b options */
130         if (tflg && ((NOW - ll.ll_time) > seconds)) {
131                 return;
132         }
133
134         if (bflg && ((NOW - ll.ll_time) < inverse_seconds)) {
135                 return;
136         }
137
138         /* Print the header only once */
139         if (!once) {
140 #ifdef HAVE_LL_HOST
141                 puts (_("Username         Port     From             Latest"));
142 #else
143                 puts (_("Username                Port     Latest"));
144 #endif
145                 once = true;
146         }
147
148         ll_time = ll.ll_time;
149         tm = localtime (&ll_time);
150 #ifdef HAVE_STRFTIME
151         strftime (ptime, sizeof (ptime), "%a %b %e %H:%M:%S %z %Y", tm);
152         cp = ptime;
153 #else
154         cp = asctime (tm);
155         cp[24] = '\0';
156 #endif
157
158         if (ll.ll_time == (time_t) 0) {
159                 cp = _("**Never logged in**\0");
160         }
161
162 #ifdef HAVE_LL_HOST
163         printf ("%-16s %-8.8s %-16.16s %s\n",
164                 pw->pw_name, ll.ll_line, ll.ll_host, cp);
165 #else
166         printf ("%-16s\t%-8.8s %s\n",
167                 pw->pw_name, ll.ll_line, cp);
168 #endif
169 }
170
171 static void print (void)
172 {
173         const struct passwd *pwent;
174         if (uflg && has_umin && has_umax && (umin == umax)) {
175                 print_one (getpwuid ((uid_t)umin));
176         } else {
177                 setpwent ();
178                 while ( (pwent = getpwent ()) != NULL ) {
179                         if (   uflg
180                             && (   (has_umin && (pwent->pw_uid < (uid_t)umin))
181                                 || (has_umax && (pwent->pw_uid > (uid_t)umax)))) {
182                                 continue;
183                         }
184                         print_one (pwent);
185                 }
186                 endpwent ();
187         }
188 }
189
190 int main (int argc, char **argv)
191 {
192         (void) setlocale (LC_ALL, "");
193         (void) bindtextdomain (PACKAGE, LOCALEDIR);
194         (void) textdomain (PACKAGE);
195
196         {
197                 int c;
198                 static struct option const longopts[] = {
199                         {"help", no_argument, NULL, 'h'},
200                         {"time", required_argument, NULL, 't'},
201                         {"before", required_argument, NULL, 'b'},
202                         {"user", required_argument, NULL, 'u'},
203                         {NULL, 0, NULL, '\0'}
204                 };
205
206                 while ((c = getopt_long (argc, argv, "ht:b:u:", longopts,
207                                          NULL)) != -1) {
208                         switch (c) {
209                         case 'h':
210                                 usage (EXIT_SUCCESS);
211                                 break;
212                         case 't':
213                         {
214                                 unsigned long days;
215                                 if (getulong (optarg, &days) == 0) {
216                                         fprintf (stderr,
217                                                  _("%s: invalid numeric argument '%s'\n"),
218                                                  "lastlog", optarg);
219                                         exit (EXIT_FAILURE);
220                                 }
221                                 seconds = (time_t) days * DAY;
222                                 tflg = true;
223                                 break;
224                         }
225                         case 'b':
226                         {
227                                 unsigned long inverse_days;
228                                 if (getulong (optarg, &inverse_days) == 0) {
229                                         fprintf (stderr,
230                                                  _("%s: invalid numeric argument '%s'\n"),
231                                                  "lastlog", optarg);
232                                         exit (EXIT_FAILURE);
233                                 }
234                                 inverse_seconds = (time_t) inverse_days * DAY;
235                                 bflg = true;
236                                 break;
237                         }
238                         case 'u':
239                         {
240                                 const struct passwd *pwent;
241                                 /*
242                                  * The user can be:
243                                  *  - a login name
244                                  *  - numerical
245                                  *  - a numerical login ID
246                                  *  - a range (-x, x-, x-y)
247                                  */
248                                 uflg = true;
249                                 /* local, no need for xgetpwnam */
250                                 pwent = getpwnam (optarg);
251                                 if (NULL != pwent) {
252                                         umin = (unsigned long) pwent->pw_uid;
253                                         has_umin = true;
254                                         umax = umin;
255                                         has_umax = true;
256                                 } else {
257                                         if (getrange (optarg,
258                                                       &umin, &has_umin,
259                                                       &umax, &has_umax) == 0) {
260                                                 fprintf (stderr,
261                                                          _("lastlog: Unknown user or range: %s\n"),
262                                                          optarg);
263                                                 exit (EXIT_FAILURE);
264                                         }
265                                 }
266                                 break;
267                         }
268                         default:
269                                 usage (EXIT_FAILURE);
270                                 break;
271                         }
272                 }
273                 if (argc > optind) {
274                         fprintf (stderr,
275                                  _("lastlog: unexpected argument: %s\n"),
276                                  argv[optind]);
277                         usage (EXIT_FAILURE);
278                 }
279         }
280
281         lastlogfile = fopen (LASTLOG_FILE, "r");
282         if (NULL == lastlogfile) {
283                 perror (LASTLOG_FILE);
284                 exit (EXIT_FAILURE);
285         }
286
287         /* Get the lastlog size */
288         if (fstat (fileno (lastlogfile), &statbuf) != 0) {
289                 fprintf (stderr,
290                          _("lastlog: Cannot get the size of %s: %s\n"),
291                          LASTLOG_FILE, strerror (errno));
292                 exit (EXIT_FAILURE);
293         }
294
295         print ();
296
297         (void) fclose (lastlogfile);
298
299         return EXIT_SUCCESS;
300 }
301