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