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