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