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