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