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