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