xref: /minix3/usr.bin/finger/finger.c (revision 406cdd95a6bb64607d1091a36d0958ce7043a81d)
1*406cdd95SThomas Cort /*	$NetBSD: finger.c,v 1.29 2009/04/12 06:18:54 lukem Exp $	*/
2*406cdd95SThomas Cort 
3*406cdd95SThomas Cort /*
4*406cdd95SThomas Cort  * Copyright (c) 1989, 1993
5*406cdd95SThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*406cdd95SThomas Cort  *
7*406cdd95SThomas Cort  * This code is derived from software contributed to Berkeley by
8*406cdd95SThomas Cort  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9*406cdd95SThomas Cort  *
10*406cdd95SThomas Cort  * Redistribution and use in source and binary forms, with or without
11*406cdd95SThomas Cort  * modification, are permitted provided that the following conditions
12*406cdd95SThomas Cort  * are met:
13*406cdd95SThomas Cort  * 1. Redistributions of source code must retain the above copyright
14*406cdd95SThomas Cort  *    notice, this list of conditions and the following disclaimer.
15*406cdd95SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
16*406cdd95SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
17*406cdd95SThomas Cort  *    documentation and/or other materials provided with the distribution.
18*406cdd95SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
19*406cdd95SThomas Cort  *    may be used to endorse or promote products derived from this software
20*406cdd95SThomas Cort  *    without specific prior written permission.
21*406cdd95SThomas Cort  *
22*406cdd95SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*406cdd95SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*406cdd95SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*406cdd95SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*406cdd95SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*406cdd95SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*406cdd95SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*406cdd95SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*406cdd95SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*406cdd95SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*406cdd95SThomas Cort  * SUCH DAMAGE.
33*406cdd95SThomas Cort  */
34*406cdd95SThomas Cort 
35*406cdd95SThomas Cort /*
36*406cdd95SThomas Cort  * Luke Mewburn <lukem@NetBSD.org> added the following on 961121:
37*406cdd95SThomas Cort  *    - mail status ("No Mail", "Mail read:...", or "New Mail ...,
38*406cdd95SThomas Cort  *	Unread since ...".)
39*406cdd95SThomas Cort  *    - 4 digit phone extensions (3210 is printed as x3210.)
40*406cdd95SThomas Cort  *    - host/office toggling in short format with -h & -o.
41*406cdd95SThomas Cort  *    - short day names (`Tue' printed instead of `Jun 21' if the
42*406cdd95SThomas Cort  *	login time is < 6 days.
43*406cdd95SThomas Cort  */
44*406cdd95SThomas Cort 
45*406cdd95SThomas Cort #include <sys/cdefs.h>
46*406cdd95SThomas Cort #ifndef lint
47*406cdd95SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
48*406cdd95SThomas Cort  The Regents of the University of California.  All rights reserved.");
49*406cdd95SThomas Cort #endif /* not lint */
50*406cdd95SThomas Cort 
51*406cdd95SThomas Cort #ifndef lint
52*406cdd95SThomas Cort #if 0
53*406cdd95SThomas Cort static char sccsid[] = "@(#)finger.c	8.5 (Berkeley) 5/4/95";
54*406cdd95SThomas Cort #else
55*406cdd95SThomas Cort __RCSID("$NetBSD: finger.c,v 1.29 2009/04/12 06:18:54 lukem Exp $");
56*406cdd95SThomas Cort #endif
57*406cdd95SThomas Cort #endif /* not lint */
58*406cdd95SThomas Cort 
59*406cdd95SThomas Cort /*
60*406cdd95SThomas Cort  * Finger prints out information about users.  It is not portable since
61*406cdd95SThomas Cort  * certain fields (e.g. the full user name, office, and phone numbers) are
62*406cdd95SThomas Cort  * extracted from the gecos field of the passwd file which other UNIXes
63*406cdd95SThomas Cort  * may not have or may use for other things.
64*406cdd95SThomas Cort  *
65*406cdd95SThomas Cort  * There are currently two output formats; the short format is one line
66*406cdd95SThomas Cort  * per user and displays login name, tty, login time, real name, idle time,
67*406cdd95SThomas Cort  * and either remote host information (default) or office location/phone
68*406cdd95SThomas Cort  * number, depending on if -h or -o is used respectively.
69*406cdd95SThomas Cort  * The long format gives the same information (in a more legible format) as
70*406cdd95SThomas Cort  * well as home directory, shell, mail info, and .plan/.project files.
71*406cdd95SThomas Cort  */
72*406cdd95SThomas Cort 
73*406cdd95SThomas Cort #include <sys/param.h>
74*406cdd95SThomas Cort 
75*406cdd95SThomas Cort #include <db.h>
76*406cdd95SThomas Cort #include <err.h>
77*406cdd95SThomas Cort #include <errno.h>
78*406cdd95SThomas Cort #include <fcntl.h>
79*406cdd95SThomas Cort #include <pwd.h>
80*406cdd95SThomas Cort #include <stdio.h>
81*406cdd95SThomas Cort #include <stdlib.h>
82*406cdd95SThomas Cort #include <string.h>
83*406cdd95SThomas Cort #include <time.h>
84*406cdd95SThomas Cort #include <unistd.h>
85*406cdd95SThomas Cort 
86*406cdd95SThomas Cort #include <locale.h>
87*406cdd95SThomas Cort #include <langinfo.h>
88*406cdd95SThomas Cort 
89*406cdd95SThomas Cort #include "utmpentry.h"
90*406cdd95SThomas Cort 
91*406cdd95SThomas Cort #include "finger.h"
92*406cdd95SThomas Cort #include "extern.h"
93*406cdd95SThomas Cort 
94*406cdd95SThomas Cort DB *db;
95*406cdd95SThomas Cort time_t now;
96*406cdd95SThomas Cort int entries, gflag, lflag, mflag, oflag, sflag, eightflag, pplan;
97*406cdd95SThomas Cort char tbuf[1024];
98*406cdd95SThomas Cort struct utmpentry *ehead;
99*406cdd95SThomas Cort 
100*406cdd95SThomas Cort static void loginlist(void);
101*406cdd95SThomas Cort static void userlist(int, char **);
102*406cdd95SThomas Cort int main(int, char **);
103*406cdd95SThomas Cort 
104*406cdd95SThomas Cort int
main(int argc,char ** argv)105*406cdd95SThomas Cort main(int argc, char **argv)
106*406cdd95SThomas Cort {
107*406cdd95SThomas Cort 	int ch;
108*406cdd95SThomas Cort 
109*406cdd95SThomas Cort 	/* Allow user's locale settings to affect character output. */
110*406cdd95SThomas Cort 	setlocale(LC_CTYPE, "");
111*406cdd95SThomas Cort 
112*406cdd95SThomas Cort 	/*
113*406cdd95SThomas Cort 	 * Reset back to the C locale, unless we are using a known
114*406cdd95SThomas Cort 	 * single-byte 8-bit locale.
115*406cdd95SThomas Cort 	 */
116*406cdd95SThomas Cort 	if (strncmp(nl_langinfo(CODESET), "ISO8859-", 8))
117*406cdd95SThomas Cort 		setlocale(LC_CTYPE, "C");
118*406cdd95SThomas Cort 
119*406cdd95SThomas Cort 	oflag = 1;		/* default to old "office" behavior */
120*406cdd95SThomas Cort 
121*406cdd95SThomas Cort 	while ((ch = getopt(argc, argv, "lmpshog8")) != -1)
122*406cdd95SThomas Cort 		switch(ch) {
123*406cdd95SThomas Cort 		case 'l':
124*406cdd95SThomas Cort 			lflag = 1;		/* long format */
125*406cdd95SThomas Cort 			break;
126*406cdd95SThomas Cort 		case 'm':
127*406cdd95SThomas Cort 			mflag = 1;		/* force exact match of names */
128*406cdd95SThomas Cort 			break;
129*406cdd95SThomas Cort 		case 'p':
130*406cdd95SThomas Cort 			pplan = 1;		/* don't show .plan/.project */
131*406cdd95SThomas Cort 			break;
132*406cdd95SThomas Cort 		case 's':
133*406cdd95SThomas Cort 			sflag = 1;		/* short format */
134*406cdd95SThomas Cort 			break;
135*406cdd95SThomas Cort 		case 'h':
136*406cdd95SThomas Cort 			oflag = 0;		/* remote host info */
137*406cdd95SThomas Cort 			break;
138*406cdd95SThomas Cort 		case 'o':
139*406cdd95SThomas Cort 			oflag = 1;		/* office info */
140*406cdd95SThomas Cort 			break;
141*406cdd95SThomas Cort 		case 'g':
142*406cdd95SThomas Cort 			gflag = 1;		/* no gecos info, besides name */
143*406cdd95SThomas Cort 			break;
144*406cdd95SThomas Cort 		case '8':
145*406cdd95SThomas Cort 			eightflag = 1;		/* 8-bit pass-through */
146*406cdd95SThomas Cort 			break;
147*406cdd95SThomas Cort 		case '?':
148*406cdd95SThomas Cort 		default:
149*406cdd95SThomas Cort 			(void)fprintf(stderr,
150*406cdd95SThomas Cort 			    "usage: finger [-lmpshog8] [login ...]\n");
151*406cdd95SThomas Cort 			exit(1);
152*406cdd95SThomas Cort 		}
153*406cdd95SThomas Cort 	argc -= optind;
154*406cdd95SThomas Cort 	argv += optind;
155*406cdd95SThomas Cort 
156*406cdd95SThomas Cort 	(void)time(&now);
157*406cdd95SThomas Cort 	setpassent(1);
158*406cdd95SThomas Cort 	entries = getutentries(NULL, &ehead);
159*406cdd95SThomas Cort 	if (argc == 0) {
160*406cdd95SThomas Cort 		/*
161*406cdd95SThomas Cort 		 * Assign explicit "small" format if no names given and -l
162*406cdd95SThomas Cort 		 * not selected.  Force the -s BEFORE we get names so proper
163*406cdd95SThomas Cort 		 * screening will be done.
164*406cdd95SThomas Cort 		 */
165*406cdd95SThomas Cort 		if (!lflag)
166*406cdd95SThomas Cort 			sflag = 1;	/* if -l not explicit, force -s */
167*406cdd95SThomas Cort 		loginlist();
168*406cdd95SThomas Cort 		if (entries == 0)
169*406cdd95SThomas Cort 			(void)printf("No one logged on.\n");
170*406cdd95SThomas Cort 	} else {
171*406cdd95SThomas Cort 		userlist(argc, argv);
172*406cdd95SThomas Cort 		/*
173*406cdd95SThomas Cort 		 * Assign explicit "large" format if names given and -s not
174*406cdd95SThomas Cort 		 * explicitly stated.  Force the -l AFTER we get names so any
175*406cdd95SThomas Cort 		 * remote finger attempts specified won't be mishandled.
176*406cdd95SThomas Cort 		 */
177*406cdd95SThomas Cort 		if (!sflag)
178*406cdd95SThomas Cort 			lflag = 1;	/* if -s not explicit, force -l */
179*406cdd95SThomas Cort 	}
180*406cdd95SThomas Cort 	if (entries) {
181*406cdd95SThomas Cort 		if (lflag)
182*406cdd95SThomas Cort 			lflag_print();
183*406cdd95SThomas Cort 		else
184*406cdd95SThomas Cort 			sflag_print();
185*406cdd95SThomas Cort 	}
186*406cdd95SThomas Cort 	return (0);
187*406cdd95SThomas Cort }
188*406cdd95SThomas Cort 
189*406cdd95SThomas Cort static void
loginlist(void)190*406cdd95SThomas Cort loginlist(void)
191*406cdd95SThomas Cort {
192*406cdd95SThomas Cort 	PERSON *pn;
193*406cdd95SThomas Cort 	DBT data, key;
194*406cdd95SThomas Cort 	struct passwd *pw;
195*406cdd95SThomas Cort 	int r, seqflag;
196*406cdd95SThomas Cort 	struct utmpentry *ep;
197*406cdd95SThomas Cort 
198*406cdd95SThomas Cort 	for (ep = ehead; ep; ep = ep->next) {
199*406cdd95SThomas Cort 		if ((pn = find_person(ep->name)) == NULL) {
200*406cdd95SThomas Cort 			if ((pw = getpwnam(ep->name)) == NULL)
201*406cdd95SThomas Cort 				continue;
202*406cdd95SThomas Cort 			pn = enter_person(pw);
203*406cdd95SThomas Cort 		}
204*406cdd95SThomas Cort 		enter_where(ep, pn);
205*406cdd95SThomas Cort 	}
206*406cdd95SThomas Cort 	if (db && lflag)
207*406cdd95SThomas Cort 		for (seqflag = R_FIRST;; seqflag = R_NEXT) {
208*406cdd95SThomas Cort 			PERSON *tmp;
209*406cdd95SThomas Cort 
210*406cdd95SThomas Cort 			r = (*db->seq)(db, &key, &data, seqflag);
211*406cdd95SThomas Cort 			if (r == -1)
212*406cdd95SThomas Cort 				err(1, "db seq");
213*406cdd95SThomas Cort 			if (r == 1)
214*406cdd95SThomas Cort 				break;
215*406cdd95SThomas Cort 			memmove(&tmp, data.data, sizeof tmp);
216*406cdd95SThomas Cort 			enter_lastlog(tmp);
217*406cdd95SThomas Cort 		}
218*406cdd95SThomas Cort }
219*406cdd95SThomas Cort 
220*406cdd95SThomas Cort static void
userlist(int argc,char ** argv)221*406cdd95SThomas Cort userlist(int argc, char **argv)
222*406cdd95SThomas Cort {
223*406cdd95SThomas Cort 	PERSON *pn;
224*406cdd95SThomas Cort 	DBT data, key;
225*406cdd95SThomas Cort 	struct passwd *pw;
226*406cdd95SThomas Cort 	int r, seqflag, *used, *ip;
227*406cdd95SThomas Cort 	char **ap, **nargv, **np, **p;
228*406cdd95SThomas Cort 	struct utmpentry *ep;
229*406cdd95SThomas Cort 
230*406cdd95SThomas Cort 	if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
231*406cdd95SThomas Cort 	    (used = calloc(argc, sizeof(int))) == NULL)
232*406cdd95SThomas Cort 		err(1, NULL);
233*406cdd95SThomas Cort 
234*406cdd95SThomas Cort 	/* Pull out all network requests. */
235*406cdd95SThomas Cort 	for (ap = p = argv, np = nargv; *p; ++p)
236*406cdd95SThomas Cort 		if (strchr(*p, '@'))
237*406cdd95SThomas Cort 			*np++ = *p;
238*406cdd95SThomas Cort 		else
239*406cdd95SThomas Cort 			*ap++ = *p;
240*406cdd95SThomas Cort 
241*406cdd95SThomas Cort 	*np++ = NULL;
242*406cdd95SThomas Cort 	*ap++ = NULL;
243*406cdd95SThomas Cort 
244*406cdd95SThomas Cort 	if (!*argv)
245*406cdd95SThomas Cort 		goto net;
246*406cdd95SThomas Cort 
247*406cdd95SThomas Cort 	/*
248*406cdd95SThomas Cort 	 * Traverse the list of possible login names and check the login name
249*406cdd95SThomas Cort 	 * and real name against the name specified by the user.
250*406cdd95SThomas Cort 	 */
251*406cdd95SThomas Cort 	if (mflag) {
252*406cdd95SThomas Cort 		for (p = argv; *p; ++p)
253*406cdd95SThomas Cort 			if ((pw = getpwnam(*p)) != NULL)
254*406cdd95SThomas Cort 				enter_person(pw);
255*406cdd95SThomas Cort 			else
256*406cdd95SThomas Cort 				warnx("%s: no such user", *p);
257*406cdd95SThomas Cort 	} else {
258*406cdd95SThomas Cort 		while ((pw = getpwent()) != NULL)
259*406cdd95SThomas Cort 			for (p = argv, ip = used; *p; ++p, ++ip)
260*406cdd95SThomas Cort 				if (match(pw, *p)) {
261*406cdd95SThomas Cort 					enter_person(pw);
262*406cdd95SThomas Cort 					*ip = 1;
263*406cdd95SThomas Cort 				}
264*406cdd95SThomas Cort 		for (p = argv, ip = used; *p; ++p, ++ip)
265*406cdd95SThomas Cort 			if (!*ip)
266*406cdd95SThomas Cort 				warnx("%s: no such user", *p);
267*406cdd95SThomas Cort 	}
268*406cdd95SThomas Cort 
269*406cdd95SThomas Cort 	/* Handle network requests. */
270*406cdd95SThomas Cort net:
271*406cdd95SThomas Cort 	for (p = nargv; *p;)
272*406cdd95SThomas Cort 		netfinger(*p++);
273*406cdd95SThomas Cort 
274*406cdd95SThomas Cort 	if (entries == 0)
275*406cdd95SThomas Cort 		goto done;
276*406cdd95SThomas Cort 
277*406cdd95SThomas Cort 	/*
278*406cdd95SThomas Cort 	 * Scan thru the list of users currently logged in, saving
279*406cdd95SThomas Cort 	 * appropriate data whenever a match occurs.
280*406cdd95SThomas Cort 	 */
281*406cdd95SThomas Cort 	for (ep = ehead; ep; ep = ep->next) {
282*406cdd95SThomas Cort 		if ((pn = find_person(ep->name)) == NULL)
283*406cdd95SThomas Cort 			continue;
284*406cdd95SThomas Cort 		enter_where(ep, pn);
285*406cdd95SThomas Cort 	}
286*406cdd95SThomas Cort 	if (db != NULL)
287*406cdd95SThomas Cort 		for (seqflag = R_FIRST;; seqflag = R_NEXT) {
288*406cdd95SThomas Cort 			PERSON *tmp;
289*406cdd95SThomas Cort 
290*406cdd95SThomas Cort 			r = (*db->seq)(db, &key, &data, seqflag);
291*406cdd95SThomas Cort 			if (r == -1)
292*406cdd95SThomas Cort 				err(1, "db seq");
293*406cdd95SThomas Cort 			if (r == 1)
294*406cdd95SThomas Cort 				break;
295*406cdd95SThomas Cort 			memmove(&tmp, data.data, sizeof tmp);
296*406cdd95SThomas Cort 			enter_lastlog(tmp);
297*406cdd95SThomas Cort 		}
298*406cdd95SThomas Cort done:
299*406cdd95SThomas Cort 	free(nargv);
300*406cdd95SThomas Cort 	free(used);
301*406cdd95SThomas Cort }
302