xref: /freebsd/usr.bin/finger/util.c (revision 5e3934b1)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes #include <sys/param.h>
36f4ac32deSDavid Malone #include <sys/socket.h>
379b50d902SRodney W. Grimes #include <sys/stat.h>
384c86f3adSPhilippe Charnier #include <ctype.h>
399b50d902SRodney W. Grimes #include <db.h>
40df3f5d9dSPeter Wemm #include <err.h>
419b50d902SRodney W. Grimes #include <errno.h>
424c86f3adSPhilippe Charnier #include <fcntl.h>
434c86f3adSPhilippe Charnier #include <paths.h>
444c86f3adSPhilippe Charnier #include <pwd.h>
459b50d902SRodney W. Grimes #include <stdio.h>
469b50d902SRodney W. Grimes #include <stdlib.h>
479b50d902SRodney W. Grimes #include <string.h>
484c86f3adSPhilippe Charnier #include <unistd.h>
49550dcc59SEd Schouten #include <utmpx.h>
509b50d902SRodney W. Grimes #include "finger.h"
51b95dbabaSYaroslav Tykhiy #include "pathnames.h"
529b50d902SRodney W. Grimes 
53f1bb2cd2SWarner Losh static void	 find_idle_and_ttywrite(WHERE *);
54f1bb2cd2SWarner Losh static void	 userinfo(PERSON *, struct passwd *);
55f1bb2cd2SWarner Losh static WHERE	*walloc(PERSON *);
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes int
match(struct passwd * pw,const char * user)58f4ac32deSDavid Malone match(struct passwd *pw, const char *user)
599b50d902SRodney W. Grimes {
604e030ba6SMark Murray 	char *p, *t;
619b50d902SRodney W. Grimes 	char name[1024];
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes 	if (!strcasecmp(pw->pw_name, user))
649b50d902SRodney W. Grimes 		return(1);
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes 	/*
679b50d902SRodney W. Grimes 	 * XXX
689b50d902SRodney W. Grimes 	 * Why do we skip asterisks!?!?
699b50d902SRodney W. Grimes 	 */
70e0d8eea1SWarner Losh 	(void)strncpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
71b59bb23bSWarner Losh 	tbuf[sizeof(tbuf) - 1] = '\0';
729b50d902SRodney W. Grimes 	if (*p == '*')
739b50d902SRodney W. Grimes 		++p;
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes 	/* Ampersands get replaced by the login name. */
769b50d902SRodney W. Grimes 	if ((p = strtok(p, ",")) == NULL)
779b50d902SRodney W. Grimes 		return(0);
789b50d902SRodney W. Grimes 
79b59bb23bSWarner Losh 	for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
809b50d902SRodney W. Grimes 		if (*t == '&') {
81e0d8eea1SWarner Losh 			(void)strncpy(t, pw->pw_name,
82e0d8eea1SWarner Losh 			    sizeof(name) - (t - name));
83e0d8eea1SWarner Losh 			name[sizeof(name) - 1] = '\0';
84b59bb23bSWarner Losh 			while (t < &name[sizeof(name) - 1] && *++t)
85b59bb23bSWarner Losh 				continue;
86e0d8eea1SWarner Losh 		} else {
879b50d902SRodney W. Grimes 			++t;
88e0d8eea1SWarner Losh 		}
89e0d8eea1SWarner Losh 	}
90e0d8eea1SWarner Losh 	*t = '\0';
91df3f5d9dSPeter Wemm 	for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
929b50d902SRodney W. Grimes 		if (!strcasecmp(p, user))
939b50d902SRodney W. Grimes 			return(1);
949b50d902SRodney W. Grimes 	return(0);
959b50d902SRodney W. Grimes }
969b50d902SRodney W. Grimes 
979b50d902SRodney W. Grimes void
enter_lastlog(PERSON * pn)98f4ac32deSDavid Malone enter_lastlog(PERSON *pn)
999b50d902SRodney W. Grimes {
1004e030ba6SMark Murray 	WHERE *w;
101550dcc59SEd Schouten 	struct utmpx *ut = NULL;
1029b50d902SRodney W. Grimes 	char doit = 0;
1039b50d902SRodney W. Grimes 
104550dcc59SEd Schouten 	if (setutxdb(UTXDB_LASTLOGIN, NULL) == 0)
105550dcc59SEd Schouten 		ut = getutxuser(pn->name);
1069b50d902SRodney W. Grimes 	if ((w = pn->whead) == NULL)
1079b50d902SRodney W. Grimes 		doit = 1;
10870ad88f3SEd Schouten 	else if (ut != NULL && ut->ut_type == USER_PROCESS) {
1099b50d902SRodney W. Grimes 		/* if last login is earlier than some current login */
1109b50d902SRodney W. Grimes 		for (; !doit && w != NULL; w = w->next)
11170ad88f3SEd Schouten 			if (w->info == LOGGEDIN &&
11270ad88f3SEd Schouten 			    w->loginat < ut->ut_tv.tv_sec)
1139b50d902SRodney W. Grimes 				doit = 1;
1149b50d902SRodney W. Grimes 		/*
1159b50d902SRodney W. Grimes 		 * and if it's not any of the current logins
1169b50d902SRodney W. Grimes 		 * can't use time comparison because there may be a small
1174c86f3adSPhilippe Charnier 		 * discrepancy since login calls time() twice
1189b50d902SRodney W. Grimes 		 */
1199b50d902SRodney W. Grimes 		for (w = pn->whead; doit && w != NULL; w = w->next)
1209b50d902SRodney W. Grimes 			if (w->info == LOGGEDIN &&
12170ad88f3SEd Schouten 			    strcmp(w->tty, ut->ut_line) == 0)
1229b50d902SRodney W. Grimes 				doit = 0;
1239b50d902SRodney W. Grimes 	}
12470ad88f3SEd Schouten 	if (ut != NULL && doit) {
1259b50d902SRodney W. Grimes 		w = walloc(pn);
1269b50d902SRodney W. Grimes 		w->info = LASTLOG;
12770ad88f3SEd Schouten 		strcpy(w->tty, ut->ut_line);
12870ad88f3SEd Schouten 		strcpy(w->host, ut->ut_host);
12970ad88f3SEd Schouten 		w->loginat = ut->ut_tv.tv_sec;
1309b50d902SRodney W. Grimes 	}
131550dcc59SEd Schouten 	endutxent();
1329b50d902SRodney W. Grimes }
1339b50d902SRodney W. Grimes 
1349b50d902SRodney W. Grimes void
enter_where(struct utmpx * ut,PERSON * pn)13570ad88f3SEd Schouten enter_where(struct utmpx *ut, PERSON *pn)
1369b50d902SRodney W. Grimes {
1374e030ba6SMark Murray 	WHERE *w;
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 	w = walloc(pn);
1409b50d902SRodney W. Grimes 	w->info = LOGGEDIN;
14170ad88f3SEd Schouten 	strcpy(w->tty, ut->ut_line);
14270ad88f3SEd Schouten 	strcpy(w->host, ut->ut_host);
14370ad88f3SEd Schouten 	w->loginat = ut->ut_tv.tv_sec;
1449b50d902SRodney W. Grimes 	find_idle_and_ttywrite(w);
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes PERSON *
enter_person(struct passwd * pw)148f4ac32deSDavid Malone enter_person(struct passwd *pw)
1499b50d902SRodney W. Grimes {
1509b50d902SRodney W. Grimes 	DBT data, key;
1519b50d902SRodney W. Grimes 	PERSON *pn;
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes 	if (db == NULL &&
1549b50d902SRodney W. Grimes 	    (db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL)) == NULL)
155df3f5d9dSPeter Wemm 		err(1, NULL);
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes 	key.data = pw->pw_name;
1589b50d902SRodney W. Grimes 	key.size = strlen(pw->pw_name);
1599b50d902SRodney W. Grimes 
1609b50d902SRodney W. Grimes 	switch ((*db->get)(db, &key, &data, 0)) {
1619b50d902SRodney W. Grimes 	case 0:
162df3f5d9dSPeter Wemm 		memmove(&pn, data.data, sizeof pn);
163df3f5d9dSPeter Wemm 		return (pn);
1649b50d902SRodney W. Grimes 	default:
1659b50d902SRodney W. Grimes 	case -1:
166df3f5d9dSPeter Wemm 		err(1, "db get");
1679b50d902SRodney W. Grimes 		/* NOTREACHED */
1689b50d902SRodney W. Grimes 	case 1:
1699b50d902SRodney W. Grimes 		++entries;
1709b50d902SRodney W. Grimes 		pn = palloc();
1719b50d902SRodney W. Grimes 		userinfo(pn, pw);
1729b50d902SRodney W. Grimes 		pn->whead = NULL;
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes 		data.size = sizeof(PERSON *);
1759b50d902SRodney W. Grimes 		data.data = &pn;
1769b50d902SRodney W. Grimes 		if ((*db->put)(db, &key, &data, 0))
177df3f5d9dSPeter Wemm 			err(1, "db put");
1789b50d902SRodney W. Grimes 		return (pn);
1799b50d902SRodney W. Grimes 	}
1809b50d902SRodney W. Grimes }
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes PERSON *
find_person(char * name)18370ad88f3SEd Schouten find_person(char *name)
1849b50d902SRodney W. Grimes {
1858829c73eSJordan K. Hubbard 	struct passwd *pw;
1868829c73eSJordan K. Hubbard 
1879b50d902SRodney W. Grimes 	DBT data, key;
188df3f5d9dSPeter Wemm 	PERSON *p;
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes 	if (!db)
1919b50d902SRodney W. Grimes 		return(NULL);
1929b50d902SRodney W. Grimes 
1938829c73eSJordan K. Hubbard 	if ((pw = getpwnam(name)) && hide(pw))
1948829c73eSJordan K. Hubbard 		return(NULL);
1958829c73eSJordan K. Hubbard 
19670ad88f3SEd Schouten 	key.data = name;
19770ad88f3SEd Schouten 	key.size = strlen(name);
1989b50d902SRodney W. Grimes 
199df3f5d9dSPeter Wemm 	if ((*db->get)(db, &key, &data, 0))
200df3f5d9dSPeter Wemm 		return (NULL);
201df3f5d9dSPeter Wemm 	memmove(&p, data.data, sizeof p);
202df3f5d9dSPeter Wemm 	return (p);
2039b50d902SRodney W. Grimes }
2049b50d902SRodney W. Grimes 
2059b50d902SRodney W. Grimes PERSON *
palloc(void)206f4ac32deSDavid Malone palloc(void)
2079b50d902SRodney W. Grimes {
2089b50d902SRodney W. Grimes 	PERSON *p;
2099b50d902SRodney W. Grimes 
21047bca8b0SJuli Mallett 	if ((p = malloc(sizeof(PERSON))) == NULL)
211df3f5d9dSPeter Wemm 		err(1, NULL);
2129b50d902SRodney W. Grimes 	return(p);
2139b50d902SRodney W. Grimes }
2149b50d902SRodney W. Grimes 
2159b50d902SRodney W. Grimes static WHERE *
walloc(PERSON * pn)216f4ac32deSDavid Malone walloc(PERSON *pn)
2179b50d902SRodney W. Grimes {
2184e030ba6SMark Murray 	WHERE *w;
2199b50d902SRodney W. Grimes 
22047bca8b0SJuli Mallett 	if ((w = malloc(sizeof(WHERE))) == NULL)
221df3f5d9dSPeter Wemm 		err(1, NULL);
2229b50d902SRodney W. Grimes 	if (pn->whead == NULL)
2239b50d902SRodney W. Grimes 		pn->whead = pn->wtail = w;
2249b50d902SRodney W. Grimes 	else {
2259b50d902SRodney W. Grimes 		pn->wtail->next = w;
2269b50d902SRodney W. Grimes 		pn->wtail = w;
2279b50d902SRodney W. Grimes 	}
2289b50d902SRodney W. Grimes 	w->next = NULL;
2299b50d902SRodney W. Grimes 	return(w);
2309b50d902SRodney W. Grimes }
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes char *
prphone(char * num)233f4ac32deSDavid Malone prphone(char *num)
2349b50d902SRodney W. Grimes {
2354e030ba6SMark Murray 	char *p;
2369b50d902SRodney W. Grimes 	int len;
237e0d8eea1SWarner Losh 	static char pbuf[20];
2389b50d902SRodney W. Grimes 
2399b50d902SRodney W. Grimes 	/* don't touch anything if the user has their own formatting */
2409b50d902SRodney W. Grimes 	for (p = num; *p; ++p)
2419b50d902SRodney W. Grimes 		if (!isdigit(*p))
2429b50d902SRodney W. Grimes 			return(num);
2439b50d902SRodney W. Grimes 	len = p - num;
2449b50d902SRodney W. Grimes 	p = pbuf;
2459b50d902SRodney W. Grimes 	switch(len) {
2469b50d902SRodney W. Grimes 	case 11:			/* +0-123-456-7890 */
2479b50d902SRodney W. Grimes 		*p++ = '+';
2489b50d902SRodney W. Grimes 		*p++ = *num++;
2499b50d902SRodney W. Grimes 		*p++ = '-';
2509b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2519b50d902SRodney W. Grimes 	case 10:			/* 012-345-6789 */
2529b50d902SRodney W. Grimes 		*p++ = *num++;
2539b50d902SRodney W. Grimes 		*p++ = *num++;
2549b50d902SRodney W. Grimes 		*p++ = *num++;
2559b50d902SRodney W. Grimes 		*p++ = '-';
2569b50d902SRodney W. Grimes 		/* FALLTHROUGH */
2579b50d902SRodney W. Grimes 	case 7:				/* 012-3456 */
2589b50d902SRodney W. Grimes 		*p++ = *num++;
2599b50d902SRodney W. Grimes 		*p++ = *num++;
2609b50d902SRodney W. Grimes 		*p++ = *num++;
2619b50d902SRodney W. Grimes 		break;
2629b50d902SRodney W. Grimes 	case 5:				/* x0-1234 */
26386641d8fSPaul Traina 	case 4:				/* x1234 */
2649b50d902SRodney W. Grimes 		*p++ = 'x';
2659b50d902SRodney W. Grimes 		*p++ = *num++;
2669b50d902SRodney W. Grimes 		break;
2679b50d902SRodney W. Grimes 	default:
2689b50d902SRodney W. Grimes 		return(num);
2699b50d902SRodney W. Grimes 	}
27086641d8fSPaul Traina 	if (len != 4) {
2719b50d902SRodney W. Grimes 	    *p++ = '-';
2729b50d902SRodney W. Grimes 	    *p++ = *num++;
27386641d8fSPaul Traina 	}
2749b50d902SRodney W. Grimes 	*p++ = *num++;
2759b50d902SRodney W. Grimes 	*p++ = *num++;
2769b50d902SRodney W. Grimes 	*p++ = *num++;
2779b50d902SRodney W. Grimes 	*p = '\0';
2789b50d902SRodney W. Grimes 	return(pbuf);
2799b50d902SRodney W. Grimes }
2809b50d902SRodney W. Grimes 
2819b50d902SRodney W. Grimes static void
find_idle_and_ttywrite(WHERE * w)282f4ac32deSDavid Malone find_idle_and_ttywrite(WHERE *w)
2839b50d902SRodney W. Grimes {
2849b50d902SRodney W. Grimes 	struct stat sb;
2853bebe991SBrian Somers 	time_t touched;
2867d7d4297SRobert Watson 	int error;
2879b50d902SRodney W. Grimes 
2889b50d902SRodney W. Grimes 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_DEV, w->tty);
2897d7d4297SRobert Watson 
2907d7d4297SRobert Watson 	error = stat(tbuf, &sb);
2917d7d4297SRobert Watson 	if (error < 0 && errno == ENOENT) {
2927d7d4297SRobert Watson 		/*
2937d7d4297SRobert Watson 		 * The terminal listed is not actually a terminal (i.e.,
2947d7d4297SRobert Watson 		 * ":0").  This is a failure, so we'll skip printing
2957d7d4297SRobert Watson 		 * out the idle time, which is non-ideal but better
2967d7d4297SRobert Watson 		 * than a bogus warning and idle time.
2977d7d4297SRobert Watson 		 */
2987d7d4297SRobert Watson 		w->idletime = -1;
2997d7d4297SRobert Watson 		return;
3007d7d4297SRobert Watson 	} else if (error < 0) {
301084c79f6SKris Kennaway 		warn("%s", tbuf);
3027d7d4297SRobert Watson 		w->idletime = -1;
3039b50d902SRodney W. Grimes 		return;
3049b50d902SRodney W. Grimes 	}
3053bebe991SBrian Somers 	touched = sb.st_atime;
3063bebe991SBrian Somers 	if (touched < w->loginat) {
3073bebe991SBrian Somers 		/* tty untouched since before login */
3083bebe991SBrian Somers 		touched = w->loginat;
3093bebe991SBrian Somers 	}
3103bebe991SBrian Somers 	w->idletime = now < touched ? 0 : now - touched;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes #define	TALKABLE	0220		/* tty is writable if 220 mode */
3139b50d902SRodney W. Grimes 	w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
3149b50d902SRodney W. Grimes }
3159b50d902SRodney W. Grimes 
3169b50d902SRodney W. Grimes static void
userinfo(PERSON * pn,struct passwd * pw)317f4ac32deSDavid Malone userinfo(PERSON *pn, struct passwd *pw)
3189b50d902SRodney W. Grimes {
3194e030ba6SMark Murray 	char *p, *t;
3209b50d902SRodney W. Grimes 	char *bp, name[1024];
32186641d8fSPaul Traina 	struct stat sb;
3229b50d902SRodney W. Grimes 
3239b50d902SRodney W. Grimes 	pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
3249b50d902SRodney W. Grimes 
3259b50d902SRodney W. Grimes 	pn->uid = pw->pw_uid;
3268728c621SChris D. Faulhaber 	if ((pn->name = strdup(pw->pw_name)) == NULL)
3278728c621SChris D. Faulhaber 		err(1, "strdup failed");
3288728c621SChris D. Faulhaber 	if ((pn->dir = strdup(pw->pw_dir)) == NULL)
3298728c621SChris D. Faulhaber 		err(1, "strdup failed");
3308728c621SChris D. Faulhaber 	if ((pn->shell = strdup(pw->pw_shell)) == NULL)
3318728c621SChris D. Faulhaber 		err(1, "strdup failed");
3329b50d902SRodney W. Grimes 
3339b50d902SRodney W. Grimes 	/* why do we skip asterisks!?!? */
334e0d8eea1SWarner Losh 	(void)strncpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
335b59bb23bSWarner Losh 	tbuf[sizeof(tbuf) - 1] = '\0';
3369b50d902SRodney W. Grimes 	if (*bp == '*')
3379b50d902SRodney W. Grimes 		++bp;
3389b50d902SRodney W. Grimes 
3399b50d902SRodney W. Grimes 	/* ampersands get replaced by the login name */
3409b50d902SRodney W. Grimes 	if (!(p = strsep(&bp, ",")))
3419b50d902SRodney W. Grimes 		return;
342b59bb23bSWarner Losh 	for (t = name; t < &name[sizeof(name) - 1] && (*t = *p) != '\0'; ++p) {
3439b50d902SRodney W. Grimes 		if (*t == '&') {
344e0d8eea1SWarner Losh 			(void)strncpy(t, pw->pw_name,
345e0d8eea1SWarner Losh 			    sizeof(name) - (t - name));
346e0d8eea1SWarner Losh 			name[sizeof(name) - 1] = '\0';
3479b50d902SRodney W. Grimes 			if (islower(*t))
3489b50d902SRodney W. Grimes 				*t = toupper(*t);
349b59bb23bSWarner Losh 			while (t < &name[sizeof(name) - 1] && *++t)
350b59bb23bSWarner Losh 				continue;
351e0d8eea1SWarner Losh 		} else {
3529b50d902SRodney W. Grimes 			++t;
353e0d8eea1SWarner Losh 		}
354e0d8eea1SWarner Losh 	}
355e0d8eea1SWarner Losh 	*t = '\0';
3568728c621SChris D. Faulhaber 	if ((pn->realname = strdup(name)) == NULL)
3578728c621SChris D. Faulhaber 		err(1, "strdup failed");
3589b50d902SRodney W. Grimes 	pn->office = ((p = strsep(&bp, ",")) && *p) ?
3599b50d902SRodney W. Grimes 	    strdup(p) : NULL;
3609b50d902SRodney W. Grimes 	pn->officephone = ((p = strsep(&bp, ",")) && *p) ?
3619b50d902SRodney W. Grimes 	    strdup(p) : NULL;
3629b50d902SRodney W. Grimes 	pn->homephone = ((p = strsep(&bp, ",")) && *p) ?
3639b50d902SRodney W. Grimes 	    strdup(p) : NULL;
364e0d8eea1SWarner Losh 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pw->pw_name);
36586641d8fSPaul Traina 	pn->mailrecv = -1;		/* -1 == not_valid */
36686641d8fSPaul Traina 	if (stat(tbuf, &sb) < 0) {
36786641d8fSPaul Traina 		if (errno != ENOENT) {
368b14d8277SPhilippe Charnier 			warn("%s", tbuf);
36986641d8fSPaul Traina 			return;
37086641d8fSPaul Traina 		}
37186641d8fSPaul Traina 	} else if (sb.st_size != 0) {
37286641d8fSPaul Traina 		pn->mailrecv = sb.st_mtime;
37386641d8fSPaul Traina 		pn->mailread = sb.st_atime;
37486641d8fSPaul Traina 	}
3759b50d902SRodney W. Grimes }
3769b50d902SRodney W. Grimes 
3778829c73eSJordan K. Hubbard /*
3788829c73eSJordan K. Hubbard  * Is this user hiding from finger?
3798829c73eSJordan K. Hubbard  * If ~<user>/.nofinger exists, return 1 (hide), else return 0 (nohide).
380d2e4ea2aSDiomidis Spinellis  * Nobody can hide from root.
3818829c73eSJordan K. Hubbard  */
3828829c73eSJordan K. Hubbard 
3838829c73eSJordan K. Hubbard int
hide(struct passwd * pw)384f4ac32deSDavid Malone hide(struct passwd *pw)
3858829c73eSJordan K. Hubbard {
386d691b79fSYaroslav Tykhiy 	struct stat st;
387167ea27bSWarner Losh 	char buf[MAXPATHLEN];
3888829c73eSJordan K. Hubbard 
389d2e4ea2aSDiomidis Spinellis 	if (invoker_root || !pw->pw_dir)
3908829c73eSJordan K. Hubbard 		return 0;
3918829c73eSJordan K. Hubbard 
392b95dbabaSYaroslav Tykhiy 	snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir, _PATH_NOFINGER);
3938829c73eSJordan K. Hubbard 
394d691b79fSYaroslav Tykhiy 	if (stat(buf, &st) == 0)
3958829c73eSJordan K. Hubbard 		return 1;
3968829c73eSJordan K. Hubbard 
3978829c73eSJordan K. Hubbard 	return 0;
3988829c73eSJordan K. Hubbard }
399