finger.c revision 87628
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/*
38 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
39 *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
40 *	Unread since ...".)
41 *    - 4 digit phone extensions (3210 is printed as x3210.)
42 *    - host/office toggling in short format with -h & -o.
43 *    - short day names (`Tue' printed instead of `Jun 21' if the
44 *	login time is < 6 days.
45 */
46
47#ifndef lint
48static const char copyright[] =
49"@(#) Copyright (c) 1989, 1993\n\
50	The Regents of the University of California.  All rights reserved.\n";
51#endif /* not lint */
52
53#if 0
54#ifndef lint
55static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
56#endif
57#endif
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/usr.bin/finger/finger.c 87628 2001-12-10 21:13:08Z dwmalone $");
61
62/*
63 * Finger prints out information about users.  It is not portable since
64 * certain fields (e.g. the full user name, office, and phone numbers) are
65 * extracted from the gecos field of the passwd file which other UNIXes
66 * may not have or may use for other things.
67 *
68 * There are currently two output formats; the short format is one line
69 * per user and displays login name, tty, login time, real name, idle time,
70 * and either remote host information (default) or office location/phone
71 * number, depending on if -h or -o is used respectively.
72 * The long format gives the same information (in a more legible format) as
73 * well as home directory, shell, mail info, and .plan/.project files.
74 */
75
76#include <db.h>
77#include <err.h>
78#include <pwd.h>
79#include <stdio.h>
80#include <stdlib.h>
81#include <string.h>
82#include <time.h>
83#include <unistd.h>
84#include <utmp.h>
85#include <locale.h>
86
87#include "finger.h"
88#include "pathnames.h"
89
90DB *db;
91time_t now;
92int entries, lflag, mflag, pplan, sflag, oflag, Tflag;
93int d_first = -1;
94char tbuf[1024];
95
96static void loginlist __P((void));
97static int option __P((int, char **));
98static void usage __P((void));
99static void userlist __P((int, char **));
100
101static int
102option(argc, argv)
103	int argc;
104	char **argv;
105{
106	int ch;
107
108	optind = 1;		/* reset getopt */
109
110	while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
111		switch(ch) {
112		case 'l':
113			lflag = 1;		/* long format */
114			break;
115		case 'm':
116			mflag = 1;		/* force exact match of names */
117			break;
118		case 'p':
119			pplan = 1;		/* don't show .plan/.project */
120			break;
121		case 's':
122			sflag = 1;		/* short format */
123			break;
124		case 'h':
125			oflag = 0;		/* remote host info */
126			break;
127		case 'o':
128			oflag = 1;		/* office info */
129			break;
130		case 'T':
131			Tflag = 1;		/* disable T/TCP */
132			break;
133		case '?':
134		default:
135			usage();
136		}
137
138	return optind;
139}
140
141static void
142usage()
143{
144	(void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
145	exit(1);
146}
147
148int
149main(argc, argv)
150	int argc;
151	char **argv;
152{
153	int envargc, argcnt;
154	char *envargv[3];
155	struct passwd *pw;
156	static char myname[] = "finger";
157
158	if (getuid() == 0 || geteuid() == 0) {
159		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
160			 setgid(pw->pw_gid);
161			 setuid(pw->pw_uid);
162		} else {
163			 setgid(UNPRIV_UGID);
164			 setuid(UNPRIV_UGID);
165		}
166	}
167
168	(void) setlocale(LC_ALL, "");
169
170				/* remove this line to get remote host */
171	oflag = 1;		/* default to old "office" behavior */
172
173	/*
174	 * Process environment variables followed by command line arguments.
175	 */
176	if ((envargv[1] = getenv("FINGER"))) {
177		envargc = 2;
178		envargv[0] = myname;
179		envargv[2] = NULL;
180		(void) option(envargc, envargv);
181	}
182
183	argcnt = option(argc, argv);
184	argc -= argcnt;
185	argv += argcnt;
186
187	(void)time(&now);
188	setpassent(1);
189	if (!*argv) {
190		/*
191		 * Assign explicit "small" format if no names given and -l
192		 * not selected.  Force the -s BEFORE we get names so proper
193		 * screening will be done.
194		 */
195		if (!lflag)
196			sflag = 1;	/* if -l not explicit, force -s */
197		loginlist();
198		if (entries == 0)
199			(void)printf("No one logged on.\n");
200	} else {
201		userlist(argc, argv);
202		/*
203		 * Assign explicit "large" format if names given and -s not
204		 * explicitly stated.  Force the -l AFTER we get names so any
205		 * remote finger attempts specified won't be mishandled.
206		 */
207		if (!sflag)
208			lflag = 1;	/* if -s not explicit, force -l */
209	}
210	if (entries) {
211		if (lflag)
212			lflag_print();
213		else
214			sflag_print();
215	}
216	return (0);
217}
218
219static void
220loginlist()
221{
222	PERSON *pn;
223	DBT data, key;
224	struct passwd *pw;
225	struct utmp user;
226	int r, sflag1;
227	char name[UT_NAMESIZE + 1];
228
229	if (!freopen(_PATH_UTMP, "r", stdin))
230		err(1, "%s", _PATH_UTMP);
231	name[UT_NAMESIZE] = '\0';
232	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
233		if (!user.ut_name[0])
234			continue;
235		if ((pn = find_person(user.ut_name)) == NULL) {
236			bcopy(user.ut_name, name, UT_NAMESIZE);
237			if ((pw = getpwnam(name)) == NULL)
238				continue;
239			if (hide(pw))
240				continue;
241			pn = enter_person(pw);
242		}
243		enter_where(&user, pn);
244	}
245	if (db && lflag)
246		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
247			PERSON *tmp;
248
249			r = (*db->seq)(db, &key, &data, sflag1);
250			if (r == -1)
251				err(1, "db seq");
252			if (r == 1)
253				break;
254			memmove(&tmp, data.data, sizeof tmp);
255			enter_lastlog(tmp);
256		}
257}
258
259static void
260userlist(argc, argv)
261	int argc;
262	char **argv;
263{
264	PERSON *pn;
265	DBT data, key;
266	struct utmp user;
267	struct passwd *pw;
268	int r, sflag1, *used, *ip;
269	char **ap, **nargv, **np, **p;
270	FILE *conf_fp;
271	char conf_alias[LINE_MAX];
272	char *conf_realname;
273	int conf_length;
274
275	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
276	    (used = calloc(argc, sizeof(int))) == NULL)
277		err(1, NULL);
278
279	/* Pull out all network requests. */
280	for (ap = p = argv, np = nargv; *p; ++p)
281		if (index(*p, '@'))
282			*np++ = *p;
283		else
284			*ap++ = *p;
285
286	*np++ = NULL;
287	*ap++ = NULL;
288
289	if (!*argv)
290		goto net;
291
292	/*
293	 * Mark any arguments beginning with '/' as invalid so that we
294	 * don't accidently confuse them with expansions from finger.conf
295	 */
296	for (p = argv, ip = used; *p; ++p, ++ip)
297	    if (**p == '/') {
298		*ip = 1;
299		warnx("%s: no such user", *p);
300	    }
301
302	/*
303	 * Traverse the finger alias configuration file of the form
304	 * alias:(user|alias), ignoring comment lines beginning '#'.
305	 */
306	if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
307	    while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
308		conf_length = strlen(conf_alias);
309		if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
310		    continue;
311		conf_alias[conf_length] = '\0';      /* Remove trailing LF */
312		if ((conf_realname = strchr(conf_alias, ':')) == NULL)
313		    continue;
314		*conf_realname = '\0';               /* Replace : with NUL */
315		for (p = argv; *p; ++p) {
316		    if (strcmp(*p, conf_alias) == NULL) {
317			if ((*p = strdup(conf_realname+1)) == NULL) {
318			    err(1, NULL);
319			}
320		    }
321		}
322	    }
323	    (void)fclose(conf_fp);
324	}
325
326	/*
327	 * Traverse the list of possible login names and check the login name
328	 * and real name against the name specified by the user. If the name
329	 * begins with a '/', try to read the file of that name instead of
330	 * gathering the traditional finger information.
331	 */
332	if (mflag)
333		for (p = argv, ip = used; *p; ++p, ++ip) {
334			if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
335				if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
336					enter_person(pw);
337				else if (!*ip)
338					warnx("%s: no such user", *p);
339			}
340		}
341	else {
342		while ((pw = getpwent()) != NULL) {
343			for (p = argv, ip = used; *p; ++p, ++ip)
344				if (**p == '/' && *ip != 1
345				    && show_text("", *p, ""))
346					*ip = 1;
347				else if (match(pw, *p) && !hide(pw)) {
348					enter_person(pw);
349					*ip = 1;
350				}
351		}
352		for (p = argv, ip = used; *p; ++p, ++ip)
353			if (!*ip)
354				warnx("%s: no such user", *p);
355	}
356
357	/* Handle network requests. */
358net:	for (p = nargv; *p;) {
359		netfinger(*p++);
360		if (*p || entries)
361		    printf("\n");
362	}
363
364	if (entries == 0)
365		return;
366
367	/*
368	 * Scan thru the list of users currently logged in, saving
369	 * appropriate data whenever a match occurs.
370	 */
371	if (!freopen(_PATH_UTMP, "r", stdin))
372		err(1, "%s", _PATH_UTMP);
373	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
374		if (!user.ut_name[0])
375			continue;
376		if ((pn = find_person(user.ut_name)) == NULL)
377			continue;
378		enter_where(&user, pn);
379	}
380	if (db)
381		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
382			PERSON *tmp;
383
384			r = (*db->seq)(db, &key, &data, sflag1);
385			if (r == -1)
386				err(1, "db seq");
387			if (r == 1)
388				break;
389			memmove(&tmp, data.data, sizeof tmp);
390			enter_lastlog(tmp);
391		}
392}
393