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