Fix UTIME_OMIT handling
[dragonfly.git] / usr.bin / finger / finger.c
blob87a162f8afa7566e4e5320fb4a56a7daadb1a8f1
1 /*
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
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. 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.
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.
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)finger.c 8.5 (Berkeley) 5/4/95
34 * $FreeBSD: src/usr.bin/finger/finger.c,v 1.15.2.9 2002/07/29 18:52:52 ume Exp $
35 * $DragonFly: src/usr.bin/finger/finger.c,v 1.6 2008/06/05 18:06:33 swildner Exp $
39 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
40 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
41 * Unread since ...".)
42 * - 4 digit phone extensions (3210 is printed as x3210.)
43 * - host/office toggling in short format with -h & -o.
44 * - short day names (`Tue' printed instead of `Jun 21' if the
45 * login time is < 6 days.
49 * Finger prints out information about users. It is not portable since
50 * certain fields (e.g. the full user name, office, and phone numbers) are
51 * extracted from the gecos field of the passwd file which other UNIXes
52 * may not have or may use for other things.
54 * There are currently two output formats; the short format is one line
55 * per user and displays login name, tty, login time, real name, idle time,
56 * and either remote host information (default) or office location/phone
57 * number, depending on if -h or -o is used respectively.
58 * The long format gives the same information (in a more legible format) as
59 * well as home directory, shell, mail info, and .plan/.project files.
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <db.h>
65 #include <err.h>
66 #include <pwd.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <time.h>
71 #include <unistd.h>
72 #include "utmpentry.h"
73 #include <locale.h>
75 #include "finger.h"
76 #include "pathnames.h"
78 DB *db;
79 time_t now;
80 int entries, gflag, lflag, mflag, pplan, sflag, oflag, eightflag;
81 sa_family_t family = PF_UNSPEC;
82 int d_first = -1;
83 char tbuf[1024];
85 static void loginlist(void);
86 static int option(int, char **);
87 static void usage(void);
88 static void userlist(int, char **);
90 static int
91 option(int argc, char **argv)
93 int ch;
95 optind = 1; /* reset getopt */
97 while ((ch = getopt(argc, argv, "468glmpsho")) != -1)
98 switch(ch) {
99 case '4':
100 family = AF_INET;
101 break;
102 case '6':
103 family = AF_INET6;
104 break;
105 case '8':
106 eightflag = 1; /* allow 8-bit passthrough */
107 break;
108 case 'g':
109 gflag = 1;
110 break;
111 case 'l':
112 lflag = 1; /* long format */
113 break;
114 case 'm':
115 mflag = 1; /* force exact match of names */
116 break;
117 case 'p':
118 pplan = 1; /* don't show .plan/.project */
119 break;
120 case 's':
121 sflag = 1; /* short format */
122 break;
123 case 'h':
124 oflag = 0; /* remote host info */
125 break;
126 case 'o':
127 oflag = 1; /* office info */
128 break;
129 case '?':
130 default:
131 usage();
134 return optind;
137 static void
138 usage(void)
140 fprintf(stderr, "usage: finger [-468lmpshoT] [login ...]\n");
141 exit(1);
145 main(int argc, char **argv)
147 int envargc, argcnt;
148 char *envargv[3];
149 struct passwd *pw;
150 static char myname[] = "finger";
152 if (getuid() == 0 || geteuid() == 0) {
153 if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
154 setgid(pw->pw_gid);
155 setuid(pw->pw_uid);
156 } else {
157 setgid(UNPRIV_UGID);
158 setuid(UNPRIV_UGID);
162 (void) setlocale(LC_ALL, "");
164 /* remove this line to get remote host */
165 oflag = 1; /* default to old "office" behavior */
168 * Process environment variables followed by command line arguments.
170 if ((envargv[1] = getenv("FINGER"))) {
171 envargc = 2;
172 envargv[0] = myname;
173 envargv[2] = NULL;
174 option(envargc, envargv);
177 argcnt = option(argc, argv);
178 argc -= argcnt;
179 argv += argcnt;
181 time(&now);
182 setpassent(1);
183 if (!*argv) {
185 * Assign explicit "small" format if no names given and -l
186 * not selected. Force the -s BEFORE we get names so proper
187 * screening will be done.
189 if (!lflag)
190 sflag = 1; /* if -l not explicit, force -s */
191 loginlist();
192 if (entries == 0)
193 printf("No one logged on.\n");
194 } else {
195 userlist(argc, argv);
197 * Assign explicit "large" format if names given and -s not
198 * explicitly stated. Force the -l AFTER we get names so any
199 * remote finger attempts specified won't be mishandled.
201 if (!sflag)
202 lflag = 1; /* if -s not explicit, force -l */
204 if (entries) {
205 if (lflag)
206 lflag_print();
207 else
208 sflag_print();
210 return (0);
213 static void
214 loginlist(void)
216 PERSON *pn;
217 DBT data, key;
218 struct passwd *pw;
219 struct utmpentry *ep;
220 int r, sflag1;
222 getutentries(NULL, &ep);
223 for (; ep; ep = ep->next) {
224 if ((pn = find_person(ep->name)) == NULL) {
225 if ((pw = getpwnam(ep->name)) == NULL)
226 continue;
227 if (hide(pw))
228 continue;
229 pn = enter_person(pw);
231 enter_where(ep, pn);
233 if (db && lflag)
234 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
235 PERSON *tmp;
237 r = (*db->seq)(db, &key, &data, sflag1);
238 if (r == -1)
239 err(1, "db seq");
240 if (r == 1)
241 break;
242 memmove(&tmp, data.data, sizeof tmp);
243 enter_lastlog(tmp);
247 static void
248 userlist(int argc, char **argv)
250 PERSON *pn;
251 DBT data, key;
252 struct utmpentry *ep;
253 struct passwd *pw;
254 int r, sflag1, *used, *ip;
255 char **ap, **nargv, **np, **p;
256 FILE *conf_fp;
257 char conf_alias[LINE_MAX];
258 char *conf_realname;
259 int conf_length;
261 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
262 (used = calloc(argc, sizeof(int))) == NULL)
263 err(1, NULL);
265 /* Pull out all network requests. */
266 for (ap = p = argv, np = nargv; *p; ++p)
267 if (strchr(*p, '@'))
268 *np++ = *p;
269 else
270 *ap++ = *p;
272 *np++ = NULL;
273 *ap++ = NULL;
275 if (!*argv)
276 goto net;
279 * Mark any arguments beginning with '/' as invalid so that we
280 * don't accidently confuse them with expansions from finger.conf
282 for (p = argv, ip = used; *p; ++p, ++ip)
283 if (**p == '/') {
284 *ip = 1;
285 warnx("%s: no such user", *p);
289 * Traverse the finger alias configuration file of the form
290 * alias:(user|alias), ignoring comment lines beginning '#'.
292 if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
293 while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
294 conf_length = strlen(conf_alias);
295 if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
296 continue;
297 conf_alias[conf_length] = '\0'; /* Remove trailing LF */
298 if ((conf_realname = strchr(conf_alias, ':')) == NULL)
299 continue;
300 *conf_realname = '\0'; /* Replace : with NUL */
301 for (p = argv; *p; ++p) {
302 if (strcmp(*p, conf_alias) == 0) {
303 if ((*p = strdup(conf_realname+1)) == NULL) {
304 err(1, NULL);
309 fclose(conf_fp);
313 * Traverse the list of possible login names and check the login name
314 * and real name against the name specified by the user. If the name
315 * begins with a '/', try to read the file of that name instead of
316 * gathering the traditional finger information.
318 if (mflag)
319 for (p = argv, ip = used; *p; ++p, ++ip) {
320 if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
321 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
322 enter_person(pw);
323 else if (!*ip)
324 warnx("%s: no such user", *p);
327 else {
328 while ((pw = getpwent()) != NULL) {
329 for (p = argv, ip = used; *p; ++p, ++ip)
330 if (**p == '/' && *ip != 1
331 && show_text("", *p, ""))
332 *ip = 1;
333 else if (match(pw, *p) && !hide(pw)) {
334 enter_person(pw);
335 *ip = 1;
338 for (p = argv, ip = used; *p; ++p, ++ip)
339 if (!*ip)
340 warnx("%s: no such user", *p);
343 /* Handle network requests. */
344 net: for (p = nargv; *p;) {
345 netfinger(*p++);
346 if (*p || entries)
347 printf("\n");
350 if (entries == 0)
351 return;
354 * Scan thru the list of users currently logged in, saving
355 * appropriate data whenever a match occurs.
357 getutentries(NULL, &ep);
358 for (; ep; ep = ep->next) {
359 if ((pn = find_person(ep->name)) == NULL)
360 continue;
361 enter_where(ep, pn);
363 if (db)
364 for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
365 PERSON *tmp;
367 r = (*db->seq)(db, &key, &data, sflag1);
368 if (r == -1)
369 err(1, "db seq");
370 if (r == 1)
371 break;
372 memmove(&tmp, data.data, sizeof tmp);
373 enter_lastlog(tmp);