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