11592Srgrimes/*
21592Srgrimes * Copyright (c) 1983, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
3531308Scharnierstatic const char copyright[] =
361592Srgrimes"@(#) Copyright (c) 1983, 1993\n\
371592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381592Srgrimes#endif /* not lint */
391592Srgrimes
401592Srgrimes#ifndef lint
4131308Scharnier#if 0
421592Srgrimesstatic char sccsid[] = "@(#)fingerd.c	8.1 (Berkeley) 6/4/93";
4331308Scharnier#endif
4412908Swollmanstatic const char rcsid[] =
4550476Speter  "$FreeBSD$";
461592Srgrimes#endif /* not lint */
471592Srgrimes
4871274Sjedgar#include <sys/types.h>
4945393Sbrian#include <sys/param.h>
501592Srgrimes#include <sys/socket.h>
511592Srgrimes#include <netinet/in.h>
5212908Swollman#include <netinet/tcp.h>
531592Srgrimes#include <arpa/inet.h>
541592Srgrimes#include <errno.h>
551592Srgrimes
561592Srgrimes#include <unistd.h>
571592Srgrimes#include <syslog.h>
5871274Sjedgar#include <libutil.h>
591592Srgrimes#include <netdb.h>
601592Srgrimes#include <stdio.h>
611592Srgrimes#include <stdlib.h>
6297635Swollman#include <string.h>
631592Srgrimes#include "pathnames.h"
641592Srgrimes
65181269Scpercivavoid logerr(const char *, ...) __printflike(1, 2) __dead2;
661592Srgrimes
671592Srgrimesint
6890148Simpmain(int argc, char *argv[])
691592Srgrimes{
7090148Simp	FILE *fp;
7190148Simp	int ch;
7290148Simp	char *lp;
7357313Sshin	struct sockaddr_storage ss;
74141918Sstefanf	socklen_t sval;
75206038Sdes	int p[2], debug, kflag, logging, pflag, secure;
761592Srgrimes#define	ENTRIES	50
771592Srgrimes	char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
7845418Sbrian	char rhost[MAXHOSTNAMELEN];
791592Srgrimes
801592Srgrimes	prog = _PATH_FINGER;
81206040Sdes	debug = logging = kflag = pflag = secure = 0;
821592Srgrimes	openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
831592Srgrimes	opterr = 0;
84206038Sdes	while ((ch = getopt(argc, argv, "dklp:s")) != -1)
851592Srgrimes		switch (ch) {
86206038Sdes		case 'd':
87206038Sdes			debug = 1;
88206038Sdes			break;
89206038Sdes		case 'k':
90206038Sdes			kflag = 1;
91206038Sdes			break;
921592Srgrimes		case 'l':
931592Srgrimes			logging = 1;
941592Srgrimes			break;
951592Srgrimes		case 'p':
961592Srgrimes			prog = optarg;
9798897Swollman			pflag = 1;
981592Srgrimes			break;
991592Srgrimes		case 's':
1001592Srgrimes			secure = 1;
1011592Srgrimes			break;
1021592Srgrimes		case '?':
1031592Srgrimes		default:
10447291Speter			logerr("illegal option -- %c", optopt);
1051592Srgrimes		}
1061592Srgrimes
10712908Swollman	/*
10812908Swollman	 * Enable server-side Transaction TCP.
10912908Swollman	 */
110206038Sdes	if (!debug) {
11112908Swollman		int one = 1;
11212908Swollman		if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one,
11312908Swollman			       sizeof one) < 0) {
11412908Swollman			logerr("setsockopt(TCP_NOPUSH) failed: %m");
11512908Swollman		}
11612908Swollman	}
11712908Swollman
1181592Srgrimes	if (!fgets(line, sizeof(line), stdin))
1191592Srgrimes		exit(1);
1208870Srgrimes
121206038Sdes	if (!debug && (logging || pflag)) {
12298897Swollman		sval = sizeof(ss);
12398897Swollman		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
12498897Swollman			logerr("getpeername: %s", strerror(errno));
12598897Swollman		realhostname_sa(rhost, sizeof rhost - 1,
12698897Swollman				(struct sockaddr *)&ss, sval);
12798897Swollman		rhost[sizeof(rhost) - 1] = '\0';
12898897Swollman		if (pflag)
12998897Swollman			setenv("FINGERD_REMOTE_HOST", rhost, 1);
13098897Swollman	}
13198897Swollman
13231168Ssef	if (logging) {
13331168Ssef		char *t;
13431168Ssef		char *end;
13531168Ssef
13631168Ssef		end = memchr(line, 0, sizeof(line));
13731168Ssef		if (end == NULL) {
13871274Sjedgar			if ((t = malloc(sizeof(line) + 1)) == NULL)
13971274Sjedgar				logerr("malloc: %s", strerror(errno));
14031168Ssef			memcpy(t, line, sizeof(line));
14131168Ssef			t[sizeof(line)] = 0;
14231168Ssef		} else {
14371274Sjedgar			if ((t = strdup(line)) == NULL)
14471274Sjedgar				logerr("strdup: %s", strerror(errno));
14531168Ssef		}
14631168Ssef		for (end = t; *end; end++)
14731168Ssef			if (*end == '\n' || *end == '\r')
14831168Ssef				*end = ' ';
14945393Sbrian		syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
15031168Ssef	}
15131168Ssef
152206038Sdes	comp = &av[2];
153206038Sdes	av[3] = "--";
154206038Sdes	if (kflag)
155206038Sdes		*comp-- = "-k";
156206038Sdes	for (lp = line, ap = &av[4];;) {
1571592Srgrimes		*ap = strtok(lp, " \t\r\n");
1581592Srgrimes		if (!*ap) {
159206038Sdes			if (secure && ap == &av[4]) {
1601592Srgrimes				puts("must provide username\r\n");
1611592Srgrimes				exit(1);
1621592Srgrimes			}
1631592Srgrimes			break;
1641592Srgrimes		}
1651592Srgrimes		if (secure && strchr(*ap, '@')) {
1666180Sphk			puts("forwarding service denied\r\n");
1671592Srgrimes			exit(1);
1681592Srgrimes		}
1691592Srgrimes
1701592Srgrimes		/* RFC742: "/[Ww]" == "-l" */
1711592Srgrimes		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
172206038Sdes			*comp-- = "-l";
1731592Srgrimes		}
17484454Sru		else if (++ap == av + ENTRIES) {
17584454Sru			*ap = NULL;
1761592Srgrimes			break;
17784454Sru		}
1781592Srgrimes		lp = NULL;
1791592Srgrimes	}
1801592Srgrimes
181127560Spjd	if ((lp = strrchr(prog, '/')) != NULL)
1821592Srgrimes		*comp = ++lp;
1831592Srgrimes	else
1841592Srgrimes		*comp = prog;
1851592Srgrimes	if (pipe(p) < 0)
18612908Swollman		logerr("pipe: %s", strerror(errno));
1871592Srgrimes
188206038Sdes	if (debug) {
189206038Sdes		fprintf(stderr, "%s", prog);
190206038Sdes		for (ap = comp; *ap != NULL; ++ap)
191206038Sdes			fprintf(stderr, " %s", *ap);
192206038Sdes		fprintf(stderr, "\n");
193206038Sdes	}
194206038Sdes
1951592Srgrimes	switch(vfork()) {
1961592Srgrimes	case 0:
1971592Srgrimes		(void)close(p[0]);
19898897Swollman		if (p[1] != STDOUT_FILENO) {
19998897Swollman			(void)dup2(p[1], STDOUT_FILENO);
2001592Srgrimes			(void)close(p[1]);
2011592Srgrimes		}
20298897Swollman		dup2(STDOUT_FILENO, STDERR_FILENO);
20398897Swollman
2041592Srgrimes		execv(prog, comp);
20598897Swollman		write(STDERR_FILENO, prog, strlen(prog));
20698897Swollman#define MSG ": cannot execute\n"
20798897Swollman		write(STDERR_FILENO, MSG, strlen(MSG));
20898897Swollman#undef MSG
2091592Srgrimes		_exit(1);
2101592Srgrimes	case -1:
21112908Swollman		logerr("fork: %s", strerror(errno));
2121592Srgrimes	}
2131592Srgrimes	(void)close(p[1]);
2141592Srgrimes	if (!(fp = fdopen(p[0], "r")))
21512908Swollman		logerr("fdopen: %s", strerror(errno));
2161592Srgrimes	while ((ch = getc(fp)) != EOF) {
2171592Srgrimes		if (ch == '\n')
2181592Srgrimes			putchar('\r');
2191592Srgrimes		putchar(ch);
2201592Srgrimes	}
2211592Srgrimes	exit(0);
2221592Srgrimes}
2231592Srgrimes
2241592Srgrimes#include <stdarg.h>
2251592Srgrimes
2261592Srgrimesvoid
22712908Swollmanlogerr(const char *fmt, ...)
2281592Srgrimes{
2291592Srgrimes	va_list ap;
2301592Srgrimes	va_start(ap, fmt);
2311592Srgrimes	(void)vsyslog(LOG_ERR, fmt, ap);
2321592Srgrimes	va_end(ap);
2331592Srgrimes	exit(1);
2341592Srgrimes	/* NOTREACHED */
2351592Srgrimes}
236