xref: /freebsd/usr.bin/finger/finger.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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
48 static 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
55 static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
56 #else
57 static const char rcsid[] =
58   "$FreeBSD$";
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 
92 #include "finger.h"
93 
94 DB *db;
95 time_t now;
96 int entries, lflag, mflag, pplan, sflag, oflag, Tflag;
97 char tbuf[1024];
98 
99 static void loginlist __P((void));
100 static void usage __P((void));
101 static void userlist __P((int, char **));
102 
103 int
104 option(argc, argv)
105 	int argc;
106 	char **argv;
107 {
108 	int ch;
109 
110 	optind = 1;		/* reset getopt */
111 
112 	while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
113 		switch(ch) {
114 		case 'l':
115 			lflag = 1;		/* long format */
116 			break;
117 		case 'm':
118 			mflag = 1;		/* force exact match of names */
119 			break;
120 		case 'p':
121 			pplan = 1;		/* don't show .plan/.project */
122 			break;
123 		case 's':
124 			sflag = 1;		/* short format */
125 			break;
126 		case 'h':
127 			oflag = 0;		/* remote host info */
128 			break;
129 		case 'o':
130 			oflag = 1;		/* office info */
131 			break;
132 		case 'T':
133 			Tflag = 1;		/* disable T/TCP */
134 			break;
135 		case '?':
136 		default:
137 			usage();
138 		}
139 
140 	return optind;
141 }
142 
143 static void
144 usage()
145 {
146 	(void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
147 	exit(1);
148 }
149 
150 int
151 main(argc, argv)
152 	int argc;
153 	char **argv;
154 {
155 	int envargc, argcnt;
156 	char *envargv[3];
157 	struct passwd *pw;
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] = "finger";
180 		envargv[2] = NULL;
181 		(void) option(envargc, envargv);
182 	}
183 
184 	argcnt = option(argc, argv);
185 	argc -= argcnt;
186 	argv += argcnt;
187 
188 	(void)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 			(void)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()
222 {
223 	register PERSON *pn;
224 	DBT data, key;
225 	struct passwd *pw;
226 	struct utmp user;
227 	int r, sflag;
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 (sflag = R_FIRST;; sflag = R_NEXT) {
248 			PERSON *tmp;
249 
250 			r = (*db->seq)(db, &key, &data, sflag);
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(argc, argv)
262 	register int argc;
263 	register char **argv;
264 {
265 	register PERSON *pn;
266 	DBT data, key;
267 	struct utmp user;
268 	struct passwd *pw;
269 	int r, sflag, *used, *ip;
270 	char **ap, **nargv, **np, **p;
271 
272 	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
273 	    (used = calloc(argc, sizeof(int))) == NULL)
274 		err(1, NULL);
275 
276 	/* Pull out all network requests. */
277 	for (ap = p = argv, np = nargv; *p; ++p)
278 		if (index(*p, '@'))
279 			*np++ = *p;
280 		else
281 			*ap++ = *p;
282 
283 	*np++ = NULL;
284 	*ap++ = NULL;
285 
286 	if (!*argv)
287 		goto net;
288 
289 	/*
290 	 * Traverse the list of possible login names and check the login name
291 	 * and real name against the name specified by the user.
292 	 */
293 	if (mflag)
294 		for (p = argv; *p; ++p)
295 			if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
296 				enter_person(pw);
297 			else
298 				warnx("%s: no such user", *p);
299 	else {
300 		while ((pw = getpwent()) != NULL) {
301 			for (p = argv, ip = used; *p; ++p, ++ip)
302 				if (match(pw, *p) && !hide(pw)) {
303 					enter_person(pw);
304 					*ip = 1;
305 				}
306 		}
307 		for (p = argv, ip = used; *p; ++p, ++ip)
308 			if (!*ip)
309 				warnx("%s: no such user", *p);
310 	}
311 
312 	/* Handle network requests. */
313 net:	for (p = nargv; *p;) {
314 		netfinger(*p++);
315 		if (*p || entries)
316 		    printf("\n");
317 	}
318 
319 	if (entries == 0)
320 		return;
321 
322 	/*
323 	 * Scan thru the list of users currently logged in, saving
324 	 * appropriate data whenever a match occurs.
325 	 */
326 	if (!freopen(_PATH_UTMP, "r", stdin))
327 		err(1, "%s", _PATH_UTMP);
328 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
329 		if (!user.ut_name[0])
330 			continue;
331 		if ((pn = find_person(user.ut_name)) == NULL)
332 			continue;
333 		enter_where(&user, pn);
334 	}
335 	if (db)
336 		for (sflag = R_FIRST;; sflag = R_NEXT) {
337 			PERSON *tmp;
338 
339 			r = (*db->seq)(db, &key, &data, sflag);
340 			if (r == -1)
341 				err(1, "db seq");
342 			if (r == 1)
343 				break;
344 			memmove(&tmp, data.data, sizeof tmp);
345 			enter_lastlog(tmp);
346 		}
347 }
348