xref: /openbsd-src/usr.bin/finger/finger.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: finger.c,v 1.18 2009/11/12 15:33:21 nicm Exp $	*/
2 
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. Neither the name of the University nor the names of its contributors
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 /*
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.
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,
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.
57  */
58 
59 #include <sys/param.h>
60 #include <sys/file.h>
61 #include <sys/stat.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67 #include <err.h>
68 #include "finger.h"
69 #include "extern.h"
70 
71 time_t now;
72 int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
73 char tbuf[1024];
74 PERSON *htab[HSIZE];
75 PERSON *phead, *ptail;
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	extern int optind;
81 	extern char *__progname;
82 	int ch;
83 	char domain[MAXHOSTNAMELEN];
84 	struct stat sb;
85 
86 	oflag = 1;		/* default to old "office" behavior */
87 
88 	while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
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;
96 		case 'M':
97 			Mflag = 1;		/* allow name matching */
98 			break;
99 		case 'p':
100 			pplan = 1;		/* don't show .plan/.project */
101 			break;
102 		case 's':
103 			sflag = 1;		/* short format */
104 			break;
105 		case 'h':
106 			oflag = 0;		/* remote host info */
107 			break;
108 		case 'o':
109 			oflag = 1;		/* office info */
110 			break;
111 		case '?':
112 		default:
113 			(void)fprintf(stderr,
114 			    "usage: %s [-hlMmops] [login ...]\n", __progname);
115 			exit(1);
116 		}
117 	argc -= optind;
118 	argv += optind;
119 
120 	/* If a domainname is set, increment mflag. */
121 	if ((getdomainname(domain, sizeof(domain)) == 0) && domain[0])
122 		mflag++;
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 	}
128 
129 	(void)time(&now);
130 	setpassent(1);
131 	if (!*argv) {
132 		/*
133 		 * Assign explicit "small" format if no names given and -l
134 		 * not selected.  Force the -s BEFORE we get names so proper
135 		 * screening will be done.
136 		 */
137 		if (!lflag)
138 			sflag = 1;	/* if -l not explicit, force -s */
139 		loginlist();
140 		if (entries == 0)
141 			(void)printf("No one logged on.\n");
142 	} else {
143 		userlist(argc, argv);
144 		/*
145 		 * Assign explicit "large" format if names given and -s not
146 		 * explicitly stated.  Force the -l AFTER we get names so any
147 		 * remote finger attempts specified won't be mishandled.
148 		 */
149 		if (!sflag)
150 			lflag = 1;	/* if -s not explicit, force -l */
151 	}
152 	if (entries != 0) {
153 		if (lflag)
154 			lflag_print();
155 		else
156 			sflag_print();
157 	}
158 	exit(0);
159 }
160 
161 void
162 loginlist(void)
163 {
164 	PERSON *pn;
165 	struct passwd *pw;
166 	struct utmp user;
167 	char name[UT_NAMESIZE + 1];
168 
169 	if (!freopen(_PATH_UTMP, "r", stdin))
170 		err(2, _PATH_UTMP);
171 	name[UT_NAMESIZE] = '\0';
172 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
173 		if (!user.ut_name[0])
174 			continue;
175 		if ((pn = find_person(user.ut_name)) == NULL) {
176 			bcopy(user.ut_name, name, UT_NAMESIZE);
177 			if ((pw = getpwnam(name)) == NULL)
178 				continue;
179 			pn = enter_person(pw);
180 		}
181 		enter_where(&user, pn);
182 	}
183 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
184 		enter_lastlog(pn);
185 }
186 
187 void
188 userlist(int argc, char **argv)
189 {
190 	int i;
191 	PERSON *pn;
192 	PERSON *nethead, **nettail;
193 	struct utmp user;
194 	struct passwd *pw;
195 	int dolocal, *used;
196 
197 	if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int))))
198 		err(2, "malloc");
199 
200 	/* pull out all network requests */
201 	for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
202 		if (!strchr(argv[i], '@')) {
203 			dolocal = 1;
204 			continue;
205 		}
206 		pn = palloc();
207 		*nettail = pn;
208 		nettail = &pn->next;
209 		pn->name = argv[i];
210 		used[i] = -1;
211 	}
212 	*nettail = NULL;
213 
214 	if (!dolocal)
215 		goto net;
216 
217 	/*
218 	 * traverse the list of possible login names and check the login name
219 	 * and real name against the name specified by the user.
220 	 */
221 	if ((mflag - Mflag) > 0) {
222 		for (i = 0; i < argc; i++)
223 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
224 				enter_person(pw);
225 				used[i] = 1;
226 			}
227 	} else while ((pw = getpwent()) != NULL)
228 		for (i = 0; i < argc; i++)
229 			if (used[i] >= 0 &&
230 			    (!strcasecmp(pw->pw_name, argv[i]) ||
231 			    match(pw, argv[i]))) {
232 				enter_person(pw);
233 				used[i] = 1;
234 			}
235 
236 	/* list errors */
237 	for (i = 0; i < argc; i++)
238 		if (!used[i])
239 			warnx("%s: no such user.", argv[i]);
240 
241 	/* handle network requests */
242 net:	for (pn = nethead; pn; pn = pn->next) {
243 		netfinger(pn->name);
244 		if (pn->next || entries)
245 			putchar('\n');
246 	}
247 
248 	free(used);
249 	if (entries == 0)
250 		return;
251 
252 	/*
253 	 * Scan thru the list of users currently logged in, saving
254 	 * appropriate data whenever a match occurs.
255 	 */
256 	if (!freopen(_PATH_UTMP, "r", stdin))
257 		err(1, _PATH_UTMP);
258 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
259 		if (!user.ut_name[0])
260 			continue;
261 		if ((pn = find_person(user.ut_name)) == NULL)
262 			continue;
263 		enter_where(&user, pn);
264 	}
265 	for (pn = phead; pn != NULL; pn = pn->next)
266 		enter_lastlog(pn);
267 }
268