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 * 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.
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
38 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
39 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
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.
48 static const char copyright[] =
49 "@(#) Copyright (c) 1989, 1993\n\
50 The Regents of the University of California. All rights reserved.\n";
55 static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
57 static const char rcsid[] =
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.
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.
76 #include <sys/param.h>
91 #include <sys/syslimits.h>
94 #include "pathnames.h"
98 int entries, lflag, mflag, pplan, sflag, oflag, Tflag;
101 static void loginlist __P((void));
102 static void usage __P((void));
103 static void userlist __P((int, char **));
112 optind = 1; /* reset getopt */
114 while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
117 lflag = 1; /* long format */
120 mflag = 1; /* force exact match of names */
123 pplan = 1; /* don't show .plan/.project */
126 sflag = 1; /* short format */
129 oflag = 0; /* remote host info */
132 oflag = 1; /* office info */
135 Tflag = 1; /* disable T/TCP */
148 (void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
161 if (getuid() == 0 || geteuid() == 0) {
162 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
171 (void) setlocale(LC_ALL, "");
173 /* remove this line to get remote host */
174 oflag = 1; /* default to old "office" behavior */
177 * Process environment variables followed by command line arguments.
179 if ((envargv[1] = getenv("FINGER"))) {
181 envargv[0] = "finger";
183 (void) option(envargc, envargv);
186 argcnt = option(argc, argv);
194 * Assign explicit "small" format if no names given and -l
195 * not selected. Force the -s BEFORE we get names so proper
196 * screening will be done.
199 sflag = 1; /* if -l not explicit, force -s */
202 (void)printf("No one logged on.\n");
204 userlist(argc, argv);
206 * Assign explicit "large" format if names given and -s not
207 * explicitly stated. Force the -l AFTER we get names so any
208 * remote finger attempts specified won't be mishandled.
211 lflag = 1; /* if -s not explicit, force -l */
230 char name[UT_NAMESIZE + 1];
232 if (!freopen(_PATH_UTMP, "r", stdin))
233 err(1, "%s", _PATH_UTMP);
234 name[UT_NAMESIZE] = '\0';
235 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
236 if (!user.ut_name[0])
238 if ((pn = find_person(user.ut_name)) == NULL) {
239 bcopy(user.ut_name, name, UT_NAMESIZE);
240 if ((pw = getpwnam(name)) == NULL)
244 pn = enter_person(pw);
246 enter_where(&user, pn);
249 for (sflag = R_FIRST;; sflag = R_NEXT) {
252 r = (*db->seq)(db, &key, &data, sflag);
257 memmove(&tmp, data.data, sizeof tmp);
265 register char **argv;
271 int r, sflag, *used, *ip;
272 char **ap, **nargv, **np, **p;
274 char conf_alias[LINE_MAX];
278 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
279 (used = calloc(argc, sizeof(int))) == NULL)
282 /* Pull out all network requests. */
283 for (ap = p = argv, np = nargv; *p; ++p)
296 * Mark any arguments beginning with '/' as invalid so that we
297 * don't accidently confuse them with expansions from finger.conf
299 for (p = argv, ip = used; *p; ++p, ++ip)
302 warnx("%s: no such user", *p);
306 * Traverse the finger alias configuration file of the form
307 * alias:(user|alias), ignoring comment lines beginning '#'.
309 if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
310 while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
311 conf_length = strlen(conf_alias);
312 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
314 conf_alias[conf_length] = '\0'; /* Remove trailing LF */
315 if ((conf_realname = strchr(conf_alias, ':')) == NULL)
317 *conf_realname = '\0'; /* Replace : with NUL */
318 for (p = argv; *p; ++p) {
319 if (strcmp(*p, conf_alias) == NULL) {
320 if ((*p = strdup(conf_realname+1)) == NULL) {
326 (void)fclose(conf_fp);
330 * Traverse the list of possible login names and check the login name
331 * and real name against the name specified by the user. If the name
332 * begins with a '/', try to read the file of that name instead of
333 * gathering the traditional finger information.
336 for (p = argv, ip = used; *p; ++p, ++ip) {
337 if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
338 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
341 warnx("%s: no such user", *p);
345 while ((pw = getpwent()) != NULL) {
346 for (p = argv, ip = used; *p; ++p, ++ip)
347 if (**p == '/' && *ip != 1
348 && show_text("", *p, ""))
350 else if (match(pw, *p) && !hide(pw)) {
355 for (p = argv, ip = used; *p; ++p, ++ip)
357 warnx("%s: no such user", *p);
360 /* Handle network requests. */
361 net: for (p = nargv; *p;) {
371 * Scan thru the list of users currently logged in, saving
372 * appropriate data whenever a match occurs.
374 if (!freopen(_PATH_UTMP, "r", stdin))
375 err(1, "%s", _PATH_UTMP);
376 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
377 if (!user.ut_name[0])
379 if ((pn = find_person(user.ut_name)) == NULL)
381 enter_where(&user, pn);
384 for (sflag = R_FIRST;; sflag = R_NEXT) {
387 r = (*db->seq)(db, &key, &data, sflag);
392 memmove(&tmp, data.data, sizeof tmp);