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