Annotation of src/usr.bin/last/last.c, Revision 1.14
1.14 ! fgsch 1: /* $OpenBSD: last.c,v 1.13 2001/01/31 17:42:26 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: last.c,v 1.6 1994/12/24 16:49:02 cgd Exp $ */
3:
4: /*
5: * Copyright (c) 1987, 1993, 1994
6: * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1987, 1993, 1994\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
46: #endif
1.14 ! fgsch 47: static char rcsid[] = "$OpenBSD: last.c,v 1.13 2001/01/31 17:42:26 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/param.h>
51: #include <sys/stat.h>
52:
53: #include <err.h>
54: #include <fcntl.h>
55: #include <paths.h>
56: #include <signal.h>
57: #include <stdio.h>
58: #include <stdlib.h>
59: #include <string.h>
60: #include <time.h>
61: #include <tzfile.h>
62: #include <unistd.h>
63: #include <utmp.h>
64:
65: #define NO 0 /* false/no */
66: #define YES 1 /* true/yes */
1.4 jdm 67: #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
1.1 deraadt 68:
69: static struct utmp buf[1024]; /* utmp read buffer */
70:
71: typedef struct arg {
72: char *name; /* argument */
73: #define HOST_TYPE -2
74: #define TTY_TYPE -3
75: #define USER_TYPE -4
76: int type; /* type of arg */
77: struct arg *next; /* linked list pointer */
78: } ARG;
79: ARG *arglist; /* head of linked list */
80:
81: typedef struct ttytab {
82: time_t logout; /* log out time */
83: char tty[UT_LINESIZE + 1]; /* terminal name */
84: struct ttytab *next; /* linked list pointer */
85: } TTY;
86: TTY *ttylist; /* head of linked list */
87:
88: static time_t currentout; /* current logout value */
89: static long maxrec; /* records to display */
90: static char *file = _PATH_WTMP; /* wtmp file */
1.6 mickey 91: static int fulltime = 0; /* Display seconds? */
1.7 deraadt 92: static time_t snaptime; /* if != 0, we will only
93: * report users logged in
94: * at this snapshot time
1.4 jdm 95: */
1.9 downsj 96: static int calculate = 0;
97: static int seconds = 0;
1.1 deraadt 98:
99: void addarg __P((int, char *));
100: TTY *addtty __P((char *));
101: void hostconv __P((char *));
102: void onintr __P((int));
103: char *ttyconv __P((char *));
1.4 jdm 104: time_t dateconv __P((char *));
1.1 deraadt 105: int want __P((struct utmp *, int));
106: void wtmp __P((void));
1.5 jdm 107: void checkargs __P((void));
1.1 deraadt 108:
1.13 deraadt 109: #define NAME_WIDTH 8
1.14 ! fgsch 110: #define HOST_WIDTH 24
1.13 deraadt 111:
1.1 deraadt 112: int
113: main(argc, argv)
114: int argc;
115: char *argv[];
116: {
117: extern int optind;
118: extern char *optarg;
119: int ch;
120: char *p;
121:
122: maxrec = -1;
1.4 jdm 123: snaptime = 0;
1.9 downsj 124: while ((ch = getopt(argc, argv, "0123456789cf:h:st:d:T")) != -1)
1.1 deraadt 125: switch (ch) {
126: case '0': case '1': case '2': case '3': case '4':
127: case '5': case '6': case '7': case '8': case '9':
128: /*
129: * kludge: last was originally designed to take
130: * a number after a dash.
131: */
132: if (maxrec == -1) {
133: p = argv[optind - 1];
134: if (p[0] == '-' && p[1] == ch && !p[2])
135: maxrec = atol(++p);
136: else
137: maxrec = atol(argv[optind] + 1);
138: if (!maxrec)
139: exit(0);
140: }
141: break;
1.9 downsj 142: case 'c':
143: calculate++;
144: break;
1.1 deraadt 145: case 'f':
146: file = optarg;
147: break;
148: case 'h':
149: hostconv(optarg);
150: addarg(HOST_TYPE, optarg);
151: break;
1.9 downsj 152: case 's':
153: seconds++;
154: break;
1.1 deraadt 155: case 't':
156: addarg(TTY_TYPE, ttyconv(optarg));
157: break;
1.4 jdm 158: case 'd':
159: snaptime = dateconv(optarg);
160: break;
1.6 mickey 161: case 'T':
1.9 downsj 162: fulltime = 1;
1.6 mickey 163: break;
1.1 deraadt 164: case '?':
165: default:
166: (void)fprintf(stderr,
1.9 downsj 167: "usage: last [-#] [-cns] [-f file] [-T] [-t tty]"
1.12 deraadt 168: " [-h host] [-d [[CC]YY][MMDD]hhmm[.SS]"
1.7 deraadt 169: " [user ...]\n");
1.1 deraadt 170: exit(1);
171: }
172:
173: if (argc) {
174: setlinebuf(stdout);
175: for (argv += optind; *argv; ++argv) {
176: #define COMPATIBILITY
177: #ifdef COMPATIBILITY
178: /* code to allow "last p5" to work */
179: addarg(TTY_TYPE, ttyconv(*argv));
180: #endif
181: addarg(USER_TYPE, *argv);
182: }
183: }
1.5 jdm 184:
185: checkargs();
1.1 deraadt 186: wtmp();
187: exit(0);
188: }
189:
190: /*
1.5 jdm 191: * checkargs --
192: * if snaptime is set, print warning if usernames, or -t or -h
193: * flags are also provided
194: */
195:
196: void
197: checkargs()
198: {
199: ARG *step;
200: int ttyflag = 0;
201:
202: if (!snaptime)
203: return;
204:
205: if (!arglist)
206: return;
207:
208: for (step = arglist; step; step = step->next)
209: switch (step->type) {
210: case HOST_TYPE:
1.7 deraadt 211: (void)fprintf(stderr,
212: "Warning: Ignoring hostname flag\n");
1.5 jdm 213: break;
214: case TTY_TYPE:
215: if (!ttyflag) { /* don't print this twice */
1.7 deraadt 216: (void)fprintf(stderr,
217: "Warning: Ignoring tty flag\n");
1.5 jdm 218: ttyflag = 1;
219: }
220: break;
221: case USER_TYPE:
1.7 deraadt 222: (void)fprintf(stderr,
223: "Warning: Ignoring username[s]\n");
1.5 jdm 224: break;
225: default:
226: /* PRINT NOTHING */
227: }
228: }
229:
230:
231: /*
1.1 deraadt 232: * wtmp --
233: * read through the wtmp file
234: */
235: void
236: wtmp()
237: {
1.6 mickey 238: struct utmp *bp; /* current structure */
239: TTY *T; /* tty list entry */
240: struct stat stb; /* stat of file for size */
1.8 deraadt 241: time_t delta; /* time difference */
1.9 downsj 242: time_t total;
1.8 deraadt 243: off_t bl;
1.6 mickey 244: int timesize; /* how long time string gonna be */
1.1 deraadt 245: int bytes, wfd;
246: char *ct, *crmsg;
1.6 mickey 247: int snapfound = 0; /* found snapshot entry? */
1.1 deraadt 248: if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
249: err(1, "%s", file);
250: bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
251:
1.6 mickey 252: if (fulltime)
253: timesize = 8; /* HH:MM:SS */
254: else
255: timesize = 5; /* HH:MM */
256:
1.1 deraadt 257: (void)time(&buf[0].ut_time);
258: (void)signal(SIGINT, onintr);
259: (void)signal(SIGQUIT, onintr);
260:
261: while (--bl >= 0) {
1.11 millert 262: if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
1.1 deraadt 263: (bytes = read(wfd, buf, sizeof(buf))) == -1)
264: err(1, "%s", file);
265: for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
266: /*
267: * if the terminal line is '~', the machine stopped.
268: * see utmp(5) for more info.
269: */
270: if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
271: /* everybody just logged out */
272: for (T = ttylist; T; T = T->next)
273: T->logout = -bp->ut_time;
274: currentout = -bp->ut_time;
275: crmsg = strncmp(bp->ut_name, "shutdown",
276: UT_NAMESIZE) ? "crash" : "shutdown";
1.4 jdm 277: /*
278: * if we're in snapshop mode, we want to
279: * exit if this shutdown/reboot appears
280: * while we we are tracking the active
281: * range
282: */
283: if (snaptime && snapfound)
284: return;
285: /*
286: * don't print shutdown/reboot entries
1.5 jdm 287: * unless flagged for
1.4 jdm 288: */
1.5 jdm 289: if (want(bp, NO)) {
1.9 downsj 290: if (seconds) {
291: printf("%-*.*s %-*.*s %-*.*s %ld \n",
1.13 deraadt 292: NAME_WIDTH, UT_NAMESIZE,
1.9 downsj 293: bp->ut_name, UT_LINESIZE,
294: UT_LINESIZE, bp->ut_line,
1.13 deraadt 295: HOST_WIDTH, UT_HOSTSIZE,
1.9 downsj 296: bp->ut_host, bp->ut_time);
297: } else {
298: ct = ctime(&bp->ut_time);
1.6 mickey 299: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
1.13 deraadt 300: NAME_WIDTH, UT_NAMESIZE,
1.9 downsj 301: bp->ut_name, UT_LINESIZE,
302: UT_LINESIZE, bp->ut_line,
1.13 deraadt 303: HOST_WIDTH, UT_HOSTSIZE,
1.9 downsj 304: bp->ut_host, ct, timesize,
305: timesize, ct + 11);
306: }
1.1 deraadt 307: if (maxrec != -1 && !--maxrec)
308: return;
309: }
310: continue;
311: }
312: /*
313: * if the line is '{' or '|', date got set; see
314: * utmp(5) for more info.
315: */
316: if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
317: && !bp->ut_line[1]) {
1.5 jdm 318: if (want(bp, NO)) {
1.9 downsj 319: if (seconds) {
320: printf("%-*.*s %-*.*s %-*.*s %ld \n",
1.13 deraadt 321: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
1.9 downsj 322: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
1.13 deraadt 323: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
1.9 downsj 324: bp->ut_time);
325: } else {
326: ct = ctime(&bp->ut_time);
1.6 mickey 327: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
1.13 deraadt 328: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
1.9 downsj 329: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
1.13 deraadt 330: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
1.9 downsj 331: ct, timesize, timesize, ct + 11);
332: }
1.1 deraadt 333: if (maxrec && !--maxrec)
334: return;
335: }
336: continue;
337: }
338: /* find associated tty */
339: for (T = ttylist;; T = T->next) {
340: if (!T) {
341: /* add new one */
342: T = addtty(bp->ut_line);
343: break;
344: }
345: if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
346: break;
347: }
1.4 jdm 348: /*
349: * print record if not in snapshot mode and wanted
350: * or in snapshot mode and in snapshot range
351: */
352: if (bp->ut_name[0] &&
1.5 jdm 353: ((want(bp, YES)) ||
1.4 jdm 354: (bp->ut_time < snaptime &&
355: (T->logout > snaptime || !T->logout ||
356: T->logout < 0)))) {
357: snapfound = 1;
1.9 downsj 358: if (seconds) {
359: printf("%-*.*s %-*.*s %-*.*s %ld ",
1.13 deraadt 360: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
1.9 downsj 361: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
1.13 deraadt 362: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
1.9 downsj 363: bp->ut_time);
364: } else {
365: ct = ctime(&bp->ut_time);
1.6 mickey 366: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
1.13 deraadt 367: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
1.6 mickey 368: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
1.13 deraadt 369: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
1.6 mickey 370: ct, timesize, timesize, ct + 11);
1.9 downsj 371: }
1.1 deraadt 372: if (!T->logout)
373: puts(" still logged in");
374: else {
375: if (T->logout < 0) {
376: T->logout = -T->logout;
377: printf("- %s", crmsg);
1.9 downsj 378: } else {
379: if (seconds)
380: printf("- %ld",
381: T->logout);
382: else
383: printf("- %*.*s",
384: timesize, timesize,
385: ctime(&T->logout)+11);
1.1 deraadt 386: }
387: delta = T->logout - bp->ut_time;
1.9 downsj 388: if (seconds)
389: printf(" (%ld)\n", delta);
390: else {
391: if (delta < SECSPERDAY)
392: printf(" (%*.*s)\n",
393: timesize, timesize,
394: asctime(gmtime(&delta))+11);
395: else
396: printf(" (%ld+%*.*s)\n",
397: delta / SECSPERDAY,
398: timesize, timesize,
399: asctime(gmtime(&delta))+11);
400: }
401: if (calculate)
402: total += delta;
1.1 deraadt 403: }
404: if (maxrec != -1 && !--maxrec)
405: return;
406: }
407: T->logout = bp->ut_time;
408: }
1.9 downsj 409: }
410: if (calculate) {
411: if ((total / SECSPERDAY) > 0) {
412: int days = (total / SECSPERDAY);
413: total -= (days * SECSPERDAY);
414:
415: printf("\nTotal time: %d days, %*.*s\n",
416: days, timesize, timesize,
417: asctime(gmtime(&total))+11);
418: } else
419: printf("\nTotal time: %*.*s\n",
420: timesize, timesize,
421: asctime(gmtime(&total))+11);
1.1 deraadt 422: }
423: ct = ctime(&buf[0].ut_time);
1.10 deraadt 424: printf("\nwtmp begins %10.10s %*.*s %4.4s\n", ct, timesize, timesize,
425: ct + 11, ct + 20);
1.1 deraadt 426: }
427:
428: /*
429: * want --
430: * see if want this entry
431: */
432: int
433: want(bp, check)
434: struct utmp *bp;
435: int check;
436: {
437: ARG *step;
438:
439: if (check)
440: /*
441: * when uucp and ftp log in over a network, the entry in
442: * the utmp file is the name plus their process id. See
443: * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
444: */
445: if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
446: bp->ut_line[3] = '\0';
447: else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
448: bp->ut_line[4] = '\0';
1.5 jdm 449:
450: if (snaptime) /* if snaptime is set, return NO */
451: return (NO);
452:
1.1 deraadt 453: if (!arglist)
454: return (YES);
455:
456: for (step = arglist; step; step = step->next)
457: switch(step->type) {
458: case HOST_TYPE:
459: if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
460: return (YES);
461: break;
462: case TTY_TYPE:
463: if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
464: return (YES);
465: break;
466: case USER_TYPE:
467: if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
468: return (YES);
469: break;
1.5 jdm 470: }
471:
1.1 deraadt 472: return (NO);
473: }
474:
475: /*
476: * addarg --
477: * add an entry to a linked list of arguments
478: */
479: void
480: addarg(type, arg)
481: int type;
482: char *arg;
483: {
484: ARG *cur;
485:
486: if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
487: err(1, "malloc failure");
488: cur->next = arglist;
489: cur->type = type;
490: cur->name = arg;
491: arglist = cur;
492: }
493:
494: /*
495: * addtty --
496: * add an entry to a linked list of ttys
497: */
498: TTY *
499: addtty(ttyname)
500: char *ttyname;
501: {
502: TTY *cur;
503:
504: if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
505: err(1, "malloc failure");
506: cur->next = ttylist;
507: cur->logout = currentout;
508: memmove(cur->tty, ttyname, UT_LINESIZE);
509: return (ttylist = cur);
510: }
511:
512: /*
513: * hostconv --
514: * convert the hostname to search pattern; if the supplied host name
515: * has a domain attached that is the same as the current domain, rip
516: * off the domain suffix since that's what login(1) does.
517: */
518: void
519: hostconv(arg)
520: char *arg;
521: {
522: static int first = 1;
523: static char *hostdot, name[MAXHOSTNAMELEN];
524: char *argdot;
525:
526: if (!(argdot = strchr(arg, '.')))
527: return;
528: if (first) {
529: first = 0;
530: if (gethostname(name, sizeof(name)))
531: err(1, "gethostname");
532: hostdot = strchr(name, '.');
533: }
534: if (hostdot && !strcasecmp(hostdot, argdot))
535: *argdot = '\0';
536: }
537:
538: /*
539: * ttyconv --
540: * convert tty to correct name.
541: */
542: char *
543: ttyconv(arg)
544: char *arg;
545: {
546: char *mval;
547:
548: /*
549: * kludge -- we assume that all tty's end with
550: * a two character suffix.
551: */
552: if (strlen(arg) == 2) {
553: /* either 6 for "ttyxx" or 8 for "console" */
554: if (!(mval = malloc((u_int)8)))
555: err(1, "malloc failure");
556: if (!strcmp(arg, "co"))
557: (void)strcpy(mval, "console");
558: else {
559: (void)strcpy(mval, "tty");
560: (void)strcpy(mval + 3, arg);
561: }
562: return (mval);
563: }
564: if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
565: return (arg + 5);
566: return (arg);
567: }
1.4 jdm 568:
569: /*
570: * dateconv --
571: * Convert the snapshot time in command line given in the format
1.12 deraadt 572: * [[CC]YY][MMDD]hhmm[.ss]] to a time_t.
1.4 jdm 573: * Derived from atime_arg1() in usr.bin/touch/touch.c
574: */
575: time_t
576: dateconv(arg)
1.7 deraadt 577: char *arg;
1.4 jdm 578: {
1.7 deraadt 579: time_t timet;
580: struct tm *t;
581: int yearset;
582: char *p;
583:
584: /* Start with the current time. */
585: if (time(&timet) < 0)
586: err(1, "time");
587: if ((t = localtime(&timet)) == NULL)
588: err(1, "localtime");
589:
590: /* [[yy]yy][mmdd]hhmm[.ss] */
591: if ((p = strchr(arg, '.')) == NULL)
592: t->tm_sec = 0; /* Seconds defaults to 0. */
593: else {
594: if (strlen(p + 1) != 2)
595: goto terr;
596: *p++ = '\0';
597: t->tm_sec = ATOI2(p);
598: }
599:
600: yearset = 0;
601: switch (strlen(arg)) {
602: case 12: /* ccyymmddhhmm */
603: t->tm_year = ATOI2(arg);
604: t->tm_year *= 100;
605: yearset = 1;
606: /* FALLTHOUGH */
607: case 10: /* yymmddhhmm */
608: if (yearset) {
609: yearset = ATOI2(arg);
610: t->tm_year += yearset;
611: } else {
612: yearset = ATOI2(arg);
613: if (yearset < 69)
614: t->tm_year = yearset + 2000;
615: else
616: t->tm_year = yearset + 1900;
617: }
618: t->tm_year -= 1900; /* Convert to UNIX time. */
619: /* FALLTHROUGH */
620: case 8: /* MMDDhhmm */
621: t->tm_mon = ATOI2(arg);
622: --t->tm_mon; /* Convert from 01-12 to 00-11 */
623: t->tm_mday = ATOI2(arg);
624: t->tm_hour = ATOI2(arg);
625: t->tm_min = ATOI2(arg);
626: break;
627: case 4: /* hhmm */
628: t->tm_hour = ATOI2(arg);
629: t->tm_min = ATOI2(arg);
630: break;
631: default:
632: goto terr;
633: }
634: t->tm_isdst = -1; /* Figure out DST. */
635: timet = mktime(t);
636: if (timet == -1)
637: terr: errx(1,
638: "out of range or illegal time specification: [[yy]yy][mmdd]hhmm[.ss]");
639: return timet;
1.4 jdm 640: }
641:
1.1 deraadt 642:
643: /*
644: * onintr --
645: * on interrupt, we inform the user how far we've gotten
646: */
647: void
648: onintr(signo)
649: int signo;
650: {
651: char *ct;
652:
653: ct = ctime(&buf[0].ut_time);
1.6 mickey 654: printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
1.1 deraadt 655: if (signo == SIGINT)
656: exit(1);
657: (void)fflush(stdout); /* fix required for rsh */
658: }