last.c revision 1.8 1 /* $NetBSD: last.c,v 1.8 1997/08/24 13:57:55 kleink 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.8 1997/08/24 13:57:55 kleink 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 bl, delta; /* time difference */
176 int timesize; /* how much of time string to print */
177 int bytes, wfd;
178 char *ct, *crmsg;
179
180 crmsg = NULL;
181
182 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
183 err(1, "%s", file);
184 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
185
186 if (fulltime)
187 timesize = 8; /* HH:MM:SS */
188 else
189 timesize = 5; /* HH:MM */
190
191 (void)time(&buf[0].ut_time);
192 (void)signal(SIGINT, onintr);
193 (void)signal(SIGQUIT, onintr);
194
195 while (--bl >= 0) {
196 if (lseek(wfd, (off_t)(bl * sizeof(buf)), SEEK_SET) == -1 ||
197 (bytes = read(wfd, buf, sizeof(buf))) == -1)
198 err(1, "%s", file);
199 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
200 /*
201 * if the terminal line is '~', the machine stopped.
202 * see utmp(5) for more info.
203 */
204 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
205 /* everybody just logged out */
206 for (T = ttylist; T; T = T->next)
207 T->logout = -bp->ut_time;
208 currentout = -bp->ut_time;
209 crmsg = strncmp(bp->ut_name, "shutdown",
210 UT_NAMESIZE) ? "crash" : "shutdown";
211 if (want(bp, NO)) {
212 ct = ctime(&bp->ut_time);
213 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
214 UT_NAMESIZE, UT_NAMESIZE,
215 bp->ut_name, UT_LINESIZE,
216 UT_LINESIZE, bp->ut_line,
217 UT_HOSTSIZE, UT_HOSTSIZE,
218 bp->ut_host, ct, timesize,
219 timesize, ct + 11);
220 if (maxrec != -1 && !--maxrec)
221 return;
222 }
223 continue;
224 }
225 /*
226 * if the line is '{' or '|', date got set; see
227 * utmp(5) for more info.
228 */
229 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
230 && !bp->ut_line[1]) {
231 if (want(bp, NO)) {
232 ct = ctime(&bp->ut_time);
233 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
234 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
235 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
236 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
237 ct, timesize, timesize, ct + 11);
238 if (maxrec && !--maxrec)
239 return;
240 }
241 continue;
242 }
243 /* find associated tty */
244 for (T = ttylist;; T = T->next) {
245 if (!T) {
246 /* add new one */
247 T = addtty(bp->ut_line);
248 break;
249 }
250 if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
251 break;
252 }
253 if (bp->ut_name[0] && want(bp, YES)) {
254 ct = ctime(&bp->ut_time);
255 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
256 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
257 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
258 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
259 ct, timesize, timesize, ct + 11);
260 if (!T->logout)
261 puts(" still logged in");
262 else {
263 if (T->logout < 0) {
264 T->logout = -T->logout;
265 printf("- %s", crmsg);
266 }
267 else
268 printf("- %*.*s",
269 timesize, timesize,
270 ctime(&T->logout)+11);
271 delta = T->logout - bp->ut_time;
272 if (delta < SECSPERDAY)
273 printf(" (%*.*s)\n",
274 timesize, timesize,
275 asctime(gmtime(&delta))+11);
276 else
277 printf(" (%ld+%*.*s)\n",
278 delta / SECSPERDAY,
279 timesize, timesize,
280 asctime(gmtime(&delta))+11);
281 }
282 if (maxrec != -1 && !--maxrec)
283 return;
284 }
285 T->logout = bp->ut_time;
286 }
287 }
288 ct = ctime(&buf[0].ut_time);
289 printf("\nwtmp begins %10.10s %*.*s \n", ct, timesize, timesize, ct + 11);
290 }
291
292 /*
293 * want --
294 * see if want this entry
295 */
296 int
297 want(bp, check)
298 struct utmp *bp;
299 int check;
300 {
301 ARG *step;
302
303 if (check)
304 /*
305 * when uucp and ftp log in over a network, the entry in
306 * the utmp file is the name plus their process id. See
307 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
308 */
309 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
310 bp->ut_line[3] = '\0';
311 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
312 bp->ut_line[4] = '\0';
313 if (!arglist)
314 return (YES);
315
316 for (step = arglist; step; step = step->next)
317 switch(step->type) {
318 case HOST_TYPE:
319 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
320 return (YES);
321 break;
322 case TTY_TYPE:
323 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
324 return (YES);
325 break;
326 case USER_TYPE:
327 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
328 return (YES);
329 break;
330 }
331 return (NO);
332 }
333
334 /*
335 * addarg --
336 * add an entry to a linked list of arguments
337 */
338 void
339 addarg(type, arg)
340 int type;
341 char *arg;
342 {
343 ARG *cur;
344
345 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
346 err(1, "malloc failure");
347 cur->next = arglist;
348 cur->type = type;
349 cur->name = arg;
350 arglist = cur;
351 }
352
353 /*
354 * addtty --
355 * add an entry to a linked list of ttys
356 */
357 TTY *
358 addtty(ttyname)
359 char *ttyname;
360 {
361 TTY *cur;
362
363 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
364 err(1, "malloc failure");
365 cur->next = ttylist;
366 cur->logout = currentout;
367 memmove(cur->tty, ttyname, UT_LINESIZE);
368 return (ttylist = cur);
369 }
370
371 /*
372 * hostconv --
373 * convert the hostname to search pattern; if the supplied host name
374 * has a domain attached that is the same as the current domain, rip
375 * off the domain suffix since that's what login(1) does.
376 */
377 void
378 hostconv(arg)
379 char *arg;
380 {
381 static int first = 1;
382 static char *hostdot, name[MAXHOSTNAMELEN];
383 char *argdot;
384
385 if (!(argdot = strchr(arg, '.')))
386 return;
387 if (first) {
388 first = 0;
389 if (gethostname(name, sizeof(name)))
390 err(1, "gethostname");
391 hostdot = strchr(name, '.');
392 }
393 if (hostdot && !strcasecmp(hostdot, argdot))
394 *argdot = '\0';
395 }
396
397 /*
398 * ttyconv --
399 * convert tty to correct name.
400 */
401 char *
402 ttyconv(arg)
403 char *arg;
404 {
405 char *mval;
406
407 /*
408 * kludge -- we assume that all tty's end with
409 * a two character suffix.
410 */
411 if (strlen(arg) == 2) {
412 /* either 6 for "ttyxx" or 8 for "console" */
413 if (!(mval = malloc((u_int)8)))
414 err(1, "malloc failure");
415 if (!strcmp(arg, "co"))
416 (void)strcpy(mval, "console");
417 else {
418 (void)strcpy(mval, "tty");
419 (void)strcpy(mval + 3, arg);
420 }
421 return (mval);
422 }
423 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
424 return (arg + 5);
425 return (arg);
426 }
427
428 /*
429 * onintr --
430 * on interrupt, we inform the user how far we've gotten
431 */
432 void
433 onintr(signo)
434 int signo;
435 {
436 char *ct;
437
438 ct = ctime(&buf[0].ut_time);
439 printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
440 if (signo == SIGINT)
441 exit(1);
442 (void)fflush(stdout); /* fix required for rsh */
443 }
444