caf4768be1bc730cb309b33322d4f78b14a2f886
[dragonfly.git] / usr.bin / finger / util.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7  *
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.
23  *
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.
35  *
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.2 2003/06/17 04:29:26 dillon Exp $
39  */
40
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <ctype.h>
44 #include <db.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <paths.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <utmp.h>
55 #include "finger.h"
56 #include "pathnames.h"
57
58 static void      find_idle_and_ttywrite(WHERE *);
59 static void      userinfo(PERSON *, struct passwd *);
60 static WHERE    *walloc(PERSON *);
61
62 int
63 match(pw, user)
64         struct passwd *pw;
65         char *user;
66 {
67         char *p, *t;
68         char name[1024];
69
70         if (!strcasecmp(pw->pw_name, user))
71                 return(1);
72
73         /*
74          * XXX
75          * Why do we skip asterisks!?!?
76          */
77         (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
78         tbuf[sizeof(tbuf) - 1] = '\0';
79         if (*p == '*')
80                 ++p;
81
82         /* Ampersands get replaced by the login name. */
83         if ((p = strtok(p, ",")) == NULL)
84                 return(0);
85
86         for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
87                 if (*t == '&') { 
88                         (void)strncpy(t, pw->pw_name, 
89                             sizeof(name) - (t - name));
90                         name[sizeof(name) - 1] = '\0';
91                         while (t < &name[sizeof(name) - 1] && *++t)
92                                 continue;
93                 } else {
94                         ++t;
95                 }
96         }
97         *t = '\0';
98         for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
99                 if (!strcasecmp(p, user))
100                         return(1);
101         return(0);
102 }
103
104 void
105 enter_lastlog(pn)
106         PERSON *pn;
107 {
108         WHERE *w;
109         static int opened, fd;
110         struct lastlog ll;
111         char doit = 0;
112
113         /* some systems may not maintain lastlog, don't report errors. */
114         if (!opened) {
115                 fd = open(_PATH_LASTLOG, O_RDONLY, 0);
116                 opened = 1;
117         }
118         if (fd == -1 ||
119             lseek(fd, (long)pn->uid * sizeof(ll), SEEK_SET) !=
120             (long)pn->uid * sizeof(ll) ||
121             read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
122                         /* as if never logged in */
123                         ll.ll_line[0] = ll.ll_host[0] = '\0';
124                         ll.ll_time = 0;
125                 }
126         if ((w = pn->whead) == NULL)
127                 doit = 1;
128         else if (ll.ll_time != 0) {
129                 /* if last login is earlier than some current login */
130                 for (; !doit && w != NULL; w = w->next)
131                         if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
132                                 doit = 1;
133                 /*
134                  * and if it's not any of the current logins
135                  * can't use time comparison because there may be a small
136                  * discrepancy since login calls time() twice
137                  */
138                 for (w = pn->whead; doit && w != NULL; w = w->next)
139                         if (w->info == LOGGEDIN &&
140                             strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
141                                 doit = 0;
142         }
143         if (doit) {
144                 w = walloc(pn);
145                 w->info = LASTLOG;
146                 bcopy(ll.ll_line, w->tty, UT_LINESIZE);
147                 w->tty[UT_LINESIZE] = 0;
148                 bcopy(ll.ll_host, w->host, UT_HOSTSIZE);
149                 w->host[UT_HOSTSIZE] = 0;
150                 w->loginat = ll.ll_time;
151         }
152 }
153
154 void
155 enter_where(ut, pn)
156         struct utmp *ut;
157         PERSON *pn;
158 {
159         WHERE *w;
160
161         w = walloc(pn);
162         w->info = LOGGEDIN;
163         bcopy(ut->ut_line, w->tty, UT_LINESIZE);
164         w->tty[UT_LINESIZE] = 0;
165         bcopy(ut->ut_host, w->host, UT_HOSTSIZE);
166         w->host[UT_HOSTSIZE] = 0;
167         w->loginat = (time_t)ut->ut_time;
168         find_idle_and_ttywrite(w);
169 }
170
171 PERSON *
172 enter_person(pw)
173         struct passwd *pw;
174 {
175         DBT data, key;
176         PERSON *pn;
177
178         if (db == NULL &&
179             (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
180                 err(1, NULL);
181
182         key.data = pw->pw_name;
183         key.size = strlen(pw->pw_name);
184
185         switch ((*db->get)(db, &key, &data, 0)) {
186         case 0:
187                 memmove(&pn, data.data, sizeof pn);
188                 return (pn);
189         default:
190         case -1:
191                 err(1, "db get");
192                 /* NOTREACHED */
193         case 1:
194                 ++entries;
195                 pn = palloc();
196                 userinfo(pn, pw);
197                 pn->whead = NULL;
198
199                 data.size = sizeof(PERSON *);
200                 data.data = &pn;
201                 if ((*db->put)(db, &key, &data, 0))
202                         err(1, "db put");
203                 return (pn);
204         }
205 }
206
207 PERSON *
208 find_person(name)
209         char *name;
210 {
211         struct passwd *pw;
212
213         int cnt;
214         DBT data, key;
215         PERSON *p;
216         char buf[UT_NAMESIZE + 1];
217
218         if (!db)
219                 return(NULL);
220
221         if ((pw = getpwnam(name)) && hide(pw))
222                 return(NULL);
223
224         /* Name may be only UT_NAMESIZE long and not NUL terminated. */
225         for (cnt = 0; cnt < UT_NAMESIZE && *name; ++name, ++cnt)
226                 buf[cnt] = *name;
227         buf[cnt] = '\0';
228         key.data = buf;
229         key.size = cnt;
230
231         if ((*db->get)(db, &key, &data, 0))
232                 return (NULL);
233         memmove(&p, data.data, sizeof p);
234         return (p);
235 }
236
237 PERSON *
238 palloc()
239 {
240         PERSON *p;
241
242         if ((p = malloc(sizeof(PERSON))) == NULL)
243                 err(1, NULL);
244         return(p);
245 }
246
247 static WHERE *
248 walloc(pn)
249         PERSON *pn;
250 {
251         WHERE *w;
252
253         if ((w = malloc(sizeof(WHERE))) == NULL)
254                 err(1, NULL);
255         if (pn->whead == NULL)
256                 pn->whead = pn->wtail = w;
257         else {
258                 pn->wtail->next = w;
259                 pn->wtail = w;
260         }
261         w->next = NULL;
262         return(w);
263 }
264
265 char *
266 prphone(num)
267         char *num;
268 {
269         char *p;
270         int len;
271         static char pbuf[20];
272
273         /* don't touch anything if the user has their own formatting */
274         for (p = num; *p; ++p)
275                 if (!isdigit(*p))
276                         return(num);
277         len = p - num;
278         p = pbuf;
279         switch(len) {
280         case 11:                        /* +0-123-456-7890 */
281                 *p++ = '+';
282                 *p++ = *num++;
283                 *p++ = '-';
284                 /* FALLTHROUGH */
285         case 10:                        /* 012-345-6789 */
286                 *p++ = *num++;
287                 *p++ = *num++;
288                 *p++ = *num++;
289                 *p++ = '-';
290                 /* FALLTHROUGH */
291         case 7:                         /* 012-3456 */
292                 *p++ = *num++;
293                 *p++ = *num++;
294                 *p++ = *num++;
295                 break;
296         case 5:                         /* x0-1234 */
297         case 4:                         /* x1234 */
298                 *p++ = 'x';
299                 *p++ = *num++;
300                 break;
301         default:
302                 return(num);
303         }
304         if (len != 4) {
305             *p++ = '-';
306             *p++ = *num++;
307         }
308         *p++ = *num++;
309         *p++ = *num++;
310         *p++ = *num++;
311         *p = '\0';
312         return(pbuf);
313 }
314
315 static void
316 find_idle_and_ttywrite(w)
317         WHERE *w;
318 {
319         extern time_t now;
320         struct stat sb;
321         time_t touched;
322
323         (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
324         if (stat(tbuf, &sb) < 0) {
325                 warn("%s", tbuf);
326                 return;
327         }
328         touched = sb.st_atime;
329         if (touched < w->loginat) {
330                 /* tty untouched since before login */
331                 touched = w->loginat;
332         }
333         w->idletime = now < touched ? 0 : now - touched;
334
335 #define TALKABLE        0220            /* tty is writable if 220 mode */
336         w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
337 }
338
339 static void
340 userinfo(pn, pw)
341         PERSON *pn;
342         struct passwd *pw;
343 {
344         char *p, *t;
345         char *bp, name[1024];
346         struct stat sb;
347
348         pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
349
350         pn->uid = pw->pw_uid;
351         if ((pn->name = strdup(pw->pw_name)) == NULL)
352                 err(1, "strdup failed");
353         if ((pn->dir = strdup(pw->pw_dir)) == NULL)
354                 err(1, "strdup failed");
355         if ((pn->shell = strdup(pw->pw_shell)) == NULL)
356                 err(1, "strdup failed");
357
358         /* why do we skip asterisks!?!? */
359         (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
360         tbuf[sizeof(tbuf) - 1] = '\0';
361         if (*bp == '*')
362                 ++bp;
363
364         /* ampersands get replaced by the login name */
365         if (!(p = strsep(&bp, ",")))
366                 return;
367         for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
368                 if (*t == '&') {
369                         (void)strncpy(t, pw->pw_name, 
370                             sizeof(name) - (t - name));
371                         name[sizeof(name) - 1] = '\0';
372                         if (islower(*t))
373                                 *t = toupper(*t);
374                         while (t < &name[sizeof(name) - 1] && *++t)
375                                 continue;
376                 } else {
377                         ++t;
378                 }
379         }
380         *t = '\0';
381         if ((pn->realname = strdup(name)) == NULL)
382                 err(1, "strdup failed");
383         pn->office = ((p = strsep(&bp, ",")) && *p) ?
384             strdup(p) : NULL;
385         pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
386             strdup(p) : NULL;
387         pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
388             strdup(p) : NULL;
389         (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
390         pn->mailrecv = -1;              /* -1 == not_valid */
391         if (stat(tbuf, &sb) < 0) {
392                 if (errno != ENOENT) {
393                         warn("%s", tbuf);
394                         return;
395                 }
396         } else if (sb.st_size != 0) {
397                 pn->mailrecv = sb.st_mtime;
398                 pn->mailread = sb.st_atime;
399         }
400 }
401
402 /*
403  * Is this user hiding from finger?
404  * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
405  */
406
407 int
408 hide(pw)
409         struct passwd *pw;
410 {
411         struct stat st;
412         char buf[MAXPATHLEN];
413
414         if (!pw->pw_dir)
415                 return 0;
416
417         snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
418
419         if (stat(buf, &st) == 0)
420                 return 1;
421
422         return 0;
423 }