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