want.c revision 1.4 1 /* $NetBSD: want.c,v 1.4 2004/11/19 21:41:25 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 *, int);
36
37 static const char *
38 gethost(struct utmp* ut, int numeric)
39 {
40 #if FIRSTVALID == 0
41 return numeric ? "" : ut->ut_host;
42 #else
43 if (numeric) {
44 static char buf[512];
45 buf[0] = '\0';
46 (void)sockaddr_snprintf(buf, sizeof(buf), "%a",
47 (struct sockaddr *)&ut->ut_ss);
48 return buf;
49 } else
50 return ut->ut_host;
51 #endif
52 }
53
54 /*
55 * wtmp --
56 * read through the wtmp file
57 */
58 void
59 wtmp(const char *file, int namesz, int linesz, int hostsz, int numeric)
60 {
61 struct utmp *bp; /* current structure */
62 TTY *T; /* tty list entry */
63 struct stat stb; /* stat of file for sz */
64 time_t delta; /* time difference */
65 off_t bl;
66 int bytes, wfd;
67 char *ct, *crmsg;
68 size_t len = sizeof(*buf) * MAXUTMP;
69
70 if ((buf = malloc(len)) == NULL)
71 err(1, "Cannot allocate utmp buffer");
72
73 crmsg = NULL;
74
75 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
76 err(1, "%s", file);
77 bl = (stb.st_size + len - 1) / len;
78
79 buf[FIRSTVALID].ut_timefld = time(NULL);
80 (void)signal(SIGINT, onintr);
81 (void)signal(SIGQUIT, onintr);
82
83 while (--bl >= 0) {
84 if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
85 (bytes = read(wfd, buf, len)) == -1)
86 err(1, "%s", file);
87 for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
88 /*
89 * if the terminal line is '~', the machine stopped.
90 * see utmp(5) for more info.
91 */
92 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
93 /* everybody just logged out */
94 for (T = ttylist; T; T = T->next)
95 T->logout = -bp->ut_timefld;
96 currentout = -bp->ut_timefld;
97 crmsg = strncmp(bp->ut_name, "shutdown",
98 namesz) ? "crash" : "shutdown";
99 if (want(bp, NO)) {
100 ct = fmttime(bp->ut_timefld, fulltime);
101 printf("%-*.*s %-*.*s %-*.*s %s\n",
102 namesz, namesz, bp->ut_name,
103 linesz, linesz, bp->ut_line,
104 hostsz, hostsz,
105 gethost(bp, numeric), ct);
106 if (maxrec != -1 && !--maxrec)
107 return;
108 }
109 continue;
110 }
111 /*
112 * if the line is '{' or '|', date got set; see
113 * utmp(5) for more info.
114 */
115 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
116 && !bp->ut_line[1]) {
117 if (want(bp, NO)) {
118 ct = fmttime(bp->ut_timefld, fulltime);
119 printf("%-*.*s %-*.*s %-*.*s %s\n",
120 namesz, namesz,
121 bp->ut_name,
122 linesz, linesz,
123 bp->ut_line,
124 hostsz, hostsz,
125 gethost(bp, numeric),
126 ct);
127 if (maxrec && !--maxrec)
128 return;
129 }
130 continue;
131 }
132 /* find associated tty */
133 for (T = ttylist;; T = T->next) {
134 if (!T) {
135 /* add new one */
136 T = addtty(bp->ut_line);
137 break;
138 }
139 if (!strncmp(T->tty, bp->ut_line, LINESIZE))
140 break;
141 }
142 if (TYPE(bp) == SIGNATURE)
143 continue;
144 if (bp->ut_name[0] && want(bp, YES)) {
145 ct = fmttime(bp->ut_timefld, fulltime);
146 printf("%-*.*s %-*.*s %-*.*s %s ",
147 namesz, namesz, bp->ut_name,
148 linesz, linesz, bp->ut_line,
149 hostsz, hostsz,
150 gethost(bp, numeric),
151 ct);
152 if (!T->logout)
153 puts(" still logged in");
154 else {
155 if (T->logout < 0) {
156 T->logout = -T->logout;
157 printf("- %s", crmsg);
158 }
159 else
160 printf("- %s",
161 fmttime(T->logout,
162 fulltime | TIMEONLY));
163 delta = T->logout - bp->ut_timefld;
164 if (delta < SECSPERDAY)
165 printf(" (%s)\n",
166 fmttime(delta,
167 fulltime | TIMEONLY | GMT));
168 else
169 printf(" (%ld+%s)\n",
170 delta / SECSPERDAY,
171 fmttime(delta,
172 fulltime | TIMEONLY | GMT));
173 }
174 if (maxrec != -1 && !--maxrec)
175 return;
176 }
177 T->logout = bp->ut_timefld;
178 }
179 }
180 fulltime = 1; /* show full time */
181 crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
182 if ((ct = strrchr(file, '/')) != NULL)
183 ct++;
184 printf("\n%s begins %s\n", ct ? ct : file, crmsg);
185 }
186
187 /*
188 * want --
189 * see if want this entry
190 */
191 static int
192 want(struct utmp *bp, int check)
193 {
194 ARG *step;
195
196 if (check) {
197 /*
198 * when uucp and ftp log in over a network, the entry in
199 * the utmp file is the name plus their process id. See
200 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
201 */
202 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
203 bp->ut_line[3] = '\0';
204 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
205 bp->ut_line[4] = '\0';
206 }
207 if (!arglist)
208 return (YES);
209
210 for (step = arglist; step; step = step->next)
211 switch(step->type) {
212 case HOST_TYPE:
213 if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
214 return (YES);
215 break;
216 case TTY_TYPE:
217 if (!strncmp(step->name, bp->ut_line, LINESIZE))
218 return (YES);
219 break;
220 case USER_TYPE:
221 if (!strncmp(step->name, bp->ut_name, NAMESIZE))
222 return (YES);
223 break;
224 }
225 return (NO);
226 }
227
228 /*
229 * onintr --
230 * on interrupt, we inform the user how far we've gotten
231 */
232 static void
233 onintr(int signo)
234 {
235
236 printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
237 FULLTIME));
238 if (signo == SIGINT)
239 exit(1);
240 (void)fflush(stdout); /* fix required for rsh */
241 }
242