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