]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/last/last.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / usr.bin / last / last.c
1 /*
2  * Copyright (c) 1987, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2018 Philip Paeps
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1987, 1993, 1994\n\
34         The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36
37 #ifndef lint
38 static const char sccsid[] = "@(#)last.c        8.2 (Berkeley) 4/2/94";
39 #endif /* not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/stat.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <langinfo.h>
50 #include <locale.h>
51 #include <paths.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <timeconv.h>
58 #include <unistd.h>
59 #include <utmpx.h>
60 #include <sys/queue.h>
61
62 #include <libxo/xo.h>
63
64 #define NO      0                               /* false/no */
65 #define YES     1                               /* true/yes */
66 #define ATOI2(ar)       ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
67
68 typedef struct arg {
69         char    *name;                          /* argument */
70 #define REBOOT_TYPE     -1
71 #define HOST_TYPE       -2
72 #define TTY_TYPE        -3
73 #define USER_TYPE       -4
74         int     type;                           /* type of arg */
75         struct arg      *next;                  /* linked list pointer */
76 } ARG;
77 static ARG      *arglist;                       /* head of linked list */
78
79 static SLIST_HEAD(, idtab) idlist;
80
81 struct idtab {
82         time_t  logout;                         /* log out time */
83         char    id[sizeof ((struct utmpx *)0)->ut_id]; /* identifier */
84         SLIST_ENTRY(idtab) list;
85 };
86
87 static const    char *crmsg;                    /* cause of last reboot */
88 static time_t   currentout;                     /* current logout value */
89 static long     maxrec;                         /* records to display */
90 static const    char *file = NULL;              /* utx.log file */
91 static int      sflag = 0;                      /* show delta in seconds */
92 static int      width = 5;                      /* show seconds in delta */
93 static int      yflag;                          /* show year */
94 static int      d_first;
95 static int      snapfound = 0;                  /* found snapshot entry? */
96 static time_t   snaptime;                       /* if != 0, we will only
97                                                  * report users logged in
98                                                  * at this snapshot time
99                                                  */
100
101 static void      addarg(int, char *);
102 static time_t    dateconv(char *);
103 static void      doentry(struct utmpx *);
104 static void      hostconv(char *);
105 static void      printentry(struct utmpx *, struct idtab *);
106 static char     *ttyconv(char *);
107 static int       want(struct utmpx *);
108 static void      usage(void);
109 static void      wtmp(void);
110
111 static void
112 usage(void)
113 {
114         xo_error(
115 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
116 "            [-n maxrec] [-t tty] [user ...]\n");
117         exit(1);
118 }
119
120 int
121 main(int argc, char *argv[])
122 {
123         int ch;
124         char *p;
125
126         (void) setlocale(LC_TIME, "");
127         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
128
129         argc = xo_parse_args(argc, argv);
130         if (argc < 0)
131                 exit(1);
132         atexit(xo_finish_atexit);
133
134         maxrec = -1;
135         snaptime = 0;
136         while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
137                 switch (ch) {
138                 case '0': case '1': case '2': case '3': case '4':
139                 case '5': case '6': case '7': case '8': case '9':
140                         /*
141                          * kludge: last was originally designed to take
142                          * a number after a dash.
143                          */
144                         if (maxrec == -1) {
145                                 p = strchr(argv[optind - 1], ch);
146                                 if (p == NULL)
147                                         p = strchr(argv[optind], ch);
148                                 maxrec = atol(p);
149                                 if (!maxrec)
150                                         exit(0);
151                         }
152                         break;
153                 case 'd':
154                         snaptime = dateconv(optarg);
155                         break;
156                 case 'f':
157                         file = optarg;
158                         break;
159                 case 'h':
160                         hostconv(optarg);
161                         addarg(HOST_TYPE, optarg);
162                         break;
163                 case 'n':
164                         errno = 0;
165                         maxrec = strtol(optarg, &p, 10);
166                         if (p == optarg || *p != '\0' || errno != 0 ||
167                             maxrec <= 0)
168                                 xo_errx(1, "%s: bad line count", optarg);
169                         break;
170                 case 's':
171                         sflag++;        /* Show delta as seconds */
172                         break;
173                 case 't':
174                         addarg(TTY_TYPE, ttyconv(optarg));
175                         break;
176                 case 'w':
177                         width = 8;
178                         break;
179                 case 'y':
180                         yflag++;
181                         break;
182                 case '?':
183                 default:
184                         usage();
185                 }
186
187         if (sflag && width == 8) usage();
188
189         if (argc) {
190                 setlinebuf(stdout);
191                 for (argv += optind; *argv; ++argv) {
192                         if (strcmp(*argv, "reboot") == 0)
193                                 addarg(REBOOT_TYPE, *argv);
194 #define COMPATIBILITY
195 #ifdef  COMPATIBILITY
196                         /* code to allow "last p5" to work */
197                         addarg(TTY_TYPE, ttyconv(*argv));
198 #endif
199                         addarg(USER_TYPE, *argv);
200                 }
201         }
202         wtmp();
203         exit(0);
204 }
205
206 /*
207  * wtmp --
208  *      read through the utx.log file
209  */
210 static void
211 wtmp(void)
212 {
213         struct utmpx *buf = NULL;
214         struct utmpx *ut;
215         static unsigned int amount = 0;
216         time_t t;
217         char ct[80];
218         struct tm *tm;
219
220         SLIST_INIT(&idlist);
221         (void)time(&t);
222
223         xo_open_container("last-information");
224
225         /* Load the last entries from the file. */
226         if (setutxdb(UTXDB_LOG, file) != 0)
227                 xo_err(1, "%s", file);
228         while ((ut = getutxent()) != NULL) {
229                 if (amount % 128 == 0) {
230                         buf = realloc(buf, (amount + 128) * sizeof *ut);
231                         if (buf == NULL)
232                                 xo_err(1, "realloc");
233                 }
234                 memcpy(&buf[amount++], ut, sizeof *ut);
235                 if (t > ut->ut_tv.tv_sec)
236                         t = ut->ut_tv.tv_sec;
237         }
238         endutxent();
239
240         /* Display them in reverse order. */
241         xo_open_list("last");
242         while (amount > 0)
243                 doentry(&buf[--amount]);
244         xo_close_list("last");
245         free(buf);
246         tm = localtime(&t);
247         (void) strftime(ct, sizeof(ct), "%+", tm);
248         xo_emit("\n{:utxdb/%s}", (file == NULL) ? "utx.log" : file);
249         xo_attr("seconds", "%lu", (unsigned long) t);
250         xo_emit(" begins {:begins/%s}\n", ct);
251         xo_close_container("last-information");
252 }
253
254 /*
255  * doentry --
256  *      process a single utx.log entry
257  */
258 static void
259 doentry(struct utmpx *bp)
260 {
261         struct idtab *tt;
262
263         /* the machine stopped */
264         if (bp->ut_type == BOOT_TIME || bp->ut_type == SHUTDOWN_TIME) {
265                 /* everybody just logged out */
266                 while ((tt = SLIST_FIRST(&idlist)) != NULL) {
267                         SLIST_REMOVE_HEAD(&idlist, list);
268                         free(tt);
269                 }
270                 currentout = -bp->ut_tv.tv_sec;
271                 crmsg = bp->ut_type != SHUTDOWN_TIME ?
272                     "crash" : "shutdown";
273                 /*
274                  * if we're in snapshot mode, we want to exit if this
275                  * shutdown/reboot appears while we we are tracking the
276                  * active range
277                  */
278                 if (snaptime && snapfound)
279                         exit(0);
280                 /*
281                  * don't print shutdown/reboot entries unless flagged for
282                  */
283                 if (!snaptime && want(bp))
284                         printentry(bp, NULL);
285                 return;
286         }
287         /* date got set */
288         if (bp->ut_type == OLD_TIME || bp->ut_type == NEW_TIME) {
289                 if (want(bp) && !snaptime)
290                         printentry(bp, NULL);
291                 return;
292         }
293
294         if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS)
295                 return;
296
297         /* find associated identifier */
298         SLIST_FOREACH(tt, &idlist, list)
299             if (!memcmp(tt->id, bp->ut_id, sizeof bp->ut_id))
300                     break;
301
302         if (tt == NULL) {
303                 /* add new one */
304                 tt = malloc(sizeof(struct idtab));
305                 if (tt == NULL)
306                         xo_errx(1, "malloc failure");
307                 tt->logout = currentout;
308                 memcpy(tt->id, bp->ut_id, sizeof bp->ut_id);
309                 SLIST_INSERT_HEAD(&idlist, tt, list);
310         }
311
312         /*
313          * print record if not in snapshot mode and wanted
314          * or in snapshot mode and in snapshot range
315          */
316         if (bp->ut_type == USER_PROCESS && (want(bp) ||
317             (bp->ut_tv.tv_sec < snaptime &&
318             (tt->logout > snaptime || tt->logout < 1)))) {
319                 snapfound = 1;
320                 printentry(bp, tt);
321         }
322         tt->logout = bp->ut_tv.tv_sec;
323 }
324
325 /*
326  * printentry --
327  *      output an entry
328  *
329  * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
330  * logout type (crash/shutdown) as appropriate.
331  */
332 static void
333 printentry(struct utmpx *bp, struct idtab *tt)
334 {
335         char ct[80];
336         struct tm *tm;
337         time_t  delta;                          /* time difference */
338         time_t  t;
339
340         if (maxrec != -1 && !maxrec--)
341                 exit(0);
342         xo_open_instance("last");
343         t = bp->ut_tv.tv_sec;
344         tm = localtime(&t);
345         (void) strftime(ct, sizeof(ct), d_first ?
346             (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
347             (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
348         switch (bp->ut_type) {
349         case BOOT_TIME:
350                 xo_emit("{:user/%-42s/%s}", "boot time");
351                 break;
352         case SHUTDOWN_TIME:
353                 xo_emit("{:user/%-42s/%s}", "shutdown time");
354                 break;
355         case OLD_TIME:
356                 xo_emit("{:user/%-42s/%s}", "old time");
357                 break;
358         case NEW_TIME:
359                 xo_emit("{:user/%-42s/%s}", "new time");
360                 break;
361         case USER_PROCESS:
362                 xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
363                     bp->ut_user, bp->ut_line, bp->ut_host);
364                 break;
365         }
366         xo_attr("seconds", "%lu", (unsigned long)t);
367         xo_emit(" {:login-time/%s%c/%s}", ct, tt == NULL ? '\n' : ' ');
368         if (tt == NULL)
369                 goto end;
370         if (!tt->logout) {
371                 xo_emit("  {:logout-time/still logged in}\n");
372                 goto end;
373         }
374         if (tt->logout < 0) {
375                 tt->logout = -tt->logout;
376                 xo_emit("- {:logout-reason/%s}", crmsg);
377         } else {
378                 tm = localtime(&tt->logout);
379                 (void) strftime(ct, sizeof(ct), "%R", tm);
380                 xo_attr("seconds", "%lu", (unsigned long)tt->logout);
381                 xo_emit("- {:logout-time/%s}", ct);
382         }
383         delta = tt->logout - bp->ut_tv.tv_sec;
384         xo_attr("seconds", "%ld", (long)delta);
385         if (sflag) {
386                 xo_emit("  ({:session-length/%8ld})\n", (long)delta);
387         } else {
388                 tm = gmtime(&delta);
389                 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
390                 if (delta < 86400)
391                         xo_emit("  ({:session-length/%s})\n", ct);
392                 else
393                         xo_emit(" ({:session-length/%ld+%s})\n",
394                             (long)delta / 86400, ct);
395         }
396
397 end:
398         xo_close_instance("last");
399 }
400
401 /*
402  * want --
403  *      see if want this entry
404  */
405 static int
406 want(struct utmpx *bp)
407 {
408         ARG *step;
409
410         if (snaptime)
411                 return (NO);
412
413         if (!arglist)
414                 return (YES);
415
416         for (step = arglist; step; step = step->next)
417                 switch(step->type) {
418                 case REBOOT_TYPE:
419                         if (bp->ut_type == BOOT_TIME ||
420                             bp->ut_type == SHUTDOWN_TIME)
421                                 return (YES);
422                         break;
423                 case HOST_TYPE:
424                         if (!strcasecmp(step->name, bp->ut_host))
425                                 return (YES);
426                         break;
427                 case TTY_TYPE:
428                         if (!strcmp(step->name, bp->ut_line))
429                                 return (YES);
430                         break;
431                 case USER_TYPE:
432                         if (!strcmp(step->name, bp->ut_user))
433                                 return (YES);
434                         break;
435                 }
436         return (NO);
437 }
438
439 /*
440  * addarg --
441  *      add an entry to a linked list of arguments
442  */
443 static void
444 addarg(int type, char *arg)
445 {
446         ARG *cur;
447
448         if ((cur = malloc(sizeof(ARG))) == NULL)
449                 xo_errx(1, "malloc failure");
450         cur->next = arglist;
451         cur->type = type;
452         cur->name = arg;
453         arglist = cur;
454 }
455
456 /*
457  * hostconv --
458  *      convert the hostname to search pattern; if the supplied host name
459  *      has a domain attached that is the same as the current domain, rip
460  *      off the domain suffix since that's what login(1) does.
461  */
462 static void
463 hostconv(char *arg)
464 {
465         static int first = 1;
466         static char *hostdot, name[MAXHOSTNAMELEN];
467         char *argdot;
468
469         if (!(argdot = strchr(arg, '.')))
470                 return;
471         if (first) {
472                 first = 0;
473                 if (gethostname(name, sizeof(name)))
474                         xo_err(1, "gethostname");
475                 hostdot = strchr(name, '.');
476         }
477         if (hostdot && !strcasecmp(hostdot, argdot))
478                 *argdot = '\0';
479 }
480
481 /*
482  * ttyconv --
483  *      convert tty to correct name.
484  */
485 static char *
486 ttyconv(char *arg)
487 {
488         char *mval;
489
490         /*
491          * kludge -- we assume that all tty's end with
492          * a two character suffix.
493          */
494         if (strlen(arg) == 2) {
495                 /* either 6 for "ttyxx" or 8 for "console" */
496                 if ((mval = malloc(8)) == NULL)
497                         xo_errx(1, "malloc failure");
498                 if (!strcmp(arg, "co"))
499                         (void)strcpy(mval, "console");
500                 else {
501                         (void)strcpy(mval, "tty");
502                         (void)strcpy(mval + 3, arg);
503                 }
504                 return (mval);
505         }
506         if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
507                 return (arg + 5);
508         return (arg);
509 }
510
511 /*
512  * dateconv --
513  *      Convert the snapshot time in command line given in the format
514  *      [[CC]YY]MMDDhhmm[.SS]] to a time_t.
515  *      Derived from atime_arg1() in usr.bin/touch/touch.c
516  */
517 static time_t
518 dateconv(char *arg)
519 {
520         time_t timet;
521         struct tm *t;
522         int yearset;
523         char *p;
524
525         /* Start with the current time. */
526         if (time(&timet) < 0)
527                 xo_err(1, "time");
528         if ((t = localtime(&timet)) == NULL)
529                 xo_err(1, "localtime");
530
531         /* [[CC]YY]MMDDhhmm[.SS] */
532         if ((p = strchr(arg, '.')) == NULL)
533                 t->tm_sec = 0;          /* Seconds defaults to 0. */
534         else {
535                 if (strlen(p + 1) != 2)
536                         goto terr;
537                 *p++ = '\0';
538                 t->tm_sec = ATOI2(p);
539         }
540
541         yearset = 0;
542         switch (strlen(arg)) {
543         case 12:                        /* CCYYMMDDhhmm */
544                 t->tm_year = ATOI2(arg);
545                 t->tm_year *= 100;
546                 yearset = 1;
547                 /* FALLTHROUGH */
548         case 10:                        /* YYMMDDhhmm */
549                 if (yearset) {
550                         yearset = ATOI2(arg);
551                         t->tm_year += yearset;
552                 } else {
553                         yearset = ATOI2(arg);
554                         if (yearset < 69)
555                                 t->tm_year = yearset + 2000;
556                         else
557                                 t->tm_year = yearset + 1900;
558                 }
559                 t->tm_year -= 1900;     /* Convert to UNIX time. */
560                 /* FALLTHROUGH */
561         case 8:                         /* MMDDhhmm */
562                 t->tm_mon = ATOI2(arg);
563                 --t->tm_mon;            /* Convert from 01-12 to 00-11 */
564                 t->tm_mday = ATOI2(arg);
565                 t->tm_hour = ATOI2(arg);
566                 t->tm_min = ATOI2(arg);
567                 break;
568         case 4:                         /* hhmm */
569                 t->tm_hour = ATOI2(arg);
570                 t->tm_min = ATOI2(arg);
571                 break;
572         default:
573                 goto terr;
574         }
575         t->tm_isdst = -1;               /* Figure out DST. */
576         timet = mktime(t);
577         if (timet == -1)
578 terr:           xo_errx(1,
579         "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
580         return timet;
581 }