finger.c revision 27169
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
481590Srgrimesstatic 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[] =
5827169Scharnier	"$Id$";
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>
9123693Speter
921590Srgrimes#include "finger.h"
931590Srgrimes
941590SrgrimesDB *db;
951590Srgrimestime_t now;
9614631Solahint entries, lflag, mflag, pplan, sflag, oflag, Tflag;
971590Srgrimeschar tbuf[1024];
981590Srgrimes
991590Srgrimesstatic void loginlist __P((void));
10027169Scharnierstatic void usage __P((void));
1011590Srgrimesstatic void userlist __P((int, char **));
1021590Srgrimes
1032589Spstint
1042589Spstoption(argc, argv)
1051590Srgrimes	int argc;
1061590Srgrimes	char **argv;
1071590Srgrimes{
1081590Srgrimes	int ch;
1091590Srgrimes
1102589Spst	optind = 1;		/* reset getopt */
1112537Spst
11224360Simp	while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
1131590Srgrimes		switch(ch) {
1141590Srgrimes		case 'l':
1151590Srgrimes			lflag = 1;		/* long format */
1161590Srgrimes			break;
1171590Srgrimes		case 'm':
1181590Srgrimes			mflag = 1;		/* force exact match of names */
1191590Srgrimes			break;
1201590Srgrimes		case 'p':
1211590Srgrimes			pplan = 1;		/* don't show .plan/.project */
1221590Srgrimes			break;
1231590Srgrimes		case 's':
1241590Srgrimes			sflag = 1;		/* short format */
1251590Srgrimes			break;
1262537Spst		case 'h':
1272537Spst			oflag = 0;		/* remote host info */
1282537Spst			break;
1292537Spst		case 'o':
1302537Spst			oflag = 1;		/* office info */
1312537Spst			break;
13214631Solah		case 'T':
13314631Solah			Tflag = 1;		/* disable T/TCP */
13414631Solah			break;
1351590Srgrimes		case '?':
1361590Srgrimes		default:
13727169Scharnier			usage();
1381590Srgrimes		}
1391590Srgrimes
1402589Spst	return optind;
1412589Spst}
1422589Spst
14327169Scharnierstatic void
14427169Scharnierusage()
14527169Scharnier{
14627169Scharnier	(void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
14727169Scharnier	exit(1);
14827169Scharnier}
14927169Scharnier
15027169Scharnierint
1512589Spstmain(argc, argv)
1522589Spst	int argc;
1532589Spst	char **argv;
1542589Spst{
15527169Scharnier	int envargc, argcnt;
1562589Spst	char *envargv[3];
1572589Spst
15811811Sache	(void) setlocale(LC_ALL, "");
15911759Sache
1602589Spst				/* remove this line to get remote host */
1612589Spst	oflag = 1;		/* default to old "office" behavior */
1622589Spst
1632589Spst	/*
1642589Spst	 * Process environment variables followed by command line arguments.
1652589Spst	 */
1662589Spst	if ((envargv[1] = getenv("FINGER"))) {
1672589Spst		envargc = 2;
1682589Spst		envargv[0] = "finger";
1692589Spst		envargv[2] = NULL;
1702589Spst		(void) option(envargc, envargv);
1712589Spst	}
1722589Spst
1732589Spst	argcnt = option(argc, argv);
1742589Spst	argc -= argcnt;
1752589Spst	argv += argcnt;
1762589Spst
1771590Srgrimes	(void)time(&now);
1781590Srgrimes	setpassent(1);
1791590Srgrimes	if (!*argv) {
1801590Srgrimes		/*
1811590Srgrimes		 * Assign explicit "small" format if no names given and -l
1821590Srgrimes		 * not selected.  Force the -s BEFORE we get names so proper
1831590Srgrimes		 * screening will be done.
1841590Srgrimes		 */
1851590Srgrimes		if (!lflag)
1861590Srgrimes			sflag = 1;	/* if -l not explicit, force -s */
1871590Srgrimes		loginlist();
1881590Srgrimes		if (entries == 0)
1891590Srgrimes			(void)printf("No one logged on.\n");
1901590Srgrimes	} else {
1911590Srgrimes		userlist(argc, argv);
1921590Srgrimes		/*
1931590Srgrimes		 * Assign explicit "large" format if names given and -s not
1941590Srgrimes		 * explicitly stated.  Force the -l AFTER we get names so any
1951590Srgrimes		 * remote finger attempts specified won't be mishandled.
1961590Srgrimes		 */
1971590Srgrimes		if (!sflag)
1981590Srgrimes			lflag = 1;	/* if -s not explicit, force -l */
1991590Srgrimes	}
2001590Srgrimes	if (entries)
2011590Srgrimes		if (lflag)
2021590Srgrimes			lflag_print();
2031590Srgrimes		else
2041590Srgrimes			sflag_print();
20523693Speter	return (0);
2061590Srgrimes}
2071590Srgrimes
2081590Srgrimesstatic void
2091590Srgrimesloginlist()
2101590Srgrimes{
2111590Srgrimes	register PERSON *pn;
2121590Srgrimes	DBT data, key;
2131590Srgrimes	struct passwd *pw;
2141590Srgrimes	struct utmp user;
2151590Srgrimes	int r, sflag;
2161590Srgrimes	char name[UT_NAMESIZE + 1];
2171590Srgrimes
2181590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
21923693Speter		err(1, "%s", _PATH_UTMP);
22027169Scharnier	name[UT_NAMESIZE] = '\0';
2211590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
2221590Srgrimes		if (!user.ut_name[0])
2231590Srgrimes			continue;
2241590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL) {
2251590Srgrimes			bcopy(user.ut_name, name, UT_NAMESIZE);
2261590Srgrimes			if ((pw = getpwnam(name)) == NULL)
2271590Srgrimes				continue;
2285369Sjkh			if (hide(pw))
2295369Sjkh				continue;
2301590Srgrimes			pn = enter_person(pw);
2311590Srgrimes		}
2321590Srgrimes		enter_where(&user, pn);
2331590Srgrimes	}
2341590Srgrimes	if (db && lflag)
2351590Srgrimes		for (sflag = R_FIRST;; sflag = R_NEXT) {
23623693Speter			PERSON *tmp;
23723693Speter
2381590Srgrimes			r = (*db->seq)(db, &key, &data, sflag);
2391590Srgrimes			if (r == -1)
24023693Speter				err(1, "db seq");
2411590Srgrimes			if (r == 1)
2421590Srgrimes				break;
24323693Speter			memmove(&tmp, data.data, sizeof tmp);
24423693Speter			enter_lastlog(tmp);
2451590Srgrimes		}
2461590Srgrimes}
2471590Srgrimes
2481590Srgrimesstatic void
2491590Srgrimesuserlist(argc, argv)
2501590Srgrimes	register int argc;
2511590Srgrimes	register char **argv;
2521590Srgrimes{
2531590Srgrimes	register PERSON *pn;
2541590Srgrimes	DBT data, key;
2551590Srgrimes	struct utmp user;
2561590Srgrimes	struct passwd *pw;
2571590Srgrimes	int r, sflag, *used, *ip;
2581590Srgrimes	char **ap, **nargv, **np, **p;
2591590Srgrimes
2601590Srgrimes	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
2611590Srgrimes	    (used = calloc(argc, sizeof(int))) == NULL)
26223693Speter		err(1, NULL);
2631590Srgrimes
2641590Srgrimes	/* Pull out all network requests. */
2651590Srgrimes	for (ap = p = argv, np = nargv; *p; ++p)
2661590Srgrimes		if (index(*p, '@'))
2671590Srgrimes			*np++ = *p;
2681590Srgrimes		else
2691590Srgrimes			*ap++ = *p;
2701590Srgrimes
2711590Srgrimes	*np++ = NULL;
2721590Srgrimes	*ap++ = NULL;
2731590Srgrimes
2741590Srgrimes	if (!*argv)
2751590Srgrimes		goto net;
2761590Srgrimes
2771590Srgrimes	/*
2781590Srgrimes	 * Traverse the list of possible login names and check the login name
2791590Srgrimes	 * and real name against the name specified by the user.
2801590Srgrimes	 */
2811590Srgrimes	if (mflag)
2821590Srgrimes		for (p = argv; *p; ++p)
28323693Speter			if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
2841590Srgrimes				enter_person(pw);
2851590Srgrimes			else
28627169Scharnier				warnx("%s: no such user", *p);
2871590Srgrimes	else {
28823693Speter		while ((pw = getpwent()) != NULL) {
2891590Srgrimes			for (p = argv, ip = used; *p; ++p, ++ip)
2909280Sache				if (match(pw, *p) && !hide(pw)) {
2911590Srgrimes					enter_person(pw);
2921590Srgrimes					*ip = 1;
2931590Srgrimes				}
2945369Sjkh		}
2951590Srgrimes		for (p = argv, ip = used; *p; ++p, ++ip)
2961590Srgrimes			if (!*ip)
29727169Scharnier				warnx("%s: no such user", *p);
2981590Srgrimes	}
2991590Srgrimes
3001590Srgrimes	/* Handle network requests. */
3014991Spstnet:	for (p = nargv; *p;) {
3021590Srgrimes		netfinger(*p++);
3034991Spst		if (*p || entries)
3044991Spst		    printf("\n");
3054991Spst	}
3061590Srgrimes
3071590Srgrimes	if (entries == 0)
3081590Srgrimes		return;
3091590Srgrimes
3101590Srgrimes	/*
3111590Srgrimes	 * Scan thru the list of users currently logged in, saving
3121590Srgrimes	 * appropriate data whenever a match occurs.
3131590Srgrimes	 */
3141590Srgrimes	if (!freopen(_PATH_UTMP, "r", stdin))
31523693Speter		err(1, "%s", _PATH_UTMP);
3161590Srgrimes	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
3171590Srgrimes		if (!user.ut_name[0])
3181590Srgrimes			continue;
3191590Srgrimes		if ((pn = find_person(user.ut_name)) == NULL)
3201590Srgrimes			continue;
3211590Srgrimes		enter_where(&user, pn);
3221590Srgrimes	}
3231590Srgrimes	if (db)
3241590Srgrimes		for (sflag = R_FIRST;; sflag = R_NEXT) {
32523693Speter			PERSON *tmp;
32623693Speter
3271590Srgrimes			r = (*db->seq)(db, &key, &data, sflag);
3281590Srgrimes			if (r == -1)
32923693Speter				err(1, "db seq");
3301590Srgrimes			if (r == 1)
3311590Srgrimes				break;
33223693Speter			memmove(&tmp, data.data, sizeof tmp);
33323693Speter			enter_lastlog(tmp);
3341590Srgrimes		}
3351590Srgrimes}
336