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