finger.c revision 67467
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
531590Srgrimes#ifndef lint
5427169Scharnier#if 0
5523693Speterstatic char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
5627169Scharnier#else
5727169Scharnierstatic const char rcsid[] =
5850477Speter  "$FreeBSD: head/usr.bin/finger/finger.c 67467 2000-10-23 12:18:34Z ru $";
5927169Scharnier#endif
601590Srgrimes#endif /* not lint */
611590Srgrimes
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
761590Srgrimes#include <sys/param.h>
7723693Speter
7823693Speter#include <db.h>
7923693Speter#include <err.h>
8023693Speter#include <errno.h>
811590Srgrimes#include <fcntl.h>
821590Srgrimes#include <pwd.h>
831590Srgrimes#include <stdio.h>
841590Srgrimes#include <stdlib.h>
851590Srgrimes#include <string.h>
8623693Speter#include <time.h>
8723693Speter#include <unistd.h>
8823693Speter#include <utmp.h>
891590Srgrimes#include <db.h>
9011759Sache#include <locale.h>
9164775Sbrian#include <sys/syslimits.h>
9223693Speter
931590Srgrimes#include "finger.h"
9464775Sbrian#include "pathnames.h"
951590Srgrimes
961590SrgrimesDB *db;
971590Srgrimestime_t now;
9814631Solahint entries, lflag, mflag, pplan, sflag, oflag, Tflag;
991590Srgrimeschar tbuf[1024];
1001590Srgrimes
1011590Srgrimesstatic void loginlist __P((void));
10227169Scharnierstatic void usage __P((void));
1031590Srgrimesstatic void userlist __P((int, char **));
1041590Srgrimes
1052589Spstint
1062589Spstoption(argc, argv)
1071590Srgrimes	int argc;
1081590Srgrimes	char **argv;
1091590Srgrimes{
1101590Srgrimes	int ch;
1111590Srgrimes
1122589Spst	optind = 1;		/* reset getopt */
1132537Spst
11424360Simp	while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
1151590Srgrimes		switch(ch) {
1161590Srgrimes		case 'l':
1171590Srgrimes			lflag = 1;		/* long format */
1181590Srgrimes			break;
1191590Srgrimes		case 'm':
1201590Srgrimes			mflag = 1;		/* force exact match of names */
1211590Srgrimes			break;
1221590Srgrimes		case 'p':
1231590Srgrimes			pplan = 1;		/* don't show .plan/.project */
1241590Srgrimes			break;
1251590Srgrimes		case 's':
1261590Srgrimes			sflag = 1;		/* short format */
1271590Srgrimes			break;
1282537Spst		case 'h':
1292537Spst			oflag = 0;		/* remote host info */
1302537Spst			break;
1312537Spst		case 'o':
1322537Spst			oflag = 1;		/* office info */
1332537Spst			break;
13414631Solah		case 'T':
13514631Solah			Tflag = 1;		/* disable T/TCP */
13614631Solah			break;
1371590Srgrimes		case '?':
1381590Srgrimes		default:
13927169Scharnier			usage();
1401590Srgrimes		}
1411590Srgrimes
1422589Spst	return optind;
1432589Spst}
1442589Spst
14527169Scharnierstatic void
14627169Scharnierusage()
14727169Scharnier{
14827169Scharnier	(void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
14927169Scharnier	exit(1);
15027169Scharnier}
15127169Scharnier
15227169Scharnierint
1532589Spstmain(argc, argv)
1542589Spst	int argc;
1552589Spst	char **argv;
1562589Spst{
15727169Scharnier	int envargc, argcnt;
1582589Spst	char *envargv[3];
15946662Sobrien	struct passwd *pw;
1602589Spst
16146662Sobrien	if (getuid() == 0 || geteuid() == 0) {
16246662Sobrien		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
16346662Sobrien			 setgid(pw->pw_gid);
16446662Sobrien			 setuid(pw->pw_uid);
16546662Sobrien		} else {
16646662Sobrien			 setgid(UNPRIV_UGID);
16746662Sobrien			 setuid(UNPRIV_UGID);
16846662Sobrien		}
16946662Sobrien	}
17046662Sobrien
17111811Sache	(void) setlocale(LC_ALL, "");
17211759Sache
1732589Spst				/* remove this line to get remote host */
1742589Spst	oflag = 1;		/* default to old "office" behavior */
1752589Spst
1762589Spst	/*
1772589Spst	 * Process environment variables followed by command line arguments.
1782589Spst	 */
1792589Spst	if ((envargv[1] = getenv("FINGER"))) {
1802589Spst		envargc = 2;
1812589Spst		envargv[0] = "finger";
1822589Spst		envargv[2] = NULL;
1832589Spst		(void) option(envargc, envargv);
1842589Spst	}
1852589Spst
1862589Spst	argcnt = option(argc, argv);
1872589Spst	argc -= argcnt;
1882589Spst	argv += argcnt;
1892589Spst
1901590Srgrimes	(void)time(&now);
1911590Srgrimes	setpassent(1);
1921590Srgrimes	if (!*argv) {
1931590Srgrimes		/*
1941590Srgrimes		 * Assign explicit "small" format if no names given and -l
1951590Srgrimes		 * not selected.  Force the -s BEFORE we get names so proper
1961590Srgrimes		 * screening will be done.
1971590Srgrimes		 */
1981590Srgrimes		if (!lflag)
1991590Srgrimes			sflag = 1;	/* if -l not explicit, force -s */
2001590Srgrimes		loginlist();
2011590Srgrimes		if (entries == 0)
2021590Srgrimes			(void)printf("No one logged on.\n");
2031590Srgrimes	} else {
2041590Srgrimes		userlist(argc, argv);
2051590Srgrimes		/*
2061590Srgrimes		 * Assign explicit "large" format if names given and -s not
2071590Srgrimes		 * explicitly stated.  Force the -l AFTER we get names so any
2081590Srgrimes		 * remote finger attempts specified won't be mishandled.
2091590Srgrimes		 */
2101590Srgrimes		if (!sflag)
2111590Srgrimes			lflag = 1;	/* if -s not explicit, force -l */
2121590Srgrimes	}
21348566Sbillf	if (entries) {
2141590Srgrimes		if (lflag)
2151590Srgrimes			lflag_print();
2161590Srgrimes		else
2171590Srgrimes			sflag_print();
21848566Sbillf	}
21923693Speter	return (0);
2201590Srgrimes}
2211590Srgrimes
2221590Srgrimesstatic void
2231590Srgrimesloginlist()
2241590Srgrimes{
2251590Srgrimes	register PERSON *pn;
2261590Srgrimes	DBT data, key;
2271590Srgrimes	struct passwd *pw;
2281590Srgrimes	struct utmp user;
2291590Srgrimes	int r, sflag;
2301590Srgrimes	char name[UT_NAMESIZE + 1];
2311590Srgrimes
2321590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
23323693Speter		err(1, "%s", _PATH_UTMP);
23427169Scharnier	name[UT_NAMESIZE] = '\0';
2351590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
2361590Srgrimes		if (!user.ut_name[0])
2371590Srgrimes			continue;
2381590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL) {
2391590Srgrimes			bcopy(user.ut_name, name, UT_NAMESIZE);
2401590Srgrimes			if ((pw = getpwnam(name)) == NULL)
2411590Srgrimes				continue;
2425369Sjkh			if (hide(pw))
2435369Sjkh				continue;
2441590Srgrimes			pn = enter_person(pw);
2451590Srgrimes		}
2461590Srgrimes		enter_where(&user, pn);
2471590Srgrimes	}
2481590Srgrimes	if (db && lflag)
2491590Srgrimes		for (sflag = R_FIRST;; sflag = R_NEXT) {
25023693Speter			PERSON *tmp;
25123693Speter
2521590Srgrimes			r = (*db->seq)(db, &key, &data, sflag);
2531590Srgrimes			if (r == -1)
25423693Speter				err(1, "db seq");
2551590Srgrimes			if (r == 1)
2561590Srgrimes				break;
25723693Speter			memmove(&tmp, data.data, sizeof tmp);
25823693Speter			enter_lastlog(tmp);
2591590Srgrimes		}
2601590Srgrimes}
2611590Srgrimes
2621590Srgrimesstatic void
2631590Srgrimesuserlist(argc, argv)
2641590Srgrimes	register int argc;
2651590Srgrimes	register char **argv;
2661590Srgrimes{
2671590Srgrimes	register PERSON *pn;
2681590Srgrimes	DBT data, key;
2691590Srgrimes	struct utmp user;
2701590Srgrimes	struct passwd *pw;
2711590Srgrimes	int r, sflag, *used, *ip;
2721590Srgrimes	char **ap, **nargv, **np, **p;
27364775Sbrian	FILE *conf_fp;
27464775Sbrian	char conf_alias[LINE_MAX];
27564775Sbrian	char *conf_realname;
27664775Sbrian	int conf_length;
2771590Srgrimes
2781590Srgrimes	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
2791590Srgrimes	    (used = calloc(argc, sizeof(int))) == NULL)
28023693Speter		err(1, NULL);
2811590Srgrimes
2821590Srgrimes	/* Pull out all network requests. */
2831590Srgrimes	for (ap = p = argv, np = nargv; *p; ++p)
2841590Srgrimes		if (index(*p, '@'))
2851590Srgrimes			*np++ = *p;
2861590Srgrimes		else
2871590Srgrimes			*ap++ = *p;
2881590Srgrimes
2891590Srgrimes	*np++ = NULL;
2901590Srgrimes	*ap++ = NULL;
2911590Srgrimes
2921590Srgrimes	if (!*argv)
2931590Srgrimes		goto net;
2941590Srgrimes
2951590Srgrimes	/*
29666563Sbrian	 * Mark any arguments beginning with '/' as invalid so that we
29766563Sbrian	 * don't accidently confuse them with expansions from finger.conf
29866563Sbrian	 */
29966563Sbrian	for (p = argv, ip = used; *p; ++p, ++ip)
30066563Sbrian	    if (**p == '/') {
30166563Sbrian		*ip = 1;
30266563Sbrian		warnx("%s: no such user", *p);
30366563Sbrian	    }
30466563Sbrian
30566563Sbrian	/*
30664775Sbrian	 * Traverse the finger alias configuration file of the form
30764775Sbrian	 * alias:(user|alias), ignoring comment lines beginning '#'.
30864775Sbrian	 */
30964775Sbrian	if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
31064775Sbrian	    while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
31164775Sbrian		conf_length = strlen(conf_alias);
31264775Sbrian		if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
31364775Sbrian		    continue;
31464775Sbrian		conf_alias[conf_length] = '\0';      /* Remove trailing LF */
31564775Sbrian		if ((conf_realname = strchr(conf_alias, ':')) == NULL)
31664775Sbrian		    continue;
31764775Sbrian		*conf_realname = '\0';               /* Replace : with NUL */
31864775Sbrian		for (p = argv; *p; ++p) {
31964775Sbrian		    if (strcmp(*p, conf_alias) == NULL) {
32064775Sbrian			if ((*p = strdup(conf_realname+1)) == NULL) {
32164775Sbrian			    err(1, NULL);
32264775Sbrian			}
32364775Sbrian		    }
32464775Sbrian		}
32564775Sbrian	    }
32664775Sbrian	    (void)fclose(conf_fp);
32764775Sbrian	}
32864775Sbrian
32964775Sbrian	/*
3301590Srgrimes	 * Traverse the list of possible login names and check the login name
33165064Sbrian	 * and real name against the name specified by the user. If the name
33265064Sbrian	 * begins with a '/', try to read the file of that name instead of
33365064Sbrian	 * gathering the traditional finger information.
3341590Srgrimes	 */
3351590Srgrimes	if (mflag)
33666675Sru		for (p = argv, ip = used; *p; ++p, ++ip) {
33766675Sru			if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
33865064Sbrian				if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
33965064Sbrian					enter_person(pw);
34066675Sru				else if (!*ip)
34165064Sbrian					warnx("%s: no such user", *p);
34265064Sbrian			}
34365064Sbrian		}
3441590Srgrimes	else {
34565787Sbrian		while ((pw = getpwent()) != NULL) {
3461590Srgrimes			for (p = argv, ip = used; *p; ++p, ++ip)
34765064Sbrian				if (**p == '/' && *ip != 1
34865787Sbrian				    && show_text("", *p, ""))
34965064Sbrian					*ip = 1;
35065787Sbrian				else if (match(pw, *p) && !hide(pw)) {
3511590Srgrimes					enter_person(pw);
3521590Srgrimes					*ip = 1;
3531590Srgrimes				}
3545369Sjkh		}
3551590Srgrimes		for (p = argv, ip = used; *p; ++p, ++ip)
3561590Srgrimes			if (!*ip)
35727169Scharnier				warnx("%s: no such user", *p);
3581590Srgrimes	}
3591590Srgrimes
3601590Srgrimes	/* Handle network requests. */
3614991Spstnet:	for (p = nargv; *p;) {
3621590Srgrimes		netfinger(*p++);
3634991Spst		if (*p || entries)
3644991Spst		    printf("\n");
3654991Spst	}
3661590Srgrimes
3671590Srgrimes	if (entries == 0)
3681590Srgrimes		return;
3691590Srgrimes
3701590Srgrimes	/*
3711590Srgrimes	 * Scan thru the list of users currently logged in, saving
3721590Srgrimes	 * appropriate data whenever a match occurs.
3731590Srgrimes	 */
3741590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
37523693Speter		err(1, "%s", _PATH_UTMP);
3761590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
3771590Srgrimes		if (!user.ut_name[0])
3781590Srgrimes			continue;
3791590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL)
3801590Srgrimes			continue;
3811590Srgrimes		enter_where(&user, pn);
3821590Srgrimes	}
3831590Srgrimes	if (db)
3841590Srgrimes		for (sflag = R_FIRST;; sflag = R_NEXT) {
38523693Speter			PERSON *tmp;
38623693Speter
3871590Srgrimes			r = (*db->seq)(db, &key, &data, sflag);
3881590Srgrimes			if (r == -1)
38923693Speter				err(1, "db seq");
3901590Srgrimes			if (r == 1)
3911590Srgrimes				break;
39223693Speter			memmove(&tmp, data.data, sizeof tmp);
39323693Speter			enter_lastlog(tmp);
3941590Srgrimes		}
3951590Srgrimes}
396