Annotation of src/usr.bin/last/last.c, Revision 1.7
1.7 ! deraadt 1: /* $OpenBSD: last.c,v 1.6 1997/07/25 02:23:34 mickey 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.7 ! deraadt 47: static char rcsid[] = "$OpenBSD: last.c,v 1.6 1997/07/25 02:23:34 mickey 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 */
230: time_t bl, delta; /* time difference */
231: int timesize; /* how long time string gonna be */
1.1 deraadt 232: int bytes, wfd;
233: char *ct, *crmsg;
1.6 mickey 234: int snapfound = 0; /* found snapshot entry? */
1.1 deraadt 235: if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
236: err(1, "%s", file);
237: bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
238:
1.6 mickey 239: if (fulltime)
240: timesize = 8; /* HH:MM:SS */
241: else
242: timesize = 5; /* HH:MM */
243:
1.1 deraadt 244: (void)time(&buf[0].ut_time);
245: (void)signal(SIGINT, onintr);
246: (void)signal(SIGQUIT, onintr);
247:
248: while (--bl >= 0) {
249: if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
250: (bytes = read(wfd, buf, sizeof(buf))) == -1)
251: err(1, "%s", file);
252: for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
253: /*
254: * if the terminal line is '~', the machine stopped.
255: * see utmp(5) for more info.
256: */
257: if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
258: /* everybody just logged out */
259: for (T = ttylist; T; T = T->next)
260: T->logout = -bp->ut_time;
261: currentout = -bp->ut_time;
262: crmsg = strncmp(bp->ut_name, "shutdown",
263: UT_NAMESIZE) ? "crash" : "shutdown";
1.4 jdm 264: /*
265: * if we're in snapshop mode, we want to
266: * exit if this shutdown/reboot appears
267: * while we we are tracking the active
268: * range
269: */
270: if (snaptime && snapfound)
271: return;
272: /*
273: * don't print shutdown/reboot entries
1.5 jdm 274: * unless flagged for
1.4 jdm 275: */
1.5 jdm 276: if (want(bp, NO)) {
1.1 deraadt 277: ct = ctime(&bp->ut_time);
1.6 mickey 278: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
1.1 deraadt 279: UT_NAMESIZE, UT_NAMESIZE,
280: bp->ut_name, UT_LINESIZE,
281: UT_LINESIZE, bp->ut_line,
282: UT_HOSTSIZE, UT_HOSTSIZE,
1.6 mickey 283: bp->ut_host, ct, timesize,
284: timesize, ct + 11);
1.1 deraadt 285: if (maxrec != -1 && !--maxrec)
286: return;
287: }
288: continue;
289: }
290: /*
291: * if the line is '{' or '|', date got set; see
292: * utmp(5) for more info.
293: */
294: if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
295: && !bp->ut_line[1]) {
1.5 jdm 296: if (want(bp, NO)) {
1.1 deraadt 297: ct = ctime(&bp->ut_time);
1.6 mickey 298: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
1.1 deraadt 299: UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
300: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
301: UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
1.6 mickey 302: ct, timesize, timesize, ct + 11);
1.1 deraadt 303: if (maxrec && !--maxrec)
304: return;
305: }
306: continue;
307: }
308: /* find associated tty */
309: for (T = ttylist;; T = T->next) {
310: if (!T) {
311: /* add new one */
312: T = addtty(bp->ut_line);
313: break;
314: }
315: if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
316: break;
317: }
1.4 jdm 318: /*
319: * print record if not in snapshot mode and wanted
320: * or in snapshot mode and in snapshot range
321: */
322: if (bp->ut_name[0] &&
1.5 jdm 323: ((want(bp, YES)) ||
1.4 jdm 324: (bp->ut_time < snaptime &&
325: (T->logout > snaptime || !T->logout ||
326: T->logout < 0)))) {
327: snapfound = 1;
1.1 deraadt 328: ct = ctime(&bp->ut_time);
1.6 mickey 329: printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
330: UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
331: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
332: UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
333: ct, timesize, timesize, ct + 11);
1.1 deraadt 334: if (!T->logout)
335: puts(" still logged in");
336: else {
337: if (T->logout < 0) {
338: T->logout = -T->logout;
339: printf("- %s", crmsg);
340: }
341: else
1.6 mickey 342: printf("- %*.*s",
343: timesize, timesize,
1.1 deraadt 344: ctime(&T->logout)+11);
345: delta = T->logout - bp->ut_time;
346: if (delta < SECSPERDAY)
1.6 mickey 347: printf(" (%*.*s)\n",
348: timesize, timesize,
1.1 deraadt 349: asctime(gmtime(&delta))+11);
350: else
1.6 mickey 351: printf(" (%ld+%*.*s)\n",
1.1 deraadt 352: delta / SECSPERDAY,
1.6 mickey 353: timesize, timesize,
1.1 deraadt 354: asctime(gmtime(&delta))+11);
355: }
356: if (maxrec != -1 && !--maxrec)
357: return;
358: }
359: T->logout = bp->ut_time;
360: }
361: }
362: ct = ctime(&buf[0].ut_time);
1.6 mickey 363: printf("\nwtmp begins %10.10s %*.*s \n", ct, timesize, timesize, ct + 11);
1.1 deraadt 364: }
365:
366: /*
367: * want --
368: * see if want this entry
369: */
370: int
371: want(bp, check)
372: struct utmp *bp;
373: int check;
374: {
375: ARG *step;
376:
377: if (check)
378: /*
379: * when uucp and ftp log in over a network, the entry in
380: * the utmp file is the name plus their process id. See
381: * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
382: */
383: if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
384: bp->ut_line[3] = '\0';
385: else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
386: bp->ut_line[4] = '\0';
1.5 jdm 387:
388: if (snaptime) /* if snaptime is set, return NO */
389: return (NO);
390:
1.1 deraadt 391: if (!arglist)
392: return (YES);
393:
394: for (step = arglist; step; step = step->next)
395: switch(step->type) {
396: case HOST_TYPE:
397: if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
398: return (YES);
399: break;
400: case TTY_TYPE:
401: if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
402: return (YES);
403: break;
404: case USER_TYPE:
405: if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
406: return (YES);
407: break;
1.5 jdm 408: }
409:
1.1 deraadt 410: return (NO);
411: }
412:
413: /*
414: * addarg --
415: * add an entry to a linked list of arguments
416: */
417: void
418: addarg(type, arg)
419: int type;
420: char *arg;
421: {
422: ARG *cur;
423:
424: if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
425: err(1, "malloc failure");
426: cur->next = arglist;
427: cur->type = type;
428: cur->name = arg;
429: arglist = cur;
430: }
431:
432: /*
433: * addtty --
434: * add an entry to a linked list of ttys
435: */
436: TTY *
437: addtty(ttyname)
438: char *ttyname;
439: {
440: TTY *cur;
441:
442: if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
443: err(1, "malloc failure");
444: cur->next = ttylist;
445: cur->logout = currentout;
446: memmove(cur->tty, ttyname, UT_LINESIZE);
447: return (ttylist = cur);
448: }
449:
450: /*
451: * hostconv --
452: * convert the hostname to search pattern; if the supplied host name
453: * has a domain attached that is the same as the current domain, rip
454: * off the domain suffix since that's what login(1) does.
455: */
456: void
457: hostconv(arg)
458: char *arg;
459: {
460: static int first = 1;
461: static char *hostdot, name[MAXHOSTNAMELEN];
462: char *argdot;
463:
464: if (!(argdot = strchr(arg, '.')))
465: return;
466: if (first) {
467: first = 0;
468: if (gethostname(name, sizeof(name)))
469: err(1, "gethostname");
470: hostdot = strchr(name, '.');
471: }
472: if (hostdot && !strcasecmp(hostdot, argdot))
473: *argdot = '\0';
474: }
475:
476: /*
477: * ttyconv --
478: * convert tty to correct name.
479: */
480: char *
481: ttyconv(arg)
482: char *arg;
483: {
484: char *mval;
485:
486: /*
487: * kludge -- we assume that all tty's end with
488: * a two character suffix.
489: */
490: if (strlen(arg) == 2) {
491: /* either 6 for "ttyxx" or 8 for "console" */
492: if (!(mval = malloc((u_int)8)))
493: err(1, "malloc failure");
494: if (!strcmp(arg, "co"))
495: (void)strcpy(mval, "console");
496: else {
497: (void)strcpy(mval, "tty");
498: (void)strcpy(mval + 3, arg);
499: }
500: return (mval);
501: }
502: if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
503: return (arg + 5);
504: return (arg);
505: }
1.4 jdm 506:
507: /*
508: * dateconv --
509: * Convert the snapshot time in command line given in the format
1.7 ! deraadt 510: * [[yy]yy][mmdd]hhmm[.ss]] to a time_t.
1.4 jdm 511: * Derived from atime_arg1() in usr.bin/touch/touch.c
512: */
513: time_t
514: dateconv(arg)
1.7 ! deraadt 515: char *arg;
1.4 jdm 516: {
1.7 ! deraadt 517: time_t timet;
! 518: struct tm *t;
! 519: int yearset;
! 520: char *p;
! 521:
! 522: /* Start with the current time. */
! 523: if (time(&timet) < 0)
! 524: err(1, "time");
! 525: if ((t = localtime(&timet)) == NULL)
! 526: err(1, "localtime");
! 527:
! 528: /* [[yy]yy][mmdd]hhmm[.ss] */
! 529: if ((p = strchr(arg, '.')) == NULL)
! 530: t->tm_sec = 0; /* Seconds defaults to 0. */
! 531: else {
! 532: if (strlen(p + 1) != 2)
! 533: goto terr;
! 534: *p++ = '\0';
! 535: t->tm_sec = ATOI2(p);
! 536: }
! 537:
! 538: yearset = 0;
! 539: switch (strlen(arg)) {
! 540: case 12: /* ccyymmddhhmm */
! 541: t->tm_year = ATOI2(arg);
! 542: t->tm_year *= 100;
! 543: yearset = 1;
! 544: /* FALLTHOUGH */
! 545: case 10: /* yymmddhhmm */
! 546: if (yearset) {
! 547: yearset = ATOI2(arg);
! 548: t->tm_year += yearset;
! 549: } else {
! 550: yearset = ATOI2(arg);
! 551: if (yearset < 69)
! 552: t->tm_year = yearset + 2000;
! 553: else
! 554: t->tm_year = yearset + 1900;
! 555: }
! 556: t->tm_year -= 1900; /* Convert to UNIX time. */
! 557: /* FALLTHROUGH */
! 558: case 8: /* MMDDhhmm */
! 559: t->tm_mon = ATOI2(arg);
! 560: --t->tm_mon; /* Convert from 01-12 to 00-11 */
! 561: t->tm_mday = ATOI2(arg);
! 562: t->tm_hour = ATOI2(arg);
! 563: t->tm_min = ATOI2(arg);
! 564: break;
! 565: case 4: /* hhmm */
! 566: t->tm_hour = ATOI2(arg);
! 567: t->tm_min = ATOI2(arg);
! 568: break;
! 569: default:
! 570: goto terr;
! 571: }
! 572: t->tm_isdst = -1; /* Figure out DST. */
! 573: timet = mktime(t);
! 574: if (timet == -1)
! 575: terr: errx(1,
! 576: "out of range or illegal time specification: [[yy]yy][mmdd]hhmm[.ss]");
! 577: return timet;
1.4 jdm 578: }
579:
1.1 deraadt 580:
581: /*
582: * onintr --
583: * on interrupt, we inform the user how far we've gotten
584: */
585: void
586: onintr(signo)
587: int signo;
588: {
589: char *ct;
590:
591: ct = ctime(&buf[0].ut_time);
1.6 mickey 592: printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
1.1 deraadt 593: if (signo == SIGINT)
594: exit(1);
595: (void)fflush(stdout); /* fix required for rsh */
596: }