last.c revision 1.7 1 /* $NetBSD: last.c,v 1.7 1997/07/17 02:36:56 perry 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 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1987, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
45 #endif
46 static char rcsid[] = "$NetBSD: last.c,v 1.7 1997/07/17 02:36:56 perry Exp $";
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/stat.h>
51
52 #include <err.h>
53 #include <fcntl.h>
54 #include <paths.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62 #include <utmp.h>
63
64 #define NO 0 /* false/no */
65 #define YES 1 /* true/yes */
66
67 static struct utmp buf[1024]; /* utmp read buffer */
68
69 typedef struct arg {
70 char *name; /* argument */
71 #define HOST_TYPE -2
72 #define TTY_TYPE -3
73 #define USER_TYPE -4
74 int type; /* type of arg */
75 struct arg *next; /* linked list pointer */
76 } ARG;
77 ARG *arglist; /* head of linked list */
78
79 typedef struct ttytab {
80 time_t logout; /* log out time */
81 char tty[UT_LINESIZE + 1]; /* terminal name */
82 struct ttytab *next; /* linked list pointer */
83 } TTY;
84 TTY *ttylist; /* head of linked list */
85
86 static time_t currentout; /* current logout value */
87 static long maxrec; /* records to display */
88 static char *file = _PATH_WTMP; /* wtmp file */
89 static int fulltime = 0; /* Display seconds? */
90
91 void addarg __P((int, char *));
92 TTY *addtty __P((char *));
93 void hostconv __P((char *));
94 void onintr __P((int));
95 char *ttyconv __P((char *));
96 int want __P((struct utmp *, int));
97 void wtmp __P((void));
98
99 int
100 main(argc, argv)
101 int argc;
102 char *argv[];
103 {
104 extern int optind;
105 extern char *optarg;
106 int ch;
107 char *p;
108
109 maxrec = -1;
110 while ((ch = getopt(argc, argv, "0123456789f:h:t:T")) != EOF)
111 switch (ch) {
112 case '0': case '1': case '2': case '3': case '4':
113 case '5': case '6': case '7': case '8': case '9':
114 /*
115 * kludge: last was originally designed to take
116 * a number after a dash.
117 */
118 if (maxrec == -1) {
119 p = argv[optind - 1];
120 if (p[0] == '-' && p[1] == ch && !p[2])
121 maxrec = atol(++p);
122 else
123 maxrec = atol(argv[optind] + 1);
124 if (!maxrec)
125 exit(0);
126 }
127 break;
128 case 'f':
129 file = optarg;
130 break;
131 case 'h':
132 hostconv(optarg);
133 addarg(HOST_TYPE, optarg);
134 break;
135 case 't':
136 addarg(TTY_TYPE, ttyconv(optarg));
137 break;
138 case 'T':
139 fulltime = 1;
140 break;
141 case '?':
142 default:
143 (void)fprintf(stderr,
144 "usage: last [-#] [-f file] [-t tty] [-h hostname] [-T] [user ...]\n");
145 exit(1);
146 }
147
148 if (argc) {
149 setlinebuf(stdout);
150 for (argv += optind; *argv; ++argv) {
151 #define COMPATIBILITY
152 #ifdef COMPATIBILITY
153 /* code to allow "last p5" to work */
154 addarg(TTY_TYPE, ttyconv(*argv));
155 #endif
156 addarg(USER_TYPE, *argv);
157 }
158 }
159 wtmp();
160 exit(0);
161 }
162
163 /*
164 * wtmp --
165 * read through the wtmp file
166 */
167 void
168 wtmp()
169 {
170 struct utmp *bp; /* current structure */
171 TTY *T; /* tty list entry */
172 struct stat stb; /* stat of file for size */
173 time_t bl, delta; /* time difference */
174 int timesize; /* how much of time string to print */
175 int bytes, wfd;
176 char *ct, *crmsg;
177
178 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
179 err(1, "%s", file);
180 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
181
182 if (fulltime)
183 timesize = 8; /* HH:MM:SS */
184 else
185 timesize = 5; /* HH:MM */
186
187 (void)time(&buf[0].ut_time);
188 (void)signal(SIGINT, onintr);
189 (void)signal(SIGQUIT, onintr);
190
191 while (--bl >= 0) {
192 if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
193 (bytes = read(wfd, buf, sizeof(buf))) == -1)
194 err(1, "%s", file);
195 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
196 /*
197 * if the terminal line is '~', the machine stopped.
198 * see utmp(5) for more info.
199 */
200 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
201 /* everybody just logged out */
202 for (T = ttylist; T; T = T->next)
203 T->logout = -bp->ut_time;
204 currentout = -bp->ut_time;
205 crmsg = strncmp(bp->ut_name, "shutdown",
206 UT_NAMESIZE) ? "crash" : "shutdown";
207 if (want(bp, NO)) {
208 ct = ctime(&bp->ut_time);
209 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
210 UT_NAMESIZE, UT_NAMESIZE,
211 bp->ut_name, UT_LINESIZE,
212 UT_LINESIZE, bp->ut_line,
213 UT_HOSTSIZE, UT_HOSTSIZE,
214 bp->ut_host, ct, timesize,
215 timesize, ct + 11);
216 if (maxrec != -1 && !--maxrec)
217 return;
218 }
219 continue;
220 }
221 /*
222 * if the line is '{' or '|', date got set; see
223 * utmp(5) for more info.
224 */
225 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
226 && !bp->ut_line[1]) {
227 if (want(bp, NO)) {
228 ct = ctime(&bp->ut_time);
229 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s \n",
230 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
231 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
232 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
233 ct, timesize, timesize, ct + 11);
234 if (maxrec && !--maxrec)
235 return;
236 }
237 continue;
238 }
239 /* find associated tty */
240 for (T = ttylist;; T = T->next) {
241 if (!T) {
242 /* add new one */
243 T = addtty(bp->ut_line);
244 break;
245 }
246 if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
247 break;
248 }
249 if (bp->ut_name[0] && want(bp, YES)) {
250 ct = ctime(&bp->ut_time);
251 printf("%-*.*s %-*.*s %-*.*s %10.10s %*.*s ",
252 UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
253 UT_LINESIZE, UT_LINESIZE, bp->ut_line,
254 UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
255 ct, timesize, timesize, ct + 11);
256 if (!T->logout)
257 puts(" still logged in");
258 else {
259 if (T->logout < 0) {
260 T->logout = -T->logout;
261 printf("- %s", crmsg);
262 }
263 else
264 printf("- %*.*s",
265 timesize, timesize,
266 ctime(&T->logout)+11);
267 delta = T->logout - bp->ut_time;
268 if (delta < SECSPERDAY)
269 printf(" (%*.*s)\n",
270 timesize, timesize,
271 asctime(gmtime(&delta))+11);
272 else
273 printf(" (%ld+%*.*s)\n",
274 delta / SECSPERDAY,
275 timesize, timesize,
276 asctime(gmtime(&delta))+11);
277 }
278 if (maxrec != -1 && !--maxrec)
279 return;
280 }
281 T->logout = bp->ut_time;
282 }
283 }
284 ct = ctime(&buf[0].ut_time);
285 printf("\nwtmp begins %10.10s %*.*s \n", ct, timesize, timesize, ct + 11);
286 }
287
288 /*
289 * want --
290 * see if want this entry
291 */
292 int
293 want(bp, check)
294 struct utmp *bp;
295 int check;
296 {
297 ARG *step;
298
299 if (check)
300 /*
301 * when uucp and ftp log in over a network, the entry in
302 * the utmp file is the name plus their process id. See
303 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
304 */
305 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
306 bp->ut_line[3] = '\0';
307 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
308 bp->ut_line[4] = '\0';
309 if (!arglist)
310 return (YES);
311
312 for (step = arglist; step; step = step->next)
313 switch(step->type) {
314 case HOST_TYPE:
315 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
316 return (YES);
317 break;
318 case TTY_TYPE:
319 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
320 return (YES);
321 break;
322 case USER_TYPE:
323 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
324 return (YES);
325 break;
326 }
327 return (NO);
328 }
329
330 /*
331 * addarg --
332 * add an entry to a linked list of arguments
333 */
334 void
335 addarg(type, arg)
336 int type;
337 char *arg;
338 {
339 ARG *cur;
340
341 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
342 err(1, "malloc failure");
343 cur->next = arglist;
344 cur->type = type;
345 cur->name = arg;
346 arglist = cur;
347 }
348
349 /*
350 * addtty --
351 * add an entry to a linked list of ttys
352 */
353 TTY *
354 addtty(ttyname)
355 char *ttyname;
356 {
357 TTY *cur;
358
359 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
360 err(1, "malloc failure");
361 cur->next = ttylist;
362 cur->logout = currentout;
363 memmove(cur->tty, ttyname, UT_LINESIZE);
364 return (ttylist = cur);
365 }
366
367 /*
368 * hostconv --
369 * convert the hostname to search pattern; if the supplied host name
370 * has a domain attached that is the same as the current domain, rip
371 * off the domain suffix since that's what login(1) does.
372 */
373 void
374 hostconv(arg)
375 char *arg;
376 {
377 static int first = 1;
378 static char *hostdot, name[MAXHOSTNAMELEN];
379 char *argdot;
380
381 if (!(argdot = strchr(arg, '.')))
382 return;
383 if (first) {
384 first = 0;
385 if (gethostname(name, sizeof(name)))
386 err(1, "gethostname");
387 hostdot = strchr(name, '.');
388 }
389 if (hostdot && !strcasecmp(hostdot, argdot))
390 *argdot = '\0';
391 }
392
393 /*
394 * ttyconv --
395 * convert tty to correct name.
396 */
397 char *
398 ttyconv(arg)
399 char *arg;
400 {
401 char *mval;
402
403 /*
404 * kludge -- we assume that all tty's end with
405 * a two character suffix.
406 */
407 if (strlen(arg) == 2) {
408 /* either 6 for "ttyxx" or 8 for "console" */
409 if (!(mval = malloc((u_int)8)))
410 err(1, "malloc failure");
411 if (!strcmp(arg, "co"))
412 (void)strcpy(mval, "console");
413 else {
414 (void)strcpy(mval, "tty");
415 (void)strcpy(mval + 3, arg);
416 }
417 return (mval);
418 }
419 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
420 return (arg + 5);
421 return (arg);
422 }
423
424 /*
425 * onintr --
426 * on interrupt, we inform the user how far we've gotten
427 */
428 void
429 onintr(signo)
430 int signo;
431 {
432 char *ct;
433
434 ct = ctime(&buf[0].ut_time);
435 printf("\ninterrupted %10.10s %8.8s \n", ct, ct + 11);
436 if (signo == SIGINT)
437 exit(1);
438 (void)fflush(stdout); /* fix required for rsh */
439 }
440