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