fingerd.c revision 98897
150479Speter/*
2327Sjkh * Copyright (c) 1983, 1993
3327Sjkh *	The Regents of the University of California.  All rights reserved.
4327Sjkh *
5327Sjkh * Redistribution and use in source and binary forms, with or without
6327Sjkh * modification, are permitted provided that the following conditions
7327Sjkh * are met:
8327Sjkh * 1. Redistributions of source code must retain the above copyright
9327Sjkh *    notice, this list of conditions and the following disclaimer.
10327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
11327Sjkh *    notice, this list of conditions and the following disclaimer in the
12327Sjkh *    documentation and/or other materials provided with the distribution.
13327Sjkh * 3. All advertising materials mentioning features or use of this software
14327Sjkh *    must display the following acknowledgement:
15327Sjkh *	This product includes software developed by the University of
16327Sjkh *	California, Berkeley and its contributors.
17327Sjkh * 4. Neither the name of the University nor the names of its contributors
18327Sjkh *    may be used to endorse or promote products derived from this software
19327Sjkh *    without specific prior written permission.
20327Sjkh *
21327Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22327Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23327Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24327Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25327Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2674699Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2774699Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285399Sswallace * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2967300Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305399Sswallace * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315399Sswallace * SUCH DAMAGE.
325399Sswallace */
335399Sswallace
345399Sswallace#ifndef lint
355399Sswallacestatic const char copyright[] =
36103149Ssobomax"@(#) Copyright (c) 1983, 1993\n\
37103149Ssobomax	The Regents of the University of California.  All rights reserved.\n";
38103149Ssobomax#endif /* not lint */
39103149Ssobomax
40103149Ssobomax#ifndef lint
41103149Ssobomax#if 0
42103149Ssobomaxstatic char sccsid[] = "@(#)fingerd.c	8.1 (Berkeley) 6/4/93";
43103149Ssobomax#endif
44103149Ssobomaxstatic const char rcsid[] =
45103149Ssobomax  "$FreeBSD: head/libexec/fingerd/fingerd.c 98897 2002-06-26 21:46:56Z wollman $";
46103149Ssobomax#endif /* not lint */
47103149Ssobomax
48103149Ssobomax#include <sys/types.h>
49103149Ssobomax#include <sys/param.h>
50103149Ssobomax#include <sys/socket.h>
51103149Ssobomax#include <netinet/in.h>
52103149Ssobomax#include <netinet/tcp.h>
53327Sjkh#include <arpa/inet.h>
5474699Ssobomax#include <errno.h>
5574699Ssobomax
5674699Ssobomax#include <unistd.h>
5774699Ssobomax#include <syslog.h>
5874699Ssobomax#include <libutil.h>
5974699Ssobomax#include <netdb.h>
6074699Ssobomax#include <stdio.h>
6174699Ssobomax#include <stdlib.h>
62327Sjkh#include <string.h>
63411Sjkh#include "pathnames.h"
64112572Smdodd
65379Sjkhvoid logerr(const char *, ...);
6611780Sjkh
67392Sjkhint
6896030Ssobomaxmain(int argc, char *argv[])
6972174Ssobomax{
7074699Ssobomax	FILE *fp;
71327Sjkh	int ch;
7284745Ssobomax	char *lp;
7384745Ssobomax	struct sockaddr_storage ss;
7484745Ssobomax	int p[2], logging, pflag, secure, sval;
7584745Ssobomax#define	ENTRIES	50
7684745Ssobomax	char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
7784745Ssobomax	char rhost[MAXHOSTNAMELEN];
7884745Ssobomax
7984750Ssobomax	prog = _PATH_FINGER;
80327Sjkh	logging = pflag = secure = 0;
81327Sjkh	openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
82	opterr = 0;
83	while ((ch = getopt(argc, argv, "lp:s")) != -1)
84		switch (ch) {
85		case 'l':
86			logging = 1;
87			break;
88		case 'p':
89			prog = optarg;
90			pflag = 1;
91			break;
92		case 's':
93			secure = 1;
94			break;
95		case '?':
96		default:
97			logerr("illegal option -- %c", optopt);
98		}
99
100	/*
101	 * Enable server-side Transaction TCP.
102	 */
103	{
104		int one = 1;
105		if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one,
106			       sizeof one) < 0) {
107			logerr("setsockopt(TCP_NOPUSH) failed: %m");
108		}
109	}
110
111	if (!fgets(line, sizeof(line), stdin))
112		exit(1);
113
114	if (logging || pflag) {
115		sval = sizeof(ss);
116		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
117			logerr("getpeername: %s", strerror(errno));
118		realhostname_sa(rhost, sizeof rhost - 1,
119				(struct sockaddr *)&ss, sval);
120		rhost[sizeof(rhost) - 1] = '\0';
121		if (pflag)
122			setenv("FINGERD_REMOTE_HOST", rhost, 1);
123	}
124
125	if (logging) {
126		char *t;
127		char *end;
128
129		end = memchr(line, 0, sizeof(line));
130		if (end == NULL) {
131			if ((t = malloc(sizeof(line) + 1)) == NULL)
132				logerr("malloc: %s", strerror(errno));
133			memcpy(t, line, sizeof(line));
134			t[sizeof(line)] = 0;
135		} else {
136			if ((t = strdup(line)) == NULL)
137				logerr("strdup: %s", strerror(errno));
138		}
139		for (end = t; *end; end++)
140			if (*end == '\n' || *end == '\r')
141				*end = ' ';
142		syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
143	}
144
145	comp = &av[1];
146	av[2] = "--";
147	for (lp = line, ap = &av[3];;) {
148		*ap = strtok(lp, " \t\r\n");
149		if (!*ap) {
150			if (secure && ap == &av[3]) {
151				puts("must provide username\r\n");
152				exit(1);
153			}
154			break;
155		}
156		if (secure && strchr(*ap, '@')) {
157			puts("forwarding service denied\r\n");
158			exit(1);
159		}
160
161		/* RFC742: "/[Ww]" == "-l" */
162		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
163			av[1] = "-l";
164			comp = &av[0];
165		}
166		else if (++ap == av + ENTRIES) {
167			*ap = NULL;
168			break;
169		}
170		lp = NULL;
171	}
172
173	if (lp = strrchr(prog, '/'))
174		*comp = ++lp;
175	else
176		*comp = prog;
177	if (pipe(p) < 0)
178		logerr("pipe: %s", strerror(errno));
179
180	switch(vfork()) {
181	case 0:
182		(void)close(p[0]);
183		if (p[1] != STDOUT_FILENO) {
184			(void)dup2(p[1], STDOUT_FILENO);
185			(void)close(p[1]);
186		}
187		dup2(STDOUT_FILENO, STDERR_FILENO);
188
189		execv(prog, comp);
190		write(STDERR_FILENO, prog, strlen(prog));
191#define MSG ": cannot execute\n"
192		write(STDERR_FILENO, MSG, strlen(MSG));
193#undef MSG
194		_exit(1);
195	case -1:
196		logerr("fork: %s", strerror(errno));
197	}
198	(void)close(p[1]);
199	if (!(fp = fdopen(p[0], "r")))
200		logerr("fdopen: %s", strerror(errno));
201	while ((ch = getc(fp)) != EOF) {
202		if (ch == '\n')
203			putchar('\r');
204		putchar(ch);
205	}
206	exit(0);
207}
208
209#include <stdarg.h>
210
211void
212logerr(const char *fmt, ...)
213{
214	va_list ap;
215	va_start(ap, fmt);
216	(void)vsyslog(LOG_ERR, fmt, ap);
217	va_end(ap);
218	exit(1);
219	/* NOTREACHED */
220}
221