2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
37 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
39 * - 4 digit phone extensions (3210 is printed as x3210.)
40 * - host/office toggling in short format with -h & -o.
41 * - short day names (`Tue' printed instead of `Jun 21' if the
42 * login time is < 6 days.
46 * Finger prints out information about users. It is not portable since
47 * certain fields (e.g. the full user name, office, and phone numbers) are
48 * extracted from the gecos field of the passwd file which other UNIXes
49 * may not have or may use for other things.
51 * There are currently two output formats; the short format is one line
52 * per user and displays login name, tty, login time, real name, idle time,
53 * and either remote host information (default) or office location/phone
54 * number, depending on if -h or -o is used respectively.
55 * The long format gives the same information (in a more legible format) as
56 * well as home directory, shell, mail info, and .plan/.project files.
59 #include <sys/types.h>
60 #include <sys/socket.h>
73 #include "pathnames.h"
77 static int kflag, mflag, sflag;
78 int entries, gflag, lflag, pplan, oflag;
79 sa_family_t family = PF_UNSPEC;
84 static void loginlist(void);
85 static int option(int, char **);
86 static void usage(void) __dead2;
87 static void userlist(int, char **);
90 option(int argc, char **argv)
94 optind = 1; /* reset getopt */
96 while ((ch = getopt(argc, argv, "46gklmpsho")) != -1)
108 kflag = 1; /* keep going without utmp */
111 lflag = 1; /* long format */
114 mflag = 1; /* force exact match of names */
117 pplan = 1; /* don't show .plan/.project */
120 sflag = 1; /* short format */
123 oflag = 0; /* remote host info */
126 oflag = 1; /* office info */
139 (void)fprintf(stderr,
140 "usage: finger [-46gklmpsho] [user ...] [user@host ...]\n");
145 main(int argc, char **argv)
150 static char myname[] = "finger";
152 if (getuid() == 0 || geteuid() == 0) {
154 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
155 if (setgid(pw->pw_gid) != 0)
157 if (setuid(pw->pw_uid) != 0)
160 if (setgid(UNPRIV_UGID) != 0)
162 if (setuid(UNPRIV_UGID) != 0)
167 (void) setlocale(LC_ALL, "");
169 /* remove this line to get remote host */
170 oflag = 1; /* default to old "office" behavior */
173 * Process environment variables followed by command line arguments.
175 if ((envargv[1] = getenv("FINGER"))) {
179 (void) option(envargc, envargv);
182 argcnt = option(argc, argv);
190 * Assign explicit "small" format if no names given and -l
191 * not selected. Force the -s BEFORE we get names so proper
192 * screening will be done.
195 sflag = 1; /* if -l not explicit, force -s */
198 (void)printf("No one logged on.\n");
200 userlist(argc, argv);
202 * Assign explicit "large" format if names given and -s not
203 * explicitly stated. Force the -l AFTER we get names so any
204 * remote finger attempts specified won't be mishandled.
207 lflag = 1; /* if -s not explicit, force -l */
228 errx(1, "can't list logins without reading utmp");
231 while ((user = getutxent()) != NULL) {
232 if (user->ut_type != USER_PROCESS)
234 if ((pn = find_person(user->ut_user)) == NULL) {
235 if ((pw = getpwnam(user->ut_user)) == NULL)
239 pn = enter_person(pw);
241 enter_where(user, pn);
245 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
248 r = (*db->seq)(db, &key, &data, sflag1);
253 memmove(&tmp, data.data, sizeof tmp);
259 userlist(int argc, char **argv)
265 int r, sflag1, *used, *ip;
266 char **ap, **nargv, **np, **p;
268 char conf_alias[LINE_MAX];
272 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
273 (used = calloc(argc, sizeof(int))) == NULL)
276 /* Pull out all network requests. */
277 for (ap = p = argv, np = nargv; *p; ++p)
290 * Mark any arguments beginning with '/' as invalid so that we
291 * don't accidentally confuse them with expansions from finger.conf
293 for (p = argv, ip = used; *p; ++p, ++ip)
296 warnx("%s: no such user", *p);
300 * Traverse the finger alias configuration file of the form
301 * alias:(user|alias), ignoring comment lines beginning '#'.
303 if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
304 while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
305 conf_length = strlen(conf_alias);
306 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
308 conf_alias[conf_length] = '\0'; /* Remove trailing LF */
309 if ((conf_realname = strchr(conf_alias, ':')) == NULL)
311 *conf_realname = '\0'; /* Replace : with NUL */
312 for (p = argv; *p; ++p) {
313 if (strcmp(*p, conf_alias) == 0) {
314 if ((*p = strdup(conf_realname+1)) == NULL) {
320 (void)fclose(conf_fp);
324 * Traverse the list of possible login names and check the login name
325 * and real name against the name specified by the user. If the name
326 * begins with a '/', try to read the file of that name instead of
327 * gathering the traditional finger information.
330 for (p = argv, ip = used; *p; ++p, ++ip) {
331 if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
332 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
335 warnx("%s: no such user", *p);
339 while ((pw = getpwent()) != NULL) {
340 for (p = argv, ip = used; *p; ++p, ++ip)
341 if (**p == '/' && *ip != 1
342 && show_text("", *p, ""))
344 else if (match(pw, *p) && !hide(pw)) {
349 for (p = argv, ip = used; *p; ++p, ++ip)
351 warnx("%s: no such user", *p);
354 /* Handle network requests. */
355 net: for (p = nargv; *p;) {
370 * Scan thru the list of users currently logged in, saving
371 * appropriate data whenever a match occurs.
374 while ((user = getutxent()) != NULL) {
375 if (user->ut_type != USER_PROCESS)
377 if ((pn = find_person(user->ut_user)) == NULL)
379 enter_where(user, pn);
383 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
386 r = (*db->seq)(db, &key, &data, sflag1);
391 memmove(&tmp, data.data, sizeof tmp);