Annotation of src/usr.bin/finger/finger.c, Revision 1.21
1.21 ! deraadt 1: /* $OpenBSD: finger.c,v 1.20 2015/08/20 22:32:41 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: /*
46: * Finger prints out information about users. It is not portable since
47: * certain fields (e.g. the full user name, office, and phone numbers) are
48: * extracted from the gecos field of the passwd file which other UNIXes
49: * may not have or may use for other things.
50: *
51: * There are currently two output formats; the short format is one line
52: * per user and displays login name, tty, login time, real name, idle time,
1.7 kstailey 53: * and either remote host information (default) or office location/phone
54: * number, depending on if -h or -o is used respectively.
55: * The long format gives the same information (in a more legible format) as
56: * well as home directory, shell, mail info, and .plan/.project files.
1.1 deraadt 57: */
58:
59: #include <sys/file.h>
1.9 downsj 60: #include <sys/stat.h>
1.1 deraadt 61: #include <stdio.h>
62: #include <stdlib.h>
1.7 kstailey 63: #include <string.h>
64: #include <time.h>
1.11 deraadt 65: #include <unistd.h>
1.19 deraadt 66: #include <limits.h>
1.8 mickey 67: #include <err.h>
1.1 deraadt 68: #include "finger.h"
1.7 kstailey 69: #include "extern.h"
1.1 deraadt 70:
71: time_t now;
1.7 kstailey 72: int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
1.1 deraadt 73: char tbuf[1024];
1.16 tedu 74: PERSON *htab[HSIZE];
75: PERSON *phead, *ptail;
1.1 deraadt 76:
1.3 deraadt 77: int
1.14 deraadt 78: main(int argc, char *argv[])
1.1 deraadt 79: {
80: extern int optind;
1.8 mickey 81: extern char *__progname;
1.1 deraadt 82: int ch;
1.19 deraadt 83: char domain[HOST_NAME_MAX+1];
1.9 downsj 84: struct stat sb;
1.1 deraadt 85:
1.7 kstailey 86: oflag = 1; /* default to old "office" behavior */
87:
88: while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
1.1 deraadt 89: switch(ch) {
90: case 'l':
91: lflag = 1; /* long format */
92: break;
93: case 'm':
94: mflag = 1; /* force exact match of names */
95: break;
1.4 downsj 96: case 'M':
97: Mflag = 1; /* allow name matching */
98: break;
1.1 deraadt 99: case 'p':
100: pplan = 1; /* don't show .plan/.project */
101: break;
102: case 's':
103: sflag = 1; /* short format */
104: break;
1.7 kstailey 105: case 'h':
106: oflag = 0; /* remote host info */
107: break;
108: case 'o':
109: oflag = 1; /* office info */
110: break;
1.1 deraadt 111: case '?':
112: default:
113: (void)fprintf(stderr,
1.15 jmc 114: "usage: %s [-hlMmops] [login ...]\n", __progname);
1.1 deraadt 115: exit(1);
116: }
117: argc -= optind;
118: argv += optind;
119:
1.9 downsj 120: /* If a domainname is set, increment mflag. */
1.11 deraadt 121: if ((getdomainname(domain, sizeof(domain)) == 0) && domain[0])
1.4 downsj 122: mflag++;
1.9 downsj 123: /* If _PATH_MP_DB is larger than 1MB, increment mflag. */
124: if (stat(_PATH_MP_DB, &sb) == 0) {
125: if (sb.st_size > 1048576)
126: mflag++;
127: }
1.4 downsj 128:
1.21 ! deraadt 129: if (tame("stdio getpw rpath inet", NULL) == -1)
! 130: err(1, "tame");
! 131:
1.1 deraadt 132: (void)time(&now);
133: setpassent(1);
134: if (!*argv) {
135: /*
136: * Assign explicit "small" format if no names given and -l
137: * not selected. Force the -s BEFORE we get names so proper
138: * screening will be done.
139: */
1.21 ! deraadt 140: if (tame("stdio getpw rpath", NULL) == -1)
! 141: err(1, "tame");
1.1 deraadt 142: if (!lflag)
143: sflag = 1; /* if -l not explicit, force -s */
144: loginlist();
145: if (entries == 0)
146: (void)printf("No one logged on.\n");
147: } else {
148: userlist(argc, argv);
149: /*
150: * Assign explicit "large" format if names given and -s not
151: * explicitly stated. Force the -l AFTER we get names so any
152: * remote finger attempts specified won't be mishandled.
153: */
154: if (!sflag)
155: lflag = 1; /* if -s not explicit, force -l */
156: }
157: if (entries != 0) {
158: if (lflag)
159: lflag_print();
160: else
161: sflag_print();
162: }
163: exit(0);
164: }
165:
1.7 kstailey 166: void
1.14 deraadt 167: loginlist(void)
1.1 deraadt 168: {
1.7 kstailey 169: PERSON *pn;
1.1 deraadt 170: struct passwd *pw;
171: struct utmp user;
172: char name[UT_NAMESIZE + 1];
173:
1.8 mickey 174: if (!freopen(_PATH_UTMP, "r", stdin))
175: err(2, _PATH_UTMP);
1.7 kstailey 176: name[UT_NAMESIZE] = '\0';
1.1 deraadt 177: while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
178: if (!user.ut_name[0])
179: continue;
180: if ((pn = find_person(user.ut_name)) == NULL) {
181: bcopy(user.ut_name, name, UT_NAMESIZE);
182: if ((pw = getpwnam(name)) == NULL)
183: continue;
184: pn = enter_person(pw);
185: }
186: enter_where(&user, pn);
187: }
188: for (pn = phead; lflag && pn != NULL; pn = pn->next)
189: enter_lastlog(pn);
190: }
191:
1.3 deraadt 192: void
1.14 deraadt 193: userlist(int argc, char **argv)
1.1 deraadt 194: {
1.12 mpech 195: int i;
196: PERSON *pn;
1.1 deraadt 197: PERSON *nethead, **nettail;
198: struct utmp user;
199: struct passwd *pw;
200: int dolocal, *used;
201:
1.20 deraadt 202: if (!(used = calloc((u_int)argc, (u_int)sizeof(int))))
1.8 mickey 203: err(2, "malloc");
1.1 deraadt 204:
205: /* pull out all network requests */
206: for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
1.6 millert 207: if (!strchr(argv[i], '@')) {
1.1 deraadt 208: dolocal = 1;
209: continue;
210: }
211: pn = palloc();
212: *nettail = pn;
213: nettail = &pn->next;
214: pn->name = argv[i];
215: used[i] = -1;
216: }
217: *nettail = NULL;
218:
219: if (!dolocal)
220: goto net;
1.21 ! deraadt 221:
! 222: if (nettail == &nethead)
! 223: if (tame("stdio getpw rpath", NULL) == -1)
! 224: err(1, "tame");
1.1 deraadt 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:
1.18 nicm 257: free(used);
1.1 deraadt 258: if (entries == 0)
259: return;
260:
261: /*
262: * Scan thru the list of users currently logged in, saving
263: * appropriate data whenever a match occurs.
264: */
1.8 mickey 265: if (!freopen(_PATH_UTMP, "r", stdin))
266: err(1, _PATH_UTMP);
1.1 deraadt 267: while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
268: if (!user.ut_name[0])
269: continue;
270: if ((pn = find_person(user.ut_name)) == NULL)
271: continue;
272: enter_where(&user, pn);
273: }
274: for (pn = phead; pn != NULL; pn = pn->next)
275: enter_lastlog(pn);
276: }