Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)util.c   8.3 (Berkeley) 4/28/95
33  * $FreeBSD: src/usr.bin/finger/util.c,v 1.8.2.6 2002/07/03 01:14:24 des Exp $
34  * $DragonFly: src/usr.bin/finger/util.c,v 1.3 2003/10/04 20:36:44 hmp Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <ctype.h>
41 #include <db.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <paths.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "utmpentry.h"
52 #include <utmp.h>
53 #include "finger.h"
54 #include "pathnames.h"
55
56 static void      find_idle_and_ttywrite(WHERE *);
57 static void      userinfo(PERSON *, struct passwd *);
58 static WHERE    *walloc(PERSON *);
59
60 int
61 match(struct passwd *pw, char *user)
62 {
63         char *p, *t;
64         char name[1024];
65
66         if (!strcasecmp(pw->pw_name, user))
67                 return(1);
68
69         /*
70          * XXX
71          * Why do we skip asterisks!?!?
72          */
73         (void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
74         tbuf[sizeof(tbuf) - 1] = '\0';
75         if (*p == '*')
76                 ++p;
77
78         /* Ampersands get replaced by the login name. */
79         if ((p = strtok(p, ",")) == NULL)
80                 return(0);
81
82         for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
83                 if (*t == '&') { 
84                         (void)strncpy(t, pw->pw_name, 
85                             sizeof(name) - (t - name));
86                         name[sizeof(name) - 1] = '\0';
87                         while (t < &name[sizeof(name) - 1] && *++t)
88                                 continue;
89                 } else {
90                         ++t;
91                 }
92         }
93         *t = '\0';
94         for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
95                 if (!strcasecmp(p, user))
96                         return(1);
97         return(0);
98 }
99
100 void
101 enter_lastlog(PERSON *pn)
102 {
103         WHERE *w;
104         static int opened, fd;
105         struct lastlog ll;
106         char doit = 0;
107
108         /* some systems may not maintain lastlog, don't report errors. */
109         if (!opened) {
110                 fd = open(_PATH_LASTLOG, O_RDONLY, 0);
111                 opened = 1;
112         }
113         if (fd == -1 ||
114             lseek(fd, (long)pn->uid * sizeof(ll), SEEK_SET) !=
115             (off_t)((long)pn->uid * sizeof(ll)) ||
116             read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
117                         /* as if never logged in */
118                         ll.ll_line[0] = ll.ll_host[0] = '\0';
119                         ll.ll_time = 0;
120                 }
121         if ((w = pn->whead) == NULL)
122                 doit = 1;
123         else if (ll.ll_time != 0) {
124                 /* if last login is earlier than some current login */
125                 for (; !doit && w != NULL; w = w->next)
126                         if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
127                                 doit = 1;
128                 /*
129                  * and if it's not any of the current logins
130                  * can't use time comparison because there may be a small
131                  * discrepancy since login calls time() twice
132                  */
133                 for (w = pn->whead; doit && w != NULL; w = w->next)
134                         if (w->info == LOGGEDIN &&
135                             strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
136                                 doit = 0;
137         }
138         if (doit) {
139                 w = walloc(pn);
140                 w->info = LASTLOG;
141                 asprintf(&w->tty, "%s", ll.ll_line);
142                 asprintf(&w->host, "%s", ll.ll_host);
143                 w->loginat = ll.ll_time;
144         }
145 }
146
147 void
148 enter_where(struct utmpentry *ep, PERSON *pn)
149 {
150         WHERE *w;
151
152         w = walloc(pn);
153         w->info = LOGGEDIN;
154         w->tty = ep->line;
155         w->host = ep->host;
156         w->loginat = (time_t)ep->tv.tv_sec;
157         find_idle_and_ttywrite(w);
158 }
159
160 PERSON *
161 enter_person(struct passwd *pw)
162 {
163         DBT data, key;
164         PERSON *pn;
165
166         if (db == NULL &&
167             (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
168                 err(1, NULL);
169
170         key.data = pw->pw_name;
171         key.size = strlen(pw->pw_name);
172
173         switch ((*db->get)(db, &key, &data, 0)) {
174         case 0:
175                 memmove(&pn, data.data, sizeof pn);
176                 return (pn);
177         default:
178         case -1:
179                 err(1, "db get");
180                 /* NOTREACHED */
181         case 1:
182                 ++entries;
183                 pn = palloc();
184                 userinfo(pn, pw);
185                 pn->whead = NULL;
186
187                 data.size = sizeof(PERSON *);
188                 data.data = &pn;
189                 if ((*db->put)(db, &key, &data, 0))
190                         err(1, "db put");
191                 return (pn);
192         }
193 }
194
195 PERSON *
196 find_person(char *name)
197 {
198         struct passwd *pw;
199
200         int cnt;
201         DBT data, key;
202         PERSON *p;
203         char buf[UT_NAMESIZE + 1];
204
205         if (!db)
206                 return(NULL);
207
208         if ((pw = getpwnam(name)) && hide(pw))
209                 return(NULL);
210
211         /* Name may be only UT_NAMESIZE long and not NUL terminated. */
212         for (cnt = 0; cnt < UT_NAMESIZE && *name; ++name, ++cnt)
213                 buf[cnt] = *name;
214         buf[cnt] = '\0';
215         key.data = buf;
216         key.size = cnt;
217
218         if ((*db->get)(db, &key, &data, 0))
219                 return (NULL);
220         memmove(&p, data.data, sizeof p);
221         return (p);
222 }
223
224 PERSON *
225 palloc(void)
226 {
227         PERSON *p;
228
229         if ((p = malloc(sizeof(PERSON))) == NULL)
230                 err(1, NULL);
231         return(p);
232 }
233
234 static WHERE *
235 walloc(PERSON *pn)
236 {
237         WHERE *w;
238
239         if ((w = malloc(sizeof(WHERE))) == NULL)
240                 err(1, NULL);
241         bzero(w, sizeof(WHERE));
242         if (pn->whead == NULL) {
243                 pn->whead = pn->wtail = w;
244         } else {
245                 pn->wtail->next = w;
246                 pn->wtail = w;
247         }
248         w->next = NULL;
249         return(w);
250 }
251
252 char *
253 prphone(char *num)
254 {
255         char *p;
256         int len;
257         static char pbuf[20];
258
259         /* don't touch anything if the user has their own formatting */
260         for (p = num; *p; ++p)
261                 if (!isdigit(*p))
262                         return(num);
263         len = p - num;
264         p = pbuf;
265         switch(len) {
266         case 11:                        /* +0-123-456-7890 */
267                 *p++ = '+';
268                 *p++ = *num++;
269                 *p++ = '-';
270                 /* FALLTHROUGH */
271         case 10:                        /* 012-345-6789 */
272                 *p++ = *num++;
273                 *p++ = *num++;
274                 *p++ = *num++;
275                 *p++ = '-';
276                 /* FALLTHROUGH */
277         case 7:                         /* 012-3456 */
278                 *p++ = *num++;
279                 *p++ = *num++;
280                 *p++ = *num++;
281                 break;
282         case 5:                         /* x0-1234 */
283         case 4:                         /* x1234 */
284                 *p++ = 'x';
285                 *p++ = *num++;
286                 break;
287         default:
288                 return(num);
289         }
290         if (len != 4) {
291             *p++ = '-';
292             *p++ = *num++;
293         }
294         *p++ = *num++;
295         *p++ = *num++;
296         *p++ = *num++;
297         *p = '\0';
298         return(pbuf);
299 }
300
301 static void
302 find_idle_and_ttywrite(WHERE *w)
303 {
304         struct stat sb;
305         time_t touched;
306
307         (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
308         if (stat(tbuf, &sb) < 0) {
309                 warn("%s", tbuf);
310                 return;
311         }
312         touched = sb.st_atime;
313         if (touched < w->loginat) {
314                 /* tty untouched since before login */
315                 touched = w->loginat;
316         }
317         w->idletime = now < touched ? 0 : now - touched;
318
319 #define TALKABLE        0220            /* tty is writable if 220 mode */
320         w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
321 }
322
323 static void
324 userinfo(PERSON *pn, struct passwd *pw)
325 {
326         char *p, *t;
327         char *bp, name[1024];
328         struct stat sb;
329
330         pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
331
332         pn->uid = pw->pw_uid;
333         if ((pn->name = strdup(pw->pw_name)) == NULL)
334                 err(1, "strdup failed");
335         if ((pn->dir = strdup(pw->pw_dir)) == NULL)
336                 err(1, "strdup failed");
337         if ((pn->shell = strdup(pw->pw_shell)) == NULL)
338                 err(1, "strdup failed");
339
340         /* why do we skip asterisks!?!? */
341         (void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
342         tbuf[sizeof(tbuf) - 1] = '\0';
343         if (*bp == '*')
344                 ++bp;
345
346         /* ampersands get replaced by the login name */
347         if (!(p = strsep(&bp, ",")))
348                 return;
349         for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
350                 if (*t == '&') {
351                         (void)strncpy(t, pw->pw_name, 
352                             sizeof(name) - (t - name));
353                         name[sizeof(name) - 1] = '\0';
354                         if (islower(*t))
355                                 *t = toupper(*t);
356                         while (t < &name[sizeof(name) - 1] && *++t)
357                                 continue;
358                 } else {
359                         ++t;
360                 }
361         }
362         *t = '\0';
363         if ((pn->realname = strdup(name)) == NULL)
364                 err(1, "strdup failed");
365         pn->office = ((p = strsep(&bp, ",")) && *p) ?
366             strdup(p) : NULL;
367         pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
368             strdup(p) : NULL;
369         pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
370             strdup(p) : NULL;
371         (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
372         pn->mailrecv = -1;              /* -1 == not_valid */
373         if (stat(tbuf, &sb) < 0) {
374                 if (errno != ENOENT) {
375                         warn("%s", tbuf);
376                         return;
377                 }
378         } else if (sb.st_size != 0) {
379                 pn->mailrecv = sb.st_mtime;
380                 pn->mailread = sb.st_atime;
381         }
382 }
383
384 /*
385  * Is this user hiding from finger?
386  * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
387  */
388
389 int
390 hide(struct passwd *pw)
391 {
392         struct stat st;
393         char buf[MAXPATHLEN];
394
395         if (!pw->pw_dir)
396                 return 0;
397
398         snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
399
400         if (stat(buf, &st) == 0)
401                 return 1;
402
403         return 0;
404 }