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