1 /* 2 * Copyright (c) 1987, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD$ 34 */ 35 36 #ifndef lint 37 static const char copyright[] = 38 "@(#) Copyright (c) 1987, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 static const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94"; 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/stat.h> 48 49 #include <err.h> 50 #include <fcntl.h> 51 #include <langinfo.h> 52 #include <locale.h> 53 #include <paths.h> 54 #include <signal.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <time.h> 59 #include <unistd.h> 60 #include <utmp.h> 61 #include <sys/queue.h> 62 63 #define NO 0 /* false/no */ 64 #define YES 1 /* true/yes */ 65 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 66 67 static struct utmp buf[1024]; /* utmp read buffer */ 68 69 typedef struct arg { 70 char *name; /* argument */ 71 #define HOST_TYPE -2 72 #define TTY_TYPE -3 73 #define USER_TYPE -4 74 int type; /* type of arg */ 75 struct arg *next; /* linked list pointer */ 76 } ARG; 77 ARG *arglist; /* head of linked list */ 78 79 LIST_HEAD(ttylisthead, ttytab) ttylist; 80 81 struct ttytab { 82 time_t logout; /* log out time */ 83 char tty[UT_LINESIZE + 1]; /* terminal name */ 84 LIST_ENTRY(ttytab) list; 85 }; 86 87 static const char *crmsg; /* cause of last reboot */ 88 static long currentout, /* current logout value */ 89 maxrec; /* records to display */ 90 static const char *file = _PATH_WTMP; /* wtmp file */ 91 static int sflag = 0; /* show delta in seconds */ 92 static int width = 5; /* show seconds in delta */ 93 static int yflag; /* show year */ 94 static int d_first; 95 static int snapfound = 0; /* found snapshot entry? */ 96 static time_t snaptime; /* if != 0, we will only 97 * report users logged in 98 * at this snapshot time 99 */ 100 101 void addarg(int, char *); 102 time_t dateconv(char *); 103 void doentry(struct utmp *); 104 void hostconv(char *); 105 void onintr(int); 106 void printentry(struct utmp *, struct ttytab *); 107 char *ttyconv(char *); 108 int want(struct utmp *); 109 void usage(void); 110 void wtmp(void); 111 112 void 113 usage(void) 114 { 115 (void)fprintf(stderr, 116 "usage: last [-#] [-y] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n" 117 "\t[-t tty] [-s|w] [user ...]\n"); 118 exit(1); 119 } 120 121 int 122 main(argc, argv) 123 int argc; 124 char *argv[]; 125 { 126 int ch; 127 char *p; 128 129 (void) setlocale(LC_TIME, ""); 130 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 131 132 maxrec = -1; 133 snaptime = 0; 134 while ((ch = getopt(argc, argv, "0123456789d:f:h:st:wy")) != -1) 135 switch (ch) { 136 case '0': case '1': case '2': case '3': case '4': 137 case '5': case '6': case '7': case '8': case '9': 138 /* 139 * kludge: last was originally designed to take 140 * a number after a dash. 141 */ 142 if (maxrec == -1) { 143 p = argv[optind - 1]; 144 if (p[0] == '-' && p[1] == ch && !p[2]) 145 maxrec = atol(++p); 146 else 147 maxrec = atol(argv[optind] + 1); 148 if (!maxrec) 149 exit(0); 150 } 151 break; 152 case 'd': 153 snaptime = dateconv(optarg); 154 break; 155 case 'f': 156 file = optarg; 157 break; 158 case 'h': 159 hostconv(optarg); 160 addarg(HOST_TYPE, optarg); 161 break; 162 case 's': 163 sflag++; /* Show delta as seconds */ 164 break; 165 case 't': 166 addarg(TTY_TYPE, ttyconv(optarg)); 167 break; 168 case 'w': 169 width = 8; 170 break; 171 case 'y': 172 yflag++; 173 break; 174 case '?': 175 default: 176 usage(); 177 } 178 179 if (sflag && width == 8) usage(); 180 181 if (argc) { 182 setlinebuf(stdout); 183 for (argv += optind; *argv; ++argv) { 184 #define COMPATIBILITY 185 #ifdef COMPATIBILITY 186 /* code to allow "last p5" to work */ 187 addarg(TTY_TYPE, ttyconv(*argv)); 188 #endif 189 addarg(USER_TYPE, *argv); 190 } 191 } 192 wtmp(); 193 exit(0); 194 } 195 196 /* 197 * wtmp -- 198 * read through the wtmp file 199 */ 200 void 201 wtmp() 202 { 203 struct utmp *bp; /* current structure */ 204 struct stat stb; /* stat of file for size */ 205 long bl; 206 int bytes, wfd; 207 char ct[80]; 208 struct tm *tm; 209 time_t t; 210 211 LIST_INIT(&ttylist); 212 213 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 214 err(1, "%s", file); 215 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 216 217 (void)time(&t); 218 buf[0].ut_time = _time_to_int(t); 219 (void)signal(SIGINT, onintr); 220 (void)signal(SIGQUIT, onintr); 221 222 while (--bl >= 0) { 223 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 224 (bytes = read(wfd, buf, sizeof(buf))) == -1) 225 err(1, "%s", file); 226 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) 227 doentry(bp); 228 } 229 t = _int_to_time(buf[0].ut_time); 230 tm = localtime(&t); 231 (void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm); 232 printf("%s", ct); 233 } 234 235 /* 236 * doentry -- 237 * process a single wtmp entry 238 */ 239 void 240 doentry(bp) 241 struct utmp *bp; 242 { 243 struct ttytab *tt, *ttx; /* ttylist entry */ 244 245 /* 246 * if the terminal line is '~', the machine stopped. 247 * see utmp(5) for more info. 248 */ 249 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) { 250 /* everybody just logged out */ 251 for (tt = LIST_FIRST(&ttylist); tt;) { 252 LIST_REMOVE(tt, list); 253 ttx = tt; 254 tt = LIST_NEXT(tt, list); 255 free(ttx); 256 } 257 currentout = -bp->ut_time; 258 crmsg = strncmp(bp->ut_name, "shutdown", UT_NAMESIZE) ? 259 "crash" : "shutdown"; 260 /* 261 * if we're in snapshot mode, we want to exit if this 262 * shutdown/reboot appears while we we are tracking the 263 * active range 264 */ 265 if (snaptime && snapfound) 266 exit(0); 267 /* 268 * don't print shutdown/reboot entries unless flagged for 269 */ 270 if (!snaptime && want(bp)) 271 printentry(bp, NULL); 272 return; 273 } 274 /* 275 * if the line is '{' or '|', date got set; see 276 * utmp(5) for more info. 277 */ 278 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') && 279 !bp->ut_line[1]) { 280 if (want(bp) && !snaptime) 281 printentry(bp, NULL); 282 return; 283 } 284 /* find associated tty */ 285 LIST_FOREACH(tt, &ttylist, list) 286 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 287 break; 288 289 if (tt == NULL) { 290 /* add new one */ 291 tt = malloc(sizeof(struct ttytab)); 292 if (tt == NULL) 293 errx(1, "malloc failure"); 294 tt->logout = currentout; 295 strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 296 LIST_INSERT_HEAD(&ttylist, tt, list); 297 } 298 299 /* 300 * print record if not in snapshot mode and wanted 301 * or in snapshot mode and in snapshot range 302 */ 303 if (bp->ut_name[0] && (want(bp) || (bp->ut_time < snaptime && 304 (tt->logout > snaptime || tt->logout < 1)))) { 305 snapfound = 1; 306 /* 307 * when uucp and ftp log in over a network, the entry in 308 * the utmp file is the name plus their process id. See 309 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 310 */ 311 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 312 bp->ut_line[3] = '\0'; 313 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 314 bp->ut_line[4] = '\0'; 315 printentry(bp, tt); 316 } 317 tt->logout = bp->ut_time; 318 } 319 320 /* 321 * printentry -- 322 * output an entry 323 * 324 * If `tt' is non-NULL, use it and `crmsg' to print the logout time or 325 * logout type (crash/shutdown) as appropriate. 326 */ 327 void 328 printentry(bp, tt) 329 struct utmp *bp; 330 struct ttytab *tt; 331 { 332 char ct[80]; 333 struct tm *tm; 334 time_t delta; /* time difference */ 335 time_t t; 336 337 if (maxrec != -1 && !maxrec--) 338 exit(0); 339 t = _int_to_time(bp->ut_time); 340 tm = localtime(&t); 341 (void) strftime(ct, sizeof(ct), d_first ? 342 (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") : 343 (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm); 344 printf("%-*.*s %-*.*s %-*.*s %s%c", 345 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 346 UT_LINESIZE, UT_LINESIZE, bp->ut_line, 347 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 348 ct, tt == NULL ? '\n' : ' '); 349 if (tt == NULL) 350 return; 351 if (!tt->logout) { 352 puts(" still logged in"); 353 return; 354 } 355 if (tt->logout < 0) { 356 tt->logout = -tt->logout; 357 printf("- %s", crmsg); 358 } else { 359 tm = localtime(&tt->logout); 360 (void) strftime(ct, sizeof(ct), "%R", tm); 361 printf("- %s", ct); 362 } 363 delta = tt->logout - bp->ut_time; 364 if (sflag) { 365 printf(" (%8ld)\n", (long)delta); 366 } else { 367 tm = gmtime(&delta); 368 (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); 369 if (delta < 86400) 370 printf(" (%s)\n", ct); 371 else 372 printf(" (%ld+%s)\n", (long)delta / 86400, ct); 373 } 374 } 375 376 /* 377 * want -- 378 * see if want this entry 379 */ 380 int 381 want(bp) 382 struct utmp *bp; 383 { 384 ARG *step; 385 386 if (snaptime) 387 return (NO); 388 389 if (!arglist) 390 return (YES); 391 392 for (step = arglist; step; step = step->next) 393 switch(step->type) { 394 case HOST_TYPE: 395 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 396 return (YES); 397 break; 398 case TTY_TYPE: 399 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 400 return (YES); 401 break; 402 case USER_TYPE: 403 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 404 return (YES); 405 break; 406 } 407 return (NO); 408 } 409 410 /* 411 * addarg -- 412 * add an entry to a linked list of arguments 413 */ 414 void 415 addarg(type, arg) 416 int type; 417 char *arg; 418 { 419 ARG *cur; 420 421 if ((cur = malloc(sizeof(ARG))) == NULL) 422 errx(1, "malloc failure"); 423 cur->next = arglist; 424 cur->type = type; 425 cur->name = arg; 426 arglist = cur; 427 } 428 429 /* 430 * hostconv -- 431 * convert the hostname to search pattern; if the supplied host name 432 * has a domain attached that is the same as the current domain, rip 433 * off the domain suffix since that's what login(1) does. 434 */ 435 void 436 hostconv(arg) 437 char *arg; 438 { 439 static int first = 1; 440 static char *hostdot, name[MAXHOSTNAMELEN]; 441 char *argdot; 442 443 if (!(argdot = strchr(arg, '.'))) 444 return; 445 if (first) { 446 first = 0; 447 if (gethostname(name, sizeof(name))) 448 err(1, "gethostname"); 449 hostdot = strchr(name, '.'); 450 } 451 if (hostdot && !strcasecmp(hostdot, argdot)) 452 *argdot = '\0'; 453 } 454 455 /* 456 * ttyconv -- 457 * convert tty to correct name. 458 */ 459 char * 460 ttyconv(arg) 461 char *arg; 462 { 463 char *mval; 464 465 /* 466 * kludge -- we assume that all tty's end with 467 * a two character suffix. 468 */ 469 if (strlen(arg) == 2) { 470 /* either 6 for "ttyxx" or 8 for "console" */ 471 if ((mval = malloc(8)) == NULL) 472 errx(1, "malloc failure"); 473 if (!strcmp(arg, "co")) 474 (void)strcpy(mval, "console"); 475 else { 476 (void)strcpy(mval, "tty"); 477 (void)strcpy(mval + 3, arg); 478 } 479 return (mval); 480 } 481 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 482 return (arg + 5); 483 return (arg); 484 } 485 486 /* 487 * dateconv -- 488 * Convert the snapshot time in command line given in the format 489 * [[CC]YY]MMDDhhmm[.SS]] to a time_t. 490 * Derived from atime_arg1() in usr.bin/touch/touch.c 491 */ 492 time_t 493 dateconv(arg) 494 char *arg; 495 { 496 time_t timet; 497 struct tm *t; 498 int yearset; 499 char *p; 500 501 /* Start with the current time. */ 502 if (time(&timet) < 0) 503 err(1, "time"); 504 if ((t = localtime(&timet)) == NULL) 505 err(1, "localtime"); 506 507 /* [[CC]YY]MMDDhhmm[.SS] */ 508 if ((p = strchr(arg, '.')) == NULL) 509 t->tm_sec = 0; /* Seconds defaults to 0. */ 510 else { 511 if (strlen(p + 1) != 2) 512 goto terr; 513 *p++ = '\0'; 514 t->tm_sec = ATOI2(p); 515 } 516 517 yearset = 0; 518 switch (strlen(arg)) { 519 case 12: /* CCYYMMDDhhmm */ 520 t->tm_year = ATOI2(arg); 521 t->tm_year *= 100; 522 yearset = 1; 523 /* FALLTHOUGH */ 524 case 10: /* YYMMDDhhmm */ 525 if (yearset) { 526 yearset = ATOI2(arg); 527 t->tm_year += yearset; 528 } else { 529 yearset = ATOI2(arg); 530 if (yearset < 69) 531 t->tm_year = yearset + 2000; 532 else 533 t->tm_year = yearset + 1900; 534 } 535 t->tm_year -= 1900; /* Convert to UNIX time. */ 536 /* FALLTHROUGH */ 537 case 8: /* MMDDhhmm */ 538 t->tm_mon = ATOI2(arg); 539 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 540 t->tm_mday = ATOI2(arg); 541 t->tm_hour = ATOI2(arg); 542 t->tm_min = ATOI2(arg); 543 break; 544 case 4: /* hhmm */ 545 t->tm_hour = ATOI2(arg); 546 t->tm_min = ATOI2(arg); 547 break; 548 default: 549 goto terr; 550 } 551 t->tm_isdst = -1; /* Figure out DST. */ 552 timet = mktime(t); 553 if (timet == -1) 554 terr: errx(1, 555 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 556 return timet; 557 } 558 559 560 /* 561 * onintr -- 562 * on interrupt, we inform the user how far we've gotten 563 */ 564 void 565 onintr(signo) 566 int signo; 567 { 568 char ct[80]; 569 struct tm *tm; 570 time_t t = _int_to_time(buf[0].ut_time); 571 572 tm = localtime(&t); 573 (void) strftime(ct, sizeof(ct), 574 d_first ? "%a %e %b %R" : "%a %b %e %R", 575 tm); 576 printf("\ninterrupted %s\n", ct); 577 if (signo == SIGINT) 578 exit(1); 579 (void)fflush(stdout); /* fix required for rsh */ 580 } 581