xref: /minix3/libexec/fingerd/fingerd.c (revision 1d842c6a572919b97a89b154c4c3a6d5f11575ef)
1*1d842c6aSThomas Cort /*	$NetBSD: fingerd.c,v 1.27 2012/03/15 02:02:21 joerg Exp $	*/
2*1d842c6aSThomas Cort 
3*1d842c6aSThomas Cort /*
4*1d842c6aSThomas Cort  * Copyright (c) 1983, 1993
5*1d842c6aSThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*1d842c6aSThomas Cort  *
7*1d842c6aSThomas Cort  * Redistribution and use in source and binary forms, with or without
8*1d842c6aSThomas Cort  * modification, are permitted provided that the following conditions
9*1d842c6aSThomas Cort  * are met:
10*1d842c6aSThomas Cort  * 1. Redistributions of source code must retain the above copyright
11*1d842c6aSThomas Cort  *    notice, this list of conditions and the following disclaimer.
12*1d842c6aSThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
13*1d842c6aSThomas Cort  *    notice, this list of conditions and the following disclaimer in the
14*1d842c6aSThomas Cort  *    documentation and/or other materials provided with the distribution.
15*1d842c6aSThomas Cort  * 3. Neither the name of the University nor the names of its contributors
16*1d842c6aSThomas Cort  *    may be used to endorse or promote products derived from this software
17*1d842c6aSThomas Cort  *    without specific prior written permission.
18*1d842c6aSThomas Cort  *
19*1d842c6aSThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*1d842c6aSThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*1d842c6aSThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*1d842c6aSThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*1d842c6aSThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*1d842c6aSThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*1d842c6aSThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*1d842c6aSThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*1d842c6aSThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*1d842c6aSThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*1d842c6aSThomas Cort  * SUCH DAMAGE.
30*1d842c6aSThomas Cort  */
31*1d842c6aSThomas Cort 
32*1d842c6aSThomas Cort #include <sys/cdefs.h>
33*1d842c6aSThomas Cort #ifndef lint
34*1d842c6aSThomas Cort __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35*1d842c6aSThomas Cort  The Regents of the University of California.  All rights reserved.");
36*1d842c6aSThomas Cort #endif /* not lint */
37*1d842c6aSThomas Cort 
38*1d842c6aSThomas Cort #ifndef lint
39*1d842c6aSThomas Cort #if 0
40*1d842c6aSThomas Cort static char sccsid[] = "from: @(#)fingerd.c	8.1 (Berkeley) 6/4/93";
41*1d842c6aSThomas Cort #else
42*1d842c6aSThomas Cort __RCSID("$NetBSD: fingerd.c,v 1.27 2012/03/15 02:02:21 joerg Exp $");
43*1d842c6aSThomas Cort #endif
44*1d842c6aSThomas Cort #endif /* not lint */
45*1d842c6aSThomas Cort 
46*1d842c6aSThomas Cort #include <sys/param.h>
47*1d842c6aSThomas Cort #include <sys/socket.h>
48*1d842c6aSThomas Cort #include <netinet/in.h>
49*1d842c6aSThomas Cort #include <arpa/inet.h>
50*1d842c6aSThomas Cort #include <errno.h>
51*1d842c6aSThomas Cort 
52*1d842c6aSThomas Cort #include <unistd.h>
53*1d842c6aSThomas Cort #include <syslog.h>
54*1d842c6aSThomas Cort #include <netdb.h>
55*1d842c6aSThomas Cort #include <stdarg.h>
56*1d842c6aSThomas Cort #include <stdio.h>
57*1d842c6aSThomas Cort #include <stdlib.h>
58*1d842c6aSThomas Cort #include <string.h>
59*1d842c6aSThomas Cort #include "pathnames.h"
60*1d842c6aSThomas Cort 
61*1d842c6aSThomas Cort __dead static void my_err(const char *, ...) __printflike(1, 2);
62*1d842c6aSThomas Cort 
63*1d842c6aSThomas Cort int
main(int argc,char * argv[])64*1d842c6aSThomas Cort main(int argc, char *argv[])
65*1d842c6aSThomas Cort {
66*1d842c6aSThomas Cort 	FILE *fp;
67*1d842c6aSThomas Cort 	int ch, ac = 2;
68*1d842c6aSThomas Cort 	char *lp = NULL /* XXX gcc */;
69*1d842c6aSThomas Cort 	struct sockaddr_storage ss;
70*1d842c6aSThomas Cort 	int p[2], logging, no_forward, user_required, short_list;
71*1d842c6aSThomas Cort 	socklen_t sval;
72*1d842c6aSThomas Cort #define	ENTRIES	50
73*1d842c6aSThomas Cort 	char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog, *s;
74*1d842c6aSThomas Cort #if 0
75*1d842c6aSThomas Cort 	const char *av[ENTRIES + 1], **comp;
76*1d842c6aSThomas Cort 	const char *prog;
77*1d842c6aSThomas Cort #endif
78*1d842c6aSThomas Cort 	char hostbuf[MAXHOSTNAMELEN];
79*1d842c6aSThomas Cort 
80*1d842c6aSThomas Cort 	prog = __UNCONST(_PATH_FINGER);
81*1d842c6aSThomas Cort 	logging = no_forward = user_required = short_list = 0;
82*1d842c6aSThomas Cort 	openlog("fingerd", LOG_PID, LOG_DAEMON);
83*1d842c6aSThomas Cort 	opterr = 0;
84*1d842c6aSThomas Cort 	while ((ch = getopt(argc, argv, "gsluShmpP:8")) != -1) {
85*1d842c6aSThomas Cort 		switch (ch) {
86*1d842c6aSThomas Cort 		case 'l':
87*1d842c6aSThomas Cort 			logging = 1;
88*1d842c6aSThomas Cort 			break;
89*1d842c6aSThomas Cort 		case 'P':
90*1d842c6aSThomas Cort 			prog = optarg;
91*1d842c6aSThomas Cort 			break;
92*1d842c6aSThomas Cort 		case 's':
93*1d842c6aSThomas Cort 			no_forward = 1;
94*1d842c6aSThomas Cort 			break;
95*1d842c6aSThomas Cort 		case 'u':
96*1d842c6aSThomas Cort 			user_required = 1;
97*1d842c6aSThomas Cort 			break;
98*1d842c6aSThomas Cort 		case 'S':
99*1d842c6aSThomas Cort 			short_list = 1;
100*1d842c6aSThomas Cort 			av[ac++] = __UNCONST("-s");
101*1d842c6aSThomas Cort 			break;
102*1d842c6aSThomas Cort 		case 'h':
103*1d842c6aSThomas Cort 			av[ac++] = __UNCONST("-h");
104*1d842c6aSThomas Cort 			break;
105*1d842c6aSThomas Cort 		case 'm':
106*1d842c6aSThomas Cort 			av[ac++] = __UNCONST("-m");
107*1d842c6aSThomas Cort 			break;
108*1d842c6aSThomas Cort 		case 'p':
109*1d842c6aSThomas Cort 			av[ac++] = __UNCONST("-p");
110*1d842c6aSThomas Cort 			break;
111*1d842c6aSThomas Cort 		case 'g':
112*1d842c6aSThomas Cort 			av[ac++] = __UNCONST("-g");
113*1d842c6aSThomas Cort 			break;
114*1d842c6aSThomas Cort 		case '8':
115*1d842c6aSThomas Cort 			av[ac++] = __UNCONST("-8");
116*1d842c6aSThomas Cort 			break;
117*1d842c6aSThomas Cort 		case '?':
118*1d842c6aSThomas Cort 		default:
119*1d842c6aSThomas Cort 			my_err("illegal option -- %c", optopt);
120*1d842c6aSThomas Cort 		}
121*1d842c6aSThomas Cort 		if (ac >= ENTRIES)
122*1d842c6aSThomas Cort 			my_err("Too many options provided");
123*1d842c6aSThomas Cort 	}
124*1d842c6aSThomas Cort 
125*1d842c6aSThomas Cort 
126*1d842c6aSThomas Cort 	if (logging) {
127*1d842c6aSThomas Cort 		sval = sizeof(ss);
128*1d842c6aSThomas Cort 		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
129*1d842c6aSThomas Cort 			my_err("getpeername: %s", strerror(errno));
130*1d842c6aSThomas Cort 		(void)getnameinfo((struct sockaddr *)&ss, sval,
131*1d842c6aSThomas Cort 				hostbuf, sizeof(hostbuf), NULL, 0, 0);
132*1d842c6aSThomas Cort 		lp = hostbuf;
133*1d842c6aSThomas Cort 	}
134*1d842c6aSThomas Cort 
135*1d842c6aSThomas Cort 	if (!fgets(line, sizeof(line), stdin)) {
136*1d842c6aSThomas Cort 		if (logging)
137*1d842c6aSThomas Cort 			syslog(LOG_NOTICE, "query from %s", lp);
138*1d842c6aSThomas Cort 		exit(1);
139*1d842c6aSThomas Cort 	}
140*1d842c6aSThomas Cort 	while ((s = strrchr(line, '\n')) != NULL ||
141*1d842c6aSThomas Cort 	    (s = strrchr(line, '\r')) != NULL)
142*1d842c6aSThomas Cort 		*s = '\0';
143*1d842c6aSThomas Cort 
144*1d842c6aSThomas Cort 	if (logging) {
145*1d842c6aSThomas Cort 		if (*line == '\0')
146*1d842c6aSThomas Cort 			syslog(LOG_NOTICE, "query from %s", lp);
147*1d842c6aSThomas Cort 		else
148*1d842c6aSThomas Cort 			syslog(LOG_NOTICE, "query from %s: %s", lp, line);
149*1d842c6aSThomas Cort 	}
150*1d842c6aSThomas Cort 
151*1d842c6aSThomas Cort 	if (ac >= ENTRIES)
152*1d842c6aSThomas Cort 		my_err("Too many options provided");
153*1d842c6aSThomas Cort 	av[ac++] = __UNCONST("--");
154*1d842c6aSThomas Cort 	comp = &av[1];
155*1d842c6aSThomas Cort 	for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
156*1d842c6aSThomas Cort 		if ((*ap = strtok(lp, " \t\r\n")) == NULL)
157*1d842c6aSThomas Cort 			break;
158*1d842c6aSThomas Cort 		lp = NULL;
159*1d842c6aSThomas Cort 		if (no_forward && strchr(*ap, '@')) {
160*1d842c6aSThomas Cort 			(void) puts("forwarding service denied\r\n");
161*1d842c6aSThomas Cort 			exit(1);
162*1d842c6aSThomas Cort 		}
163*1d842c6aSThomas Cort 
164*1d842c6aSThomas Cort 		ch = strlen(*ap);
165*1d842c6aSThomas Cort 		while ((*ap)[ch-1] == '@')
166*1d842c6aSThomas Cort 			(*ap)[--ch] = '\0';
167*1d842c6aSThomas Cort 		if (**ap == '\0')
168*1d842c6aSThomas Cort 			continue;
169*1d842c6aSThomas Cort 
170*1d842c6aSThomas Cort 		/* RFC1196: "/[Ww]" == "-l" */
171*1d842c6aSThomas Cort 		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
172*1d842c6aSThomas Cort 			if (!short_list) {
173*1d842c6aSThomas Cort 				av[1] = __UNCONST("-l");
174*1d842c6aSThomas Cort 				comp = &av[0];
175*1d842c6aSThomas Cort 			}
176*1d842c6aSThomas Cort 		} else {
177*1d842c6aSThomas Cort 			ap++;
178*1d842c6aSThomas Cort 			ac++;
179*1d842c6aSThomas Cort 		}
180*1d842c6aSThomas Cort 	}
181*1d842c6aSThomas Cort 	av[ENTRIES - 1] = NULL;
182*1d842c6aSThomas Cort 
183*1d842c6aSThomas Cort 	if ((lp = strrchr(prog, '/')))
184*1d842c6aSThomas Cort 		*comp = ++lp;
185*1d842c6aSThomas Cort 	else
186*1d842c6aSThomas Cort 		*comp = prog;
187*1d842c6aSThomas Cort 
188*1d842c6aSThomas Cort 	if (user_required) {
189*1d842c6aSThomas Cort 		for (ap = comp + 1; strcmp("--", *(ap++)); );
190*1d842c6aSThomas Cort 		if (*ap == NULL) {
191*1d842c6aSThomas Cort 			(void) puts("must provide username\r\n");
192*1d842c6aSThomas Cort 			exit(1);
193*1d842c6aSThomas Cort 		}
194*1d842c6aSThomas Cort 	}
195*1d842c6aSThomas Cort 
196*1d842c6aSThomas Cort 	if (pipe(p) < 0)
197*1d842c6aSThomas Cort 		my_err("pipe: %s", strerror(errno));
198*1d842c6aSThomas Cort 
199*1d842c6aSThomas Cort 	switch(fork()) {
200*1d842c6aSThomas Cort 	case 0:
201*1d842c6aSThomas Cort 		(void) close(p[0]);
202*1d842c6aSThomas Cort 		if (p[1] != 1) {
203*1d842c6aSThomas Cort 			(void) dup2(p[1], 1);
204*1d842c6aSThomas Cort 			(void) close(p[1]);
205*1d842c6aSThomas Cort 		}
206*1d842c6aSThomas Cort 		execv(prog, comp);
207*1d842c6aSThomas Cort 		my_err("execv: %s: %s", prog, strerror(errno));
208*1d842c6aSThomas Cort 		_exit(1);
209*1d842c6aSThomas Cort 	case -1:
210*1d842c6aSThomas Cort 		my_err("fork: %s", strerror(errno));
211*1d842c6aSThomas Cort 	}
212*1d842c6aSThomas Cort 	(void) close(p[1]);
213*1d842c6aSThomas Cort 	if (!(fp = fdopen(p[0], "r")))
214*1d842c6aSThomas Cort 		my_err("fdopen: %s", strerror(errno));
215*1d842c6aSThomas Cort 	while ((ch = getc(fp)) != EOF) {
216*1d842c6aSThomas Cort 		if (ch == '\n')
217*1d842c6aSThomas Cort 			putchar('\r');
218*1d842c6aSThomas Cort 		putchar(ch);
219*1d842c6aSThomas Cort 	}
220*1d842c6aSThomas Cort 	exit(0);
221*1d842c6aSThomas Cort }
222*1d842c6aSThomas Cort 
223*1d842c6aSThomas Cort static void
my_err(const char * fmt,...)224*1d842c6aSThomas Cort my_err(const char *fmt, ...)
225*1d842c6aSThomas Cort {
226*1d842c6aSThomas Cort 	va_list ap;
227*1d842c6aSThomas Cort 
228*1d842c6aSThomas Cort 	va_start(ap, fmt);
229*1d842c6aSThomas Cort 	(void) vsyslog(LOG_ERR, fmt, ap);
230*1d842c6aSThomas Cort 	va_end(ap);
231*1d842c6aSThomas Cort 	exit(1);
232*1d842c6aSThomas Cort 	/* NOTREACHED */
233*1d842c6aSThomas Cort }
234