last.c revision 1.30 1 /* $NetBSD: last.c,v 1.30 2007/01/06 13:31:06 cbiere 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT(
35 "@(#) Copyright (c) 1987, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n");
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
42 #endif
43 __RCSID("$NetBSD: last.c,v 1.30 2007/01/06 13:31:06 cbiere Exp $");
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <paths.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <tzfile.h>
59 #include <unistd.h>
60 #include <arpa/inet.h>
61 #ifdef SUPPORT_UTMPX
62 #include <utmpx.h>
63 #endif
64 #ifdef SUPPORT_UTMP
65 #include <utmp.h>
66 #endif
67 #include <util.h>
68
69 #ifndef UT_NAMESIZE
70 #define UT_NAMESIZE 8
71 #define UT_LINESIZE 8
72 #define UT_HOSTSIZE 16
73 #endif
74 #ifndef SIGNATURE
75 #define SIGNATURE -1
76 #endif
77
78
79
80 #define NO 0 /* false/no */
81 #define YES 1 /* true/yes */
82
83 #define TBUFLEN 30 /* length of time string buffer */
84 #define TFMT "%a %b %d %R" /* strftime format string */
85 #define LTFMT "%a %b %d %Y %T" /* strftime long format string */
86 #define TFMTS "%R" /* strftime format string - time only */
87 #define LTFMTS "%T" /* strftime long format string - " */
88
89 /* fmttime() flags */
90 #define FULLTIME 0x1 /* show year, seconds */
91 #define TIMEONLY 0x2 /* show time only, not date */
92 #define GMT 0x4 /* show time at GMT, for offsets only */
93
94 #define MAXUTMP 1024
95
96 typedef struct arg {
97 char *name; /* argument */
98 #define HOST_TYPE -2
99 #define TTY_TYPE -3
100 #define USER_TYPE -4
101 int type; /* type of arg */
102 struct arg *next; /* linked list pointer */
103 } ARG;
104 static ARG *arglist; /* head of linked list */
105
106 typedef struct ttytab {
107 time_t logout; /* log out time */
108 char tty[128]; /* terminal name */
109 struct ttytab *next; /* linked list pointer */
110 } TTY;
111 static TTY *ttylist; /* head of linked list */
112
113 static time_t currentout; /* current logout value */
114 static long maxrec; /* records to display */
115 static int fulltime = 0; /* Display seconds? */
116 static int xflag; /* Assume file is wtmpx format */
117
118 int main(int, char *[]);
119
120 static void addarg(int, char *);
121 static TTY *addtty(const char *);
122 static void hostconv(char *);
123 static char *ttyconv(char *);
124 #ifdef SUPPORT_UTMPX
125 static void wtmpx(const char *, int, int, int, int);
126 #endif
127 #ifdef SUPPORT_UTMP
128 static void wtmp(const char *, int, int, int, int);
129 #endif
130 static char *fmttime(time_t, int);
131 static void usage(void);
132
133 static
134 void usage(void)
135 {
136 (void)fprintf(stderr, "usage: %s [-#%s] [-nTx] [-f file]"
137 " [-H hostsize] [-h host] [-L linesize]\n"
138 "\t [-N namesize] [-t tty] [user ...]\n", getprogname(),
139 #ifdef NOTYET_SUPPORT_UTMPX
140 "w"
141 #else
142 ""
143 #endif
144 );
145 exit(EXIT_FAILURE);
146 }
147
148 int
149 main(int argc, char *argv[])
150 {
151 int ch;
152 char *p;
153 char *file = NULL;
154 int namesize = UT_NAMESIZE;
155 int linesize = UT_LINESIZE;
156 int hostsize = UT_HOSTSIZE;
157 int numeric = 0;
158
159 maxrec = -1;
160
161 while ((ch = getopt(argc, argv, "0123456789f:H:h:L:nN:Tt:x")) != -1)
162 switch (ch) {
163 case '0': case '1': case '2': case '3': case '4':
164 case '5': case '6': case '7': case '8': case '9':
165 /*
166 * kludge: last was originally designed to take
167 * a number after a dash.
168 */
169 if (maxrec == -1) {
170 p = argv[optind - 1];
171 if (p[0] == '-' && p[1] == ch && !p[2])
172 maxrec = atol(++p);
173 else if (optind < argc)
174 maxrec = atol(argv[optind] + 1);
175 else
176 usage();
177 if (!maxrec)
178 return 0;
179 }
180 break;
181 case 'f':
182 file = optarg;
183 if ('\0' == file[0])
184 usage();
185 break;
186 case 'H':
187 hostsize = atoi(optarg);
188 if (hostsize < 1)
189 usage();
190 break;
191 case 'h':
192 hostconv(optarg);
193 addarg(HOST_TYPE, optarg);
194 break;
195 case 'L':
196 linesize = atoi(optarg);
197 if (linesize < 1)
198 usage();
199 break;
200 case 'N':
201 namesize = atoi(optarg);
202 if (namesize < 1)
203 usage();
204 break;
205 case 'n':
206 numeric = 1;
207 break;
208 case 'T':
209 fulltime = 1;
210 break;
211 case 't':
212 addarg(TTY_TYPE, ttyconv(optarg));
213 break;
214 case 'x':
215 xflag = 1;
216 break;
217 case '?':
218 default:
219 usage();
220 }
221
222 if (argc) {
223 setlinebuf(stdout);
224 for (argv += optind; *argv; ++argv) {
225 #define COMPATIBILITY
226 #ifdef COMPATIBILITY
227 /* code to allow "last p5" to work */
228 addarg(TTY_TYPE, ttyconv(*argv));
229 #endif
230 addarg(USER_TYPE, *argv);
231 }
232 }
233 if (file == NULL) {
234 #ifdef SUPPORT_UTMPX
235 if (access(_PATH_WTMPX, R_OK) == 0)
236 file = _PATH_WTMPX;
237 else
238 #endif
239 #ifdef SUPPORT_UTMP
240 if (access(_PATH_WTMP, R_OK) == 0)
241 file = _PATH_WTMP;
242 #endif
243 if (file == NULL)
244 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
245 errx(EXIT_FAILURE, "Cannot access `%s' or `%s'", _PATH_WTMPX,
246 _PATH_WTMP);
247 #elif defined(SUPPORT_UTMPX)
248 errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMPX);
249 #elif defined(SUPPORT_UTMP)
250 errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMP);
251 #else
252 errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
253 #endif
254 }
255 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
256 if (file[strlen(file) - 1] == 'x' || xflag)
257 wtmpx(file, namesize, linesize, hostsize, numeric);
258 else
259 wtmp(file, namesize, linesize, hostsize, numeric);
260 #elif defined(SUPPORT_UTMPX)
261 wtmpx(file, namesize, linesize, hostsize, numeric);
262 #elif defined(SUPPORT_UTMP)
263 wtmp(file, namesize, linesize, hostsize, numeric);
264 #else
265 errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
266 #endif
267 exit(EXIT_SUCCESS);
268 }
269
270
271 /*
272 * addarg --
273 * add an entry to a linked list of arguments
274 */
275 static void
276 addarg(int type, char *arg)
277 {
278 ARG *cur;
279
280 if (!(cur = (ARG *)malloc(sizeof(ARG))))
281 err(EXIT_FAILURE, "malloc failure");
282 cur->next = arglist;
283 cur->type = type;
284 cur->name = arg;
285 arglist = cur;
286 }
287
288 /*
289 * addtty --
290 * add an entry to a linked list of ttys
291 */
292 static TTY *
293 addtty(const char *ttyname)
294 {
295 TTY *cur;
296
297 if (!(cur = (TTY *)malloc(sizeof(TTY))))
298 err(EXIT_FAILURE, "malloc failure");
299 cur->next = ttylist;
300 cur->logout = currentout;
301 memmove(cur->tty, ttyname, sizeof(cur->tty));
302 return (ttylist = cur);
303 }
304
305 /*
306 * hostconv --
307 * convert the hostname to search pattern; if the supplied host name
308 * has a domain attached that is the same as the current domain, rip
309 * off the domain suffix since that's what login(1) does.
310 */
311 static void
312 hostconv(char *arg)
313 {
314 static int first = 1;
315 static char *hostdot, name[MAXHOSTNAMELEN + 1];
316 char *argdot;
317
318 if (!(argdot = strchr(arg, '.')))
319 return;
320 if (first) {
321 first = 0;
322 if (gethostname(name, sizeof(name)))
323 err(EXIT_FAILURE, "gethostname");
324 name[sizeof(name) - 1] = '\0';
325 hostdot = strchr(name, '.');
326 }
327 if (hostdot && !strcasecmp(hostdot, argdot))
328 *argdot = '\0';
329 }
330
331 /*
332 * ttyconv --
333 * convert tty to correct name.
334 */
335 static char *
336 ttyconv(char *arg)
337 {
338 char *mval;
339
340 if (!strcmp(arg, "co"))
341 return ("console");
342 /*
343 * kludge -- we assume that all tty's end with
344 * a two character suffix.
345 */
346 if (strlen(arg) == 2) {
347 if (asprintf(&mval, "tty%s", arg) == -1)
348 err(EXIT_FAILURE, "malloc failure");
349 return (mval);
350 }
351 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
352 return (&arg[sizeof(_PATH_DEV) - 1]);
353 return (arg);
354 }
355
356 /*
357 * fmttime --
358 * return pointer to (static) formatted time string.
359 */
360 static char *
361 fmttime(time_t t, int flags)
362 {
363 struct tm *tm;
364 static char tbuf[TBUFLEN];
365
366 tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
367 strftime(tbuf, sizeof(tbuf),
368 (flags & TIMEONLY)
369 ? (flags & FULLTIME ? LTFMTS : TFMTS)
370 : (flags & FULLTIME ? LTFMT : TFMT),
371 tm);
372 return (tbuf);
373 }
374
375 #ifdef SUPPORT_UTMP
376 #define TYPE(a) 0
377 #define NAMESIZE UT_NAMESIZE
378 #define LINESIZE UT_LINESIZE
379 #define HOSTSIZE UT_HOSTSIZE
380 #define ut_timefld ut_time
381 #define FIRSTVALID 0
382 #include "want.c"
383 #undef TYPE /*(a)*/
384 #undef NAMESIZE
385 #undef LINESIZE
386 #undef HOSTSIZE
387 #undef ut_timefld
388 #undef FIRSTVALID
389 #endif
390
391 #ifdef SUPPORT_UTMPX
392 #define utmp utmpx
393 #define want wantx
394 #define wtmp wtmpx
395 #define gethost gethostx
396 #define buf bufx
397 #define onintr onintrx
398 #define TYPE(a) (a)->ut_type
399 #define NAMESIZE UTX_USERSIZE
400 #define LINESIZE UTX_LINESIZE
401 #define HOSTSIZE UTX_HOSTSIZE
402 #define ut_timefld ut_xtime
403 #define FIRSTVALID 1
404 #include "want.c"
405 #endif
406