2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 2018 Philip Paeps
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. 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.
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
33 #include <sys/param.h>
34 #include <sys/capsicum.h>
35 #include <sys/queue.h>
38 #include <capsicum_helpers.h>
56 #define NO 0 /* false/no */
57 #define YES 1 /* true/yes */
58 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
61 char *name; /* argument */
62 #define REBOOT_TYPE -1
66 int type; /* type of arg */
67 struct arg *next; /* linked list pointer */
69 static ARG *arglist; /* head of linked list */
71 static SLIST_HEAD(, idtab) idlist;
74 time_t logout; /* log out time */
75 char id[sizeof ((struct utmpx *)0)->ut_id]; /* identifier */
76 SLIST_ENTRY(idtab) list;
79 static const char *crmsg; /* cause of last reboot */
80 static time_t currentout; /* current logout value */
81 static long maxrec; /* records to display */
82 static const char *file = NULL; /* utx.log file */
83 static int sflag = 0; /* show delta in seconds */
84 static int width = 5; /* show seconds in delta */
85 static int yflag; /* show year */
87 static int snapfound = 0; /* found snapshot entry? */
88 static time_t snaptime; /* if != 0, we will only
89 * report users logged in
90 * at this snapshot time
93 static void addarg(int, char *);
94 static time_t dateconv(char *);
95 static void doentry(struct utmpx *);
96 static void hostconv(char *);
97 static void printentry(struct utmpx *, struct idtab *);
98 static char *ttyconv(char *);
99 static int want(struct utmpx *);
100 static void usage(void);
101 static void wtmp(void);
107 "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
108 " [-n maxrec] [-t tty] [user ...]\n");
113 main(int argc, char *argv[])
118 (void) setlocale(LC_TIME, "");
119 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
121 argc = xo_parse_args(argc, argv);
124 atexit(xo_finish_atexit);
128 while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1)
130 case '0': case '1': case '2': case '3': case '4':
131 case '5': case '6': case '7': case '8': case '9':
133 * kludge: last was originally designed to take
134 * a number after a dash.
137 p = strchr(argv[optind - 1], ch);
139 p = strchr(argv[optind], ch);
146 snaptime = dateconv(optarg);
153 addarg(HOST_TYPE, optarg);
157 maxrec = strtol(optarg, &p, 10);
158 if (p == optarg || *p != '\0' || errno != 0 ||
160 xo_errx(1, "%s: bad line count", optarg);
163 sflag++; /* Show delta as seconds */
166 addarg(TTY_TYPE, ttyconv(optarg));
179 if (caph_limit_stdio() < 0)
180 xo_err(1, "can't limit stdio rights");
182 caph_cache_catpages();
185 /* Cache UTX database. */
186 if (setutxdb(UTXDB_LOG, file) != 0)
187 xo_err(1, "%s", file != NULL ? file : "(default utx db)");
189 if (caph_enter() < 0)
190 xo_err(1, "cap_enter");
192 if (sflag && width == 8) usage();
196 for (argv += optind; *argv; ++argv) {
197 if (strcmp(*argv, "reboot") == 0)
198 addarg(REBOOT_TYPE, *argv);
199 #define COMPATIBILITY
201 /* code to allow "last p5" to work */
202 addarg(TTY_TYPE, ttyconv(*argv));
204 addarg(USER_TYPE, *argv);
213 * read through the utx.log file
218 struct utmpx *buf = NULL;
220 static unsigned int amount = 0;
228 xo_open_container("last-information");
230 /* Load the last entries from the file. */
231 while ((ut = getutxent()) != NULL) {
232 if (amount % 128 == 0) {
233 buf = realloc(buf, (amount + 128) * sizeof *ut);
235 xo_err(1, "realloc");
237 memcpy(&buf[amount++], ut, sizeof *ut);
238 if (t > ut->ut_tv.tv_sec)
239 t = ut->ut_tv.tv_sec;
243 /* Display them in reverse order. */
244 xo_open_list("last");
246 doentry(&buf[--amount]);
247 xo_close_list("last");
250 (void) strftime(ct, sizeof(ct), "%+", tm);
251 xo_emit("\n{:utxdb/%s}", (file == NULL) ? "utx.log" : file);
252 xo_attr("seconds", "%lu", (unsigned long) t);
253 xo_emit(" begins {:begins/%hs}\n", ct);
254 xo_close_container("last-information");
259 * process a single utx.log entry
262 doentry(struct utmpx *bp)
266 /* the machine stopped */
267 if (bp->ut_type == BOOT_TIME || bp->ut_type == SHUTDOWN_TIME) {
268 /* everybody just logged out */
269 while ((tt = SLIST_FIRST(&idlist)) != NULL) {
270 SLIST_REMOVE_HEAD(&idlist, list);
273 currentout = -bp->ut_tv.tv_sec;
274 crmsg = bp->ut_type != SHUTDOWN_TIME ?
275 "crash" : "shutdown";
277 * if we're in snapshot mode, we want to exit if this
278 * shutdown/reboot appears while we are tracking the
281 if (snaptime && snapfound)
284 * don't print shutdown/reboot entries unless flagged for
286 if (!snaptime && want(bp))
287 printentry(bp, NULL);
291 if (bp->ut_type == OLD_TIME || bp->ut_type == NEW_TIME) {
292 if (want(bp) && !snaptime)
293 printentry(bp, NULL);
297 if (bp->ut_type != USER_PROCESS && bp->ut_type != DEAD_PROCESS)
300 /* find associated identifier */
301 SLIST_FOREACH(tt, &idlist, list)
302 if (!memcmp(tt->id, bp->ut_id, sizeof bp->ut_id))
307 tt = malloc(sizeof(struct idtab));
309 xo_errx(1, "malloc failure");
310 tt->logout = currentout;
311 memcpy(tt->id, bp->ut_id, sizeof bp->ut_id);
312 SLIST_INSERT_HEAD(&idlist, tt, list);
316 * print record if not in snapshot mode and wanted
317 * or in snapshot mode and in snapshot range
319 if (bp->ut_type == USER_PROCESS && (want(bp) ||
320 (bp->ut_tv.tv_sec < snaptime &&
321 (tt->logout > snaptime || tt->logout < 1)))) {
325 tt->logout = bp->ut_tv.tv_sec;
332 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
333 * logout type (crash/shutdown) as appropriate.
336 printentry(struct utmpx *bp, struct idtab *tt)
340 time_t delta; /* time difference */
343 if (maxrec != -1 && !maxrec--)
345 xo_open_instance("last");
346 t = bp->ut_tv.tv_sec;
348 (void) strftime(ct, sizeof(ct), d_first ?
349 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
350 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
351 switch (bp->ut_type) {
353 xo_emit("{:user/%-42s/%s}", "boot time");
356 xo_emit("{:user/%-42s/%s}", "shutdown time");
359 xo_emit("{:user/%-42s/%s}", "old time");
362 xo_emit("{:user/%-42s/%s}", "new time");
365 xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
366 bp->ut_user, bp->ut_line, bp->ut_host);
369 xo_attr("seconds", "%lu", (unsigned long)t);
370 xo_emit(" {:login-time/%hs%c/%s}", ct, tt == NULL ? '\n' : ' ');
374 xo_emit(" {:logout-time/still logged in}\n");
377 if (tt->logout < 0) {
378 tt->logout = -tt->logout;
379 xo_emit("- {:logout-reason/%s}", crmsg);
381 tm = localtime(&tt->logout);
382 (void) strftime(ct, sizeof(ct), "%R", tm);
383 xo_attr("seconds", "%lu", (unsigned long)tt->logout);
384 xo_emit("- {:logout-time/%s}", ct);
386 delta = tt->logout - bp->ut_tv.tv_sec;
387 xo_attr("seconds", "%ld", (long)delta);
389 xo_emit(" ({:session-length/%8ld})\n", (long)delta);
392 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
394 xo_emit(" ({:session-length/%s})\n", ct);
396 xo_emit(" ({:session-length/%ld+%s})\n",
397 (long)delta / 86400, ct);
401 xo_close_instance("last");
406 * see if want this entry
409 want(struct utmpx *bp)
419 for (step = arglist; step; step = step->next)
422 if (bp->ut_type == BOOT_TIME ||
423 bp->ut_type == SHUTDOWN_TIME)
427 if (!strcasecmp(step->name, bp->ut_host))
431 if (!strcmp(step->name, bp->ut_line))
435 if (!strcmp(step->name, bp->ut_user))
444 * add an entry to a linked list of arguments
447 addarg(int type, char *arg)
451 if ((cur = malloc(sizeof(ARG))) == NULL)
452 xo_errx(1, "malloc failure");
461 * convert the hostname to search pattern; if the supplied host name
462 * has a domain attached that is the same as the current domain, rip
463 * off the domain suffix since that's what login(1) does.
468 static int first = 1;
469 static char *hostdot, name[MAXHOSTNAMELEN];
472 if (!(argdot = strchr(arg, '.')))
476 if (gethostname(name, sizeof(name)))
477 xo_err(1, "gethostname");
478 hostdot = strchr(name, '.');
480 if (hostdot && !strcasecmp(hostdot, argdot))
486 * convert tty to correct name.
494 * kludge -- we assume that all tty's end with
495 * a two character suffix.
497 if (strlen(arg) == 2) {
498 /* either 6 for "ttyxx" or 8 for "console" */
499 if ((mval = malloc(8)) == NULL)
500 xo_errx(1, "malloc failure");
501 if (!strcmp(arg, "co"))
502 (void)strcpy(mval, "console");
504 (void)strcpy(mval, "tty");
505 (void)strcpy(mval + 3, arg);
509 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
516 * Convert the snapshot time in command line given in the format
517 * [[CC]YY]MMDDhhmm[.SS]] to a time_t.
518 * Derived from atime_arg1() in usr.bin/touch/touch.c
528 /* Start with the current time. */
529 if (time(&timet) < 0)
531 if ((t = localtime(&timet)) == NULL)
532 xo_err(1, "localtime");
534 /* [[CC]YY]MMDDhhmm[.SS] */
535 if ((p = strchr(arg, '.')) == NULL)
536 t->tm_sec = 0; /* Seconds defaults to 0. */
538 if (strlen(p + 1) != 2)
541 t->tm_sec = ATOI2(p);
545 switch (strlen(arg)) {
546 case 12: /* CCYYMMDDhhmm */
547 t->tm_year = ATOI2(arg);
551 case 10: /* YYMMDDhhmm */
553 yearset = ATOI2(arg);
554 t->tm_year += yearset;
556 yearset = ATOI2(arg);
558 t->tm_year = yearset + 2000;
560 t->tm_year = yearset + 1900;
562 t->tm_year -= 1900; /* Convert to UNIX time. */
564 case 8: /* MMDDhhmm */
565 t->tm_mon = ATOI2(arg);
566 --t->tm_mon; /* Convert from 01-12 to 00-11 */
567 t->tm_mday = ATOI2(arg);
568 t->tm_hour = ATOI2(arg);
569 t->tm_min = ATOI2(arg);
572 t->tm_hour = ATOI2(arg);
573 t->tm_min = ATOI2(arg);
578 t->tm_isdst = -1; /* Figure out DST. */
582 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");