Annotation of src/usr.bin/last/last.c, Revision 1.11
1.11 ! millert 1: /* $OpenBSD: last.c,v 1.10 1998/04/25 00:40:18 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.11 ! millert 47: static char rcsid[] = "$OpenBSD: last.c,v 1.10 1998/04/25 00:40:18 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:
109: int
110: main(argc, argv)
111: int argc;
112: char *argv[];
113: {
114: extern int optind;
115: extern char *optarg;
116: int ch;
117: char *p;
118:
119: maxrec = -1;
1.4 jdm 120: snaptime = 0;
1.9 downsj 121: while ((ch = getopt(argc, argv, "0123456789cf:h:st:d:T")) != -1)
1.1 deraadt 122: switch (ch) {
123: case '0': case '1': case '2': case '3': case '4':
124: case '5': case '6': case '7': case '8': case '9':
125: /*
126: * kludge: last was originally designed to take
127: * a number after a dash.
128: */
129: if (maxrec == -1) {
130: p = argv[optind - 1];
131: if (p[0] == '-' && p[1] == ch && !p[2])
132: maxrec = atol(++p);
133: else
134: maxrec = atol(argv[optind] + 1);
135: if (!maxrec)
136: exit(0);
137: }
138: break;
1.9 downsj 139: case 'c':
140: calculate++;
141: break;
1.1 deraadt 142: case 'f':
143: file = optarg;
144: break;
145: case 'h':
146: hostconv(optarg);
147: addarg(HOST_TYPE, optarg);
148: break;
1.9 downsj 149: case 's':
150: seconds++;
151: break;
1.1 deraadt 152: case 't':
153: addarg(TTY_TYPE, ttyconv(optarg));
154: break;
1.4 jdm 155: case 'd':
156: snaptime = dateconv(optarg);
157: break;
1.6 mickey 158: case 'T':
1.9 downsj 159: fulltime = 1;
1.6 mickey 160: break;
1.1 deraadt 161: case '?':
162: default:
163: (void)fprintf(stderr,
1.9 downsj 164: "usage: last [-#] [-cns] [-f file] [-T] [-t tty]"
1.7 deraadt 165: " [-h host] [-d [[yy]yy[mm[dd[hh]]]]mm[.ss]]"
166: " [user ...]\n");
1.1 deraadt 167: exit(1);
168: }
169:
170: if (argc) {
171: setlinebuf(stdout);
172: for (argv += optind; *argv; ++argv) {
173: #define COMPATIBILITY
174: #ifdef COMPATIBILITY
175: /* code to allow "last p5" to work */
176: addarg(TTY_TYPE, ttyconv(*argv));
177: #endif
178: addarg(USER_TYPE, *argv);
179: }
180: }
1.5 jdm 181:
182: checkargs();
1.1 deraadt 183: wtmp();
184: exit(0);
185: }
186:
187: /*
1.5 jdm 188: * checkargs --
189: * if snaptime is set, print warning if usernames, or -t or -h
190: * flags are also provided
191: */
192:
193: void
194: checkargs()
195: {
196: ARG *step;
197: int ttyflag = 0;
198:
199: if (!snaptime)
200: return;
201:
202: if (!arglist)
203: return;
204:
205: for (step = arglist; step; step = step->next)
206: switch (step->type) {
207: case HOST_TYPE:
1.7 deraadt 208: (void)fprintf(stderr,
209: "Warning: Ignoring hostname flag\n");
1.5 jdm 210: break;
211: case TTY_TYPE:
212: if (!ttyflag) { /* don't print this twice */
1.7 deraadt 213: (void)fprintf(stderr,
214: "Warning: Ignoring tty flag\n");
1.5 jdm 215: ttyflag = 1;
216: }
217: break;
218: case USER_TYPE:
1.7 deraadt 219: (void)fprintf(stderr,
220: "Warning: Ignoring username[s]\n");
1.5 jdm 221: break;
222: default:
223: /* PRINT NOTHING */
224: }
225: }
226:
227:
228: /*
1.1 deraadt 229: * wtmp --
230: * read through the wtmp file
231: */
232: void
233: wtmp()
234: {
1.6 mickey 235: struct utmp *bp; /* current structure */
236: TTY *T; /* tty list entry */
237: struct stat stb; /* stat of file for size */
1.8 deraadt 238: time_t delta; /* time difference */
1.9 downsj 239: time_t total;
1.8 deraadt 240: off_t bl;
1.6 mickey 241: int timesize; /* how long time string gonna be */
1.1 deraadt 242: int bytes, wfd;
243: char *ct, *crmsg;
1.6 mickey 244: int snapfound = 0; /* found snapshot entry? */
1.1 deraadt 245: if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
246: err(1, "%s", file);
247: bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
248:
1.6 mickey 249: if (fulltime)
250: timesize = 8; /* HH:MM:SS */
251: else
252: timesize = 5; /* HH:MM */
253:
1.1 deraadt 254: (void)time(&buf[0].ut_time);
255: (void)signal(SIGINT, onintr);
256: (void)signal(SIGQUIT, onintr);
257:
258: while (--bl >= 0) {
1.11 ! millert 259: if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
1.1 deraadt 260: (bytes = read(wfd, buf, sizeof(buf))) == -1)
261: err(1, "%s", file);
262: for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
263: /*
264: * if the terminal line is '~', the machine stopped.
265: * see utmp(5) for more info.
266: */
267: if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
268: /* everybody just logged out */
269: for (T = ttylist; T; T = T->next)
270: T->logout = -bp->ut_time;
271: currentout = -bp->ut_time;
272: crmsg = strncmp(bp->ut_name, "shutdown",
273: UT_NAMESIZE) ? "crash" : "shutdown";
1.4 jdm 274: /*
275: * if we're in snapshop mode, we want to
276: * exit if this shutdown/reboot appears
277: * while we we are tracking the active
278: * range
279: */
280: if (snaptime && snapfound)
281: return;
282: /*
283: * don't print shutdown/reboot entries
1.5 jdm 284: * unless flagged for
1.4 jdm 285: */
1.5 jdm 286: if (want(bp, NO)) {
1.9 downsj 287: if (seconds) {
288: printf("%-*.*s %-*.*s %-*.*s %ld \n",
289: UT_NAMESIZE, UT_NAMESIZE,
290: bp->ut_name, UT_LINESIZE,
291: UT_LINESIZE, bp->ut_line,
292: UT_HOSTSIZE, UT_HOSTSIZE,
293: bp->ut_host, bp->ut_time);
294: } else {
295: ct = ctime(&bp->ut_time);
1.6 mickey 296: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
1.9 downsj 297: UT_NAMESIZE, UT_NAMESIZE,
298: bp->ut_name, UT_LINESIZE,
299: UT_LINESIZE, bp->ut_line,
300: UT_HOSTSIZE, UT_HOSTSIZE,
301: bp->ut_host, ct, timesize,
302: timesize, ct + 11);
303: }
1.1 deraadt 304: if (maxrec != -1 && !--maxrec)
305: return;
306: }
307: continue;
308: }
309: /*
310: * if the line is '{' or '|', date got set; see
311: * utmp(5) for more info.
312: */
313: if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
314: && !bp->ut_line[1]) {
1.5 jdm 315: if (want(bp, NO)) {
1.9 downsj 316: if (seconds) {
317: printf("%-*.*s %-*.*s %-*.*s %ld \n",
318: UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
319: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
320: UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
321: bp->ut_time);
322: } else {
323: ct = ctime(&bp->ut_time);
1.6 mickey 324: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
1.9 downsj 325: UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
326: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
327: UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
328: ct, timesize, timesize, ct + 11);
329: }
1.1 deraadt 330: if (maxrec && !--maxrec)
331: return;
332: }
333: continue;
334: }
335: /* find associated tty */
336: for (T = ttylist;; T = T->next) {
337: if (!T) {
338: /* add new one */
339: T = addtty(bp->ut_line);
340: break;
341: }
342: if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
343: break;
344: }
1.4 jdm 345: /*
346: * print record if not in snapshot mode and wanted
347: * or in snapshot mode and in snapshot range
348: */
349: if (bp->ut_name[0] &&
1.5 jdm 350: ((want(bp, YES)) ||
1.4 jdm 351: (bp->ut_time < snaptime &&
352: (T->logout > snaptime || !T->logout ||
353: T->logout < 0)))) {
354: snapfound = 1;
1.9 downsj 355: if (seconds) {
356: printf("%-*.*s %-*.*s %-*.*s %ld ",
357: UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
358: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
359: UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
360: bp->ut_time);
361: } else {
362: ct = ctime(&bp->ut_time);
1.6 mickey 363: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
364: UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
365: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
366: UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
367: ct, timesize, timesize, ct + 11);
1.9 downsj 368: }
1.1 deraadt 369: if (!T->logout)
370: puts(" still logged in");
371: else {
372: if (T->logout < 0) {
373: T->logout = -T->logout;
374: printf("- %s", crmsg);
1.9 downsj 375: } else {
376: if (seconds)
377: printf("- %ld",
378: T->logout);
379: else
380: printf("- %*.*s",
381: timesize, timesize,
382: ctime(&T->logout)+11);
1.1 deraadt 383: }
384: delta = T->logout - bp->ut_time;
1.9 downsj 385: if (seconds)
386: printf(" (%ld)\n", delta);
387: else {
388: if (delta < SECSPERDAY)
389: printf(" (%*.*s)\n",
390: timesize, timesize,
391: asctime(gmtime(&delta))+11);
392: else
393: printf(" (%ld+%*.*s)\n",
394: delta / SECSPERDAY,
395: timesize, timesize,
396: asctime(gmtime(&delta))+11);
397: }
398: if (calculate)
399: total += delta;
1.1 deraadt 400: }
401: if (maxrec != -1 && !--maxrec)
402: return;
403: }
404: T->logout = bp->ut_time;
405: }
1.9 downsj 406: }
407: if (calculate) {
408: if ((total / SECSPERDAY) > 0) {
409: int days = (total / SECSPERDAY);
410: total -= (days * SECSPERDAY);
411:
412: printf("\nTotal time: %d days, %*.*s\n",
413: days, timesize, timesize,
414: asctime(gmtime(&total))+11);
415: } else
416: printf("\nTotal time: %*.*s\n",
417: timesize, timesize,
418: asctime(gmtime(&total))+11);
1.1 deraadt 419: }
420: ct = ctime(&buf[0].ut_time);
1.10 deraadt 421: printf("\nwtmp begins %10.10s %*.*s %4.4s\n", ct, timesize, timesize,
422: ct + 11, ct + 20);
1.1 deraadt 423: }
424:
425: /*
426: * want --
427: * see if want this entry
428: */
429: int
430: want(bp, check)
431: struct utmp *bp;
432: int check;
433: {
434: ARG *step;
435:
436: if (check)
437: /*
438: * when uucp and ftp log in over a network, the entry in
439: * the utmp file is the name plus their process id. See
440: * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
441: */
442: if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
443: bp->ut_line[3] = '\0';
444: else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
445: bp->ut_line[4] = '\0';
1.5 jdm 446:
447: if (snaptime) /* if snaptime is set, return NO */
448: return (NO);
449:
1.1 deraadt 450: if (!arglist)
451: return (YES);
452:
453: for (step = arglist; step; step = step->next)
454: switch(step->type) {
455: case HOST_TYPE:
456: if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
457: return (YES);
458: break;
459: case TTY_TYPE:
460: if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
461: return (YES);
462: break;
463: case USER_TYPE:
464: if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
465: return (YES);
466: break;
1.5 jdm 467: }
468:
1.1 deraadt 469: return (NO);
470: }
471:
472: /*
473: * addarg --
474: * add an entry to a linked list of arguments
475: */
476: void
477: addarg(type, arg)
478: int type;
479: char *arg;
480: {
481: ARG *cur;
482:
483: if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
484: err(1, "malloc failure");
485: cur->next = arglist;
486: cur->type = type;
487: cur->name = arg;
488: arglist = cur;
489: }
490:
491: /*
492: * addtty --
493: * add an entry to a linked list of ttys
494: */
495: TTY *
496: addtty(ttyname)
497: char *ttyname;
498: {
499: TTY *cur;
500:
501: if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
502: err(1, "malloc failure");
503: cur->next = ttylist;
504: cur->logout = currentout;
505: memmove(cur->tty, ttyname, UT_LINESIZE);
506: return (ttylist = cur);
507: }
508:
509: /*
510: * hostconv --
511: * convert the hostname to search pattern; if the supplied host name
512: * has a domain attached that is the same as the current domain, rip
513: * off the domain suffix since that's what login(1) does.
514: */
515: void
516: hostconv(arg)
517: char *arg;
518: {
519: static int first = 1;
520: static char *hostdot, name[MAXHOSTNAMELEN];
521: char *argdot;
522:
523: if (!(argdot = strchr(arg, '.')))
524: return;
525: if (first) {
526: first = 0;
527: if (gethostname(name, sizeof(name)))
528: err(1, "gethostname");
529: hostdot = strchr(name, '.');
530: }
531: if (hostdot && !strcasecmp(hostdot, argdot))
532: *argdot = '\0';
533: }
534:
535: /*
536: * ttyconv --
537: * convert tty to correct name.
538: */
539: char *
540: ttyconv(arg)
541: char *arg;
542: {
543: char *mval;
544:
545: /*
546: * kludge -- we assume that all tty's end with
547: * a two character suffix.
548: */
549: if (strlen(arg) == 2) {
550: /* either 6 for "ttyxx" or 8 for "console" */
551: if (!(mval = malloc((u_int)8)))
552: err(1, "malloc failure");
553: if (!strcmp(arg, "co"))
554: (void)strcpy(mval, "console");
555: else {
556: (void)strcpy(mval, "tty");
557: (void)strcpy(mval + 3, arg);
558: }
559: return (mval);
560: }
561: if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
562: return (arg + 5);
563: return (arg);
564: }
1.4 jdm 565:
566: /*
567: * dateconv --
568: * Convert the snapshot time in command line given in the format
1.7 deraadt 569: * [[yy]yy][mmdd]hhmm[.ss]] to a time_t.
1.4 jdm 570: * Derived from atime_arg1() in usr.bin/touch/touch.c
571: */
572: time_t
573: dateconv(arg)
1.7 deraadt 574: char *arg;
1.4 jdm 575: {
1.7 deraadt 576: time_t timet;
577: struct tm *t;
578: int yearset;
579: char *p;
580:
581: /* Start with the current time. */
582: if (time(&timet) < 0)
583: err(1, "time");
584: if ((t = localtime(&timet)) == NULL)
585: err(1, "localtime");
586:
587: /* [[yy]yy][mmdd]hhmm[.ss] */
588: if ((p = strchr(arg, '.')) == NULL)
589: t->tm_sec = 0; /* Seconds defaults to 0. */
590: else {
591: if (strlen(p + 1) != 2)
592: goto terr;
593: *p++ = '\0';
594: t->tm_sec = ATOI2(p);
595: }
596:
597: yearset = 0;
598: switch (strlen(arg)) {
599: case 12: /* ccyymmddhhmm */
600: t->tm_year = ATOI2(arg);
601: t->tm_year *= 100;
602: yearset = 1;
603: /* FALLTHOUGH */
604: case 10: /* yymmddhhmm */
605: if (yearset) {
606: yearset = ATOI2(arg);
607: t->tm_year += yearset;
608: } else {
609: yearset = ATOI2(arg);
610: if (yearset < 69)
611: t->tm_year = yearset + 2000;
612: else
613: t->tm_year = yearset + 1900;
614: }
615: t->tm_year -= 1900; /* Convert to UNIX time. */
616: /* FALLTHROUGH */
617: case 8: /* MMDDhhmm */
618: t->tm_mon = ATOI2(arg);
619: --t->tm_mon; /* Convert from 01-12 to 00-11 */
620: t->tm_mday = ATOI2(arg);
621: t->tm_hour = ATOI2(arg);
622: t->tm_min = ATOI2(arg);
623: break;
624: case 4: /* hhmm */
625: t->tm_hour = ATOI2(arg);
626: t->tm_min = ATOI2(arg);
627: break;
628: default:
629: goto terr;
630: }
631: t->tm_isdst = -1; /* Figure out DST. */
632: timet = mktime(t);
633: if (timet == -1)
634: terr: errx(1,
635: "out of range or illegal time specification: [[yy]yy][mmdd]hhmm[.ss]");
636: return timet;
1.4 jdm 637: }
638:
1.1 deraadt 639:
640: /*
641: * onintr --
642: * on interrupt, we inform the user how far we've gotten
643: */
644: void
645: onintr(signo)
646: int signo;
647: {
648: char *ct;
649:
650: ct = ctime(&buf[0].ut_time);
1.6 mickey 651: printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
1.1 deraadt 652: if (signo == SIGINT)
653: exit(1);
654: (void)fflush(stdout); /* fix required for rsh */
655: }