xref: /freebsd/usr.bin/finger/util.c (revision 4c86f3ad84e2482a236c19d96c0422a5717d532a)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
38b14d8277SPhilippe Charnier #if 0
39df3f5d9dSPeter Wemm static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/28/95";
404c86f3adSPhilippe Charnier #endif
41b14d8277SPhilippe Charnier static const char rcsid[] =
42c3aac50fSPeter Wemm   "$FreeBSD$";
439b50d902SRodney W. Grimes #endif /* not lint */
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/param.h>
469b50d902SRodney W. Grimes #include <sys/stat.h>
474c86f3adSPhilippe Charnier #include <ctype.h>
489b50d902SRodney W. Grimes #include <db.h>
49df3f5d9dSPeter Wemm #include <err.h>
509b50d902SRodney W. Grimes #include <errno.h>
514c86f3adSPhilippe Charnier #include <fcntl.h>
524c86f3adSPhilippe Charnier #include <paths.h>
534c86f3adSPhilippe Charnier #include <pwd.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <stdlib.h>
569b50d902SRodney W. Grimes #include <string.h>
574c86f3adSPhilippe Charnier #include <unistd.h>
584c86f3adSPhilippe Charnier #include <utmp.h>
599b50d902SRodney W. Grimes #include "finger.h"
609b50d902SRodney W. Grimes 
619b50d902SRodney W. Grimes static void	 find_idle_and_ttywrite __P((WHERE *));
629b50d902SRodney W. Grimes static void	 userinfo __P((PERSON *, struct passwd *));
639b50d902SRodney W. Grimes static WHERE	*walloc __P((PERSON *));
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes int
669b50d902SRodney W. Grimes match(pw, user)
679b50d902SRodney W. Grimes 	struct passwd *pw;
689b50d902SRodney W. Grimes 	char *user;
699b50d902SRodney W. Grimes {
709b50d902SRodney W. Grimes 	register char *p, *t;
719b50d902SRodney W. Grimes 	char name[1024];
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes 	if (!strcasecmp(pw->pw_name, user))
749b50d902SRodney W. Grimes 		return(1);
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes 	/*
779b50d902SRodney W. Grimes 	 * XXX
789b50d902SRodney W. Grimes 	 * Why do we skip asterisks!?!?
799b50d902SRodney W. Grimes 	 */
80e0d8eea1SWarner Losh 	(void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
81b59bb23bSWarner Losh 	tbuf[sizeof(tbuf) - 1] = '\0';
829b50d902SRodney W. Grimes 	if (*p == '*')
839b50d902SRodney W. Grimes 		++p;
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes 	/* Ampersands get replaced by the login name. */
869b50d902SRodney W. Grimes 	if ((p = strtok(p, ",")) == NULL)
879b50d902SRodney W. Grimes 		return(0);
889b50d902SRodney W. Grimes 
89b59bb23bSWarner Losh 	for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
909b50d902SRodney W. Grimes 		if (*t == '&') {
91e0d8eea1SWarner Losh 			(void)strncpy(t, pw->pw_name,
92e0d8eea1SWarner Losh 			    sizeof(name) - (t - name));
93e0d8eea1SWarner Losh 			name[sizeof(name) - 1] = '\0';
94b59bb23bSWarner Losh 			while (t < &name[sizeof(name) - 1] && *++t)
95b59bb23bSWarner Losh 				continue;
96e0d8eea1SWarner Losh 		} else {
979b50d902SRodney W. Grimes 			++t;
98e0d8eea1SWarner Losh 		}
99e0d8eea1SWarner Losh 	}
100e0d8eea1SWarner Losh 	*t = '\0';
101df3f5d9dSPeter Wemm 	for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
1029b50d902SRodney W. Grimes 		if (!strcasecmp(p, user))
1039b50d902SRodney W. Grimes 			return(1);
1049b50d902SRodney W. Grimes 	return(0);
1059b50d902SRodney W. Grimes }
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes void
1089b50d902SRodney W. Grimes enter_lastlog(pn)
1099b50d902SRodney W. Grimes 	register PERSON *pn;
1109b50d902SRodney W. Grimes {
1119b50d902SRodney W. Grimes 	register WHERE *w;
1129b50d902SRodney W. Grimes 	static int opened, fd;
1139b50d902SRodney W. Grimes 	struct lastlog ll;
1149b50d902SRodney W. Grimes 	char doit = 0;
1159b50d902SRodney W. Grimes 
1169b50d902SRodney W. Grimes 	/* some systems may not maintain lastlog, don't report errors. */
1179b50d902SRodney W. Grimes 	if (!opened) {
1189b50d902SRodney W. Grimes 		fd = open(_PATH_LASTLOG, O_RDONLY, 0);
1199b50d902SRodney W. Grimes 		opened = 1;
1209b50d902SRodney W. Grimes 	}
1219b50d902SRodney W. Grimes 	if (fd == -1 ||
1229b50d902SRodney W. Grimes 	    lseek(fd, (long)pn->uid * sizeof(ll), SEEK_SET) !=
1239b50d902SRodney W. Grimes 	    (long)pn->uid * sizeof(ll) ||
1249b50d902SRodney W. Grimes 	    read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
1259b50d902SRodney W. Grimes 			/* as if never logged in */
126b14d8277SPhilippe Charnier 			ll.ll_line[0] = ll.ll_host[0] = '\0';
1279b50d902SRodney W. Grimes 			ll.ll_time = 0;
1289b50d902SRodney W. Grimes 		}
1299b50d902SRodney W. Grimes 	if ((w = pn->whead) == NULL)
1309b50d902SRodney W. Grimes 		doit = 1;
1319b50d902SRodney W. Grimes 	else if (ll.ll_time != 0) {
1329b50d902SRodney W. Grimes 		/* if last login is earlier than some current login */
1339b50d902SRodney W. Grimes 		for (; !doit && w != NULL; w = w->next)
1349b50d902SRodney W. Grimes 			if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
1359b50d902SRodney W. Grimes 				doit = 1;
1369b50d902SRodney W. Grimes 		/*
1379b50d902SRodney W. Grimes 		 * and if it's not any of the current logins
1389b50d902SRodney W. Grimes 		 * can't use time comparison because there may be a small
1394c86f3adSPhilippe Charnier 		 * discrepancy since login calls time() twice
1409b50d902SRodney W. Grimes 		 */
1419b50d902SRodney W. Grimes 		for (w = pn->whead; doit && w != NULL; w = w->next)
1429b50d902SRodney W. Grimes 			if (w->info == LOGGEDIN &&
1439b50d902SRodney W. Grimes 			    strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
1449b50d902SRodney W. Grimes 				doit = 0;
1459b50d902SRodney W. Grimes 	}
1469b50d902SRodney W. Grimes 	if (doit) {
1479b50d902SRodney W. Grimes 		w = walloc(pn);
1489b50d902SRodney W. Grimes 		w->info = LASTLOG;
1499b50d902SRodney W. Grimes 		bcopy(ll.ll_line, w->tty, UT_LINESIZE);
1509b50d902SRodney W. Grimes 		w->tty[UT_LINESIZE] = 0;
1519b50d902SRodney W. Grimes 		bcopy(ll.ll_host, w->host, UT_HOSTSIZE);
1529b50d902SRodney W. Grimes 		w->host[UT_HOSTSIZE] = 0;
1539b50d902SRodney W. Grimes 		w->loginat = ll.ll_time;
1549b50d902SRodney W. Grimes 	}
1559b50d902SRodney W. Grimes }
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes void
1589b50d902SRodney W. Grimes enter_where(ut, pn)
1599b50d902SRodney W. Grimes 	struct utmp *ut;
1609b50d902SRodney W. Grimes 	PERSON *pn;
1619b50d902SRodney W. Grimes {
1629b50d902SRodney W. Grimes 	register WHERE *w;
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes 	w = walloc(pn);
1659b50d902SRodney W. Grimes 	w->info = LOGGEDIN;
1669b50d902SRodney W. Grimes 	bcopy(ut->ut_line, w->tty, UT_LINESIZE);
1679b50d902SRodney W. Grimes 	w->tty[UT_LINESIZE] = 0;
1689b50d902SRodney W. Grimes 	bcopy(ut->ut_host, w->host, UT_HOSTSIZE);
1699b50d902SRodney W. Grimes 	w->host[UT_HOSTSIZE] = 0;
1709b50d902SRodney W. Grimes 	w->loginat = (time_t)ut->ut_time;
1719b50d902SRodney W. Grimes 	find_idle_and_ttywrite(w);
1729b50d902SRodney W. Grimes }
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes PERSON *
1759b50d902SRodney W. Grimes enter_person(pw)
1769b50d902SRodney W. Grimes 	register struct passwd *pw;
1779b50d902SRodney W. Grimes {
1789b50d902SRodney W. Grimes 	DBT data, key;
1799b50d902SRodney W. Grimes 	PERSON *pn;
1809b50d902SRodney W. Grimes 
1819b50d902SRodney W. Grimes 	if (db == NULL &&
1829b50d902SRodney W. Grimes 	    (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
183df3f5d9dSPeter Wemm 		err(1, NULL);
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes 	key.data = pw->pw_name;
1869b50d902SRodney W. Grimes 	key.size = strlen(pw->pw_name);
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes 	switch ((*db->get)(db, &key, &data, 0)) {
1899b50d902SRodney W. Grimes 	case 0:
190df3f5d9dSPeter Wemm 		memmove(&pn, data.data, sizeof pn);
191df3f5d9dSPeter Wemm 		return (pn);
1929b50d902SRodney W. Grimes 	default:
1939b50d902SRodney W. Grimes 	case -1:
194df3f5d9dSPeter Wemm 		err(1, "db get");
1959b50d902SRodney W. Grimes 		/* NOTREACHED */
1969b50d902SRodney W. Grimes 	case 1:
1979b50d902SRodney W. Grimes 		++entries;
1989b50d902SRodney W. Grimes 		pn = palloc();
1999b50d902SRodney W. Grimes 		userinfo(pn, pw);
2009b50d902SRodney W. Grimes 		pn->whead = NULL;
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes 		data.size = sizeof(PERSON *);
2039b50d902SRodney W. Grimes 		data.data = &pn;
2049b50d902SRodney W. Grimes 		if ((*db->put)(db, &key, &data, 0))
205df3f5d9dSPeter Wemm 			err(1, "db put");
2069b50d902SRodney W. Grimes 		return (pn);
2079b50d902SRodney W. Grimes 	}
2089b50d902SRodney W. Grimes }
2099b50d902SRodney W. Grimes 
2109b50d902SRodney W. Grimes PERSON *
2119b50d902SRodney W. Grimes find_person(name)
2129b50d902SRodney W. Grimes 	char *name;
2139b50d902SRodney W. Grimes {
2148829c73eSJordan K. Hubbard 	struct passwd *pw;
2158829c73eSJordan K. Hubbard 
2169b50d902SRodney W. Grimes 	register int cnt;
2179b50d902SRodney W. Grimes 	DBT data, key;
218df3f5d9dSPeter Wemm 	PERSON *p;
2199b50d902SRodney W. Grimes 	char buf[UT_NAMESIZE + 1];
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes 	if (!db)
2229b50d902SRodney W. Grimes 		return(NULL);
2239b50d902SRodney W. Grimes 
2248829c73eSJordan K. Hubbard 	if ((pw = getpwnam(name)) && hide(pw))
2258829c73eSJordan K. Hubbard 		return(NULL);
2268829c73eSJordan K. Hubbard 
2279b50d902SRodney W. Grimes 	/* Name may be only UT_NAMESIZE long and not NUL terminated. */
2289b50d902SRodney W. Grimes 	for (cnt = 0; cnt < UT_NAMESIZE && *name; ++name, ++cnt)
2299b50d902SRodney W. Grimes 		buf[cnt] = *name;
2309b50d902SRodney W. Grimes 	buf[cnt] = '\0';
2319b50d902SRodney W. Grimes 	key.data = buf;
2329b50d902SRodney W. Grimes 	key.size = cnt;
2339b50d902SRodney W. Grimes 
234df3f5d9dSPeter Wemm 	if ((*db->get)(db, &key, &data, 0))
235df3f5d9dSPeter Wemm 		return (NULL);
236df3f5d9dSPeter Wemm 	memmove(&p, data.data, sizeof p);
237df3f5d9dSPeter Wemm 	return (p);
2389b50d902SRodney W. Grimes }
2399b50d902SRodney W. Grimes 
2409b50d902SRodney W. Grimes PERSON *
2419b50d902SRodney W. Grimes palloc()
2429b50d902SRodney W. Grimes {
2439b50d902SRodney W. Grimes 	PERSON *p;
2449b50d902SRodney W. Grimes 
2459b50d902SRodney W. Grimes 	if ((p = malloc((u_int) sizeof(PERSON))) == NULL)
246df3f5d9dSPeter Wemm 		err(1, NULL);
2479b50d902SRodney W. Grimes 	return(p);
2489b50d902SRodney W. Grimes }
2499b50d902SRodney W. Grimes 
2509b50d902SRodney W. Grimes static WHERE *
2519b50d902SRodney W. Grimes walloc(pn)
2529b50d902SRodney W. Grimes 	register PERSON *pn;
2539b50d902SRodney W. Grimes {
2549b50d902SRodney W. Grimes 	register WHERE *w;
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes 	if ((w = malloc((u_int) sizeof(WHERE))) == NULL)
257df3f5d9dSPeter Wemm 		err(1, NULL);
2589b50d902SRodney W. Grimes 	if (pn->whead == NULL)
2599b50d902SRodney W. Grimes 		pn->whead = pn->wtail = w;
2609b50d902SRodney W. Grimes 	else {
2619b50d902SRodney W. Grimes 		pn->wtail->next = w;
2629b50d902SRodney W. Grimes 		pn->wtail = w;
2639b50d902SRodney W. Grimes 	}
2649b50d902SRodney W. Grimes 	w->next = NULL;
2659b50d902SRodney W. Grimes 	return(w);
2669b50d902SRodney W. Grimes }
2679b50d902SRodney W. Grimes 
2689b50d902SRodney W. Grimes char *
2699b50d902SRodney W. Grimes prphone(num)
2709b50d902SRodney W. Grimes 	char *num;
2719b50d902SRodney W. Grimes {
2729b50d902SRodney W. Grimes 	register char *p;
2739b50d902SRodney W. Grimes 	int len;
274e0d8eea1SWarner Losh 	static char pbuf[20];
2759b50d902SRodney W. Grimes 
2769b50d902SRodney W. Grimes 	/* don't touch anything if the user has their own formatting */
2779b50d902SRodney W. Grimes 	for (p = num; *p; ++p)
2789b50d902SRodney W. Grimes 		if (!isdigit(*p))
2799b50d902SRodney W. Grimes 			return(num);
2809b50d902SRodney W. Grimes 	len = p - num;
2819b50d902SRodney W. Grimes 	p = pbuf;
2829b50d902SRodney W. Grimes 	switch(len) {
2839b50d902SRodney W. Grimes 	case 11:			/* +0-123-456-7890 */
2849b50d902SRodney W. Grimes 		*p++ = '+';
2859b50d902SRodney W. Grimes 		*p++ = *num++;
2869b50d902SRodney W. Grimes 		*p++ = '-';
2879b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2889b50d902SRodney W. Grimes 	case 10:			/* 012-345-6789 */
2899b50d902SRodney W. Grimes 		*p++ = *num++;
2909b50d902SRodney W. Grimes 		*p++ = *num++;
2919b50d902SRodney W. Grimes 		*p++ = *num++;
2929b50d902SRodney W. Grimes 		*p++ = '-';
2939b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2949b50d902SRodney W. Grimes 	case 7:				/* 012-3456 */
2959b50d902SRodney W. Grimes 		*p++ = *num++;
2969b50d902SRodney W. Grimes 		*p++ = *num++;
2979b50d902SRodney W. Grimes 		*p++ = *num++;
2989b50d902SRodney W. Grimes 		break;
2999b50d902SRodney W. Grimes 	case 5:				/* x0-1234 */
30086641d8fSPaul Traina 	case 4:				/* x1234 */
3019b50d902SRodney W. Grimes 		*p++ = 'x';
3029b50d902SRodney W. Grimes 		*p++ = *num++;
3039b50d902SRodney W. Grimes 		break;
3049b50d902SRodney W. Grimes 	default:
3059b50d902SRodney W. Grimes 		return(num);
3069b50d902SRodney W. Grimes 	}
30786641d8fSPaul Traina 	if (len != 4) {
3089b50d902SRodney W. Grimes 	    *p++ = '-';
3099b50d902SRodney W. Grimes 	    *p++ = *num++;
31086641d8fSPaul Traina 	}
3119b50d902SRodney W. Grimes 	*p++ = *num++;
3129b50d902SRodney W. Grimes 	*p++ = *num++;
3139b50d902SRodney W. Grimes 	*p++ = *num++;
3149b50d902SRodney W. Grimes 	*p = '\0';
3159b50d902SRodney W. Grimes 	return(pbuf);
3169b50d902SRodney W. Grimes }
3179b50d902SRodney W. Grimes 
3189b50d902SRodney W. Grimes static void
3199b50d902SRodney W. Grimes find_idle_and_ttywrite(w)
3209b50d902SRodney W. Grimes 	register WHERE *w;
3219b50d902SRodney W. Grimes {
3229b50d902SRodney W. Grimes 	extern time_t now;
3239b50d902SRodney W. Grimes 	struct stat sb;
3243bebe991SBrian Somers 	time_t touched;
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
3279b50d902SRodney W. Grimes 	if (stat(tbuf, &sb) < 0) {
328084c79f6SKris Kennaway 		warn("%s", tbuf);
3299b50d902SRodney W. Grimes 		return;
3309b50d902SRodney W. Grimes 	}
3313bebe991SBrian Somers 	touched = sb.st_atime;
3323bebe991SBrian Somers 	if (touched < w->loginat) {
3333bebe991SBrian Somers 		/* tty untouched since before login */
3343bebe991SBrian Somers 		touched = w->loginat;
3353bebe991SBrian Somers 	}
3363bebe991SBrian Somers 	w->idletime = now < touched ? 0 : now - touched;
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes #define	TALKABLE	0220		/* tty is writable if 220 mode */
3399b50d902SRodney W. Grimes 	w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
3409b50d902SRodney W. Grimes }
3419b50d902SRodney W. Grimes 
3429b50d902SRodney W. Grimes static void
3439b50d902SRodney W. Grimes userinfo(pn, pw)
3449b50d902SRodney W. Grimes 	register PERSON *pn;
3459b50d902SRodney W. Grimes 	register struct passwd *pw;
3469b50d902SRodney W. Grimes {
3479b50d902SRodney W. Grimes 	register char *p, *t;
3489b50d902SRodney W. Grimes 	char *bp, name[1024];
34986641d8fSPaul Traina 	struct stat sb;
3509b50d902SRodney W. Grimes 
3519b50d902SRodney W. Grimes 	pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
3529b50d902SRodney W. Grimes 
3539b50d902SRodney W. Grimes 	pn->uid = pw->pw_uid;
3548728c621SChris D. Faulhaber 	if ((pn->name = strdup(pw->pw_name)) == NULL)
3558728c621SChris D. Faulhaber 		err(1, "strdup failed");
3568728c621SChris D. Faulhaber 	if ((pn->dir = strdup(pw->pw_dir)) == NULL)
3578728c621SChris D. Faulhaber 		err(1, "strdup failed");
3588728c621SChris D. Faulhaber 	if ((pn->shell = strdup(pw->pw_shell)) == NULL)
3598728c621SChris D. Faulhaber 		err(1, "strdup failed");
3609b50d902SRodney W. Grimes 
3619b50d902SRodney W. Grimes 	/* why do we skip asterisks!?!? */
362e0d8eea1SWarner Losh 	(void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
363b59bb23bSWarner Losh 	tbuf[sizeof(tbuf) - 1] = '\0';
3649b50d902SRodney W. Grimes 	if (*bp == '*')
3659b50d902SRodney W. Grimes 		++bp;
3669b50d902SRodney W. Grimes 
3679b50d902SRodney W. Grimes 	/* ampersands get replaced by the login name */
3689b50d902SRodney W. Grimes 	if (!(p = strsep(&bp, ",")))
3699b50d902SRodney W. Grimes 		return;
370b59bb23bSWarner Losh 	for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
3719b50d902SRodney W. Grimes 		if (*t == '&') {
372e0d8eea1SWarner Losh 			(void)strncpy(t, pw->pw_name,
373e0d8eea1SWarner Losh 			    sizeof(name) - (t - name));
374e0d8eea1SWarner Losh 			name[sizeof(name) - 1] = '\0';
3759b50d902SRodney W. Grimes 			if (islower(*t))
3769b50d902SRodney W. Grimes 				*t = toupper(*t);
377b59bb23bSWarner Losh 			while (t < &name[sizeof(name) - 1] && *++t)
378b59bb23bSWarner Losh 				continue;
379e0d8eea1SWarner Losh 		} else {
3809b50d902SRodney W. Grimes 			++t;
381e0d8eea1SWarner Losh 		}
382e0d8eea1SWarner Losh 	}
383e0d8eea1SWarner Losh 	*t = '\0';
3848728c621SChris D. Faulhaber 	if ((pn->realname = strdup(name)) == NULL)
3858728c621SChris D. Faulhaber 		err(1, "strdup failed");
3869b50d902SRodney W. Grimes 	pn->office = ((p = strsep(&bp, ",")) && *p) ?
3879b50d902SRodney W. Grimes 	    strdup(p) : NULL;
3889b50d902SRodney W. Grimes 	pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
3899b50d902SRodney W. Grimes 	    strdup(p) : NULL;
3909b50d902SRodney W. Grimes 	pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
3919b50d902SRodney W. Grimes 	    strdup(p) : NULL;
392e0d8eea1SWarner Losh 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
39386641d8fSPaul Traina 	pn->mailrecv = -1;		/* -1 == not_valid */
39486641d8fSPaul Traina 	if (stat(tbuf, &sb) < 0) {
39586641d8fSPaul Traina 		if (errno != ENOENT) {
396b14d8277SPhilippe Charnier 			warn("%s", tbuf);
39786641d8fSPaul Traina 			return;
39886641d8fSPaul Traina 		}
39986641d8fSPaul Traina 	} else if (sb.st_size != 0) {
40086641d8fSPaul Traina 		pn->mailrecv = sb.st_mtime;
40186641d8fSPaul Traina 		pn->mailread = sb.st_atime;
40286641d8fSPaul Traina 	}
4039b50d902SRodney W. Grimes }
4049b50d902SRodney W. Grimes 
4058829c73eSJordan K. Hubbard /*
4068829c73eSJordan K. Hubbard  * Is this user hiding from finger?
4078829c73eSJordan K. Hubbard  * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
4088829c73eSJordan K. Hubbard  */
4098829c73eSJordan K. Hubbard 
4108829c73eSJordan K. Hubbard int
4118829c73eSJordan K. Hubbard hide(pw)
4128829c73eSJordan K. Hubbard 	struct passwd *pw;
4138829c73eSJordan K. Hubbard {
4148829c73eSJordan K. Hubbard 	char buf[MAXPATHLEN+1];
4158829c73eSJordan K. Hubbard 
4168829c73eSJordan K. Hubbard 	if (!pw->pw_dir)
4178829c73eSJordan K. Hubbard 		return 0;
4188829c73eSJordan K. Hubbard 
419e0d8eea1SWarner Losh 	snprintf(buf, sizeof(buf), "%s/.nofinger", pw->pw_dir);
420e0d8eea1SWarner Losh 	buf[sizeof(buf) - 1] = '\0';
4218829c73eSJordan K. Hubbard 
4228829c73eSJordan K. Hubbard 	if (access(buf, F_OK) == 0)
4238829c73eSJordan K. Hubbard 		return 1;
4248829c73eSJordan K. Hubbard 
4258829c73eSJordan K. Hubbard 	return 0;
4268829c73eSJordan K. Hubbard }
427