xref: /freebsd/usr.bin/finger/finger.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 	"$Id: finger.c,v 1.12 1997/07/02 06:34:48 charnier Exp $";
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 	return (0);
217 }
218 
219 static void
220 loginlist()
221 {
222 	register PERSON *pn;
223 	DBT data, key;
224 	struct passwd *pw;
225 	struct utmp user;
226 	int r, sflag;
227 	char name[UT_NAMESIZE + 1];
228 
229 	if (!freopen(_PATH_UTMP, "r", stdin))
230 		err(1, "%s", _PATH_UTMP);
231 	name[UT_NAMESIZE] = '\0';
232 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
233 		if (!user.ut_name[0])
234 			continue;
235 		if ((pn = find_person(user.ut_name)) == NULL) {
236 			bcopy(user.ut_name, name, UT_NAMESIZE);
237 			if ((pw = getpwnam(name)) == NULL)
238 				continue;
239 			if (hide(pw))
240 				continue;
241 			pn = enter_person(pw);
242 		}
243 		enter_where(&user, pn);
244 	}
245 	if (db && lflag)
246 		for (sflag = R_FIRST;; sflag = R_NEXT) {
247 			PERSON *tmp;
248 
249 			r = (*db->seq)(db, &key, &data, sflag);
250 			if (r == -1)
251 				err(1, "db seq");
252 			if (r == 1)
253 				break;
254 			memmove(&tmp, data.data, sizeof tmp);
255 			enter_lastlog(tmp);
256 		}
257 }
258 
259 static void
260 userlist(argc, argv)
261 	register int argc;
262 	register char **argv;
263 {
264 	register PERSON *pn;
265 	DBT data, key;
266 	struct utmp user;
267 	struct passwd *pw;
268 	int r, sflag, *used, *ip;
269 	char **ap, **nargv, **np, **p;
270 
271 	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
272 	    (used = calloc(argc, sizeof(int))) == NULL)
273 		err(1, NULL);
274 
275 	/* Pull out all network requests. */
276 	for (ap = p = argv, np = nargv; *p; ++p)
277 		if (index(*p, '@'))
278 			*np++ = *p;
279 		else
280 			*ap++ = *p;
281 
282 	*np++ = NULL;
283 	*ap++ = NULL;
284 
285 	if (!*argv)
286 		goto net;
287 
288 	/*
289 	 * Traverse the list of possible login names and check the login name
290 	 * and real name against the name specified by the user.
291 	 */
292 	if (mflag)
293 		for (p = argv; *p; ++p)
294 			if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
295 				enter_person(pw);
296 			else
297 				warnx("%s: no such user", *p);
298 	else {
299 		while ((pw = getpwent()) != NULL) {
300 			for (p = argv, ip = used; *p; ++p, ++ip)
301 				if (match(pw, *p) && !hide(pw)) {
302 					enter_person(pw);
303 					*ip = 1;
304 				}
305 		}
306 		for (p = argv, ip = used; *p; ++p, ++ip)
307 			if (!*ip)
308 				warnx("%s: no such user", *p);
309 	}
310 
311 	/* Handle network requests. */
312 net:	for (p = nargv; *p;) {
313 		netfinger(*p++);
314 		if (*p || entries)
315 		    printf("\n");
316 	}
317 
318 	if (entries == 0)
319 		return;
320 
321 	/*
322 	 * Scan thru the list of users currently logged in, saving
323 	 * appropriate data whenever a match occurs.
324 	 */
325 	if (!freopen(_PATH_UTMP, "r", stdin))
326 		err(1, "%s", _PATH_UTMP);
327 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
328 		if (!user.ut_name[0])
329 			continue;
330 		if ((pn = find_person(user.ut_name)) == NULL)
331 			continue;
332 		enter_where(&user, pn);
333 	}
334 	if (db)
335 		for (sflag = R_FIRST;; sflag = R_NEXT) {
336 			PERSON *tmp;
337 
338 			r = (*db->seq)(db, &key, &data, sflag);
339 			if (r == -1)
340 				err(1, "db seq");
341 			if (r == 1)
342 				break;
343 			memmove(&tmp, data.data, sizeof tmp);
344 			enter_lastlog(tmp);
345 		}
346 }
347