last.c revision 1.11 1 /* $NetBSD: last.c,v 1.11 1998/08/25 20:59:38 ross Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1987, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
46 #endif
47 __RCSID("$NetBSD: last.c,v 1.11 1998/08/25 20:59:38 ross Exp $");
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52
53 #include <err.h>
54 #include <fcntl.h>
55 #include <paths.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <tzfile.h>
62 #include <unistd.h>
63 #include <utmp.h>
64
65 #define NO 0 /* false/no */
66 #define YES 1 /* true/yes */
67
68 static struct utmp buf[1024]; /* utmp read buffer */
69
70 typedef struct arg {
71 char *name; /* argument */
72 #define HOST_TYPE -2
73 #define TTY_TYPE -3
74 #define USER_TYPE -4
75 int type; /* type of arg */
76 struct arg *next; /* linked list pointer */
77 } ARG;
78 ARG *arglist; /* head of linked list */
79
80 typedef struct ttytab {
81 time_t logout; /* log out time */
82 char tty[UT_LINESIZE + 1]; /* terminal name */
83 struct ttytab *next; /* linked list pointer */
84 } TTY;
85 TTY *ttylist; /* head of linked list */
86
87 static time_t currentout; /* current logout value */
88 static long maxrec; /* records to display */
89 static char *file = _PATH_WTMP; /* wtmp file */
90 static int fulltime = 0; /* Display seconds? */
91
92 int main __P((int, char *[]));
93 void addarg __P((int, char *));
94 TTY *addtty __P((char *));
95 void hostconv __P((char *));
96 void onintr __P((int));
97 char *ttyconv __P((char *));
98 int want __P((struct utmp *, int));
99 void wtmp __P((void));
100
101 int
102 main(argc, argv)
103 int argc;
104 char *argv[];
105 {
106 extern int optind;
107 extern char *optarg;
108 int ch;
109 char *p;
110
111 maxrec = -1;
112 while ((ch = getopt(argc, argv, "0123456789f:h:t:T")) != -1)
113 switch (ch) {
114 case '0': case '1': case '2': case '3': case '4':
115 case '5': case '6': case '7': case '8': case '9':
116 /*
117 * kludge: last was originally designed to take
118 * a number after a dash.
119 */
120 if (maxrec == -1) {
121 p = argv[optind - 1];
122 if (p[0] == '-' && p[1] == ch && !p[2])
123 maxrec = atol(++p);
124 else
125 maxrec = atol(argv[optind] + 1);
126 if (!maxrec)
127 exit(0);
128 }
129 break;
130 case 'f':
131 file = optarg;
132 break;
133 case 'h':
134 hostconv(optarg);
135 addarg(HOST_TYPE, optarg);
136 break;
137 case 't':
138 addarg(TTY_TYPE, ttyconv(optarg));
139 break;
140 case 'T':
141 fulltime = 1;
142 break;
143 case '?':
144 default:
145 (void)fprintf(stderr,
146 "usage: last [-#] [-f file] [-t tty] [-h hostname] [-T] [user ...]\n");
147 exit(1);
148 }
149
150 if (argc) {
151 setlinebuf(stdout);
152 for (argv += optind; *argv; ++argv) {
153 #define COMPATIBILITY
154 #ifdef COMPATIBILITY
155 /* code to allow "last p5" to work */
156 addarg(TTY_TYPE, ttyconv(*argv));
157 #endif
158 addarg(USER_TYPE, *argv);
159 }
160 }
161 wtmp();
162 exit(0);
163 }
164
165 /*
166 * wtmp --
167 * read through the wtmp file
168 */
169 void
170 wtmp()
171 {
172 struct utmp *bp; /* current structure */
173 TTY *T; /* tty list entry */
174 struct stat stb; /* stat of file for size */
175 time_t delta; /* time difference */
176 off_t bl;
177 int timesize; /* how much of time string to print */
178 int bytes, wfd;
179 char *ct, *crmsg;
180
181 crmsg = NULL;
182
183 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
184 err(1, "%s", file);
185 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
186
187 if (fulltime)
188 timesize = 8; /* HH:MM:SS */
189 else
190 timesize = 5; /* HH:MM */
191
192 (void)time(&buf[0].ut_time);
193 (void)signal(SIGINT, onintr);
194 (void)signal(SIGQUIT, onintr);
195
196 while (--bl >= 0) {
197 if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
198 (bytes = read(wfd, buf, sizeof(buf))) == -1)
199 err(1, "%s", file);
200 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
201 /*
202 * if the terminal line is '~', the machine stopped.
203 * see utmp(5) for more info.
204 */
205 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
206 /* everybody just logged out */
207 for (T = ttylist; T; T = T->next)
208 T->logout = -bp->ut_time;
209 currentout = -bp->ut_time;
210 crmsg = strncmp(bp->ut_name, "shutdown",
211 UT_NAMESIZE) ? "crash" : "shutdown";
212 if (want(bp, NO)) {
213 ct = ctime(&bp->ut_time);
214 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
215 UT_NAMESIZE, UT_NAMESIZE,
216 bp->ut_name, UT_LINESIZE,
217 UT_LINESIZE, bp->ut_line,
218 UT_HOSTSIZE, UT_HOSTSIZE,
219 bp->ut_host, ct, timesize,
220 timesize, ct + 11);
221 if (maxrec != -1 && !--maxrec)
222 return;
223 }
224 continue;
225 }
226 /*
227 * if the line is '{' or '|', date got set; see
228 * utmp(5) for more info.
229 */
230 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
231 && !bp->ut_line[1]) {
232 if (want(bp, NO)) {
233 ct = ctime(&bp->ut_time);
234 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
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, timesize, timesize, ct + 11);
239 if (maxrec && !--maxrec)
240 return;
241 }
242 continue;
243 }
244 /* find associated tty */
245 for (T = ttylist;; T = T->next) {
246 if (!T) {
247 /* add new one */
248 T = addtty(bp->ut_line);
249 break;
250 }
251 if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
252 break;
253 }
254 if (bp->ut_name[0] && want(bp, YES)) {
255 ct = ctime(&bp->ut_time);
256 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
257 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
258 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
259 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
260 ct, timesize, timesize, ct + 11);
261 if (!T->logout)
262 puts(" still logged in");
263 else {
264 if (T->logout < 0) {
265 T->logout = -T->logout;
266 printf("- %s", crmsg);
267 }
268 else
269 printf("- %*.*s",
270 timesize, timesize,
271 ctime(&T->logout)+11);
272 delta = T->logout - bp->ut_time;
273 if (delta < SECSPERDAY)
274 printf(" (%*.*s)\n",
275 timesize, timesize,
276 asctime(gmtime(&delta))+11);
277 else
278 printf(" (%ld+%*.*s)\n",
279 delta / SECSPERDAY,
280 timesize, timesize,
281 asctime(gmtime(&delta))+11);
282 }
283 if (maxrec != -1 && !--maxrec)
284 return;
285 }
286 T->logout = bp->ut_time;
287 }
288 }
289 ct = ctime(&buf[0].ut_time);
290 printf("\nwtmp begins %10.10s %*.*s \n", ct, timesize, timesize, ct + 11);
291 }
292
293 /*
294 * want --
295 * see if want this entry
296 */
297 int
298 want(bp, check)
299 struct utmp *bp;
300 int check;
301 {
302 ARG *step;
303
304 if (check) {
305 /*
306 * when uucp and ftp log in over a network, the entry in
307 * the utmp file is the name plus their process id. See
308 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
309 */
310 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
311 bp->ut_line[3] = '\0';
312 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
313 bp->ut_line[4] = '\0';
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 * addtty --
357 * add an entry to a linked list of ttys
358 */
359 TTY *
360 addtty(ttyname)
361 char *ttyname;
362 {
363 TTY *cur;
364
365 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
366 err(1, "malloc failure");
367 cur->next = ttylist;
368 cur->logout = currentout;
369 memmove(cur->tty, ttyname, UT_LINESIZE);
370 return (ttylist = cur);
371 }
372
373 /*
374 * hostconv --
375 * convert the hostname to search pattern; if the supplied host name
376 * has a domain attached that is the same as the current domain, rip
377 * off the domain suffix since that's what login(1) does.
378 */
379 void
380 hostconv(arg)
381 char *arg;
382 {
383 static int first = 1;
384 static char *hostdot, name[MAXHOSTNAMELEN + 1];
385 char *argdot;
386
387 if (!(argdot = strchr(arg, '.')))
388 return;
389 if (first) {
390 first = 0;
391 if (gethostname(name, sizeof(name)))
392 err(1, "gethostname");
393 name[sizeof(name) - 1] = '\0';
394 hostdot = strchr(name, '.');
395 }
396 if (hostdot && !strcasecmp(hostdot, argdot))
397 *argdot = '\0';
398 }
399
400 /*
401 * ttyconv --
402 * convert tty to correct name.
403 */
404 char *
405 ttyconv(arg)
406 char *arg;
407 {
408 char *mval;
409
410 /*
411 * kludge -- we assume that all tty's end with
412 * a two character suffix.
413 */
414 if (strlen(arg) == 2) {
415 /* either 6 for "ttyxx" or 8 for "console" */
416 if (!(mval = malloc((u_int)8)))
417 err(1, "malloc failure");
418 if (!strcmp(arg, "co"))
419 (void)strcpy(mval, "console");
420 else {
421 (void)strcpy(mval, "tty");
422 (void)strcpy(mval + 3, arg);
423 }
424 return (mval);
425 }
426 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
427 return (arg + 5);
428 return (arg);
429 }
430
431 /*
432 * onintr --
433 * on interrupt, we inform the user how far we've gotten
434 */
435 void
436 onintr(signo)
437 int signo;
438 {
439 char *ct;
440
441 ct = ctime(&buf[0].ut_time);
442 printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
443 if (signo == SIGINT)
444 exit(1);
445 (void)fflush(stdout); /* fix required for rsh */
446 }
447