]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/finger/finger.c
* Add the readline(3) API to libedit. The libedit versions of
[FreeBSD/FreeBSD.git] / usr.bin / finger / finger.c
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  * 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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
35  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
36  *      Unread since ...".)
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.
41  */
42
43 #ifndef lint
44 static const char copyright[] =
45 "@(#) Copyright (c) 1989, 1993\n\
46         The Regents of the University of California.  All rights reserved.\n";
47 #endif /* not lint */
48
49 #if 0
50 #ifndef lint
51 static char sccsid[] = "@(#)finger.c    8.5 (Berkeley) 5/4/95";
52 #endif
53 #endif
54
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57
58 /*
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.
63  *
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.
70  */
71
72 #include <sys/types.h>
73 #include <sys/socket.h>
74 #include <db.h>
75 #include <err.h>
76 #include <pwd.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <time.h>
81 #include <unistd.h>
82 #include <utmpx.h>
83 #include <locale.h>
84
85 #include "finger.h"
86 #include "pathnames.h"
87
88 DB *db;
89 time_t now;
90 int entries, gflag, kflag, lflag, mflag, pplan, sflag, oflag;
91 sa_family_t family = PF_UNSPEC;
92 int d_first = -1;
93 char tbuf[1024];
94 int invoker_root = 0;
95
96 static void loginlist(void);
97 static int option(int, char **);
98 static void usage(void);
99 static void userlist(int, char **);
100
101 static int
102 option(int argc, char **argv)
103 {
104         int ch;
105
106         optind = 1;             /* reset getopt */
107
108         while ((ch = getopt(argc, argv, "46gklmpsho")) != -1)
109                 switch(ch) {
110                 case '4':
111                         family = AF_INET;
112                         break;
113                 case '6':
114                         family = AF_INET6;
115                         break;
116                 case 'g':
117                         gflag = 1;
118                         break;
119                 case 'k':
120                         kflag = 1;              /* keep going without utmp */
121                         break;
122                 case 'l':
123                         lflag = 1;              /* long format */
124                         break;
125                 case 'm':
126                         mflag = 1;              /* force exact match of names */
127                         break;
128                 case 'p':
129                         pplan = 1;              /* don't show .plan/.project */
130                         break;
131                 case 's':
132                         sflag = 1;              /* short format */
133                         break;
134                 case 'h':
135                         oflag = 0;              /* remote host info */
136                         break;
137                 case 'o':
138                         oflag = 1;              /* office info */
139                         break;
140                 case '?':
141                 default:
142                         usage();
143                 }
144
145         return optind;
146 }
147
148 static void
149 usage(void)
150 {
151         (void)fprintf(stderr,
152             "usage: finger [-46gklmpsho] [user ...] [user@host ...]\n");
153         exit(1);
154 }
155
156 int
157 main(int argc, char **argv)
158 {
159         int envargc, argcnt;
160         char *envargv[3];
161         struct passwd *pw;
162         static char myname[] = "finger";
163
164         if (getuid() == 0 || geteuid() == 0) {
165                 invoker_root = 1;
166                 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
167                          setgid(pw->pw_gid);
168                          setuid(pw->pw_uid);
169                 } else {
170                          setgid(UNPRIV_UGID);
171                          setuid(UNPRIV_UGID);
172                 }
173         }
174
175         (void) setlocale(LC_ALL, "");
176
177                                 /* remove this line to get remote host */
178         oflag = 1;              /* default to old "office" behavior */
179
180         /*
181          * Process environment variables followed by command line arguments.
182          */
183         if ((envargv[1] = getenv("FINGER"))) {
184                 envargc = 2;
185                 envargv[0] = myname;
186                 envargv[2] = NULL;
187                 (void) option(envargc, envargv);
188         }
189
190         argcnt = option(argc, argv);
191         argc -= argcnt;
192         argv += argcnt;
193
194         (void)time(&now);
195         setpassent(1);
196         if (!*argv) {
197                 /*
198                  * Assign explicit "small" format if no names given and -l
199                  * not selected.  Force the -s BEFORE we get names so proper
200                  * screening will be done.
201                  */
202                 if (!lflag)
203                         sflag = 1;      /* if -l not explicit, force -s */
204                 loginlist();
205                 if (entries == 0)
206                         (void)printf("No one logged on.\n");
207         } else {
208                 userlist(argc, argv);
209                 /*
210                  * Assign explicit "large" format if names given and -s not
211                  * explicitly stated.  Force the -l AFTER we get names so any
212                  * remote finger attempts specified won't be mishandled.
213                  */
214                 if (!sflag)
215                         lflag = 1;      /* if -s not explicit, force -l */
216         }
217         if (entries) {
218                 if (lflag)
219                         lflag_print();
220                 else
221                         sflag_print();
222         }
223         return (0);
224 }
225
226 static void
227 loginlist(void)
228 {
229         PERSON *pn;
230         DBT data, key;
231         struct passwd *pw;
232         struct utmpx *user;
233         int r, sflag1;
234
235         if (kflag)
236                 errx(1, "can't list logins without reading utmp");
237
238         setutxent();
239         while ((user = getutxent()) != NULL) {
240                 if (user->ut_type != USER_PROCESS)
241                         continue;
242                 if ((pn = find_person(user->ut_user)) == NULL) {
243                         if ((pw = getpwnam(user->ut_user)) == NULL)
244                                 continue;
245                         if (hide(pw))
246                                 continue;
247                         pn = enter_person(pw);
248                 }
249                 enter_where(user, pn);
250         }
251         endutxent();
252         if (db && lflag)
253                 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
254                         PERSON *tmp;
255
256                         r = (*db->seq)(db, &key, &data, sflag1);
257                         if (r == -1)
258                                 err(1, "db seq");
259                         if (r == 1)
260                                 break;
261                         memmove(&tmp, data.data, sizeof tmp);
262                         enter_lastlog(tmp);
263                 }
264 }
265
266 static void
267 userlist(int argc, char **argv)
268 {
269         PERSON *pn;
270         DBT data, key;
271         struct utmpx *user;
272         struct passwd *pw;
273         int r, sflag1, *used, *ip;
274         char **ap, **nargv, **np, **p;
275         FILE *conf_fp;
276         char conf_alias[LINE_MAX];
277         char *conf_realname;
278         int conf_length;
279
280         if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
281             (used = calloc(argc, sizeof(int))) == NULL)
282                 err(1, NULL);
283
284         /* Pull out all network requests. */
285         for (ap = p = argv, np = nargv; *p; ++p)
286                 if (index(*p, '@'))
287                         *np++ = *p;
288                 else
289                         *ap++ = *p;
290
291         *np++ = NULL;
292         *ap++ = NULL;
293
294         if (!*argv)
295                 goto net;
296
297         /*
298          * Mark any arguments beginning with '/' as invalid so that we 
299          * don't accidently confuse them with expansions from finger.conf
300          */
301         for (p = argv, ip = used; *p; ++p, ++ip)
302             if (**p == '/') {
303                 *ip = 1;
304                 warnx("%s: no such user", *p);
305             }
306
307         /*
308          * Traverse the finger alias configuration file of the form
309          * alias:(user|alias), ignoring comment lines beginning '#'.
310          */
311         if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
312             while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
313                 conf_length = strlen(conf_alias);
314                 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
315                     continue;
316                 conf_alias[conf_length] = '\0';      /* Remove trailing LF */
317                 if ((conf_realname = strchr(conf_alias, ':')) == NULL)
318                     continue;
319                 *conf_realname = '\0';               /* Replace : with NUL */
320                 for (p = argv; *p; ++p) {
321                     if (strcmp(*p, conf_alias) == 0) {
322                         if ((*p = strdup(conf_realname+1)) == NULL) {
323                             err(1, NULL);
324                         }
325                     }
326                 }
327             }
328             (void)fclose(conf_fp);
329         }
330
331         /*
332          * Traverse the list of possible login names and check the login name
333          * and real name against the name specified by the user. If the name
334          * begins with a '/', try to read the file of that name instead of
335          * gathering the traditional finger information.
336          */
337         if (mflag)
338                 for (p = argv, ip = used; *p; ++p, ++ip) {
339                         if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
340                                 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
341                                         enter_person(pw);
342                                 else if (!*ip)
343                                         warnx("%s: no such user", *p);
344                         }
345                 }
346         else {
347                 while ((pw = getpwent()) != NULL) {
348                         for (p = argv, ip = used; *p; ++p, ++ip)
349                                 if (**p == '/' && *ip != 1
350                                     && show_text("", *p, ""))
351                                         *ip = 1;
352                                 else if (match(pw, *p) && !hide(pw)) {
353                                         enter_person(pw);
354                                         *ip = 1;
355                                 }
356                 }
357                 for (p = argv, ip = used; *p; ++p, ++ip)
358                         if (!*ip)
359                                 warnx("%s: no such user", *p);
360         }
361
362         /* Handle network requests. */
363 net:    for (p = nargv; *p;) {
364                 netfinger(*p++);
365                 if (*p || entries)
366                     printf("\n");
367         }
368
369         free(used);
370         if (entries == 0)
371                 return;
372
373         if (kflag)
374                 return;
375
376         /*
377          * Scan thru the list of users currently logged in, saving
378          * appropriate data whenever a match occurs.
379          */
380         setutxent();
381         while ((user = getutxent()) != NULL) {
382                 if (user->ut_type != USER_PROCESS)
383                         continue;
384                 if ((pn = find_person(user->ut_user)) == NULL)
385                         continue;
386                 enter_where(user, pn);
387         }
388         endutxent();
389         if (db)
390                 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
391                         PERSON *tmp;
392
393                         r = (*db->seq)(db, &key, &data, sflag1);
394                         if (r == -1)
395                                 err(1, "db seq");
396                         if (r == 1)
397                                 break;
398                         memmove(&tmp, data.data, sizeof tmp);
399                         enter_lastlog(tmp);
400                 }
401 }