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