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