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