kernel - Remove msf buffer code
[dragonfly.git] / usr.bin / finger / util.c
blob5279ec13f3704755606b5abeb0183722aa04ecdb
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#)util.c 8.3 (Berkeley) 4/28/95
37 * $FreeBSD: src/usr.bin/finger/util.c,v 1.8.2.6 2002/07/03 01:14:24 des Exp $
38 * $DragonFly: src/usr.bin/finger/util.c,v 1.3 2003/10/04 20:36:44 hmp Exp $
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/stat.h>
44 #include <ctype.h>
45 #include <db.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <paths.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <utmp.h>
56 #include "finger.h"
57 #include "pathnames.h"
59 static void find_idle_and_ttywrite(WHERE *);
60 static void userinfo(PERSON *, struct passwd *);
61 static WHERE *walloc(PERSON *);
63 int
64 match(struct passwd *pw, char *user)
66 char *p, *t;
67 char name[1024];
69 if (!strcasecmp(pw->pw_name, user))
70 return(1);
73 * XXX
74 * Why do we skip asterisks!?!?
76 (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
77 tbuf[sizeof(tbuf) - 1] = '\0';
78 if (*p == '*')
79 ++p;
81 /* Ampersands get replaced by the login name. */
82 if ((p = strtok(p, ",")) == NULL)
83 return(0);
85 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
86 if (*t == '&') {
87 (void)strncpy(t, pw->pw_name,
88 sizeof(name) - (t - name));
89 name[sizeof(name) - 1] = '\0';
90 while (t < &name[sizeof(name) - 1] && *++t)
91 continue;
92 } else {
93 ++t;
96 *t = '\0';
97 for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
98 if (!strcasecmp(p, user))
99 return(1);
100 return(0);
103 void
104 enter_lastlog(PERSON *pn)
106 WHERE *w;
107 static int opened, fd;
108 struct lastlog ll;
109 char doit = 0;
111 /* some systems may not maintain lastlog, don't report errors. */
112 if (!opened) {
113 fd = open(_PATH_LASTLOG, O_RDONLY, 0);
114 opened = 1;
116 if (fd == -1 ||
117 lseek(fd, (long)pn->uid * sizeof(ll), SEEK_SET) !=
118 (off_t)((long)pn->uid * sizeof(ll)) ||
119 read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
120 /* as if never logged in */
121 ll.ll_line[0] = ll.ll_host[0] = '\0';
122 ll.ll_time = 0;
124 if ((w = pn->whead) == NULL)
125 doit = 1;
126 else if (ll.ll_time != 0) {
127 /* if last login is earlier than some current login */
128 for (; !doit && w != NULL; w = w->next)
129 if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
130 doit = 1;
132 * and if it's not any of the current logins
133 * can't use time comparison because there may be a small
134 * discrepancy since login calls time() twice
136 for (w = pn->whead; doit && w != NULL; w = w->next)
137 if (w->info == LOGGEDIN &&
138 strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
139 doit = 0;
141 if (doit) {
142 w = walloc(pn);
143 w->info = LASTLOG;
144 bcopy(ll.ll_line, w->tty, UT_LINESIZE);
145 w->tty[UT_LINESIZE] = 0;
146 bcopy(ll.ll_host, w->host, UT_HOSTSIZE);
147 w->host[UT_HOSTSIZE] = 0;
148 w->loginat = ll.ll_time;
152 void
153 enter_where(struct utmp *ut, PERSON *pn)
155 WHERE *w;
157 w = walloc(pn);
158 w->info = LOGGEDIN;
159 bcopy(ut->ut_line, w->tty, UT_LINESIZE);
160 w->tty[UT_LINESIZE] = 0;
161 bcopy(ut->ut_host, w->host, UT_HOSTSIZE);
162 w->host[UT_HOSTSIZE] = 0;
163 w->loginat = (time_t)ut->ut_time;
164 find_idle_and_ttywrite(w);
167 PERSON *
168 enter_person(struct passwd *pw)
170 DBT data, key;
171 PERSON *pn;
173 if (db == NULL &&
174 (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
175 err(1, NULL);
177 key.data = pw->pw_name;
178 key.size = strlen(pw->pw_name);
180 switch ((*db->get)(db, &key, &data, 0)) {
181 case 0:
182 memmove(&pn, data.data, sizeof pn);
183 return (pn);
184 default:
185 case -1:
186 err(1, "db get");
187 /* NOTREACHED */
188 case 1:
189 ++entries;
190 pn = palloc();
191 userinfo(pn, pw);
192 pn->whead = NULL;
194 data.size = sizeof(PERSON *);
195 data.data = &pn;
196 if ((*db->put)(db, &key, &data, 0))
197 err(1, "db put");
198 return (pn);
202 PERSON *
203 find_person(char *name)
205 struct passwd *pw;
207 int cnt;
208 DBT data, key;
209 PERSON *p;
210 char buf[UT_NAMESIZE + 1];
212 if (!db)
213 return(NULL);
215 if ((pw = getpwnam(name)) && hide(pw))
216 return(NULL);
218 /* Name may be only UT_NAMESIZE long and not NUL terminated. */
219 for (cnt = 0; cnt < UT_NAMESIZE && *name; ++name, ++cnt)
220 buf[cnt] = *name;
221 buf[cnt] = '\0';
222 key.data = buf;
223 key.size = cnt;
225 if ((*db->get)(db, &key, &data, 0))
226 return (NULL);
227 memmove(&p, data.data, sizeof p);
228 return (p);
231 PERSON *
232 palloc(void)
234 PERSON *p;
236 if ((p = malloc(sizeof(PERSON))) == NULL)
237 err(1, NULL);
238 return(p);
241 static WHERE *
242 walloc(PERSON *pn)
244 WHERE *w;
246 if ((w = malloc(sizeof(WHERE))) == NULL)
247 err(1, NULL);
248 if (pn->whead == NULL)
249 pn->whead = pn->wtail = w;
250 else {
251 pn->wtail->next = w;
252 pn->wtail = w;
254 w->next = NULL;
255 return(w);
258 char *
259 prphone(char *num)
261 char *p;
262 int len;
263 static char pbuf[20];
265 /* don't touch anything if the user has their own formatting */
266 for (p = num; *p; ++p)
267 if (!isdigit(*p))
268 return(num);
269 len = p - num;
270 p = pbuf;
271 switch(len) {
272 case 11: /* +0-123-456-7890 */
273 *p++ = '+';
274 *p++ = *num++;
275 *p++ = '-';
276 /* FALLTHROUGH */
277 case 10: /* 012-345-6789 */
278 *p++ = *num++;
279 *p++ = *num++;
280 *p++ = *num++;
281 *p++ = '-';
282 /* FALLTHROUGH */
283 case 7: /* 012-3456 */
284 *p++ = *num++;
285 *p++ = *num++;
286 *p++ = *num++;
287 break;
288 case 5: /* x0-1234 */
289 case 4: /* x1234 */
290 *p++ = 'x';
291 *p++ = *num++;
292 break;
293 default:
294 return(num);
296 if (len != 4) {
297 *p++ = '-';
298 *p++ = *num++;
300 *p++ = *num++;
301 *p++ = *num++;
302 *p++ = *num++;
303 *p = '\0';
304 return(pbuf);
307 static void
308 find_idle_and_ttywrite(WHERE *w)
310 struct stat sb;
311 time_t touched;
313 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
314 if (stat(tbuf, &sb) < 0) {
315 warn("%s", tbuf);
316 return;
318 touched = sb.st_atime;
319 if (touched < w->loginat) {
320 /* tty untouched since before login */
321 touched = w->loginat;
323 w->idletime = now < touched ? 0 : now - touched;
325 #define TALKABLE 0220 /* tty is writable if 220 mode */
326 w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
329 static void
330 userinfo(PERSON *pn, struct passwd *pw)
332 char *p, *t;
333 char *bp, name[1024];
334 struct stat sb;
336 pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
338 pn->uid = pw->pw_uid;
339 if ((pn->name = strdup(pw->pw_name)) == NULL)
340 err(1, "strdup failed");
341 if ((pn->dir = strdup(pw->pw_dir)) == NULL)
342 err(1, "strdup failed");
343 if ((pn->shell = strdup(pw->pw_shell)) == NULL)
344 err(1, "strdup failed");
346 /* why do we skip asterisks!?!? */
347 (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
348 tbuf[sizeof(tbuf) - 1] = '\0';
349 if (*bp == '*')
350 ++bp;
352 /* ampersands get replaced by the login name */
353 if (!(p = strsep(&bp, ",")))
354 return;
355 for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
356 if (*t == '&') {
357 (void)strncpy(t, pw->pw_name,
358 sizeof(name) - (t - name));
359 name[sizeof(name) - 1] = '\0';
360 if (islower(*t))
361 *t = toupper(*t);
362 while (t < &name[sizeof(name) - 1] && *++t)
363 continue;
364 } else {
365 ++t;
368 *t = '\0';
369 if ((pn->realname = strdup(name)) == NULL)
370 err(1, "strdup failed");
371 pn->office = ((p = strsep(&bp, ",")) && *p) ?
372 strdup(p) : NULL;
373 pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
374 strdup(p) : NULL;
375 pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
376 strdup(p) : NULL;
377 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
378 pn->mailrecv = -1; /* -1 == not_valid */
379 if (stat(tbuf, &sb) < 0) {
380 if (errno != ENOENT) {
381 warn("%s", tbuf);
382 return;
384 } else if (sb.st_size != 0) {
385 pn->mailrecv = sb.st_mtime;
386 pn->mailread = sb.st_atime;
391 * Is this user hiding from finger?
392 * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
396 hide(struct passwd *pw)
398 struct stat st;
399 char buf[MAXPATHLEN];
401 if (!pw->pw_dir)
402 return 0;
404 snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
406 if (stat(buf, &st) == 0)
407 return 1;
409 return 0;