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