2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
34 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
35 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
37 * - 4 digit phone extensions (3210 is printed as x3210.)
38 * - host/office toggling in short format with -h & -o.
39 * - short day names (`Tue' printed instead of `Jun 21' if the
40 * login time is < 6 days.
44 static const char copyright[] =
45 "@(#) Copyright (c) 1989, 1993\n\
46 The Regents of the University of California. All rights reserved.\n";
51 static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
59 * Finger prints out information about users. It is not portable since
60 * certain fields (e.g. the full user name, office, and phone numbers) are
61 * extracted from the gecos field of the passwd file which other UNIXes
62 * may not have or may use for other things.
64 * There are currently two output formats; the short format is one line
65 * per user and displays login name, tty, login time, real name, idle time,
66 * and either remote host information (default) or office location/phone
67 * number, depending on if -h or -o is used respectively.
68 * The long format gives the same information (in a more legible format) as
69 * well as home directory, shell, mail info, and .plan/.project files.
72 #include <sys/types.h>
73 #include <sys/socket.h>
86 #include "pathnames.h"
90 int entries, gflag, kflag, lflag, mflag, pplan, sflag, oflag;
91 sa_family_t family = PF_UNSPEC;
96 static void loginlist(void);
97 static int option(int, char **);
98 static void usage(void);
99 static void userlist(int, char **);
102 option(int argc, char **argv)
106 optind = 1; /* reset getopt */
108 while ((ch = getopt(argc, argv, "46gklmpsho")) != -1)
120 kflag = 1; /* keep going without utmp */
123 lflag = 1; /* long format */
126 mflag = 1; /* force exact match of names */
129 pplan = 1; /* don't show .plan/.project */
132 sflag = 1; /* short format */
135 oflag = 0; /* remote host info */
138 oflag = 1; /* office info */
151 (void)fprintf(stderr,
152 "usage: finger [-46gklmpsho] [user ...] [user@host ...]\n");
157 main(int argc, char **argv)
162 static char myname[] = "finger";
164 if (getuid() == 0 || geteuid() == 0) {
166 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
167 if (setgid(pw->pw_gid) != 0)
169 if (setuid(pw->pw_uid) != 0)
172 if (setgid(UNPRIV_UGID) != 0)
174 if (setuid(UNPRIV_UGID) != 0)
179 (void) setlocale(LC_ALL, "");
181 /* remove this line to get remote host */
182 oflag = 1; /* default to old "office" behavior */
185 * Process environment variables followed by command line arguments.
187 if ((envargv[1] = getenv("FINGER"))) {
191 (void) option(envargc, envargv);
194 argcnt = option(argc, argv);
202 * Assign explicit "small" format if no names given and -l
203 * not selected. Force the -s BEFORE we get names so proper
204 * screening will be done.
207 sflag = 1; /* if -l not explicit, force -s */
210 (void)printf("No one logged on.\n");
212 userlist(argc, argv);
214 * Assign explicit "large" format if names given and -s not
215 * explicitly stated. Force the -l AFTER we get names so any
216 * remote finger attempts specified won't be mishandled.
219 lflag = 1; /* if -s not explicit, force -l */
240 errx(1, "can't list logins without reading utmp");
243 while ((user = getutxent()) != NULL) {
244 if (user->ut_type != USER_PROCESS)
246 if ((pn = find_person(user->ut_user)) == NULL) {
247 if ((pw = getpwnam(user->ut_user)) == NULL)
251 pn = enter_person(pw);
253 enter_where(user, pn);
257 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
260 r = (*db->seq)(db, &key, &data, sflag1);
265 memmove(&tmp, data.data, sizeof tmp);
271 userlist(int argc, char **argv)
277 int r, sflag1, *used, *ip;
278 char **ap, **nargv, **np, **p;
280 char conf_alias[LINE_MAX];
284 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
285 (used = calloc(argc, sizeof(int))) == NULL)
288 /* Pull out all network requests. */
289 for (ap = p = argv, np = nargv; *p; ++p)
302 * Mark any arguments beginning with '/' as invalid so that we
303 * don't accidently confuse them with expansions from finger.conf
305 for (p = argv, ip = used; *p; ++p, ++ip)
308 warnx("%s: no such user", *p);
312 * Traverse the finger alias configuration file of the form
313 * alias:(user|alias), ignoring comment lines beginning '#'.
315 if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
316 while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
317 conf_length = strlen(conf_alias);
318 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
320 conf_alias[conf_length] = '\0'; /* Remove trailing LF */
321 if ((conf_realname = strchr(conf_alias, ':')) == NULL)
323 *conf_realname = '\0'; /* Replace : with NUL */
324 for (p = argv; *p; ++p) {
325 if (strcmp(*p, conf_alias) == 0) {
326 if ((*p = strdup(conf_realname+1)) == NULL) {
332 (void)fclose(conf_fp);
336 * Traverse the list of possible login names and check the login name
337 * and real name against the name specified by the user. If the name
338 * begins with a '/', try to read the file of that name instead of
339 * gathering the traditional finger information.
342 for (p = argv, ip = used; *p; ++p, ++ip) {
343 if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
344 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
347 warnx("%s: no such user", *p);
351 while ((pw = getpwent()) != NULL) {
352 for (p = argv, ip = used; *p; ++p, ++ip)
353 if (**p == '/' && *ip != 1
354 && show_text("", *p, ""))
356 else if (match(pw, *p) && !hide(pw)) {
361 for (p = argv, ip = used; *p; ++p, ++ip)
363 warnx("%s: no such user", *p);
366 /* Handle network requests. */
367 net: for (p = nargv; *p;) {
381 * Scan thru the list of users currently logged in, saving
382 * appropriate data whenever a match occurs.
385 while ((user = getutxent()) != NULL) {
386 if (user->ut_type != USER_PROCESS)
388 if ((pn = find_person(user->ut_user)) == NULL)
390 enter_where(user, pn);
394 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
397 r = (*db->seq)(db, &key, &data, sflag1);
402 memmove(&tmp, data.data, sizeof tmp);