finger.c revision 106250
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
372537Spst/*
382537Spst * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
392537Spst *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
402537Spst *	Unread since ...".)
412537Spst *    - 4 digit phone extensions (3210 is printed as x3210.)
422537Spst *    - host/office toggling in short format with -h & -o.
432537Spst *    - short day names (`Tue' printed instead of `Jun 21' if the
442537Spst *	login time is < 6 days.
452537Spst */
462537Spst
471590Srgrimes#ifndef lint
4867467Srustatic const char copyright[] =
491590Srgrimes"@(#) Copyright (c) 1989, 1993\n\
501590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
511590Srgrimes#endif /* not lint */
521590Srgrimes
5387628Sdwmalone#if 0
541590Srgrimes#ifndef lint
5587628Sdwmalonestatic char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
5672109Scharnier#endif
5787628Sdwmalone#endif
581590Srgrimes
5987628Sdwmalone#include <sys/cdefs.h>
6087628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/finger/finger.c 106250 2002-10-31 13:05:56Z sobomax $");
6187628Sdwmalone
621590Srgrimes/*
631590Srgrimes * Finger prints out information about users.  It is not portable since
641590Srgrimes * certain fields (e.g. the full user name, office, and phone numbers) are
651590Srgrimes * extracted from the gecos field of the passwd file which other UNIXes
661590Srgrimes * may not have or may use for other things.
671590Srgrimes *
681590Srgrimes * There are currently two output formats; the short format is one line
691590Srgrimes * per user and displays login name, tty, login time, real name, idle time,
702537Spst * and either remote host information (default) or office location/phone
712537Spst * number, depending on if -h or -o is used respectively.
722537Spst * The long format gives the same information (in a more legible format) as
732537Spst * well as home directory, shell, mail info, and .plan/.project files.
741590Srgrimes */
751590Srgrimes
76100521Sume#include <sys/types.h>
77100521Sume#include <sys/socket.h>
7823693Speter#include <db.h>
7923693Speter#include <err.h>
801590Srgrimes#include <pwd.h>
811590Srgrimes#include <stdio.h>
821590Srgrimes#include <stdlib.h>
831590Srgrimes#include <string.h>
8423693Speter#include <time.h>
8523693Speter#include <unistd.h>
8623693Speter#include <utmp.h>
8711759Sache#include <locale.h>
8823693Speter
891590Srgrimes#include "finger.h"
9064775Sbrian#include "pathnames.h"
911590Srgrimes
921590SrgrimesDB *db;
931590Srgrimestime_t now;
9499249Sminiint entries, gflag, lflag, mflag, pplan, sflag, oflag, Tflag;
95106250Ssobomaxsa_family_t family = PF_UNSPEC;
9674586Sacheint d_first = -1;
971590Srgrimeschar tbuf[1024];
981590Srgrimes
9992920Simpstatic void loginlist(void);
10092920Simpstatic int option(int, char **);
10192920Simpstatic void usage(void);
10292920Simpstatic void userlist(int, char **);
1031590Srgrimes
10487229Smarkmstatic int
105102944Sdwmaloneoption(int argc, char **argv)
1061590Srgrimes{
1071590Srgrimes	int ch;
1081590Srgrimes
1092589Spst	optind = 1;		/* reset getopt */
1102537Spst
111100521Sume	while ((ch = getopt(argc, argv, "46glmpshoT")) != -1)
1121590Srgrimes		switch(ch) {
113100521Sume		case '4':
114100521Sume			family = AF_INET;
115100521Sume			break;
116100521Sume		case '6':
117100521Sume			family = AF_INET6;
118100521Sume			break;
11999249Smini		case 'g':
12099249Smini			gflag = 1;
12199249Smini			break;
1221590Srgrimes		case 'l':
1231590Srgrimes			lflag = 1;		/* long format */
1241590Srgrimes			break;
1251590Srgrimes		case 'm':
1261590Srgrimes			mflag = 1;		/* force exact match of names */
1271590Srgrimes			break;
1281590Srgrimes		case 'p':
1291590Srgrimes			pplan = 1;		/* don't show .plan/.project */
1301590Srgrimes			break;
1311590Srgrimes		case 's':
1321590Srgrimes			sflag = 1;		/* short format */
1331590Srgrimes			break;
1342537Spst		case 'h':
1352537Spst			oflag = 0;		/* remote host info */
1362537Spst			break;
1372537Spst		case 'o':
1382537Spst			oflag = 1;		/* office info */
1392537Spst			break;
14014631Solah		case 'T':
14114631Solah			Tflag = 1;		/* disable T/TCP */
14214631Solah			break;
1431590Srgrimes		case '?':
1441590Srgrimes		default:
14527169Scharnier			usage();
1461590Srgrimes		}
1471590Srgrimes
1482589Spst	return optind;
1492589Spst}
1502589Spst
15127169Scharnierstatic void
152102944Sdwmaloneusage(void)
15327169Scharnier{
154100521Sume	(void)fprintf(stderr, "usage: finger [-46lmpshoT] [login ...]\n");
15527169Scharnier	exit(1);
15627169Scharnier}
15727169Scharnier
15827169Scharnierint
159102944Sdwmalonemain(int argc, char **argv)
1602589Spst{
16127169Scharnier	int envargc, argcnt;
1622589Spst	char *envargv[3];
16346662Sobrien	struct passwd *pw;
16487229Smarkm	static char myname[] = "finger";
1652589Spst
16646662Sobrien	if (getuid() == 0 || geteuid() == 0) {
16746662Sobrien		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
16846662Sobrien			 setgid(pw->pw_gid);
16946662Sobrien			 setuid(pw->pw_uid);
17046662Sobrien		} else {
17146662Sobrien			 setgid(UNPRIV_UGID);
17246662Sobrien			 setuid(UNPRIV_UGID);
17346662Sobrien		}
17446662Sobrien	}
17546662Sobrien
17611811Sache	(void) setlocale(LC_ALL, "");
17711759Sache
1782589Spst				/* remove this line to get remote host */
1792589Spst	oflag = 1;		/* default to old "office" behavior */
1802589Spst
1812589Spst	/*
1822589Spst	 * Process environment variables followed by command line arguments.
1832589Spst	 */
1842589Spst	if ((envargv[1] = getenv("FINGER"))) {
1852589Spst		envargc = 2;
18687229Smarkm		envargv[0] = myname;
1872589Spst		envargv[2] = NULL;
1882589Spst		(void) option(envargc, envargv);
1892589Spst	}
1902589Spst
1912589Spst	argcnt = option(argc, argv);
1922589Spst	argc -= argcnt;
1932589Spst	argv += argcnt;
1942589Spst
1951590Srgrimes	(void)time(&now);
1961590Srgrimes	setpassent(1);
1971590Srgrimes	if (!*argv) {
1981590Srgrimes		/*
1991590Srgrimes		 * Assign explicit "small" format if no names given and -l
2001590Srgrimes		 * not selected.  Force the -s BEFORE we get names so proper
2011590Srgrimes		 * screening will be done.
2021590Srgrimes		 */
2031590Srgrimes		if (!lflag)
2041590Srgrimes			sflag = 1;	/* if -l not explicit, force -s */
2051590Srgrimes		loginlist();
2061590Srgrimes		if (entries == 0)
2071590Srgrimes			(void)printf("No one logged on.\n");
2081590Srgrimes	} else {
2091590Srgrimes		userlist(argc, argv);
2101590Srgrimes		/*
2111590Srgrimes		 * Assign explicit "large" format if names given and -s not
2121590Srgrimes		 * explicitly stated.  Force the -l AFTER we get names so any
2131590Srgrimes		 * remote finger attempts specified won't be mishandled.
2141590Srgrimes		 */
2151590Srgrimes		if (!sflag)
2161590Srgrimes			lflag = 1;	/* if -s not explicit, force -l */
2171590Srgrimes	}
21848566Sbillf	if (entries) {
2191590Srgrimes		if (lflag)
2201590Srgrimes			lflag_print();
2211590Srgrimes		else
2221590Srgrimes			sflag_print();
22348566Sbillf	}
22423693Speter	return (0);
2251590Srgrimes}
2261590Srgrimes
2271590Srgrimesstatic void
228102944Sdwmaloneloginlist(void)
2291590Srgrimes{
23087229Smarkm	PERSON *pn;
2311590Srgrimes	DBT data, key;
2321590Srgrimes	struct passwd *pw;
2331590Srgrimes	struct utmp user;
23487229Smarkm	int r, sflag1;
2351590Srgrimes	char name[UT_NAMESIZE + 1];
2361590Srgrimes
2371590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
23823693Speter		err(1, "%s", _PATH_UTMP);
23927169Scharnier	name[UT_NAMESIZE] = '\0';
2401590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
2411590Srgrimes		if (!user.ut_name[0])
2421590Srgrimes			continue;
2431590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL) {
2441590Srgrimes			bcopy(user.ut_name, name, UT_NAMESIZE);
2451590Srgrimes			if ((pw = getpwnam(name)) == NULL)
2461590Srgrimes				continue;
2475369Sjkh			if (hide(pw))
2485369Sjkh				continue;
2491590Srgrimes			pn = enter_person(pw);
2501590Srgrimes		}
2511590Srgrimes		enter_where(&user, pn);
2521590Srgrimes	}
2531590Srgrimes	if (db && lflag)
25487229Smarkm		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
25523693Speter			PERSON *tmp;
25623693Speter
25787229Smarkm			r = (*db->seq)(db, &key, &data, sflag1);
2581590Srgrimes			if (r == -1)
25923693Speter				err(1, "db seq");
2601590Srgrimes			if (r == 1)
2611590Srgrimes				break;
26223693Speter			memmove(&tmp, data.data, sizeof tmp);
26323693Speter			enter_lastlog(tmp);
2641590Srgrimes		}
2651590Srgrimes}
2661590Srgrimes
2671590Srgrimesstatic void
268102944Sdwmaloneuserlist(int argc, char **argv)
2691590Srgrimes{
27087229Smarkm	PERSON *pn;
2711590Srgrimes	DBT data, key;
2721590Srgrimes	struct utmp user;
2731590Srgrimes	struct passwd *pw;
27487229Smarkm	int r, sflag1, *used, *ip;
2751590Srgrimes	char **ap, **nargv, **np, **p;
27664775Sbrian	FILE *conf_fp;
27764775Sbrian	char conf_alias[LINE_MAX];
27864775Sbrian	char *conf_realname;
27964775Sbrian	int conf_length;
2801590Srgrimes
2811590Srgrimes	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
2821590Srgrimes	    (used = calloc(argc, sizeof(int))) == NULL)
28323693Speter		err(1, NULL);
2841590Srgrimes
2851590Srgrimes	/* Pull out all network requests. */
2861590Srgrimes	for (ap = p = argv, np = nargv; *p; ++p)
2871590Srgrimes		if (index(*p, '@'))
2881590Srgrimes			*np++ = *p;
2891590Srgrimes		else
2901590Srgrimes			*ap++ = *p;
2911590Srgrimes
2921590Srgrimes	*np++ = NULL;
2931590Srgrimes	*ap++ = NULL;
2941590Srgrimes
2951590Srgrimes	if (!*argv)
2961590Srgrimes		goto net;
2971590Srgrimes
2981590Srgrimes	/*
29966563Sbrian	 * Mark any arguments beginning with '/' as invalid so that we
30066563Sbrian	 * don't accidently confuse them with expansions from finger.conf
30166563Sbrian	 */
30266563Sbrian	for (p = argv, ip = used; *p; ++p, ++ip)
30366563Sbrian	    if (**p == '/') {
30466563Sbrian		*ip = 1;
30566563Sbrian		warnx("%s: no such user", *p);
30666563Sbrian	    }
30766563Sbrian
30866563Sbrian	/*
30964775Sbrian	 * Traverse the finger alias configuration file of the form
31064775Sbrian	 * alias:(user|alias), ignoring comment lines beginning '#'.
31164775Sbrian	 */
31264775Sbrian	if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
31364775Sbrian	    while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
31464775Sbrian		conf_length = strlen(conf_alias);
31564775Sbrian		if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
31664775Sbrian		    continue;
31764775Sbrian		conf_alias[conf_length] = '\0';      /* Remove trailing LF */
31864775Sbrian		if ((conf_realname = strchr(conf_alias, ':')) == NULL)
31964775Sbrian		    continue;
32064775Sbrian		*conf_realname = '\0';               /* Replace : with NUL */
32164775Sbrian		for (p = argv; *p; ++p) {
32264775Sbrian		    if (strcmp(*p, conf_alias) == NULL) {
32364775Sbrian			if ((*p = strdup(conf_realname+1)) == NULL) {
32464775Sbrian			    err(1, NULL);
32564775Sbrian			}
32664775Sbrian		    }
32764775Sbrian		}
32864775Sbrian	    }
32964775Sbrian	    (void)fclose(conf_fp);
33064775Sbrian	}
33164775Sbrian
33264775Sbrian	/*
3331590Srgrimes	 * Traverse the list of possible login names and check the login name
33465064Sbrian	 * and real name against the name specified by the user. If the name
33565064Sbrian	 * begins with a '/', try to read the file of that name instead of
33665064Sbrian	 * gathering the traditional finger information.
3371590Srgrimes	 */
3381590Srgrimes	if (mflag)
33966675Sru		for (p = argv, ip = used; *p; ++p, ++ip) {
34066675Sru			if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
34165064Sbrian				if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
34265064Sbrian					enter_person(pw);
34366675Sru				else if (!*ip)
34465064Sbrian					warnx("%s: no such user", *p);
34565064Sbrian			}
34665064Sbrian		}
3471590Srgrimes	else {
34865787Sbrian		while ((pw = getpwent()) != NULL) {
3491590Srgrimes			for (p = argv, ip = used; *p; ++p, ++ip)
35065064Sbrian				if (**p == '/' && *ip != 1
35165787Sbrian				    && show_text("", *p, ""))
35265064Sbrian					*ip = 1;
35365787Sbrian				else if (match(pw, *p) && !hide(pw)) {
3541590Srgrimes					enter_person(pw);
3551590Srgrimes					*ip = 1;
3561590Srgrimes				}
3575369Sjkh		}
3581590Srgrimes		for (p = argv, ip = used; *p; ++p, ++ip)
3591590Srgrimes			if (!*ip)
36027169Scharnier				warnx("%s: no such user", *p);
3611590Srgrimes	}
3621590Srgrimes
3631590Srgrimes	/* Handle network requests. */
3644991Spstnet:	for (p = nargv; *p;) {
3651590Srgrimes		netfinger(*p++);
3664991Spst		if (*p || entries)
3674991Spst		    printf("\n");
3684991Spst	}
3691590Srgrimes
3701590Srgrimes	if (entries == 0)
3711590Srgrimes		return;
3721590Srgrimes
3731590Srgrimes	/*
3741590Srgrimes	 * Scan thru the list of users currently logged in, saving
3751590Srgrimes	 * appropriate data whenever a match occurs.
3761590Srgrimes	 */
3771590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
37823693Speter		err(1, "%s", _PATH_UTMP);
3791590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
3801590Srgrimes		if (!user.ut_name[0])
3811590Srgrimes			continue;
3821590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL)
3831590Srgrimes			continue;
3841590Srgrimes		enter_where(&user, pn);
3851590Srgrimes	}
3861590Srgrimes	if (db)
38787229Smarkm		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
38823693Speter			PERSON *tmp;
38923693Speter
39087229Smarkm			r = (*db->seq)(db, &key, &data, sflag1);
3911590Srgrimes			if (r == -1)
39223693Speter				err(1, "db seq");
3931590Srgrimes			if (r == 1)
3941590Srgrimes				break;
39523693Speter			memmove(&tmp, data.data, sizeof tmp);
39623693Speter			enter_lastlog(tmp);
3971590Srgrimes		}
3981590Srgrimes}
399