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 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 char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 47 #include <err.h> 48 #include <fcntl.h> 49 #include <locale.h> 50 #include <paths.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <unistd.h> 57 #include <utmp.h> 58 #include <sys/queue.h> 59 60 #define NO 0 /* false/no */ 61 #define YES 1 /* true/yes */ 62 63 static struct utmp buf[1024]; /* utmp read buffer */ 64 65 typedef struct arg { 66 char *name; /* argument */ 67 #define HOST_TYPE -2 68 #define TTY_TYPE -3 69 #define USER_TYPE -4 70 int type; /* type of arg */ 71 struct arg *next; /* linked list pointer */ 72 } ARG; 73 ARG *arglist; /* head of linked list */ 74 75 LIST_HEAD(ttylisthead, ttytab) ttylist; 76 77 struct ttytab { 78 long logout; /* log out time */ 79 char tty[UT_LINESIZE + 1]; /* terminal name */ 80 LIST_ENTRY(ttytab) list; 81 }; 82 83 static long currentout, /* current logout value */ 84 maxrec; /* records to display */ 85 static char *file = _PATH_WTMP; /* wtmp file */ 86 87 void addarg __P((int, char *)); 88 void hostconv __P((char *)); 89 void onintr __P((int)); 90 char *ttyconv __P((char *)); 91 int want __P((struct utmp *)); 92 void wtmp __P((void)); 93 94 int 95 main(argc, argv) 96 int argc; 97 char *argv[]; 98 { 99 extern int optind; 100 extern char *optarg; 101 int ch; 102 char *p; 103 104 (void) setlocale(LC_TIME, ""); 105 106 maxrec = -1; 107 while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != -1) 108 switch (ch) { 109 case '0': case '1': case '2': case '3': case '4': 110 case '5': case '6': case '7': case '8': case '9': 111 /* 112 * kludge: last was originally designed to take 113 * a number after a dash. 114 */ 115 if (maxrec == -1) { 116 p = argv[optind - 1]; 117 if (p[0] == '-' && p[1] == ch && !p[2]) 118 maxrec = atol(++p); 119 else 120 maxrec = atol(argv[optind] + 1); 121 if (!maxrec) 122 exit(0); 123 } 124 break; 125 case 'f': 126 file = optarg; 127 break; 128 case 'h': 129 hostconv(optarg); 130 addarg(HOST_TYPE, optarg); 131 break; 132 case 't': 133 addarg(TTY_TYPE, ttyconv(optarg)); 134 break; 135 case '?': 136 default: 137 (void)fprintf(stderr, 138 "usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"); 139 exit(1); 140 } 141 142 if (argc) { 143 setlinebuf(stdout); 144 for (argv += optind; *argv; ++argv) { 145 #define COMPATIBILITY 146 #ifdef COMPATIBILITY 147 /* code to allow "last p5" to work */ 148 addarg(TTY_TYPE, ttyconv(*argv)); 149 #endif 150 addarg(USER_TYPE, *argv); 151 } 152 } 153 wtmp(); 154 exit(0); 155 } 156 157 /* 158 * wtmp -- 159 * read through the wtmp file 160 */ 161 void 162 wtmp() 163 { 164 struct utmp *bp; /* current structure */ 165 struct ttytab *tt, *ttx; /* ttylist entry */ 166 struct stat stb; /* stat of file for size */ 167 long bl, delta; /* time difference */ 168 int bytes, wfd; 169 char *crmsg; 170 char ct[80]; 171 struct tm *tm; 172 173 LIST_INIT(&ttylist); 174 175 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 176 err(1, "%s", file); 177 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 178 179 (void)time(&buf[0].ut_time); 180 (void)signal(SIGINT, onintr); 181 (void)signal(SIGQUIT, onintr); 182 183 while (--bl >= 0) { 184 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 185 (bytes = read(wfd, buf, sizeof(buf))) == -1) 186 err(1, "%s", file); 187 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) { 188 /* 189 * if the terminal line is '~', the machine stopped. 190 * see utmp(5) for more info. 191 */ 192 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) { 193 /* everybody just logged out */ 194 for (tt = ttylist.lh_first; tt;) { 195 LIST_REMOVE(tt, list); 196 ttx = tt; 197 tt = tt->list.le_next; 198 free(ttx); 199 } 200 currentout = -bp->ut_time; 201 crmsg = strncmp(bp->ut_name, "shutdown", 202 UT_NAMESIZE) ? "crash" : "shutdown"; 203 if (want(bp)) { 204 tm = localtime(&bp->ut_time); 205 (void) strftime(ct, sizeof(ct), "%c", tm); 206 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", 207 UT_NAMESIZE, UT_NAMESIZE, 208 bp->ut_name, UT_LINESIZE, 209 UT_LINESIZE, bp->ut_line, 210 UT_HOSTSIZE, UT_HOSTSIZE, 211 bp->ut_host, ct, ct + 11); 212 if (maxrec != -1 && !--maxrec) 213 return; 214 } 215 continue; 216 } 217 /* 218 * if the line is '{' or '|', date got set; see 219 * utmp(5) for more info. 220 */ 221 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') 222 && !bp->ut_line[1]) { 223 if (want(bp)) { 224 tm = localtime(&bp->ut_time); 225 (void) strftime(ct, sizeof(ct), "%c", tm); 226 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", 227 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 228 UT_LINESIZE, UT_LINESIZE, bp->ut_line, 229 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 230 ct, ct + 11); 231 if (maxrec && !--maxrec) 232 return; 233 } 234 continue; 235 } 236 if (bp->ut_name[0] == '\0' || want(bp)) { 237 /* find associated tty */ 238 for (tt = ttylist.lh_first; ; tt = tt->list.le_next) { 239 if (tt == NULL) { 240 /* add new one */ 241 tt = malloc(sizeof(struct ttytab)); 242 if (tt == NULL) 243 err(1, "malloc failure"); 244 tt->logout = currentout; 245 strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 246 LIST_INSERT_HEAD(&ttylist, tt, list); 247 break; 248 } 249 if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 250 break; 251 } 252 if (bp->ut_name[0]) { 253 /* 254 * when uucp and ftp log in over a network, the entry in 255 * the utmp file is the name plus their process id. See 256 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 257 */ 258 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 259 bp->ut_line[3] = '\0'; 260 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 261 bp->ut_line[4] = '\0'; 262 tm = localtime(&bp->ut_time); 263 (void) strftime(ct, sizeof(ct), "%c", tm); 264 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", 265 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 266 UT_LINESIZE, UT_LINESIZE, bp->ut_line, 267 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 268 ct, ct + 11); 269 if (!tt->logout) 270 puts(" still logged in"); 271 else { 272 if (tt->logout < 0) { 273 tt->logout = -tt->logout; 274 printf("- %s", crmsg); 275 } 276 else { 277 tm = localtime(&tt->logout); 278 (void) strftime(ct, sizeof(ct), "%c", tm); 279 printf("- %5.5s", ct + 11); 280 } 281 delta = tt->logout - bp->ut_time; 282 tm = gmtime(&delta); 283 (void) strftime(ct, sizeof(ct), "%c", tm); 284 if (delta < 86400) 285 printf(" (%5.5s)\n", ct + 11); 286 else 287 printf(" (%ld+%5.5s)\n", 288 delta / 86400, ct + 11); 289 } 290 LIST_REMOVE(tt, list); 291 free(tt); 292 if (maxrec != -1 && !--maxrec) 293 return; 294 } else { 295 tt->logout = bp->ut_time; 296 } 297 } 298 } 299 } 300 tm = localtime(&buf[0].ut_time); 301 (void) strftime(ct, sizeof(ct), "%c", tm); 302 printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11); 303 } 304 305 /* 306 * want -- 307 * see if want this entry 308 */ 309 int 310 want(bp) 311 struct utmp *bp; 312 { 313 ARG *step; 314 315 if (!arglist) 316 return (YES); 317 318 for (step = arglist; step; step = step->next) 319 switch(step->type) { 320 case HOST_TYPE: 321 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 322 return (YES); 323 break; 324 case TTY_TYPE: 325 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 326 return (YES); 327 break; 328 case USER_TYPE: 329 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 330 return (YES); 331 break; 332 } 333 return (NO); 334 } 335 336 /* 337 * addarg -- 338 * add an entry to a linked list of arguments 339 */ 340 void 341 addarg(type, arg) 342 int type; 343 char *arg; 344 { 345 ARG *cur; 346 347 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) 348 err(1, "malloc failure"); 349 cur->next = arglist; 350 cur->type = type; 351 cur->name = arg; 352 arglist = cur; 353 } 354 355 /* 356 * hostconv -- 357 * convert the hostname to search pattern; if the supplied host name 358 * has a domain attached that is the same as the current domain, rip 359 * off the domain suffix since that's what login(1) does. 360 */ 361 void 362 hostconv(arg) 363 char *arg; 364 { 365 static int first = 1; 366 static char *hostdot, name[MAXHOSTNAMELEN]; 367 char *argdot; 368 369 if (!(argdot = strchr(arg, '.'))) 370 return; 371 if (first) { 372 first = 0; 373 if (gethostname(name, sizeof(name))) 374 err(1, "gethostname"); 375 hostdot = strchr(name, '.'); 376 } 377 if (hostdot && !strcasecmp(hostdot, argdot)) 378 *argdot = '\0'; 379 } 380 381 /* 382 * ttyconv -- 383 * convert tty to correct name. 384 */ 385 char * 386 ttyconv(arg) 387 char *arg; 388 { 389 char *mval; 390 391 /* 392 * kludge -- we assume that all tty's end with 393 * a two character suffix. 394 */ 395 if (strlen(arg) == 2) { 396 /* either 6 for "ttyxx" or 8 for "console" */ 397 if (!(mval = malloc((u_int)8))) 398 err(1, "malloc failure"); 399 if (!strcmp(arg, "co")) 400 (void)strcpy(mval, "console"); 401 else { 402 (void)strcpy(mval, "tty"); 403 (void)strcpy(mval + 3, arg); 404 } 405 return (mval); 406 } 407 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 408 return (arg + 5); 409 return (arg); 410 } 411 412 /* 413 * onintr -- 414 * on interrupt, we inform the user how far we've gotten 415 */ 416 void 417 onintr(signo) 418 int signo; 419 { 420 char ct[80]; 421 struct tm *tm; 422 423 tm = localtime(&buf[0].ut_time); 424 (void) strftime(ct, sizeof(ct), "%c", tm); 425 printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11); 426 if (signo == SIGINT) 427 exit(1); 428 (void)fflush(stdout); /* fix required for rsh */ 429 } 430