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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)finger.c 8.5 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/finger/finger.c,v 1.15.2.9 2002/07/29 18:52:52 ume Exp $
39 * $DragonFly: src/usr.bin/finger/finger.c,v 1.6 2008/06/05 18:06:33 swildner Exp $
43 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
44 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
46 * - 4 digit phone extensions (3210 is printed as x3210.)
47 * - host/office toggling in short format with -h & -o.
48 * - short day names (`Tue' printed instead of `Jun 21' if the
49 * login time is < 6 days.
53 * Finger prints out information about users. It is not portable since
54 * certain fields (e.g. the full user name, office, and phone numbers) are
55 * extracted from the gecos field of the passwd file which other UNIXes
56 * may not have or may use for other things.
58 * There are currently two output formats; the short format is one line
59 * per user and displays login name, tty, login time, real name, idle time,
60 * and either remote host information (default) or office location/phone
61 * number, depending on if -h or -o is used respectively.
62 * The long format gives the same information (in a more legible format) as
63 * well as home directory, shell, mail info, and .plan/.project files.
66 #include <sys/types.h>
67 #include <sys/socket.h>
76 #include "utmpentry.h"
80 #include "pathnames.h"
84 int entries, gflag, lflag, mflag, pplan, sflag, oflag, Tflag, eightflag;
85 sa_family_t family = PF_UNSPEC;
89 static void loginlist(void);
90 static int option(int, char **);
91 static void usage(void);
92 static void userlist(int, char **);
95 option(int argc, char **argv)
99 optind = 1; /* reset getopt */
101 while ((ch = getopt(argc, argv, "468glmpshoT")) != -1)
110 eightflag = 1; /* allow 8-bit passthrough */
116 lflag = 1; /* long format */
119 mflag = 1; /* force exact match of names */
122 pplan = 1; /* don't show .plan/.project */
125 sflag = 1; /* short format */
128 oflag = 0; /* remote host info */
131 oflag = 1; /* office info */
134 Tflag = 1; /* disable T/TCP */
147 fprintf(stderr, "usage: finger [-468lmpshoT] [login ...]\n");
152 main(int argc, char **argv)
157 static char myname[] = "finger";
159 if (getuid() == 0 || geteuid() == 0) {
160 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
169 (void) setlocale(LC_ALL, "");
171 /* remove this line to get remote host */
172 oflag = 1; /* default to old "office" behavior */
175 * Process environment variables followed by command line arguments.
177 if ((envargv[1] = getenv("FINGER"))) {
181 option(envargc, envargv);
184 argcnt = option(argc, argv);
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.
197 sflag = 1; /* if -l not explicit, force -s */
200 printf("No one logged on.\n");
202 userlist(argc, argv);
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.
209 lflag = 1; /* if -s not explicit, force -l */
226 struct utmpentry *ep;
229 getutentries(NULL, &ep);
230 for (; ep; ep = ep->next) {
231 if ((pn = find_person(ep->name)) == NULL) {
232 if ((pw = getpwnam(ep->name)) == NULL)
236 pn = enter_person(pw);
241 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
244 r = (*db->seq)(db, &key, &data, sflag1);
249 memmove(&tmp, data.data, sizeof tmp);
255 userlist(int argc, char **argv)
259 struct utmpentry *ep;
261 int r, sflag1, *used, *ip;
262 char **ap, **nargv, **np, **p;
264 char conf_alias[LINE_MAX];
268 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
269 (used = calloc(argc, sizeof(int))) == NULL)
272 /* Pull out all network requests. */
273 for (ap = p = argv, np = nargv; *p; ++p)
286 * Mark any arguments beginning with '/' as invalid so that we
287 * don't accidently confuse them with expansions from finger.conf
289 for (p = argv, ip = used; *p; ++p, ++ip)
292 warnx("%s: no such user", *p);
296 * Traverse the finger alias configuration file of the form
297 * alias:(user|alias), ignoring comment lines beginning '#'.
299 if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
300 while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
301 conf_length = strlen(conf_alias);
302 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
304 conf_alias[conf_length] = '\0'; /* Remove trailing LF */
305 if ((conf_realname = strchr(conf_alias, ':')) == NULL)
307 *conf_realname = '\0'; /* Replace : with NUL */
308 for (p = argv; *p; ++p) {
309 if (strcmp(*p, conf_alias) == 0) {
310 if ((*p = strdup(conf_realname+1)) == NULL) {
320 * Traverse the list of possible login names and check the login name
321 * and real name against the name specified by the user. If the name
322 * begins with a '/', try to read the file of that name instead of
323 * gathering the traditional finger information.
326 for (p = argv, ip = used; *p; ++p, ++ip) {
327 if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
328 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
331 warnx("%s: no such user", *p);
335 while ((pw = getpwent()) != NULL) {
336 for (p = argv, ip = used; *p; ++p, ++ip)
337 if (**p == '/' && *ip != 1
338 && show_text("", *p, ""))
340 else if (match(pw, *p) && !hide(pw)) {
345 for (p = argv, ip = used; *p; ++p, ++ip)
347 warnx("%s: no such user", *p);
350 /* Handle network requests. */
351 net: for (p = nargv; *p;) {
361 * Scan thru the list of users currently logged in, saving
362 * appropriate data whenever a match occurs.
364 getutentries(NULL, &ep);
365 for (; ep; ep = ep->next) {
366 if ((pn = find_person(ep->name)) == NULL)
371 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
374 r = (*db->seq)(db, &key, &data, sflag1);
379 memmove(&tmp, data.data, sizeof tmp);