finger.c revision 146466
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 146466 2005-05-21 09:55:10Z ru $");
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;
94117010Sjmallettint entries, gflag, kflag, 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
111117010Sjmallett	while ((ch = getopt(argc, argv, "46gklmpshoT")) != -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;
122117010Sjmallett		case 'k':
123117010Sjmallett			kflag = 1;		/* keep going without utmp */
124117010Sjmallett			break;
1251590Srgrimes		case 'l':
1261590Srgrimes			lflag = 1;		/* long format */
1271590Srgrimes			break;
1281590Srgrimes		case 'm':
1291590Srgrimes			mflag = 1;		/* force exact match of names */
1301590Srgrimes			break;
1311590Srgrimes		case 'p':
1321590Srgrimes			pplan = 1;		/* don't show .plan/.project */
1331590Srgrimes			break;
1341590Srgrimes		case 's':
1351590Srgrimes			sflag = 1;		/* short format */
1361590Srgrimes			break;
1372537Spst		case 'h':
1382537Spst			oflag = 0;		/* remote host info */
1392537Spst			break;
1402537Spst		case 'o':
1412537Spst			oflag = 1;		/* office info */
1422537Spst			break;
14314631Solah		case 'T':
14414631Solah			Tflag = 1;		/* disable T/TCP */
14514631Solah			break;
1461590Srgrimes		case '?':
1471590Srgrimes		default:
14827169Scharnier			usage();
1491590Srgrimes		}
1501590Srgrimes
1512589Spst	return optind;
1522589Spst}
1532589Spst
15427169Scharnierstatic void
155102944Sdwmaloneusage(void)
15627169Scharnier{
157146466Sru	(void)fprintf(stderr,
158146466Sru	    "usage: finger [-46gklmpshoT] [user ...] [user@host ...]\n");
15927169Scharnier	exit(1);
16027169Scharnier}
16127169Scharnier
16227169Scharnierint
163102944Sdwmalonemain(int argc, char **argv)
1642589Spst{
16527169Scharnier	int envargc, argcnt;
1662589Spst	char *envargv[3];
16746662Sobrien	struct passwd *pw;
16887229Smarkm	static char myname[] = "finger";
1692589Spst
17046662Sobrien	if (getuid() == 0 || geteuid() == 0) {
17146662Sobrien		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
17246662Sobrien			 setgid(pw->pw_gid);
17346662Sobrien			 setuid(pw->pw_uid);
17446662Sobrien		} else {
17546662Sobrien			 setgid(UNPRIV_UGID);
17646662Sobrien			 setuid(UNPRIV_UGID);
17746662Sobrien		}
17846662Sobrien	}
17946662Sobrien
18011811Sache	(void) setlocale(LC_ALL, "");
18111759Sache
1822589Spst				/* remove this line to get remote host */
1832589Spst	oflag = 1;		/* default to old "office" behavior */
1842589Spst
1852589Spst	/*
1862589Spst	 * Process environment variables followed by command line arguments.
1872589Spst	 */
1882589Spst	if ((envargv[1] = getenv("FINGER"))) {
1892589Spst		envargc = 2;
19087229Smarkm		envargv[0] = myname;
1912589Spst		envargv[2] = NULL;
1922589Spst		(void) option(envargc, envargv);
1932589Spst	}
1942589Spst
1952589Spst	argcnt = option(argc, argv);
1962589Spst	argc -= argcnt;
1972589Spst	argv += argcnt;
1982589Spst
1991590Srgrimes	(void)time(&now);
2001590Srgrimes	setpassent(1);
2011590Srgrimes	if (!*argv) {
2021590Srgrimes		/*
2031590Srgrimes		 * Assign explicit "small" format if no names given and -l
2041590Srgrimes		 * not selected.  Force the -s BEFORE we get names so proper
2051590Srgrimes		 * screening will be done.
2061590Srgrimes		 */
2071590Srgrimes		if (!lflag)
2081590Srgrimes			sflag = 1;	/* if -l not explicit, force -s */
2091590Srgrimes		loginlist();
2101590Srgrimes		if (entries == 0)
2111590Srgrimes			(void)printf("No one logged on.\n");
2121590Srgrimes	} else {
2131590Srgrimes		userlist(argc, argv);
2141590Srgrimes		/*
2151590Srgrimes		 * Assign explicit "large" format if names given and -s not
2161590Srgrimes		 * explicitly stated.  Force the -l AFTER we get names so any
2171590Srgrimes		 * remote finger attempts specified won't be mishandled.
2181590Srgrimes		 */
2191590Srgrimes		if (!sflag)
2201590Srgrimes			lflag = 1;	/* if -s not explicit, force -l */
2211590Srgrimes	}
22248566Sbillf	if (entries) {
2231590Srgrimes		if (lflag)
2241590Srgrimes			lflag_print();
2251590Srgrimes		else
2261590Srgrimes			sflag_print();
22748566Sbillf	}
22823693Speter	return (0);
2291590Srgrimes}
2301590Srgrimes
2311590Srgrimesstatic void
232102944Sdwmaloneloginlist(void)
2331590Srgrimes{
23487229Smarkm	PERSON *pn;
2351590Srgrimes	DBT data, key;
2361590Srgrimes	struct passwd *pw;
2371590Srgrimes	struct utmp user;
23887229Smarkm	int r, sflag1;
2391590Srgrimes	char name[UT_NAMESIZE + 1];
2401590Srgrimes
241117010Sjmallett	if (kflag)
242117010Sjmallett		errx(1, "can't list logins without reading utmp");
243117010Sjmallett
2441590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
24523693Speter		err(1, "%s", _PATH_UTMP);
24627169Scharnier	name[UT_NAMESIZE] = '\0';
2471590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
2481590Srgrimes		if (!user.ut_name[0])
2491590Srgrimes			continue;
2501590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL) {
2511590Srgrimes			bcopy(user.ut_name, name, UT_NAMESIZE);
2521590Srgrimes			if ((pw = getpwnam(name)) == NULL)
2531590Srgrimes				continue;
2545369Sjkh			if (hide(pw))
2555369Sjkh				continue;
2561590Srgrimes			pn = enter_person(pw);
2571590Srgrimes		}
2581590Srgrimes		enter_where(&user, pn);
2591590Srgrimes	}
2601590Srgrimes	if (db && lflag)
26187229Smarkm		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
26223693Speter			PERSON *tmp;
26323693Speter
26487229Smarkm			r = (*db->seq)(db, &key, &data, sflag1);
2651590Srgrimes			if (r == -1)
26623693Speter				err(1, "db seq");
2671590Srgrimes			if (r == 1)
2681590Srgrimes				break;
26923693Speter			memmove(&tmp, data.data, sizeof tmp);
27023693Speter			enter_lastlog(tmp);
2711590Srgrimes		}
2721590Srgrimes}
2731590Srgrimes
2741590Srgrimesstatic void
275102944Sdwmaloneuserlist(int argc, char **argv)
2761590Srgrimes{
27787229Smarkm	PERSON *pn;
2781590Srgrimes	DBT data, key;
2791590Srgrimes	struct utmp user;
2801590Srgrimes	struct passwd *pw;
28187229Smarkm	int r, sflag1, *used, *ip;
2821590Srgrimes	char **ap, **nargv, **np, **p;
28364775Sbrian	FILE *conf_fp;
28464775Sbrian	char conf_alias[LINE_MAX];
28564775Sbrian	char *conf_realname;
28664775Sbrian	int conf_length;
2871590Srgrimes
2881590Srgrimes	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
2891590Srgrimes	    (used = calloc(argc, sizeof(int))) == NULL)
29023693Speter		err(1, NULL);
2911590Srgrimes
2921590Srgrimes	/* Pull out all network requests. */
2931590Srgrimes	for (ap = p = argv, np = nargv; *p; ++p)
2941590Srgrimes		if (index(*p, '@'))
2951590Srgrimes			*np++ = *p;
2961590Srgrimes		else
2971590Srgrimes			*ap++ = *p;
2981590Srgrimes
2991590Srgrimes	*np++ = NULL;
3001590Srgrimes	*ap++ = NULL;
3011590Srgrimes
3021590Srgrimes	if (!*argv)
3031590Srgrimes		goto net;
3041590Srgrimes
3051590Srgrimes	/*
30666563Sbrian	 * Mark any arguments beginning with '/' as invalid so that we
30766563Sbrian	 * don't accidently confuse them with expansions from finger.conf
30866563Sbrian	 */
30966563Sbrian	for (p = argv, ip = used; *p; ++p, ++ip)
31066563Sbrian	    if (**p == '/') {
31166563Sbrian		*ip = 1;
31266563Sbrian		warnx("%s: no such user", *p);
31366563Sbrian	    }
31466563Sbrian
31566563Sbrian	/*
31664775Sbrian	 * Traverse the finger alias configuration file of the form
31764775Sbrian	 * alias:(user|alias), ignoring comment lines beginning '#'.
31864775Sbrian	 */
31964775Sbrian	if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
32064775Sbrian	    while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
32164775Sbrian		conf_length = strlen(conf_alias);
32264775Sbrian		if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
32364775Sbrian		    continue;
32464775Sbrian		conf_alias[conf_length] = '\0';      /* Remove trailing LF */
32564775Sbrian		if ((conf_realname = strchr(conf_alias, ':')) == NULL)
32664775Sbrian		    continue;
32764775Sbrian		*conf_realname = '\0';               /* Replace : with NUL */
32864775Sbrian		for (p = argv; *p; ++p) {
329126960Sbde		    if (strcmp(*p, conf_alias) == 0) {
33064775Sbrian			if ((*p = strdup(conf_realname+1)) == NULL) {
33164775Sbrian			    err(1, NULL);
33264775Sbrian			}
33364775Sbrian		    }
33464775Sbrian		}
33564775Sbrian	    }
33664775Sbrian	    (void)fclose(conf_fp);
33764775Sbrian	}
33864775Sbrian
33964775Sbrian	/*
3401590Srgrimes	 * Traverse the list of possible login names and check the login name
34165064Sbrian	 * and real name against the name specified by the user. If the name
34265064Sbrian	 * begins with a '/', try to read the file of that name instead of
34365064Sbrian	 * gathering the traditional finger information.
3441590Srgrimes	 */
3451590Srgrimes	if (mflag)
34666675Sru		for (p = argv, ip = used; *p; ++p, ++ip) {
34766675Sru			if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
34865064Sbrian				if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
34965064Sbrian					enter_person(pw);
35066675Sru				else if (!*ip)
35165064Sbrian					warnx("%s: no such user", *p);
35265064Sbrian			}
35365064Sbrian		}
3541590Srgrimes	else {
35565787Sbrian		while ((pw = getpwent()) != NULL) {
3561590Srgrimes			for (p = argv, ip = used; *p; ++p, ++ip)
35765064Sbrian				if (**p == '/' && *ip != 1
35865787Sbrian				    && show_text("", *p, ""))
35965064Sbrian					*ip = 1;
36065787Sbrian				else if (match(pw, *p) && !hide(pw)) {
3611590Srgrimes					enter_person(pw);
3621590Srgrimes					*ip = 1;
3631590Srgrimes				}
3645369Sjkh		}
3651590Srgrimes		for (p = argv, ip = used; *p; ++p, ++ip)
3661590Srgrimes			if (!*ip)
36727169Scharnier				warnx("%s: no such user", *p);
3681590Srgrimes	}
3691590Srgrimes
3701590Srgrimes	/* Handle network requests. */
3714991Spstnet:	for (p = nargv; *p;) {
3721590Srgrimes		netfinger(*p++);
3734991Spst		if (*p || entries)
3744991Spst		    printf("\n");
3754991Spst	}
3761590Srgrimes
3771590Srgrimes	if (entries == 0)
3781590Srgrimes		return;
3791590Srgrimes
380117010Sjmallett	if (kflag)
381117010Sjmallett		return;
382117010Sjmallett
3831590Srgrimes	/*
3841590Srgrimes	 * Scan thru the list of users currently logged in, saving
3851590Srgrimes	 * appropriate data whenever a match occurs.
3861590Srgrimes	 */
3871590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
38823693Speter		err(1, "%s", _PATH_UTMP);
3891590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
3901590Srgrimes		if (!user.ut_name[0])
3911590Srgrimes			continue;
3921590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL)
3931590Srgrimes			continue;
3941590Srgrimes		enter_where(&user, pn);
3951590Srgrimes	}
3961590Srgrimes	if (db)
39787229Smarkm		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
39823693Speter			PERSON *tmp;
39923693Speter
40087229Smarkm			r = (*db->seq)(db, &key, &data, sflag1);
4011590Srgrimes			if (r == -1)
40223693Speter				err(1, "db seq");
4031590Srgrimes			if (r == 1)
4041590Srgrimes				break;
40523693Speter			memmove(&tmp, data.data, sizeof tmp);
40623693Speter			enter_lastlog(tmp);
4071590Srgrimes		}
4081590Srgrimes}
409