1 /* 2 * Copyright (c) 1994 Christopher G. Demetriou. 3 * @(#)Copyright (c) 1994, Simon J. Gerraty. 4 * 5 * This is free software. It comes with NO WARRANTY. 6 * Permission to use, modify and distribute this source code 7 * is granted subject to the following conditions. 8 * 1/ that the above copyright notice and this notice 9 * are preserved in all copies and that due credit be given 10 * to the author. 11 * 2/ that any changes to this code are clearly commented 12 * as such so that the author does not get blamed for bugs 13 * other than his own. 14 */ 15 16 #ifndef lint 17 static const char rcsid[] = "$OpenBSD: ac.c,v 1.19 2007/11/17 15:08:57 sobrado Exp $"; 18 #endif 19 20 #include <sys/types.h> 21 #include <sys/file.h> 22 #include <sys/time.h> 23 #include <err.h> 24 #include <errno.h> 25 #include <pwd.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <utmp.h> 30 #include <unistd.h> 31 32 /* 33 * this is for our list of currently logged in sessions 34 */ 35 struct utmp_list { 36 struct utmp_list *next; 37 struct utmp usr; 38 }; 39 40 /* 41 * this is for our list of users that are accumulating time. 42 */ 43 struct user_list { 44 struct user_list *next; 45 char name[UT_NAMESIZE+1]; 46 time_t secs; 47 }; 48 49 /* 50 * this is for chosing whether to ignore a login 51 */ 52 struct tty_list { 53 struct tty_list *next; 54 char name[UT_LINESIZE+3]; 55 size_t len; 56 int ret; 57 }; 58 59 /* 60 * globals - yes yuk 61 */ 62 #ifdef CONSOLE_TTY 63 static char *Console = CONSOLE_TTY; 64 #endif 65 static time_t Total = 0; 66 static time_t FirstTime = 0; 67 static int Flags = 0; 68 static struct user_list *Users = NULL; 69 static struct tty_list *Ttys = NULL; 70 71 #define AC_W 1 /* not _PATH_WTMP */ 72 #define AC_D 2 /* daily totals (ignore -p) */ 73 #define AC_P 4 /* per-user totals */ 74 #define AC_U 8 /* specified users only */ 75 #define AC_T 16 /* specified ttys only */ 76 77 #ifdef DEBUG 78 static int Debug = 0; 79 #endif 80 81 int main(int, char **); 82 int ac(FILE *); 83 void add_tty(char *); 84 int do_tty(char *); 85 FILE *file(char *); 86 struct utmp_list *log_in(struct utmp_list *, struct utmp *); 87 struct utmp_list *log_out(struct utmp_list *, struct utmp *); 88 int on_console(struct utmp_list *); 89 void show(char *, time_t); 90 void show_today(struct user_list *, struct utmp_list *, 91 time_t); 92 void show_users(struct user_list *); 93 struct user_list *update_user(struct user_list *, char *, time_t); 94 void usage(void); 95 96 /* 97 * open wtmp or die 98 */ 99 FILE * 100 file(char *name) 101 { 102 FILE *fp; 103 104 if (strcmp(name, "-") == 0) 105 fp = stdin; 106 else if ((fp = fopen(name, "r")) == NULL) 107 err(1, "%s", name); 108 /* in case we want to discriminate */ 109 if (strcmp(_PATH_WTMP, name)) 110 Flags |= AC_W; 111 return fp; 112 } 113 114 void 115 add_tty(char *name) 116 { 117 struct tty_list *tp; 118 char *rcp; 119 120 Flags |= AC_T; 121 122 if ((tp = malloc(sizeof(struct tty_list))) == NULL) 123 err(1, "malloc"); 124 tp->len = 0; /* full match */ 125 tp->ret = 1; /* do if match */ 126 if (*name == '!') { /* don't do if match */ 127 tp->ret = 0; 128 name++; 129 } 130 strlcpy(tp->name, name, sizeof (tp->name)); 131 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */ 132 *rcp = '\0'; 133 tp->len = strlen(tp->name); /* match len bytes only */ 134 } 135 tp->next = Ttys; 136 Ttys = tp; 137 } 138 139 /* 140 * should we process the named tty? 141 */ 142 int 143 do_tty(char *name) 144 { 145 struct tty_list *tp; 146 int def_ret = 0; 147 148 for (tp = Ttys; tp != NULL; tp = tp->next) { 149 if (tp->ret == 0) /* specific don't */ 150 def_ret = 1; /* default do */ 151 if (tp->len != 0) { 152 if (strncmp(name, tp->name, tp->len) == 0) 153 return tp->ret; 154 } else { 155 if (strncmp(name, tp->name, sizeof (tp->name)) == 0) 156 return tp->ret; 157 } 158 } 159 return def_ret; 160 } 161 162 #ifdef CONSOLE_TTY 163 /* 164 * is someone logged in on Console? 165 */ 166 int 167 on_console(struct utmp_list *head) 168 { 169 struct utmp_list *up; 170 171 for (up = head; up; up = up->next) { 172 if (strncmp(up->usr.ut_line, Console, 173 sizeof (up->usr.ut_line)) == 0) 174 return 1; 175 } 176 return 0; 177 } 178 #endif 179 180 /* 181 * update user's login time 182 */ 183 struct user_list * 184 update_user(struct user_list *head, char *name, time_t secs) 185 { 186 struct user_list *up; 187 188 for (up = head; up != NULL; up = up->next) { 189 if (strncmp(up->name, name, sizeof (up->name) - 1) == 0) { 190 up->secs += secs; 191 Total += secs; 192 return head; 193 } 194 } 195 /* 196 * not found so add new user unless specified users only 197 */ 198 if (Flags & AC_U) 199 return head; 200 201 if ((up = malloc(sizeof(struct user_list))) == NULL) 202 err(1, "malloc"); 203 up->next = head; 204 strlcpy(up->name, name, sizeof (up->name)); 205 up->secs = secs; 206 Total += secs; 207 return up; 208 } 209 210 int 211 main(int argc, char *argv[]) 212 { 213 FILE *fp; 214 int c; 215 216 fp = NULL; 217 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) { 218 switch (c) { 219 #ifdef DEBUG 220 case 'D': 221 Debug++; 222 break; 223 #endif 224 case 'c': 225 #ifdef CONSOLE_TTY 226 Console = optarg; 227 #else 228 usage(); /* XXX */ 229 #endif 230 break; 231 case 'd': 232 Flags |= AC_D; 233 break; 234 case 'p': 235 Flags |= AC_P; 236 break; 237 case 't': /* only do specified ttys */ 238 add_tty(optarg); 239 break; 240 case 'w': 241 fp = file(optarg); 242 break; 243 case '?': 244 default: 245 usage(); 246 break; 247 } 248 } 249 if (optind < argc) { 250 /* 251 * initialize user list 252 */ 253 for (; optind < argc; optind++) { 254 Users = update_user(Users, argv[optind], 0L); 255 } 256 Flags |= AC_U; /* freeze user list */ 257 } 258 if (Flags & AC_D) 259 Flags &= ~AC_P; 260 if (fp == NULL) { 261 /* 262 * if _PATH_WTMP does not exist, exit quietly 263 */ 264 if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT) 265 return 0; 266 267 fp = file(_PATH_WTMP); 268 } 269 ac(fp); 270 271 return 0; 272 } 273 274 /* 275 * print login time in decimal hours 276 */ 277 void 278 show(char *name, time_t secs) 279 { 280 (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name, 281 ((double)secs / 3600)); 282 } 283 284 void 285 show_users(struct user_list *list) 286 { 287 struct user_list *lp; 288 289 for (lp = list; lp; lp = lp->next) 290 show(lp->name, lp->secs); 291 } 292 293 /* 294 * print total login time for 24hr period in decimal hours 295 */ 296 void 297 show_today(struct user_list *users, struct utmp_list *logins, time_t secs) 298 { 299 struct user_list *up; 300 struct utmp_list *lp; 301 char date[64]; 302 time_t yesterday = secs - 1; 303 304 (void)strftime(date, sizeof (date), "%b %e total", 305 localtime(&yesterday)); 306 307 /* restore the missing second */ 308 yesterday++; 309 310 for (lp = logins; lp != NULL; lp = lp->next) { 311 secs = yesterday - lp->usr.ut_time; 312 Users = update_user(Users, lp->usr.ut_name, secs); 313 lp->usr.ut_time = yesterday; /* as if they just logged in */ 314 } 315 secs = 0; 316 for (up = users; up != NULL; up = up->next) { 317 secs += up->secs; 318 up->secs = 0; /* for next day */ 319 } 320 if (secs) 321 (void)printf("%s %11.2f\n", date, ((double)secs / 3600)); 322 } 323 324 /* 325 * log a user out and update their times. 326 * if ut_line is "~", we log all users out as the system has 327 * been shut down. 328 */ 329 struct utmp_list * 330 log_out(struct utmp_list *head, struct utmp *up) 331 { 332 struct utmp_list *lp, *lp2, *tlp; 333 time_t secs; 334 335 for (lp = head, lp2 = NULL; lp != NULL; ) 336 if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line, 337 sizeof (up->ut_line)) == 0) { 338 secs = up->ut_time - lp->usr.ut_time; 339 Users = update_user(Users, lp->usr.ut_name, secs); 340 #ifdef DEBUG 341 if (Debug) 342 printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n", 343 19, ctime(&up->ut_time), 344 sizeof (lp->usr.ut_line), lp->usr.ut_line, 345 sizeof (lp->usr.ut_name), lp->usr.ut_name, 346 secs / 3600, (secs % 3600) / 60, secs % 60); 347 #endif 348 /* 349 * now lose it 350 */ 351 tlp = lp; 352 lp = lp->next; 353 if (tlp == head) 354 head = lp; 355 else if (lp2 != NULL) 356 lp2->next = lp; 357 free(tlp); 358 } else { 359 lp2 = lp; 360 lp = lp->next; 361 } 362 return head; 363 } 364 365 366 /* 367 * if do_tty says ok, login a user 368 */ 369 struct utmp_list * 370 log_in(struct utmp_list *head, struct utmp *up) 371 { 372 struct utmp_list *lp; 373 374 /* 375 * this could be a login. if we're not dealing with 376 * the console name, say it is. 377 * 378 * If we are, and if ut_host==":0.0" we know that it 379 * isn't a real login. _But_ if we have not yet recorded 380 * someone being logged in on Console - due to the wtmp 381 * file starting after they logged in, we'll pretend they 382 * logged in, at the start of the wtmp file. 383 */ 384 385 #ifdef CONSOLE_TTY 386 if (up->ut_host[0] == ':') { 387 /* 388 * SunOS 4.0.2 does not treat ":0.0" as special but we 389 * do. 390 */ 391 if (on_console(head)) 392 return head; 393 /* 394 * ok, no recorded login, so they were here when wtmp 395 * started! Adjust ut_time! 396 */ 397 up->ut_time = FirstTime; 398 /* 399 * this allows us to pick the right logout 400 */ 401 strlcpy(up->ut_line, Console, sizeof (up->ut_line)); 402 } 403 #endif 404 /* 405 * If we are doing specified ttys only, we ignore 406 * anything else. 407 */ 408 if (Flags & AC_T) 409 if (!do_tty(up->ut_line)) 410 return head; 411 412 /* 413 * go ahead and log them in 414 */ 415 if ((lp = malloc(sizeof(struct utmp_list))) == NULL) 416 err(1, "malloc"); 417 lp->next = head; 418 head = lp; 419 memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp)); 420 #ifdef DEBUG 421 if (Debug) { 422 printf("%-.*s %-.*s: %-.*s logged in", 19, 423 ctime(&lp->usr.ut_time), sizeof (up->ut_line), 424 up->ut_line, sizeof (up->ut_name), up->ut_name); 425 if (*up->ut_host) 426 printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host); 427 putchar('\n'); 428 } 429 #endif 430 return head; 431 } 432 433 int 434 ac(FILE *fp) 435 { 436 struct utmp_list *lp, *head = NULL; 437 struct utmp usr; 438 struct tm *ltm; 439 time_t secs = 0, prev = 0; 440 int day = -1; 441 442 while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) { 443 if (!FirstTime) 444 FirstTime = usr.ut_time; 445 if (usr.ut_time < prev) 446 continue; /* broken record */ 447 prev = usr.ut_time; 448 if (Flags & AC_D) { 449 ltm = localtime(&usr.ut_time); 450 if (day >= 0 && day != ltm->tm_yday) { 451 day = ltm->tm_yday; 452 /* 453 * print yesterday's total 454 */ 455 secs = usr.ut_time; 456 secs -= ltm->tm_sec; 457 secs -= 60 * ltm->tm_min; 458 secs -= 3600 * ltm->tm_hour; 459 show_today(Users, head, secs); 460 } else 461 day = ltm->tm_yday; 462 } 463 switch(*usr.ut_line) { 464 case '|': 465 secs = usr.ut_time; 466 break; 467 case '{': 468 secs -= usr.ut_time; 469 /* 470 * adjust time for those logged in 471 */ 472 for (lp = head; lp != NULL; lp = lp->next) 473 lp->usr.ut_time -= secs; 474 break; 475 case '~': /* reboot or shutdown */ 476 head = log_out(head, &usr); 477 FirstTime = usr.ut_time; /* shouldn't be needed */ 478 break; 479 default: 480 /* 481 * if they came in on a pseudo-tty, then it is only 482 * a login session if the ut_host field is non-empty 483 */ 484 if (*usr.ut_name) { 485 if (strncmp(usr.ut_line, "tty", 3) != 0 || 486 strchr("pqrstuvwxyzPQRST", usr.ut_line[3]) != NULL || 487 *usr.ut_host != '\0') 488 head = log_in(head, &usr); 489 } else 490 head = log_out(head, &usr); 491 break; 492 } 493 } 494 (void)fclose(fp); 495 if (!(Flags & AC_W)) 496 usr.ut_time = time(NULL); 497 (void)strlcpy(usr.ut_line, "~", sizeof usr.ut_line); 498 499 if (Flags & AC_D) { 500 ltm = localtime(&usr.ut_time); 501 if (day >= 0 && day != ltm->tm_yday) { 502 /* 503 * print yesterday's total 504 */ 505 secs = usr.ut_time; 506 secs -= ltm->tm_sec; 507 secs -= 60 * ltm->tm_min; 508 secs -= 3600 * ltm->tm_hour; 509 show_today(Users, head, secs); 510 } 511 } 512 /* 513 * anyone still logged in gets time up to now 514 */ 515 head = log_out(head, &usr); 516 517 if (Flags & AC_D) 518 show_today(Users, head, time(NULL)); 519 else { 520 if (Flags & AC_P) 521 show_users(Users); 522 show("total", Total); 523 } 524 return 0; 525 } 526 527 void 528 usage(void) 529 { 530 extern char *__progname; 531 (void)fprintf(stderr, "usage: " 532 #ifdef CONSOLE_TTY 533 "%s [-dp] [-c console] [-t tty] [-w wtmp] [user ...]\n", 534 __progname); 535 #else 536 "%s [-dp] [-t tty] [-w wtmp] [user ...]\n", __progname); 537 #endif 538 exit(1); 539 } 540