xref: /openbsd/usr.bin/finger/finger.c (revision d7259957)
1 /*	$OpenBSD: finger.c,v 1.28 2022/12/04 23:50:48 cheloha 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/stat.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <limits.h>
66 #include <err.h>
67 #include "finger.h"
68 #include "extern.h"
69 
70 time_t now;
71 int entries, lflag, sflag, mflag, oflag, pplan, Mflag;
72 char tbuf[1024];
73 PERSON *htab[HSIZE];
74 PERSON *phead, *ptail;
75 
76 int
main(int argc,char * argv[])77 main(int argc, char *argv[])
78 {
79 	extern int optind;
80 	extern char *__progname;
81 	int ch;
82 	char domain[HOST_NAME_MAX+1];
83 	struct stat sb;
84 
85 	oflag = 1;		/* default to old "office" behavior */
86 
87 	while ((ch = getopt(argc, argv, "lmMpsho")) != -1)
88 		switch(ch) {
89 		case 'l':
90 			lflag = 1;		/* long format */
91 			break;
92 		case 'm':
93 			mflag = 1;		/* force exact match of names */
94 			break;
95 		case 'M':
96 			Mflag = 1;		/* allow name matching */
97 			break;
98 		case 'p':
99 			pplan = 1;		/* don't show .plan/.project */
100 			break;
101 		case 's':
102 			sflag = 1;		/* short format */
103 			break;
104 		case 'h':
105 			oflag = 0;		/* remote host info */
106 			break;
107 		case 'o':
108 			oflag = 1;		/* office info */
109 			break;
110 		default:
111 			(void)fprintf(stderr,
112 			    "usage: %s [-hlMmops] [login ...]\n", __progname);
113 			exit(1);
114 		}
115 	argc -= optind;
116 	argv += optind;
117 
118 	/* If a domainname is set, increment mflag. */
119 	if ((getdomainname(domain, sizeof(domain)) == 0) && domain[0])
120 		mflag++;
121 	/* If _PATH_MP_DB is larger than 1MB, increment mflag. */
122 	if (stat(_PATH_MP_DB, &sb) == 0) {
123 		if (sb.st_size > 1048576)
124 			mflag++;
125 	}
126 
127 	if (pledge("stdio rpath getpw dns inet", NULL) == -1)
128 		err(1, "pledge");
129 
130 	(void)time(&now);
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 (pledge("stdio rpath getpw", NULL) == -1)
138 			err(1, "pledge");
139 
140 		if (!lflag)
141 			sflag = 1;	/* if -l not explicit, force -s */
142 		loginlist();
143 		if (entries == 0)
144 			(void)printf("No one logged on.\n");
145 	} else {
146 		userlist(argc, argv);
147 		/*
148 		 * Assign explicit "large" format if names given and -s not
149 		 * explicitly stated.  Force the -l AFTER we get names so any
150 		 * remote finger attempts specified won't be mishandled.
151 		 */
152 		if (!sflag)
153 			lflag = 1;	/* if -s not explicit, force -l */
154 	}
155 	if (entries != 0) {
156 		if (lflag)
157 			lflag_print();
158 		else
159 			sflag_print();
160 	}
161 	exit(0);
162 }
163 
164 void
loginlist(void)165 loginlist(void)
166 {
167 	PERSON *pn;
168 	struct passwd *pw;
169 	struct utmp user;
170 	char name[UT_NAMESIZE + 1];
171 
172 	if (!freopen(_PATH_UTMP, "r", stdin))
173 		err(2, _PATH_UTMP);
174 	name[UT_NAMESIZE] = '\0';
175 	setpassent(1);
176 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
177 		if (!user.ut_name[0])
178 			continue;
179 		if ((pn = find_person(user.ut_name)) == NULL) {
180 			bcopy(user.ut_name, name, UT_NAMESIZE);
181 			if ((pw = getpwnam(name)) == NULL)
182 				continue;
183 			pn = enter_person(pw);
184 		}
185 		enter_where(&user, pn);
186 	}
187 	endpwent();
188 	for (pn = phead; lflag && pn != NULL; pn = pn->next)
189 		enter_lastlog(pn);
190 }
191 
192 void
userlist(int argc,char ** argv)193 userlist(int argc, char **argv)
194 {
195 	int i;
196 	PERSON *pn;
197 	PERSON *nethead, **nettail;
198 	struct utmp user;
199 	struct passwd *pw;
200 	int dolocal, *used;
201 
202 	if (!(used = calloc((u_int)argc, (u_int)sizeof(int))))
203 		err(2, "malloc");
204 
205 	/* pull out all network requests */
206 	for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
207 		if (!strchr(argv[i], '@')) {
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;
221 
222 	if (nettail == &nethead)
223 		if (pledge("stdio rpath getpw", NULL) == -1)
224 			err(1, "pledge");
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 	 */
230 	setpassent(1);
231 	if ((mflag - Mflag) > 0) {
232 		for (i = 0; i < argc; i++)
233 			if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
234 				enter_person(pw);
235 				used[i] = 1;
236 			}
237 	} else while ((pw = getpwent()) != NULL)
238 		for (i = 0; i < argc; i++)
239 			if (used[i] >= 0 &&
240 			    (!strcasecmp(pw->pw_name, argv[i]) ||
241 			    match(pw, argv[i]))) {
242 				enter_person(pw);
243 				used[i] = 1;
244 			}
245 	endpwent();
246 
247 	/* list errors */
248 	for (i = 0; i < argc; i++)
249 		if (!used[i])
250 			warnx("%s: no such user.", argv[i]);
251 
252 	/* handle network requests */
253 net:	for (pn = nethead; pn; pn = pn->next) {
254 		netfinger(pn->name);
255 		if (pn->next || entries)
256 			putchar('\n');
257 	}
258 
259 	free(used);
260 	if (entries == 0)
261 		return;
262 
263 	/*
264 	 * Scan thru the list of users currently logged in, saving
265 	 * appropriate data whenever a match occurs.
266 	 */
267 	if (!freopen(_PATH_UTMP, "r", stdin))
268 		err(1, _PATH_UTMP);
269 	while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
270 		if (!user.ut_name[0])
271 			continue;
272 		if ((pn = find_person(user.ut_name)) == NULL)
273 			continue;
274 		enter_where(&user, pn);
275 	}
276 	for (pn = phead; pn != NULL; pn = pn->next)
277 		enter_lastlog(pn);
278 }
279