xref: /dragonfly/usr.bin/finger/finger.c (revision f746689a)
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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)finger.c	8.5 (Berkeley) 5/4/95
38  * $FreeBSD: src/usr.bin/finger/finger.c,v 1.15.2.9 2002/07/29 18:52:52 ume Exp $
39  * $DragonFly: src/usr.bin/finger/finger.c,v 1.6 2008/06/05 18:06:33 swildner Exp $
40  */
41 
42 /*
43  * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
44  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
45  *	Unread since ...".)
46  *    - 4 digit phone extensions (3210 is printed as x3210.)
47  *    - host/office toggling in short format with -h & -o.
48  *    - short day names (`Tue' printed instead of `Jun 21' if the
49  *	login time is < 6 days.
50  */
51 
52 /*
53  * Finger prints out information about users.  It is not portable since
54  * certain fields (e.g. the full user name, office, and phone numbers) are
55  * extracted from the gecos field of the passwd file which other UNIXes
56  * may not have or may use for other things.
57  *
58  * There are currently two output formats; the short format is one line
59  * per user and displays login name, tty, login time, real name, idle time,
60  * and either remote host information (default) or office location/phone
61  * number, depending on if -h or -o is used respectively.
62  * The long format gives the same information (in a more legible format) as
63  * well as home directory, shell, mail info, and .plan/.project files.
64  */
65 
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <db.h>
69 #include <err.h>
70 #include <pwd.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <time.h>
75 #include <unistd.h>
76 #include <utmp.h>
77 #include <locale.h>
78 
79 #include "finger.h"
80 #include "pathnames.h"
81 
82 DB *db;
83 time_t now;
84 int entries, gflag, lflag, mflag, pplan, sflag, oflag, Tflag, eightflag;
85 sa_family_t family = PF_UNSPEC;
86 int d_first = -1;
87 char tbuf[1024];
88 
89 static void loginlist(void);
90 static int option(int, char **);
91 static void usage(void);
92 static void userlist(int, char **);
93 
94 static int
95 option(int argc, char **argv)
96 {
97 	int ch;
98 
99 	optind = 1;		/* reset getopt */
100 
101 	while ((ch = getopt(argc, argv, "468glmpshoT")) != -1)
102 		switch(ch) {
103 		case '4':
104 			family = AF_INET;
105 			break;
106 		case '6':
107 			family = AF_INET6;
108 			break;
109 		case '8':
110 			eightflag = 1;		/* allow 8-bit passthrough */
111 			break;
112 		case 'g':
113 			gflag = 1;
114 			break;
115 		case 'l':
116 			lflag = 1;		/* long format */
117 			break;
118 		case 'm':
119 			mflag = 1;		/* force exact match of names */
120 			break;
121 		case 'p':
122 			pplan = 1;		/* don't show .plan/.project */
123 			break;
124 		case 's':
125 			sflag = 1;		/* short format */
126 			break;
127 		case 'h':
128 			oflag = 0;		/* remote host info */
129 			break;
130 		case 'o':
131 			oflag = 1;		/* office info */
132 			break;
133 		case 'T':
134 			Tflag = 1;		/* disable T/TCP */
135 			break;
136 		case '?':
137 		default:
138 			usage();
139 		}
140 
141 	return optind;
142 }
143 
144 static void
145 usage(void)
146 {
147 	fprintf(stderr, "usage: finger [-468lmpshoT] [login ...]\n");
148 	exit(1);
149 }
150 
151 int
152 main(int argc, char **argv)
153 {
154 	int envargc, argcnt;
155 	char *envargv[3];
156 	struct passwd *pw;
157 	static char myname[] = "finger";
158 
159 	if (getuid() == 0 || geteuid() == 0) {
160 		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
161 			 setgid(pw->pw_gid);
162 			 setuid(pw->pw_uid);
163 		} else {
164 			 setgid(UNPRIV_UGID);
165 			 setuid(UNPRIV_UGID);
166 		}
167 	}
168 
169 	(void) setlocale(LC_ALL, "");
170 
171 				/* remove this line to get remote host */
172 	oflag = 1;		/* default to old "office" behavior */
173 
174 	/*
175 	 * Process environment variables followed by command line arguments.
176 	 */
177 	if ((envargv[1] = getenv("FINGER"))) {
178 		envargc = 2;
179 		envargv[0] = myname;
180 		envargv[2] = NULL;
181 		option(envargc, envargv);
182 	}
183 
184 	argcnt = option(argc, argv);
185 	argc -= argcnt;
186 	argv += argcnt;
187 
188 	time(&now);
189 	setpassent(1);
190 	if (!*argv) {
191 		/*
192 		 * Assign explicit "small" format if no names given and -l
193 		 * not selected.  Force the -s BEFORE we get names so proper
194 		 * screening will be done.
195 		 */
196 		if (!lflag)
197 			sflag = 1;	/* if -l not explicit, force -s */
198 		loginlist();
199 		if (entries == 0)
200 			printf("No one logged on.\n");
201 	} else {
202 		userlist(argc, argv);
203 		/*
204 		 * Assign explicit "large" format if names given and -s not
205 		 * explicitly stated.  Force the -l AFTER we get names so any
206 		 * remote finger attempts specified won't be mishandled.
207 		 */
208 		if (!sflag)
209 			lflag = 1;	/* if -s not explicit, force -l */
210 	}
211 	if (entries) {
212 		if (lflag)
213 			lflag_print();
214 		else
215 			sflag_print();
216 	}
217 	return (0);
218 }
219 
220 static void
221 loginlist(void)
222 {
223 	PERSON *pn;
224 	DBT data, key;
225 	struct passwd *pw;
226 	struct utmp user;
227 	int r, sflag1;
228 	char name[UT_NAMESIZE + 1];
229 
230 	if (!freopen(_PATH_UTMP, "r", stdin))
231 		err(1, "%s", _PATH_UTMP);
232 	name[UT_NAMESIZE] = '\0';
233 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
234 		if (!user.ut_name[0])
235 			continue;
236 		if ((pn = find_person(user.ut_name)) == NULL) {
237 			bcopy(user.ut_name, name, UT_NAMESIZE);
238 			if ((pw = getpwnam(name)) == NULL)
239 				continue;
240 			if (hide(pw))
241 				continue;
242 			pn = enter_person(pw);
243 		}
244 		enter_where(&user, pn);
245 	}
246 	if (db && lflag)
247 		for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
248 			PERSON *tmp;
249 
250 			r = (*db->seq)(db, &key, &data, sflag1);
251 			if (r == -1)
252 				err(1, "db seq");
253 			if (r == 1)
254 				break;
255 			memmove(&tmp, data.data, sizeof tmp);
256 			enter_lastlog(tmp);
257 		}
258 }
259 
260 static void
261 userlist(int argc, 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 (strchr(*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) == 0) {
316 			if ((*p = strdup(conf_realname+1)) == NULL) {
317 			    err(1, NULL);
318 			}
319 		    }
320 		}
321 	    }
322 	    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