1 /* $NetBSD: want.c,v 1.14 2011/09/16 15:39:27 joerg 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 static struct utmp *buf; 32 33 static void onintr(int); 34 static int want(struct utmp *, int); 35 static const char *gethost(struct utmp *, const char *, int); 36 37 static const char * 38 /*ARGSUSED*/ 39 gethost(struct utmp *ut, const char *host, int numeric) 40 { 41 #if FIRSTVALID == 0 42 return numeric ? "" : host; 43 #else 44 if (numeric) { 45 static char hbuf[512]; 46 hbuf[0] = '\0'; 47 (void)sockaddr_snprintf(hbuf, sizeof(hbuf), "%a", 48 (struct sockaddr *)&ut->ut_ss); 49 return hbuf; 50 } else 51 return host; 52 #endif 53 } 54 55 #define NULTERM(what) \ 56 if (check ## what) \ 57 (void)strlcpy(what ## p = what ## buf, bp->ut_ ## what, \ 58 sizeof(what ## buf)); \ 59 else \ 60 what ## p = bp->ut_ ## what 61 62 /* 63 * wtmp -- 64 * read through the wtmp file 65 */ 66 static void 67 wtmp(const char *file, int namesz, int linesz, int hostsz, int numeric) 68 { 69 struct utmp *bp; /* current structure */ 70 TTY *T; /* tty list entry */ 71 struct stat stb; /* stat of file for sz */ 72 off_t offset; 73 int wfd; 74 char *ct; 75 const char *crmsg; 76 size_t len = sizeof(*buf) * MAXUTMP; 77 char namebuf[sizeof(bp->ut_name) + 1], *namep; 78 char linebuf[sizeof(bp->ut_line) + 1], *linep; 79 char hostbuf[sizeof(bp->ut_host) + 1], *hostp; 80 int checkname = namesz > (int)sizeof(bp->ut_name); 81 int checkline = linesz > (int)sizeof(bp->ut_line); 82 int checkhost = hostsz > (int)sizeof(bp->ut_host); 83 84 if ((buf = malloc(len)) == NULL) 85 err(EXIT_FAILURE, "Cannot allocate utmp buffer"); 86 87 crmsg = NULL; 88 89 if (!strcmp(file, "-")) { 90 wfd = STDIN_FILENO; 91 file = "<stdin>"; 92 } else if ((wfd = open(file, O_RDONLY, 0)) < 0) { 93 err(EXIT_FAILURE, "%s", file); 94 } 95 96 if (lseek(wfd, 0, SEEK_CUR) < 0) { 97 const char *dir; 98 char *tfile; 99 int tempfd; 100 ssize_t tlen; 101 102 if (ESPIPE != errno) { 103 err(EXIT_FAILURE, "lseek"); 104 } 105 dir = getenv("TMPDIR"); 106 if (asprintf(&tfile, "%s/last.XXXXXX", dir ? dir : _PATH_TMP) == -1) 107 err(EXIT_FAILURE, "asprintf"); 108 tempfd = mkstemp(tfile); 109 if (tempfd < 0) { 110 err(EXIT_FAILURE, "mkstemp"); 111 } 112 unlink(tfile); 113 for (;;) { 114 tlen = read(wfd, buf, len); 115 if (tlen < 0) { 116 err(1, "%s: read", file); 117 } 118 if (tlen == 0) { 119 break; 120 } 121 if (write(tempfd, buf, tlen) != tlen) { 122 err(1, "%s: write", tfile); 123 } 124 } 125 wfd = tempfd; 126 } 127 128 if (fstat(wfd, &stb) == -1) 129 err(EXIT_FAILURE, "%s: fstat", file); 130 if (!S_ISREG(stb.st_mode)) 131 errx(EXIT_FAILURE, "%s: Not a regular file", file); 132 133 buf[FIRSTVALID].ut_timefld = time(NULL); 134 (void)signal(SIGINT, onintr); 135 (void)signal(SIGQUIT, onintr); 136 137 offset = stb.st_size; 138 /* Ignore trailing garbage or partial record */ 139 offset -= offset % (off_t) sizeof(*buf); 140 141 while (offset >= (off_t) sizeof(*buf)) { 142 ssize_t ret, i; 143 size_t size; 144 145 size = MIN((off_t)len, offset); 146 offset -= size; /* Always a multiple of sizeof(*buf) */ 147 ret = pread(wfd, buf, size, offset); 148 if (ret < 0) { 149 err(EXIT_FAILURE, "%s: pread", file); 150 } else if ((size_t) ret < size) { 151 err(EXIT_FAILURE, "%s: Unexpected end of file", file); 152 } 153 154 for (i = ret / sizeof(*buf) - 1; i >= 0; i--) { 155 bp = &buf[i]; 156 157 NULTERM(name); 158 NULTERM(line); 159 NULTERM(host); 160 /* 161 * if the terminal line is '~', the machine stopped. 162 * see utmp(5) for more info. 163 */ 164 if (linep[0] == '~' && !linep[1]) { 165 /* everybody just logged out */ 166 for (T = ttylist; T; T = T->next) 167 T->logout = -bp->ut_timefld; 168 currentout = -bp->ut_timefld; 169 crmsg = strncmp(namep, "shutdown", 170 namesz) ? "crash" : "shutdown"; 171 if (want(bp, NO)) { 172 ct = fmttime(bp->ut_timefld, fulltime); 173 printf("%-*.*s %-*.*s %-*.*s %s\n", 174 namesz, namesz, namep, 175 linesz, linesz, linep, 176 hostsz, hostsz, 177 gethost(bp, hostp, numeric), ct); 178 if (maxrec != -1 && !--maxrec) 179 return; 180 } 181 continue; 182 } 183 /* 184 * if the line is '{' or '|', date got set; see 185 * utmp(5) for more info. 186 */ 187 if ((linep[0] == '{' || linep[0] == '|') && !linep[1]) { 188 if (want(bp, NO)) { 189 ct = fmttime(bp->ut_timefld, fulltime); 190 printf("%-*.*s %-*.*s %-*.*s %s\n", 191 namesz, namesz, namep, 192 linesz, linesz, linep, 193 hostsz, hostsz, 194 gethost(bp, hostp, numeric), 195 ct); 196 if (maxrec && !--maxrec) 197 return; 198 } 199 continue; 200 } 201 /* find associated tty */ 202 for (T = ttylist;; T = T->next) { 203 if (!T) { 204 /* add new one */ 205 T = addtty(linep); 206 break; 207 } 208 if (!strncmp(T->tty, linep, LINESIZE)) 209 break; 210 } 211 if (TYPE(bp) == SIGNATURE) 212 continue; 213 if (namep[0] && want(bp, YES)) { 214 ct = fmttime(bp->ut_timefld, fulltime); 215 printf("%-*.*s %-*.*s %-*.*s %s ", 216 namesz, namesz, namep, 217 linesz, linesz, linep, 218 hostsz, hostsz, 219 gethost(bp, hostp, numeric), 220 ct); 221 if (!T->logout) 222 puts(" still logged in"); 223 else { 224 time_t delta; /* time difference */ 225 226 if (T->logout < 0) { 227 T->logout = -T->logout; 228 printf("- %s", crmsg); 229 } 230 else 231 printf("- %s", 232 fmttime(T->logout, 233 fulltime | TIMEONLY)); 234 delta = T->logout - bp->ut_timefld; 235 if (delta < SECSPERDAY) 236 printf(" (%s)\n", 237 fmttime(delta, 238 fulltime | TIMEONLY | GMT)); 239 else 240 printf(" (%lld+%s)\n", 241 (long long) 242 delta / SECSPERDAY, 243 fmttime(delta, 244 fulltime | TIMEONLY | GMT)); 245 } 246 if (maxrec != -1 && !--maxrec) 247 return; 248 } 249 T->logout = bp->ut_timefld; 250 } 251 } 252 fulltime = 1; /* show full time */ 253 crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME); 254 if ((ct = strrchr(file, '/')) != NULL) 255 ct++; 256 printf("\n%s begins %s\n", ct ? ct : file, crmsg); 257 } 258 259 /* 260 * want -- 261 * see if want this entry 262 */ 263 static int 264 want(struct utmp *bp, int check) 265 { 266 ARG *step; 267 268 if (check) { 269 /* 270 * when uucp and ftp log in over a network, the entry in 271 * the utmp file is the name plus their process id. See 272 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 273 */ 274 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 275 bp->ut_line[3] = '\0'; 276 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 277 bp->ut_line[4] = '\0'; 278 } 279 if (!arglist) 280 return (YES); 281 282 for (step = arglist; step; step = step->next) 283 switch(step->type) { 284 case HOST_TYPE: 285 if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE)) 286 return (YES); 287 break; 288 case TTY_TYPE: 289 if (!strncmp(step->name, bp->ut_line, LINESIZE)) 290 return (YES); 291 break; 292 case USER_TYPE: 293 if (!strncmp(step->name, bp->ut_name, NAMESIZE)) 294 return (YES); 295 break; 296 } 297 return (NO); 298 } 299 300 /* 301 * onintr -- 302 * on interrupt, we inform the user how far we've gotten 303 */ 304 static void 305 onintr(int signo) 306 { 307 /* FIXME: None of this is allowed in a signal handler */ 308 printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld, 309 FULLTIME)); 310 if (signo == SIGINT) { 311 (void)raise_default_signal(signo); 312 exit(EXIT_FAILURE); 313 } 314 (void)fflush(stdout); /* fix required for rsh */ 315 } 316