Initial commit for Tizen
[profile/extras/shadow-utils.git] / 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: lastlog.c 2851 2009-04-30 21:39:38Z nekral-guest $"
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 (void)
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"), stderr);
84         exit (EXIT_FAILURE);
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 = pw->pw_uid * sizeof (ll);
106
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);
110                 assert (0 == err);
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.
114                  */
115                 if (fread ((char *) &ll, sizeof (ll), 1, lastlogfile) != 1) {
116                         fprintf (stderr,
117                                  _("lastlog: Failed to get the entry for UID %lu\n"),
118                                  (unsigned long int)pw->pw_uid);
119                         exit (EXIT_FAILURE);
120                 }
121         } else {
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).
126                  */
127                 memzero (&ll, sizeof (ll));
128         }
129
130         /* Filter out entries that do not match with the -t or -b options */
131         if (tflg && ((NOW - ll.ll_time) > seconds)) {
132                 return;
133         }
134
135         if (bflg && ((NOW - ll.ll_time) < inverse_seconds)) {
136                 return;
137         }
138
139         /* Print the header only once */
140         if (!once) {
141 #ifdef HAVE_LL_HOST
142                 puts (_("Username         Port     From             Latest"));
143 #else
144                 puts (_("Username                Port     Latest"));
145 #endif
146                 once = true;
147         }
148
149         ll_time = ll.ll_time;
150         tm = localtime (&ll_time);
151 #ifdef HAVE_STRFTIME
152         strftime (ptime, sizeof (ptime), "%a %b %e %H:%M:%S %z %Y", tm);
153         cp = ptime;
154 #else
155         cp = asctime (tm);
156         cp[24] = '\0';
157 #endif
158
159         if (ll.ll_time == (time_t) 0) {
160                 cp = _("**Never logged in**\0");
161         }
162
163 #ifdef HAVE_LL_HOST
164         printf ("%-16s %-8.8s %-16.16s %s\n",
165                 pw->pw_name, ll.ll_line, ll.ll_host, cp);
166 #else
167         printf ("%-16s\t%-8.8s %s\n",
168                 pw->pw_name, ll.ll_line, cp);
169 #endif
170 }
171
172 static void print (void)
173 {
174         const struct passwd *pwent;
175         if (uflg && has_umin && has_umax && (umin == umax)) {
176                 print_one (getpwuid ((uid_t)umin));
177         } else {
178                 setpwent ();
179                 while ( (pwent = getpwent ()) != NULL ) {
180                         if (   uflg
181                             && (   (has_umin && (pwent->pw_uid < (uid_t)umin))
182                                 || (has_umax && (pwent->pw_uid > (uid_t)umax)))) {
183                                 continue;
184                         }
185                         print_one (pwent);
186                 }
187                 endpwent ();
188         }
189 }
190
191 int main (int argc, char **argv)
192 {
193         (void) setlocale (LC_ALL, "");
194         (void) bindtextdomain (PACKAGE, LOCALEDIR);
195         (void) textdomain (PACKAGE);
196
197         {
198                 int c;
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'}
205                 };
206
207                 while ((c = getopt_long (argc, argv, "ht:b:u:", longopts,
208                                          NULL)) != -1) {
209                         switch (c) {
210                         case 'h':
211                                 usage ();
212                                 break;
213                         case 't':
214                         {
215                                 unsigned long days;
216                                 if (getulong (optarg, &days) == 0) {
217                                         fprintf (stderr,
218                                                  _("%s: invalid numeric argument '%s'\n"),
219                                                  "lastlog", optarg);
220                                         exit (EXIT_FAILURE);
221                                 }
222                                 seconds = (time_t) days * DAY;
223                                 tflg = true;
224                                 break;
225                         }
226                         case 'b':
227                         {
228                                 unsigned long inverse_days;
229                                 if (getulong (optarg, &inverse_days) == 0) {
230                                         fprintf (stderr,
231                                                  _("%s: invalid numeric argument '%s'\n"),
232                                                  "lastlog", optarg);
233                                         exit (EXIT_FAILURE);
234                                 }
235                                 inverse_seconds = (time_t) inverse_days * DAY;
236                                 bflg = true;
237                                 break;
238                         }
239                         case 'u':
240                         {
241                                 const struct passwd *pwent;
242                                 /*
243                                  * The user can be:
244                                  *  - a login name
245                                  *  - numerical
246                                  *  - a numerical login ID
247                                  *  - a range (-x, x-, x-y)
248                                  */
249                                 uflg = true;
250                                 /* local, no need for xgetpwnam */
251                                 pwent = getpwnam (optarg);
252                                 if (NULL != pwent) {
253                                         umin = (unsigned long) pwent->pw_uid;
254                                         has_umin = true;
255                                         umax = umin;
256                                         has_umax = true;
257                                 } else {
258                                         if (getrange (optarg,
259                                                       &umin, &has_umin,
260                                                       &umax, &has_umax) == 0) {
261                                                 fprintf (stderr,
262                                                          _("lastlog: Unknown user or range: %s\n"),
263                                                          optarg);
264                                                 exit (EXIT_FAILURE);
265                                         }
266                                 }
267                                 break;
268                         }
269                         default:
270                                 usage ();
271                                 break;
272                         }
273                 }
274                 if (argc > optind) {
275                         fprintf (stderr,
276                                  _("lastlog: unexpected argument: %s\n"),
277                                  argv[optind]);
278                         usage();
279                 }
280         }
281
282         lastlogfile = fopen (LASTLOG_FILE, "r");
283         if (NULL == lastlogfile) {
284                 perror (LASTLOG_FILE);
285                 exit (EXIT_FAILURE);
286         }
287
288         /* Get the lastlog size */
289         if (fstat (fileno (lastlogfile), &statbuf) != 0) {
290                 fprintf (stderr,
291                          _("lastlog: Cannot get the size of %s: %s\n"),
292                          LASTLOG_FILE, strerror (errno));
293                 exit (EXIT_FAILURE);
294         }
295
296         print ();
297
298         (void) fclose (lastlogfile);
299
300         return EXIT_SUCCESS;
301 }
302