]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/last.c
login-utils: verify writing to streams was successful
[thirdparty/util-linux.git] / login-utils / last.c
1 /*
2 * Berkeley last for Linux. Currently maintained by poe@daimi.aau.dk at
3 * ftp://ftp.daimi.aau.dk/pub/linux/poe/admutil*
4 *
5 * Copyright (c) 1987 Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
22 * - added Native Language Support
23 */
24
25 /* 2001-02-14 Marek Zelem <marek@fornax.sk>
26 * - using mmap() on Linux - great speed improvement
27 */
28
29 /*
30 * This command is deprecated. The utility is in maintenance mode,
31 * meaning we keep them in source tree for backward compatibility
32 * only. Do not waste time making this command better, unless the
33 * fix is about security or other very critical issue.
34 *
35 * See Documentation/deprecated.txt for more information.
36 */
37
38 /*
39 * last
40 */
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/file.h>
44 #include <sys/types.h>
45 #include <sys/mman.h>
46 #include <signal.h>
47 #include <string.h>
48 #include <time.h>
49 #include <utmp.h>
50 #include <stdio.h>
51 #include <getopt.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58
59 #include "closestream.h"
60 #include "pathnames.h"
61 #include "nls.h"
62 #include "xalloc.h"
63 #include "c.h"
64
65 #define SECDAY (24*60*60) /* seconds in a day */
66 #define NO 0 /* false/no */
67 #define YES 1 /* true/yes */
68
69 static struct utmp utmpbuf;
70
71 #define HMAX (int)sizeof(utmpbuf.ut_host) /* size of utmp host field */
72 #define LMAX (int)sizeof(utmpbuf.ut_line) /* size of utmp tty field */
73 #define NMAX (int)sizeof(utmpbuf.ut_name) /* size of utmp name field */
74
75 #ifndef MIN
76 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
77 #endif
78
79 /* maximum sizes used for printing */
80 /* probably we want a two-pass version that computes the right length */
81 int hmax = MIN(HMAX, 16);
82 int lmax = MIN(LMAX, 8);
83 int nmax = MIN(NMAX, 16);
84
85 typedef struct arg {
86 char *name; /* argument */
87 #define HOST_TYPE -2
88 #define TTY_TYPE -3
89 #define USER_TYPE -4
90 #define INET_TYPE -5
91 int type; /* type of arg */
92 struct arg *next; /* linked list pointer */
93 } ARG;
94 ARG *arglist; /* head of linked list */
95
96 typedef struct ttytab {
97 long logout; /* log out time */
98 char tty[LMAX + 1]; /* terminal name */
99 struct ttytab *next; /* linked list pointer */
100 } TTY;
101 TTY *ttylist; /* head of linked list */
102
103 static long currentout, /* current logout value */
104 maxrec; /* records to display */
105 static char *file = _PATH_WTMP; /* wtmp file */
106
107 static int doyear = 0; /* output year in dates */
108 static int dolong = 0; /* print also ip-addr */
109
110 static void wtmp(void);
111 static void addarg(int, char *);
112 static void hostconv(char *);
113 static void onintr(int);
114 static int want(struct utmp *, int);
115 TTY *addtty(char *);
116 static char *ttyconv(char *);
117
118 int
119 main(int argc, char **argv) {
120 int ch;
121
122 setlocale(LC_ALL, "");
123 bindtextdomain(PACKAGE, LOCALEDIR);
124 textdomain(PACKAGE);
125 atexit(close_stdout);
126
127 while ((ch = getopt(argc, argv, "0123456789yli:f:h:t:")) != -1)
128 switch((char)ch) {
129 case '0': case '1': case '2': case '3': case '4':
130 case '5': case '6': case '7': case '8': case '9':
131 /*
132 * kludge: last was originally designed to take
133 * a number after a dash.
134 */
135 if (!maxrec)
136 maxrec = atol(argv[optind - 1] + 1);
137 break;
138 case 'f':
139 file = optarg;
140 break;
141 case 'h':
142 hostconv(optarg);
143 addarg(HOST_TYPE, optarg);
144 break;
145 case 't':
146 addarg(TTY_TYPE, ttyconv(optarg));
147 break;
148 case 'y':
149 doyear = 1;
150 break;
151 case 'l':
152 dolong = 1;
153 break;
154 case 'i':
155 addarg(INET_TYPE, optarg);
156 break;
157 case '?':
158 default:
159 fputs(_("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"), stderr);
160 exit(EXIT_FAILURE);
161 }
162 for (argv += optind; *argv; ++argv) {
163 #define COMPATIBILITY
164 #ifdef COMPATIBILITY
165 /* code to allow "last p5" to work */
166 addarg(TTY_TYPE, ttyconv(*argv));
167 #endif
168 addarg(USER_TYPE, *argv);
169 }
170 wtmp();
171
172 return EXIT_SUCCESS;
173 }
174
175 static char *utmp_ctime(struct utmp *u)
176 {
177 time_t t = (time_t) u->ut_time;
178 return ctime(&t);
179 }
180
181 /*
182 * print_partial_line --
183 * print the first part of each output line according to specified format
184 */
185 static void
186 print_partial_line(struct utmp *bp) {
187 char *ct;
188
189 ct = utmp_ctime(bp);
190 printf("%-*.*s %-*.*s ", nmax, nmax, bp->ut_name,
191 lmax, lmax, bp->ut_line);
192
193 if (dolong) {
194 if (bp->ut_addr) {
195 struct in_addr foo;
196 foo.s_addr = bp->ut_addr;
197 printf("%-*.*s ", hmax, hmax, inet_ntoa(foo));
198 } else {
199 printf("%-*.*s ", hmax, hmax, "");
200 }
201 } else {
202 printf("%-*.*s ", hmax, hmax, bp->ut_host);
203 }
204
205 if (doyear) {
206 printf("%10.10s %4.4s %5.5s ", ct, ct + 20, ct + 11);
207 } else {
208 printf("%10.10s %5.5s ", ct, ct + 11);
209 }
210 }
211
212 /*
213 * wtmp --
214 * read through the wtmp file
215 */
216 static void
217 wtmp(void) {
218 register struct utmp *bp; /* current structure */
219 register TTY *T; /* tty list entry */
220 long delta; /* time difference */
221 char *crmsg = NULL;
222 char *ct = NULL;
223 int fd;
224 struct utmp *utl;
225 struct stat st;
226 int utl_len;
227 int listnr = 0;
228 int i;
229
230 utmpname(file);
231
232 {
233 #if defined(_HAVE_UT_TV)
234 struct timeval tv;
235 gettimeofday(&tv, NULL);
236 utmpbuf.ut_tv.tv_sec = tv.tv_sec;
237 utmpbuf.ut_tv.tv_usec = tv.tv_usec;
238 #else
239 time_t t;
240 time(&t);
241 utmpbuf.ut_time = t;
242 #endif
243 }
244
245 (void)signal(SIGINT, onintr);
246 (void)signal(SIGQUIT, onintr);
247
248 if ((fd = open(file,O_RDONLY)) < 0)
249 err(EXIT_FAILURE, _("%s: open failed"), file);
250
251 fstat(fd, &st);
252 utl_len = st.st_size;
253 utl = mmap(NULL, utl_len, PROT_READ|PROT_WRITE,
254 MAP_PRIVATE|MAP_FILE, fd, 0);
255 if (utl == NULL)
256 err(EXIT_FAILURE, _("%s: mmap failed"), file);
257
258 listnr = utl_len/sizeof(struct utmp);
259
260 if(listnr)
261 ct = utmp_ctime(&utl[0]);
262
263 for(i = listnr - 1; i >= 0; i--) {
264 bp = utl+i;
265 /*
266 * if the terminal line is '~', the machine stopped.
267 * see utmp(5) for more info.
268 */
269 if (!strncmp(bp->ut_line, "~", LMAX)) {
270 /*
271 * utmp(5) also mentions that the user
272 * name should be 'shutdown' or 'reboot'.
273 * Not checking the name causes e.g. runlevel
274 * changes to be displayed as 'crash'. -thaele
275 */
276 if (!strncmp(bp->ut_user, "reboot", NMAX) ||
277 !strncmp(bp->ut_user, "shutdown", NMAX)) {
278 /* everybody just logged out */
279 for (T = ttylist; T; T = T->next)
280 T->logout = -bp->ut_time;
281 }
282
283 currentout = -bp->ut_time;
284 crmsg = (strncmp(bp->ut_name, "shutdown", NMAX)
285 ? "crash" : "down ");
286 if (!bp->ut_name[0])
287 (void)strcpy(bp->ut_name, "reboot");
288 if (want(bp, NO)) {
289 ct = utmp_ctime(bp);
290 if(bp->ut_type != LOGIN_PROCESS) {
291 print_partial_line(bp);
292 putchar('\n');
293 }
294 if (maxrec && !--maxrec)
295 return;
296 }
297 continue;
298 }
299 /* find associated tty */
300 for (T = ttylist;; T = T->next) {
301 if (!T) {
302 /* add new one */
303 T = addtty(bp->ut_line);
304 break;
305 }
306 if (!strncmp(T->tty, bp->ut_line, LMAX))
307 break;
308 }
309 if (bp->ut_name[0] && bp->ut_type != LOGIN_PROCESS
310 && bp->ut_type != DEAD_PROCESS
311 && want(bp, YES)) {
312
313 print_partial_line(bp);
314
315 if (!T->logout)
316 puts(_(" still logged in"));
317 else {
318 if (T->logout < 0) {
319 T->logout = -T->logout;
320 printf("- %s", crmsg);
321 }
322 else
323 printf("- %5.5s", ctime(&T->logout)+11);
324 delta = T->logout - bp->ut_time;
325 if (delta < SECDAY)
326 printf(" (%5.5s)\n", asctime(gmtime(&delta))+11);
327 else
328 printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
329 }
330 if (maxrec != -1 && !--maxrec)
331 return;
332 }
333 T->logout = bp->ut_time;
334 utmpbuf.ut_time = bp->ut_time;
335 }
336 munmap(utl,utl_len);
337 close(fd);
338 if(ct) printf(_("\nwtmp begins %s"), ct); /* ct already ends in \n */
339 }
340
341 /*
342 * want --
343 * see if want this entry
344 */
345 static int
346 want(struct utmp *bp, int check) {
347 register ARG *step;
348
349 if (check) {
350 /*
351 * when uucp and ftp log in over a network, the entry in
352 * the utmp file is the name plus their process id. See
353 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
354 */
355 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
356 bp->ut_line[3] = '\0';
357 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
358 bp->ut_line[4] = '\0';
359 }
360 if (!arglist)
361 return YES;
362
363 for (step = arglist; step; step = step->next)
364 switch(step->type) {
365 case HOST_TYPE:
366 if (!strncmp(step->name, bp->ut_host, HMAX))
367 return YES;
368 break;
369 case TTY_TYPE:
370 if (!strncmp(step->name, bp->ut_line, LMAX))
371 return YES;
372 break;
373 case USER_TYPE:
374 if (!strncmp(step->name, bp->ut_name, NMAX))
375 return YES;
376 break;
377 case INET_TYPE:
378 if ((in_addr_t) bp->ut_addr == inet_addr(step->name))
379 return YES;
380 break;
381 default:
382 abort();
383 }
384 return NO;
385 }
386
387 /*
388 * addarg --
389 * add an entry to a linked list of arguments
390 */
391 static void
392 addarg(int type, char *arg) {
393 register ARG *cur;
394
395 cur = xmalloc(sizeof(ARG));
396 cur->next = arglist;
397 cur->type = type;
398 cur->name = arg;
399 arglist = cur;
400 }
401
402 /*
403 * addtty --
404 * add an entry to a linked list of ttys
405 */
406 TTY *
407 addtty(char *ttyname) {
408 register TTY *cur;
409
410 cur = xmalloc(sizeof(TTY));
411 cur->next = ttylist;
412 cur->logout = currentout;
413 memcpy(cur->tty, ttyname, LMAX);
414 return(ttylist = cur);
415 }
416
417 /*
418 * hostconv --
419 * convert the hostname to search pattern; if the supplied host name
420 * has a domain attached that is the same as the current domain, rip
421 * off the domain suffix since that's what login(1) does.
422 */
423 static void
424 hostconv(char *arg) {
425 static int first = 1;
426 static char *hostdot,
427 name[MAXHOSTNAMELEN];
428 char *argdot;
429
430 if (!(argdot = strchr(arg, '.')))
431 return;
432 if (first) {
433 first = 0;
434 if (gethostname(name, sizeof(name)))
435 err(EXIT_FAILURE, _("gethostname failed"));
436
437 hostdot = strchr(name, '.');
438 }
439 if (hostdot && !strcmp(hostdot, argdot))
440 *argdot = '\0';
441 }
442
443 /*
444 * ttyconv --
445 * convert tty to correct name.
446 */
447 static char *
448 ttyconv(char *arg) {
449 char *mval;
450
451 /*
452 * kludge -- we assume that all tty's end with
453 * a two character suffix.
454 */
455 if (strlen(arg) == 2) {
456 /* either 6 for "ttyxx" or 8 for "console" */
457 mval = xmalloc(8);
458 if (!strncmp(arg, "co", 2))
459 (void)strcpy(mval, "console");
460 else {
461 (void)strcpy(mval, "tty");
462 (void)strncpy(mval + 3, arg, 4);
463 }
464 return mval;
465 }
466 if (!strncmp(arg, "/dev/", sizeof("/dev/") - 1))
467 return arg + 5;
468
469 return arg;
470 }
471
472 /*
473 * onintr --
474 * on interrupt, we inform the user how far we've gotten
475 */
476 static void
477 onintr(int signo) {
478 char *ct;
479
480 ct = utmp_ctime(&utmpbuf);
481 printf(_("\ninterrupted %10.10s %5.5s \n"), ct, ct + 11);
482 if (signo == SIGINT)
483 _exit(EXIT_FAILURE);
484 fflush(stdout); /* fix required for rsh */
485 }