fingerd.c revision 1.11
1/*	$OpenBSD: fingerd.c,v 1.11 1997/11/17 00:43:25 millert 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.11 1997/11/17 00:43:25 millert 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 *, ...));
65
66int
67main(argc, argv)
68	int argc;
69	char *argv[];
70{
71	register FILE *fp;
72	register int ch, ac = 2;
73	int p[2], logging, secure, user_required, short_list;
74	size_t linesiz;
75#define	ENTRIES	50
76	char **ap, *av[ENTRIES + 1], **comp, *line, *prog, *lp, *hname;
77
78	prog = _PATH_FINGER;
79	logging = secure = user_required = short_list = 0;
80	openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
81	opterr = 0;
82	while ((ch = getopt(argc, argv, "sluSmMpP:")) != -1)
83		switch (ch) {
84		case 'l':
85			logging = 1;
86			break;
87		case 'P':
88			prog = optarg;
89			break;
90		case 's':
91			secure = 1;
92			break;
93		case 'u':
94			user_required = 1;
95			break;
96		case 'S':
97			short_list = 1;
98			av[ac++] = "-s";
99			break;
100		case 'm':
101			av[ac++] = "-m";
102			break;
103		case 'M':
104			av[ac++] = "-M";
105			break;
106		case 'p':
107			av[ac++] = "-p";
108			break;
109		case '?':
110		default:
111			err("illegal option -- %c", ch);
112		}
113
114	if (logging) {
115		struct hostent *hp;
116		struct sockaddr_in sin;
117		int sval;
118
119		sval = sizeof(sin);
120		if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0)
121			err("getpeername: %s", strerror(errno));
122		if ((hp = gethostbyaddr((char *)&sin.sin_addr.s_addr,
123		    sizeof(sin.sin_addr.s_addr), AF_INET)))
124			hname = strdup(hp->h_name);
125		else
126			hname = strdup(inet_ntoa(sin.sin_addr));
127		if (hname == NULL)
128			err("Out of memory");
129	}
130
131	if ((lp = fgetln(stdin, &linesiz)) == NULL) {
132		if (logging)
133			syslog(LOG_NOTICE, "query from %s: %s", hname,
134			    feof(stdin) ? "EOF" : strerror(errno));
135		exit(1);
136	}
137	if ((line = malloc(linesiz + 1)) == NULL)
138		err("Out of memory");
139	memcpy(line, lp, linesiz);
140	line[linesiz] = '\0';
141
142	if (logging) {
143		char *tline;
144
145		if ((tline = strdup(line)) == NULL)
146			err("Out of memory");
147		/* Replace NULL, \r and \n with ' ' */
148		for (ch = 0; ch < linesiz; ch++) {
149			if (tline[ch] == '\0' || tline[ch] == '\r' ||
150			    tline[ch] == '\n')
151				tline[ch] = ' ';
152		}
153		for (lp = tline + linesiz - 1; lp >= tline && *lp == ' '; lp--)
154			*lp = '\0';
155		syslog(LOG_NOTICE, "query from %s: `%s'", hname, tline);
156		free(tline);
157	}
158
159	/*
160	 * Note: we assume that finger(1) will treat "--" as end of
161	 * command args (ie: that it uses getopt(3)).
162	 */
163	av[ac++] = "--";
164	comp = &av[1];
165	for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
166		if ((*ap = strtok(lp, " \t\r\n")) == NULL)
167			break;
168		lp = NULL;
169		if (secure && strchr(*ap, '@')) {
170			(void) puts("fowarding service denied\r");
171			exit(1);
172		}
173
174		ch = strlen(*ap);
175		while ((*ap)[ch-1] == '@')
176			(*ap)[--ch] = '\0';
177		if (**ap == '\0')
178			continue;
179
180		/* RFC1196: "/[Ww]" == "-l" */
181		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
182			if (!short_list) {
183				av[1] = "-l";
184				comp = &av[0];
185			}
186		} else {
187			ap++;
188			ac++;
189		}
190	}
191	av[ENTRIES - 1] = NULL;
192
193	if ((lp = strrchr(prog, '/')))
194		*comp = ++lp;
195	else
196		*comp = prog;
197
198	if (user_required) {
199		for (ap = comp + 1; strcmp("--", *(ap++)); );
200		if (*ap == NULL) {
201			(void) puts("must provide username\r");
202			exit(1);
203		}
204	}
205
206	if (pipe(p) < 0)
207		err("pipe: %s", strerror(errno));
208
209	switch(vfork()) {
210	case 0:
211		(void) close(p[0]);
212		if (p[1] != 1) {
213			(void) dup2(p[1], 1);
214			(void) close(p[1]);
215		}
216		execv(prog, comp);
217		err("execv: %s: %s", prog, strerror(errno));
218		_exit(1);
219	case -1:
220		err("fork: %s", strerror(errno));
221	}
222	(void) close(p[1]);
223	if (!(fp = fdopen(p[0], "r")))
224		err("fdopen: %s", strerror(errno));
225	while ((ch = getc(fp)) != EOF) {
226		if (ch == '\n')
227			putchar('\r');
228		putchar(ch);
229	}
230	exit(0);
231}
232
233#ifdef __STDC__
234#include <stdarg.h>
235#else
236#include <varargs.h>
237#endif
238
239void
240#ifdef __STDC__
241err(const char *fmt, ...)
242#else
243err(fmt, va_alist)
244	char *fmt;
245	va_dcl
246#endif
247{
248	va_list ap;
249#ifdef __STDC__
250	va_start(ap, fmt);
251#else
252	va_start(ap);
253#endif
254	(void) vsyslog(LOG_ERR, fmt, ap);
255	va_end(ap);
256	exit(1);
257	/* NOTREACHED */
258}
259