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