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