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