util.c revision 1.10 1 1.10 pk /* $NetBSD: util.c,v 1.10 1997/05/17 19:42:27 pk Exp $ */
2 1.9 tls
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.7 lukem * Portions Copyright (c) 1983, 1995, 1996 Eric P. Allman
7 1.1 cgd *
8 1.1 cgd * This code is derived from software contributed to Berkeley by
9 1.1 cgd * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.1 cgd * 3. All advertising materials mentioning features or use of this software
20 1.1 cgd * must display the following acknowledgement:
21 1.1 cgd * This product includes software developed by the University of
22 1.1 cgd * California, Berkeley and its contributors.
23 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.1 cgd * may be used to endorse or promote products derived from this software
25 1.1 cgd * without specific prior written permission.
26 1.1 cgd *
27 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 cgd * SUCH DAMAGE.
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.2 mycroft /*static char sccsid[] = "from: @(#)util.c 5.14 (Berkeley) 1/17/91";*/
42 1.10 pk static char rcsid[] = "$NetBSD: util.c,v 1.10 1997/05/17 19:42:27 pk Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.7 lukem #include <sys/types.h>
46 1.7 lukem #include <sys/uio.h>
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #include <sys/stat.h>
49 1.1 cgd #include <sys/file.h>
50 1.1 cgd #include <stdio.h>
51 1.5 cgd #include <stdlib.h>
52 1.1 cgd #include <ctype.h>
53 1.1 cgd #include <string.h>
54 1.1 cgd #include <paths.h>
55 1.3 brezak #include <errno.h>
56 1.7 lukem #include <unistd.h>
57 1.1 cgd #include "finger.h"
58 1.7 lukem #include "extern.h"
59 1.1 cgd
60 1.7 lukem void
61 1.1 cgd find_idle_and_ttywrite(w)
62 1.7 lukem WHERE *w;
63 1.1 cgd {
64 1.1 cgd struct stat sb;
65 1.1 cgd
66 1.7 lukem (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
67 1.1 cgd if (stat(tbuf, &sb) < 0) {
68 1.1 cgd (void)fprintf(stderr,
69 1.1 cgd "finger: %s: %s\n", tbuf, strerror(errno));
70 1.1 cgd return;
71 1.1 cgd }
72 1.1 cgd w->idletime = now < sb.st_atime ? 0 : now - sb.st_atime;
73 1.1 cgd
74 1.1 cgd #define TALKABLE 0220 /* tty is writable if 220 mode */
75 1.1 cgd w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
76 1.1 cgd }
77 1.1 cgd
78 1.7 lukem void
79 1.1 cgd userinfo(pn, pw)
80 1.7 lukem PERSON *pn;
81 1.7 lukem struct passwd *pw;
82 1.1 cgd {
83 1.7 lukem char *p;
84 1.7 lukem char *bp, name[1024];
85 1.3 brezak struct stat sb;
86 1.1 cgd
87 1.1 cgd pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
88 1.1 cgd
89 1.1 cgd pn->uid = pw->pw_uid;
90 1.1 cgd pn->name = strdup(pw->pw_name);
91 1.1 cgd pn->dir = strdup(pw->pw_dir);
92 1.1 cgd pn->shell = strdup(pw->pw_shell);
93 1.1 cgd
94 1.7 lukem (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
95 1.1 cgd
96 1.1 cgd /* ampersands get replaced by the login name */
97 1.1 cgd if (!(p = strsep(&bp, ",")))
98 1.1 cgd return;
99 1.7 lukem expandusername(p, pw->pw_name, name, sizeof(name));
100 1.1 cgd pn->realname = strdup(name);
101 1.1 cgd pn->office = ((p = strsep(&bp, ",")) && *p) ?
102 1.1 cgd strdup(p) : NULL;
103 1.1 cgd pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
104 1.1 cgd strdup(p) : NULL;
105 1.1 cgd pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
106 1.1 cgd strdup(p) : NULL;
107 1.7 lukem (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILSPOOL,
108 1.7 lukem pw->pw_name);
109 1.3 brezak pn->mailrecv = -1; /* -1 == not_valid */
110 1.3 brezak if (stat(tbuf, &sb) < 0) {
111 1.3 brezak if (errno != ENOENT) {
112 1.3 brezak (void)fprintf(stderr,
113 1.3 brezak "finger: %s: %s\n", tbuf, strerror(errno));
114 1.3 brezak return;
115 1.3 brezak }
116 1.3 brezak } else if (sb.st_size != 0) {
117 1.3 brezak pn->mailrecv = sb.st_mtime;
118 1.3 brezak pn->mailread = sb.st_atime;
119 1.3 brezak }
120 1.1 cgd }
121 1.1 cgd
122 1.7 lukem int
123 1.1 cgd match(pw, user)
124 1.1 cgd struct passwd *pw;
125 1.1 cgd char *user;
126 1.1 cgd {
127 1.7 lukem char *p, *t;
128 1.1 cgd char name[1024];
129 1.1 cgd
130 1.7 lukem (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
131 1.1 cgd
132 1.1 cgd /* ampersands get replaced by the login name */
133 1.1 cgd if (!(p = strtok(p, ",")))
134 1.1 cgd return(0);
135 1.7 lukem expandusername(p, pw->pw_name, name, sizeof(name));
136 1.7 lukem for (t = name; (p = strtok(t, "\t ")) != NULL; t = (char *)NULL)
137 1.1 cgd if (!strcasecmp(p, user))
138 1.1 cgd return(1);
139 1.1 cgd return(0);
140 1.1 cgd }
141 1.1 cgd
142 1.7 lukem /* inspired by usr.sbin/sendmail/util.c::buildfname */
143 1.7 lukem void
144 1.7 lukem expandusername(gecos, login, buf, buflen)
145 1.7 lukem char *gecos;
146 1.7 lukem char *login;
147 1.7 lukem char *buf;
148 1.7 lukem int buflen;
149 1.7 lukem {
150 1.7 lukem char *p, *bp;
151 1.7 lukem
152 1.7 lukem /* why do we skip asterisks!?!? */
153 1.7 lukem if (*gecos == '*')
154 1.7 lukem gecos++;
155 1.7 lukem bp = buf;
156 1.7 lukem
157 1.7 lukem /* copy gecos, interpolating & to be full name */
158 1.7 lukem for (p = gecos; *p != '\0'; p++) {
159 1.7 lukem if (bp >= &buf[buflen - 1]) {
160 1.7 lukem /* buffer overflow - just use login name */
161 1.7 lukem snprintf(buf, buflen, "%s", login);
162 1.7 lukem buf[buflen - 1] = '\0';
163 1.7 lukem return;
164 1.7 lukem }
165 1.7 lukem if (*p == '&') {
166 1.7 lukem /* interpolate full name */
167 1.7 lukem snprintf(bp, buflen - (bp - buf), "%s", login);
168 1.7 lukem *bp = toupper(*bp);
169 1.7 lukem bp += strlen(bp);
170 1.7 lukem }
171 1.7 lukem else
172 1.7 lukem *bp++ = *p;
173 1.7 lukem }
174 1.7 lukem *bp = '\0';
175 1.7 lukem }
176 1.7 lukem
177 1.7 lukem void
178 1.1 cgd enter_lastlog(pn)
179 1.7 lukem PERSON *pn;
180 1.1 cgd {
181 1.7 lukem WHERE *w;
182 1.1 cgd static int opened, fd;
183 1.1 cgd struct lastlog ll;
184 1.1 cgd char doit = 0;
185 1.1 cgd
186 1.1 cgd /* some systems may not maintain lastlog, don't report errors. */
187 1.1 cgd if (!opened) {
188 1.1 cgd fd = open(_PATH_LASTLOG, O_RDONLY, 0);
189 1.1 cgd opened = 1;
190 1.1 cgd }
191 1.1 cgd if (fd == -1 ||
192 1.6 jtc lseek(fd, (off_t)(pn->uid * sizeof(ll)), SEEK_SET) !=
193 1.1 cgd (long)pn->uid * sizeof(ll) ||
194 1.1 cgd read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
195 1.1 cgd /* as if never logged in */
196 1.10 pk ll.ll_line[0] = ll.ll_host[0] = '\0';
197 1.1 cgd ll.ll_time = 0;
198 1.1 cgd }
199 1.1 cgd if ((w = pn->whead) == NULL)
200 1.1 cgd doit = 1;
201 1.1 cgd else if (ll.ll_time != 0) {
202 1.1 cgd /* if last login is earlier than some current login */
203 1.1 cgd for (; !doit && w != NULL; w = w->next)
204 1.1 cgd if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
205 1.1 cgd doit = 1;
206 1.1 cgd /*
207 1.1 cgd * and if it's not any of the current logins
208 1.1 cgd * can't use time comparison because there may be a small
209 1.1 cgd * discrepency since login calls time() twice
210 1.1 cgd */
211 1.1 cgd for (w = pn->whead; doit && w != NULL; w = w->next)
212 1.1 cgd if (w->info == LOGGEDIN &&
213 1.1 cgd strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
214 1.1 cgd doit = 0;
215 1.1 cgd }
216 1.1 cgd if (doit) {
217 1.1 cgd w = walloc(pn);
218 1.1 cgd w->info = LASTLOG;
219 1.1 cgd bcopy(ll.ll_line, w->tty, UT_LINESIZE);
220 1.1 cgd w->tty[UT_LINESIZE] = 0;
221 1.1 cgd bcopy(ll.ll_host, w->host, UT_HOSTSIZE);
222 1.1 cgd w->host[UT_HOSTSIZE] = 0;
223 1.1 cgd w->loginat = ll.ll_time;
224 1.1 cgd }
225 1.1 cgd }
226 1.1 cgd
227 1.7 lukem void
228 1.1 cgd enter_where(ut, pn)
229 1.1 cgd struct utmp *ut;
230 1.1 cgd PERSON *pn;
231 1.1 cgd {
232 1.7 lukem WHERE *w = walloc(pn);
233 1.1 cgd
234 1.1 cgd w->info = LOGGEDIN;
235 1.1 cgd bcopy(ut->ut_line, w->tty, UT_LINESIZE);
236 1.1 cgd w->tty[UT_LINESIZE] = 0;
237 1.1 cgd bcopy(ut->ut_host, w->host, UT_HOSTSIZE);
238 1.1 cgd w->host[UT_HOSTSIZE] = 0;
239 1.1 cgd w->loginat = (time_t)ut->ut_time;
240 1.1 cgd find_idle_and_ttywrite(w);
241 1.1 cgd }
242 1.1 cgd
243 1.1 cgd PERSON *
244 1.1 cgd enter_person(pw)
245 1.7 lukem struct passwd *pw;
246 1.1 cgd {
247 1.7 lukem PERSON *pn, **pp;
248 1.1 cgd
249 1.1 cgd for (pp = htab + hash(pw->pw_name);
250 1.1 cgd *pp != NULL && strcmp((*pp)->name, pw->pw_name) != 0;
251 1.1 cgd pp = &(*pp)->hlink)
252 1.1 cgd ;
253 1.1 cgd if ((pn = *pp) == NULL) {
254 1.1 cgd pn = palloc();
255 1.1 cgd entries++;
256 1.1 cgd if (phead == NULL)
257 1.1 cgd phead = ptail = pn;
258 1.1 cgd else {
259 1.1 cgd ptail->next = pn;
260 1.1 cgd ptail = pn;
261 1.1 cgd }
262 1.1 cgd pn->next = NULL;
263 1.1 cgd pn->hlink = NULL;
264 1.1 cgd *pp = pn;
265 1.1 cgd userinfo(pn, pw);
266 1.1 cgd pn->whead = NULL;
267 1.1 cgd }
268 1.1 cgd return(pn);
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd PERSON *
272 1.1 cgd find_person(name)
273 1.1 cgd char *name;
274 1.1 cgd {
275 1.7 lukem PERSON *pn;
276 1.1 cgd
277 1.1 cgd /* name may be only UT_NAMESIZE long and not terminated */
278 1.1 cgd for (pn = htab[hash(name)];
279 1.1 cgd pn != NULL && strncmp(pn->name, name, UT_NAMESIZE) != 0;
280 1.1 cgd pn = pn->hlink)
281 1.1 cgd ;
282 1.1 cgd return(pn);
283 1.1 cgd }
284 1.1 cgd
285 1.7 lukem int
286 1.1 cgd hash(name)
287 1.7 lukem char *name;
288 1.1 cgd {
289 1.7 lukem int h, i;
290 1.1 cgd
291 1.1 cgd h = 0;
292 1.1 cgd /* name may be only UT_NAMESIZE long and not terminated */
293 1.1 cgd for (i = UT_NAMESIZE; --i >= 0 && *name;)
294 1.7 lukem h = ((h << 2 | h >> (HBITS - 2)) ^ *name++) & HMASK;
295 1.1 cgd return(h);
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd PERSON *
299 1.1 cgd palloc()
300 1.1 cgd {
301 1.1 cgd PERSON *p;
302 1.1 cgd
303 1.1 cgd if ((p = (PERSON *)malloc((u_int) sizeof(PERSON))) == NULL) {
304 1.1 cgd (void)fprintf(stderr, "finger: out of space.\n");
305 1.1 cgd exit(1);
306 1.1 cgd }
307 1.1 cgd return(p);
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd WHERE *
311 1.1 cgd walloc(pn)
312 1.7 lukem PERSON *pn;
313 1.1 cgd {
314 1.7 lukem WHERE *w;
315 1.1 cgd
316 1.1 cgd if ((w = (WHERE *)malloc((u_int) sizeof(WHERE))) == NULL) {
317 1.1 cgd (void)fprintf(stderr, "finger: out of space.\n");
318 1.1 cgd exit(1);
319 1.1 cgd }
320 1.1 cgd if (pn->whead == NULL)
321 1.1 cgd pn->whead = pn->wtail = w;
322 1.1 cgd else {
323 1.1 cgd pn->wtail->next = w;
324 1.1 cgd pn->wtail = w;
325 1.1 cgd }
326 1.1 cgd w->next = NULL;
327 1.1 cgd return(w);
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd char *
331 1.1 cgd prphone(num)
332 1.1 cgd char *num;
333 1.1 cgd {
334 1.7 lukem char *p;
335 1.1 cgd int len;
336 1.1 cgd static char pbuf[15];
337 1.1 cgd
338 1.1 cgd /* don't touch anything if the user has their own formatting */
339 1.1 cgd for (p = num; *p; ++p)
340 1.1 cgd if (!isdigit(*p))
341 1.1 cgd return(num);
342 1.1 cgd len = p - num;
343 1.1 cgd p = pbuf;
344 1.1 cgd switch(len) {
345 1.1 cgd case 11: /* +0-123-456-7890 */
346 1.1 cgd *p++ = '+';
347 1.1 cgd *p++ = *num++;
348 1.1 cgd *p++ = '-';
349 1.1 cgd /* FALLTHROUGH */
350 1.1 cgd case 10: /* 012-345-6789 */
351 1.1 cgd *p++ = *num++;
352 1.1 cgd *p++ = *num++;
353 1.1 cgd *p++ = *num++;
354 1.1 cgd *p++ = '-';
355 1.1 cgd /* FALLTHROUGH */
356 1.1 cgd case 7: /* 012-3456 */
357 1.1 cgd *p++ = *num++;
358 1.1 cgd *p++ = *num++;
359 1.1 cgd *p++ = *num++;
360 1.1 cgd break;
361 1.1 cgd case 5: /* x0-1234 */
362 1.3 brezak case 4: /* x1234 */
363 1.1 cgd *p++ = 'x';
364 1.1 cgd *p++ = *num++;
365 1.1 cgd break;
366 1.1 cgd default:
367 1.1 cgd return(num);
368 1.1 cgd }
369 1.3 brezak if (len != 4) {
370 1.3 brezak *p++ = '-';
371 1.3 brezak *p++ = *num++;
372 1.3 brezak }
373 1.1 cgd *p++ = *num++;
374 1.1 cgd *p++ = *num++;
375 1.1 cgd *p++ = *num++;
376 1.1 cgd *p = '\0';
377 1.1 cgd return(pbuf);
378 1.1 cgd }
379