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