finger.c revision 65787
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 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#ifndef lint
54#if 0
55static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
56#else
57static const char rcsid[] =
58  "$FreeBSD: head/usr.bin/finger/finger.c 65787 2000-09-12 21:58:31Z brian $";
59#endif
60#endif /* not lint */
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 <sys/param.h>
77
78#include <db.h>
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <pwd.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <time.h>
87#include <unistd.h>
88#include <utmp.h>
89#include <db.h>
90#include <locale.h>
91#include <sys/syslimits.h>
92
93#include "finger.h"
94#include "pathnames.h"
95
96DB *db;
97time_t now;
98int entries, lflag, mflag, pplan, sflag, oflag, Tflag;
99char tbuf[1024];
100
101static void loginlist __P((void));
102static void usage __P((void));
103static void userlist __P((int, char **));
104
105int
106option(argc, argv)
107	int argc;
108	char **argv;
109{
110	int ch;
111
112	optind = 1;		/* reset getopt */
113
114	while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
115		switch(ch) {
116		case 'l':
117			lflag = 1;		/* long format */
118			break;
119		case 'm':
120			mflag = 1;		/* force exact match of names */
121			break;
122		case 'p':
123			pplan = 1;		/* don't show .plan/.project */
124			break;
125		case 's':
126			sflag = 1;		/* short format */
127			break;
128		case 'h':
129			oflag = 0;		/* remote host info */
130			break;
131		case 'o':
132			oflag = 1;		/* office info */
133			break;
134		case 'T':
135			Tflag = 1;		/* disable T/TCP */
136			break;
137		case '?':
138		default:
139			usage();
140		}
141
142	return optind;
143}
144
145static void
146usage()
147{
148	(void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
149	exit(1);
150}
151
152int
153main(argc, argv)
154	int argc;
155	char **argv;
156{
157	int envargc, argcnt;
158	char *envargv[3];
159	struct passwd *pw;
160
161	if (getuid() == 0 || geteuid() == 0) {
162		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
163			 setgid(pw->pw_gid);
164			 setuid(pw->pw_uid);
165		} else {
166			 setgid(UNPRIV_UGID);
167			 setuid(UNPRIV_UGID);
168		}
169	}
170
171	(void) setlocale(LC_ALL, "");
172
173				/* remove this line to get remote host */
174	oflag = 1;		/* default to old "office" behavior */
175
176	/*
177	 * Process environment variables followed by command line arguments.
178	 */
179	if ((envargv[1] = getenv("FINGER"))) {
180		envargc = 2;
181		envargv[0] = "finger";
182		envargv[2] = NULL;
183		(void) option(envargc, envargv);
184	}
185
186	argcnt = option(argc, argv);
187	argc -= argcnt;
188	argv += argcnt;
189
190	(void)time(&now);
191	setpassent(1);
192	if (!*argv) {
193		/*
194		 * Assign explicit "small" format if no names given and -l
195		 * not selected.  Force the -s BEFORE we get names so proper
196		 * screening will be done.
197		 */
198		if (!lflag)
199			sflag = 1;	/* if -l not explicit, force -s */
200		loginlist();
201		if (entries == 0)
202			(void)printf("No one logged on.\n");
203	} else {
204		userlist(argc, argv);
205		/*
206		 * Assign explicit "large" format if names given and -s not
207		 * explicitly stated.  Force the -l AFTER we get names so any
208		 * remote finger attempts specified won't be mishandled.
209		 */
210		if (!sflag)
211			lflag = 1;	/* if -s not explicit, force -l */
212	}
213	if (entries) {
214		if (lflag)
215			lflag_print();
216		else
217			sflag_print();
218	}
219	return (0);
220}
221
222static void
223loginlist()
224{
225	register PERSON *pn;
226	DBT data, key;
227	struct passwd *pw;
228	struct utmp user;
229	int r, sflag;
230	char name[UT_NAMESIZE + 1];
231
232	if (!freopen(_PATH_UTMP, "r", stdin))
233		err(1, "%s", _PATH_UTMP);
234	name[UT_NAMESIZE] = '\0';
235	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
236		if (!user.ut_name[0])
237			continue;
238		if ((pn = find_person(user.ut_name)) == NULL) {
239			bcopy(user.ut_name, name, UT_NAMESIZE);
240			if ((pw = getpwnam(name)) == NULL)
241				continue;
242			if (hide(pw))
243				continue;
244			pn = enter_person(pw);
245		}
246		enter_where(&user, pn);
247	}
248	if (db && lflag)
249		for (sflag = R_FIRST;; sflag = R_NEXT) {
250			PERSON *tmp;
251
252			r = (*db->seq)(db, &key, &data, sflag);
253			if (r == -1)
254				err(1, "db seq");
255			if (r == 1)
256				break;
257			memmove(&tmp, data.data, sizeof tmp);
258			enter_lastlog(tmp);
259		}
260}
261
262static void
263userlist(argc, argv)
264	register int argc;
265	register char **argv;
266{
267	register PERSON *pn;
268	DBT data, key;
269	struct utmp user;
270	struct passwd *pw;
271	int r, sflag, *used, *ip;
272	char **ap, **nargv, **np, **p;
273	FILE *conf_fp;
274	char conf_alias[LINE_MAX];
275	char *conf_realname;
276	int conf_length;
277
278	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
279	    (used = calloc(argc, sizeof(int))) == NULL)
280		err(1, NULL);
281
282	/* Pull out all network requests. */
283	for (ap = p = argv, np = nargv; *p; ++p)
284		if (index(*p, '@'))
285			*np++ = *p;
286		else
287			*ap++ = *p;
288
289	*np++ = NULL;
290	*ap++ = NULL;
291
292	if (!*argv)
293		goto net;
294
295	/*
296	 * Traverse the finger alias configuration file of the form
297	 * alias:(user|alias), ignoring comment lines beginning '#'.
298	 */
299	if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
300	    while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
301		conf_length = strlen(conf_alias);
302		if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
303		    continue;
304		conf_alias[conf_length] = '\0';      /* Remove trailing LF */
305		if ((conf_realname = strchr(conf_alias, ':')) == NULL)
306		    continue;
307		*conf_realname = '\0';               /* Replace : with NUL */
308		for (p = argv; *p; ++p) {
309		    if (strcmp(*p, conf_alias) == NULL) {
310			if ((*p = strdup(conf_realname+1)) == NULL) {
311			    err(1, NULL);
312			}
313		    }
314		}
315	    }
316	    (void)fclose(conf_fp);
317	}
318
319	/*
320	 * Traverse the list of possible login names and check the login name
321	 * and real name against the name specified by the user. If the name
322	 * begins with a '/', try to read the file of that name instead of
323	 * gathering the traditional finger information.
324	 */
325	if (mflag)
326		for (p = argv; *p; ++p) {
327			if (**p != '/' || !show_text("", *p, "")) {
328				if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
329					enter_person(pw);
330			   	else
331					warnx("%s: no such user", *p);
332			}
333		}
334	else {
335		while ((pw = getpwent()) != NULL) {
336			for (p = argv, ip = used; *p; ++p, ++ip)
337				if (**p == '/' && *ip != 1
338				    && show_text("", *p, ""))
339					*ip = 1;
340				else if (match(pw, *p) && !hide(pw)) {
341					enter_person(pw);
342					*ip = 1;
343				}
344		}
345		for (p = argv, ip = used; *p; ++p, ++ip)
346			if (!*ip)
347				warnx("%s: no such user", *p);
348	}
349
350	/* Handle network requests. */
351net:	for (p = nargv; *p;) {
352		netfinger(*p++);
353		if (*p || entries)
354		    printf("\n");
355	}
356
357	if (entries == 0)
358		return;
359
360	/*
361	 * Scan thru the list of users currently logged in, saving
362	 * appropriate data whenever a match occurs.
363	 */
364	if (!freopen(_PATH_UTMP, "r", stdin))
365		err(1, "%s", _PATH_UTMP);
366	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
367		if (!user.ut_name[0])
368			continue;
369		if ((pn = find_person(user.ut_name)) == NULL)
370			continue;
371		enter_where(&user, pn);
372	}
373	if (db)
374		for (sflag = R_FIRST;; sflag = R_NEXT) {
375			PERSON *tmp;
376
377			r = (*db->seq)(db, &key, &data, sflag);
378			if (r == -1)
379				err(1, "db seq");
380			if (r == 1)
381				break;
382			memmove(&tmp, data.data, sizeof tmp);
383			enter_lastlog(tmp);
384		}
385}
386