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