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