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