Annotation of src/usr.bin/finger/finger.c, Revision 1.8
1.8 ! mickey 1: /* $OpenBSD: finger.c,v 1.7 1997/05/30 23:35:51 kstailey 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.8 ! mickey 57: static char rcsid[] = "$OpenBSD: finger.c,v 1.7 1997/05/30 23:35:51 kstailey 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>
76: #include <stdio.h>
77: #include <stdlib.h>
1.7 kstailey 78: #include <string.h>
79: #include <time.h>
1.8 ! mickey 80: #include <err.h>
1.1 deraadt 81: #include "finger.h"
1.7 kstailey 82: #include "extern.h"
1.1 deraadt 83:
84: time_t now;
1.7 kstailey 85: int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
1.1 deraadt 86: char tbuf[1024];
87:
1.3 deraadt 88: int
1.1 deraadt 89: main(argc, argv)
90: int argc;
91: char **argv;
92: {
93: extern int optind;
1.8 ! mickey 94: extern char *__progname;
1.1 deraadt 95: int ch;
1.4 downsj 96: char domain[256];
1.1 deraadt 97:
1.7 kstailey 98: oflag = 1; /* default to old "office" behavior */
99:
100: while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
1.1 deraadt 101: switch(ch) {
102: case 'l':
103: lflag = 1; /* long format */
104: break;
105: case 'm':
106: mflag = 1; /* force exact match of names */
107: break;
1.4 downsj 108: case 'M':
109: Mflag = 1; /* allow name matching */
110: break;
1.1 deraadt 111: case 'p':
112: pplan = 1; /* don't show .plan/.project */
113: break;
114: case 's':
115: sflag = 1; /* short format */
116: break;
1.7 kstailey 117: case 'h':
118: oflag = 0; /* remote host info */
119: break;
120: case 'o':
121: oflag = 1; /* office info */
122: break;
1.1 deraadt 123: case '?':
124: default:
125: (void)fprintf(stderr,
1.8 ! mickey 126: "usage: %s [-lmMpsho] [login ...]\n", __progname);
1.1 deraadt 127: exit(1);
128: }
129: argc -= optind;
130: argv += optind;
131:
1.4 downsj 132: /* if a domainname is set, increment mflag. */
133: if ((getdomainname(&domain, sizeof(domain)) == 0) && domain[0])
134: mflag++;
135:
1.1 deraadt 136: (void)time(&now);
137: setpassent(1);
138: if (!*argv) {
139: /*
140: * Assign explicit "small" format if no names given and -l
141: * not selected. Force the -s BEFORE we get names so proper
142: * screening will be done.
143: */
144: if (!lflag)
145: sflag = 1; /* if -l not explicit, force -s */
146: loginlist();
147: if (entries == 0)
148: (void)printf("No one logged on.\n");
149: } else {
150: userlist(argc, argv);
151: /*
152: * Assign explicit "large" format if names given and -s not
153: * explicitly stated. Force the -l AFTER we get names so any
154: * remote finger attempts specified won't be mishandled.
155: */
156: if (!sflag)
157: lflag = 1; /* if -s not explicit, force -l */
158: }
159: if (entries != 0) {
160: if (lflag)
161: lflag_print();
162: else
163: sflag_print();
164: }
165: exit(0);
166: }
167:
1.7 kstailey 168: void
1.1 deraadt 169: loginlist()
170: {
1.7 kstailey 171: PERSON *pn;
1.1 deraadt 172: struct passwd *pw;
173: struct utmp user;
174: char name[UT_NAMESIZE + 1];
175:
1.8 ! mickey 176: if (!freopen(_PATH_UTMP, "r", stdin))
! 177: err(2, _PATH_UTMP);
1.7 kstailey 178: name[UT_NAMESIZE] = '\0';
1.1 deraadt 179: while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
180: if (!user.ut_name[0])
181: continue;
182: if ((pn = find_person(user.ut_name)) == NULL) {
183: bcopy(user.ut_name, name, UT_NAMESIZE);
184: if ((pw = getpwnam(name)) == NULL)
185: continue;
186: pn = enter_person(pw);
187: }
188: enter_where(&user, pn);
189: }
190: for (pn = phead; lflag && pn != NULL; pn = pn->next)
191: enter_lastlog(pn);
192: }
193:
1.3 deraadt 194: void
1.1 deraadt 195: userlist(argc, argv)
1.7 kstailey 196: int argc;
197: char **argv;
1.1 deraadt 198: {
1.7 kstailey 199: register int i;
1.1 deraadt 200: register PERSON *pn;
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: }