Annotation of src/usr.bin/finger/finger.c, Revision 1.10
1.10 ! aaron 1: /* $OpenBSD: finger.c,v 1.9 1999/12/22 22:30:20 downsj Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1989 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: /*
1.7 kstailey 40: * Luke Mewburn <lukem@netbsd.org> added the following on 961121:
41: * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
42: * Unread since ...".)
43: * - 4 digit phone extensions (3210 is printed as x3210.)
44: * - host/office toggling in short format with -h & -o.
45: * - short day names (`Tue' printed instead of `Jun 21' if the
46: * login time is < 6 days.
1.1 deraadt 47: */
48:
49: #ifndef lint
50: char copyright[] =
51: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
52: All rights reserved.\n";
53: #endif /* not lint */
54:
55: #ifndef lint
56: /*static char sccsid[] = "from: @(#)finger.c 5.22 (Berkeley) 6/29/90";*/
1.10 ! aaron 57: static char rcsid[] = "$OpenBSD: finger.c,v 1.9 1999/12/22 22:30:20 downsj Exp $";
1.1 deraadt 58: #endif /* not lint */
59:
60: /*
61: * Finger prints out information about users. It is not portable since
62: * certain fields (e.g. the full user name, office, and phone numbers) are
63: * extracted from the gecos field of the passwd file which other UNIXes
64: * may not have or may use for other things.
65: *
66: * There are currently two output formats; the short format is one line
67: * per user and displays login name, tty, login time, real name, idle time,
1.7 kstailey 68: * and either remote host information (default) or office location/phone
69: * number, depending on if -h or -o is used respectively.
70: * The long format gives the same information (in a more legible format) as
71: * well as home directory, shell, mail info, and .plan/.project files.
1.1 deraadt 72: */
73:
74: #include <sys/param.h>
75: #include <sys/file.h>
1.9 downsj 76: #include <sys/stat.h>
1.1 deraadt 77: #include <stdio.h>
78: #include <stdlib.h>
1.7 kstailey 79: #include <string.h>
80: #include <time.h>
1.8 mickey 81: #include <err.h>
1.1 deraadt 82: #include "finger.h"
1.7 kstailey 83: #include "extern.h"
1.1 deraadt 84:
85: time_t now;
1.7 kstailey 86: int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
1.1 deraadt 87: char tbuf[1024];
88:
1.3 deraadt 89: int
1.1 deraadt 90: main(argc, argv)
91: int argc;
92: char **argv;
93: {
94: extern int optind;
1.8 mickey 95: extern char *__progname;
1.1 deraadt 96: int ch;
1.10 ! aaron 97: char domain[MAXHOSTNAMELEN];
1.9 downsj 98: struct stat sb;
1.1 deraadt 99:
1.7 kstailey 100: oflag = 1; /* default to old "office" behavior */
101:
102: while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
1.1 deraadt 103: switch(ch) {
104: case 'l':
105: lflag = 1; /* long format */
106: break;
107: case 'm':
108: mflag = 1; /* force exact match of names */
109: break;
1.4 downsj 110: case 'M':
111: Mflag = 1; /* allow name matching */
112: break;
1.1 deraadt 113: case 'p':
114: pplan = 1; /* don't show .plan/.project */
115: break;
116: case 's':
117: sflag = 1; /* short format */
118: break;
1.7 kstailey 119: case 'h':
120: oflag = 0; /* remote host info */
121: break;
122: case 'o':
123: oflag = 1; /* office info */
124: break;
1.1 deraadt 125: case '?':
126: default:
127: (void)fprintf(stderr,
1.8 mickey 128: "usage: %s [-lmMpsho] [login ...]\n", __progname);
1.1 deraadt 129: exit(1);
130: }
131: argc -= optind;
132: argv += optind;
133:
1.9 downsj 134: /* If a domainname is set, increment mflag. */
1.4 downsj 135: if ((getdomainname(&domain, sizeof(domain)) == 0) && domain[0])
136: mflag++;
1.9 downsj 137: /* If _PATH_MP_DB is larger than 1MB, increment mflag. */
138: if (stat(_PATH_MP_DB, &sb) == 0) {
139: if (sb.st_size > 1048576)
140: mflag++;
141: }
1.4 downsj 142:
1.1 deraadt 143: (void)time(&now);
144: setpassent(1);
145: if (!*argv) {
146: /*
147: * Assign explicit "small" format if no names given and -l
148: * not selected. Force the -s BEFORE we get names so proper
149: * screening will be done.
150: */
151: if (!lflag)
152: sflag = 1; /* if -l not explicit, force -s */
153: loginlist();
154: if (entries == 0)
155: (void)printf("No one logged on.\n");
156: } else {
157: userlist(argc, argv);
158: /*
159: * Assign explicit "large" format if names given and -s not
160: * explicitly stated. Force the -l AFTER we get names so any
161: * remote finger attempts specified won't be mishandled.
162: */
163: if (!sflag)
164: lflag = 1; /* if -s not explicit, force -l */
165: }
166: if (entries != 0) {
167: if (lflag)
168: lflag_print();
169: else
170: sflag_print();
171: }
172: exit(0);
173: }
174:
1.7 kstailey 175: void
1.1 deraadt 176: loginlist()
177: {
1.7 kstailey 178: PERSON *pn;
1.1 deraadt 179: struct passwd *pw;
180: struct utmp user;
181: char name[UT_NAMESIZE + 1];
182:
1.8 mickey 183: if (!freopen(_PATH_UTMP, "r", stdin))
184: err(2, _PATH_UTMP);
1.7 kstailey 185: name[UT_NAMESIZE] = '\0';
1.1 deraadt 186: while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
187: if (!user.ut_name[0])
188: continue;
189: if ((pn = find_person(user.ut_name)) == NULL) {
190: bcopy(user.ut_name, name, UT_NAMESIZE);
191: if ((pw = getpwnam(name)) == NULL)
192: continue;
193: pn = enter_person(pw);
194: }
195: enter_where(&user, pn);
196: }
197: for (pn = phead; lflag && pn != NULL; pn = pn->next)
198: enter_lastlog(pn);
199: }
200:
1.3 deraadt 201: void
1.1 deraadt 202: userlist(argc, argv)
1.7 kstailey 203: int argc;
204: char **argv;
1.1 deraadt 205: {
1.7 kstailey 206: register int i;
1.1 deraadt 207: register PERSON *pn;
208: PERSON *nethead, **nettail;
209: struct utmp user;
210: struct passwd *pw;
211: int dolocal, *used;
212:
1.8 mickey 213: if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int))))
214: err(2, "malloc");
1.1 deraadt 215:
216: /* pull out all network requests */
217: for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
1.6 millert 218: if (!strchr(argv[i], '@')) {
1.1 deraadt 219: dolocal = 1;
220: continue;
221: }
222: pn = palloc();
223: *nettail = pn;
224: nettail = &pn->next;
225: pn->name = argv[i];
226: used[i] = -1;
227: }
228: *nettail = NULL;
229:
230: if (!dolocal)
231: goto net;
232:
233: /*
234: * traverse the list of possible login names and check the login name
235: * and real name against the name specified by the user.
236: */
1.4 downsj 237: if ((mflag - Mflag) > 0) {
1.1 deraadt 238: for (i = 0; i < argc; i++)
239: if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
240: enter_person(pw);
241: used[i] = 1;
242: }
1.7 kstailey 243: } else while ((pw = getpwent()) != NULL)
1.1 deraadt 244: for (i = 0; i < argc; i++)
245: if (used[i] >= 0 &&
246: (!strcasecmp(pw->pw_name, argv[i]) ||
247: match(pw, argv[i]))) {
248: enter_person(pw);
249: used[i] = 1;
250: }
251:
252: /* list errors */
253: for (i = 0; i < argc; i++)
254: if (!used[i])
1.8 mickey 255: warnx("%s: no such user.", argv[i]);
1.1 deraadt 256:
257: /* handle network requests */
258: net: for (pn = nethead; pn; pn = pn->next) {
259: netfinger(pn->name);
260: if (pn->next || entries)
261: putchar('\n');
262: }
263:
264: if (entries == 0)
265: return;
266:
267: /*
268: * Scan thru the list of users currently logged in, saving
269: * appropriate data whenever a match occurs.
270: */
1.8 mickey 271: if (!freopen(_PATH_UTMP, "r", stdin))
272: err(1, _PATH_UTMP);
1.1 deraadt 273: while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
274: if (!user.ut_name[0])
275: continue;
276: if ((pn = find_person(user.ut_name)) == NULL)
277: continue;
278: enter_where(&user, pn);
279: }
280: for (pn = phead; pn != NULL; pn = pn->next)
281: enter_lastlog(pn);
282: }