last.c revision 1.10 1 /* $NetBSD: last.c,v 1.10 1998/07/06 06:51:08 mrg 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.10 1998/07/06 06:51:08 mrg 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 if (!arglist)
315 return (YES);
316
317 for (step = arglist; step; step = step->next)
318 switch(step->type) {
319 case HOST_TYPE:
320 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
321 return (YES);
322 break;
323 case TTY_TYPE:
324 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
325 return (YES);
326 break;
327 case USER_TYPE:
328 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
329 return (YES);
330 break;
331 }
332 return (NO);
333 }
334
335 /*
336 * addarg --
337 * add an entry to a linked list of arguments
338 */
339 void
340 addarg(type, arg)
341 int type;
342 char *arg;
343 {
344 ARG *cur;
345
346 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
347 err(1, "malloc failure");
348 cur->next = arglist;
349 cur->type = type;
350 cur->name = arg;
351 arglist = cur;
352 }
353
354 /*
355 * addtty --
356 * add an entry to a linked list of ttys
357 */
358 TTY *
359 addtty(ttyname)
360 char *ttyname;
361 {
362 TTY *cur;
363
364 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
365 err(1, "malloc failure");
366 cur->next = ttylist;
367 cur->logout = currentout;
368 memmove(cur->tty, ttyname, UT_LINESIZE);
369 return (ttylist = cur);
370 }
371
372 /*
373 * hostconv --
374 * convert the hostname to search pattern; if the supplied host name
375 * has a domain attached that is the same as the current domain, rip
376 * off the domain suffix since that's what login(1) does.
377 */
378 void
379 hostconv(arg)
380 char *arg;
381 {
382 static int first = 1;
383 static char *hostdot, name[MAXHOSTNAMELEN + 1];
384 char *argdot;
385
386 if (!(argdot = strchr(arg, '.')))
387 return;
388 if (first) {
389 first = 0;
390 if (gethostname(name, sizeof(name)))
391 err(1, "gethostname");
392 name[sizeof(name) - 1] = '\0';
393 hostdot = strchr(name, '.');
394 }
395 if (hostdot && !strcasecmp(hostdot, argdot))
396 *argdot = '\0';
397 }
398
399 /*
400 * ttyconv --
401 * convert tty to correct name.
402 */
403 char *
404 ttyconv(arg)
405 char *arg;
406 {
407 char *mval;
408
409 /*
410 * kludge -- we assume that all tty's end with
411 * a two character suffix.
412 */
413 if (strlen(arg) == 2) {
414 /* either 6 for "ttyxx" or 8 for "console" */
415 if (!(mval = malloc((u_int)8)))
416 err(1, "malloc failure");
417 if (!strcmp(arg, "co"))
418 (void)strcpy(mval, "console");
419 else {
420 (void)strcpy(mval, "tty");
421 (void)strcpy(mval + 3, arg);
422 }
423 return (mval);
424 }
425 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
426 return (arg + 5);
427 return (arg);
428 }
429
430 /*
431 * onintr --
432 * on interrupt, we inform the user how far we've gotten
433 */
434 void
435 onintr(signo)
436 int signo;
437 {
438 char *ct;
439
440 ct = ctime(&buf[0].ut_time);
441 printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
442 if (signo == SIGINT)
443 exit(1);
444 (void)fflush(stdout); /* fix required for rsh */
445 }
446