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