fingerd.c revision 181269
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: head/libexec/fingerd/fingerd.c 181269 2008-08-04 01:25:48Z cperciva $";
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;
75141918Sstefanf	int p[2], 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;
8198897Swollman	logging = pflag = secure = 0;
821592Srgrimes	openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
831592Srgrimes	opterr = 0;
8498897Swollman	while ((ch = getopt(argc, argv, "lp:s")) != -1)
851592Srgrimes		switch (ch) {
861592Srgrimes		case 'l':
871592Srgrimes			logging = 1;
881592Srgrimes			break;
891592Srgrimes		case 'p':
901592Srgrimes			prog = optarg;
9198897Swollman			pflag = 1;
921592Srgrimes			break;
931592Srgrimes		case 's':
941592Srgrimes			secure = 1;
951592Srgrimes			break;
961592Srgrimes		case '?':
971592Srgrimes		default:
9847291Speter			logerr("illegal option -- %c", optopt);
991592Srgrimes		}
1001592Srgrimes
10112908Swollman	/*
10212908Swollman	 * Enable server-side Transaction TCP.
10312908Swollman	 */
10412908Swollman	{
10512908Swollman		int one = 1;
10612908Swollman		if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one,
10712908Swollman			       sizeof one) < 0) {
10812908Swollman			logerr("setsockopt(TCP_NOPUSH) failed: %m");
10912908Swollman		}
11012908Swollman	}
11112908Swollman
1121592Srgrimes	if (!fgets(line, sizeof(line), stdin))
1131592Srgrimes		exit(1);
1148870Srgrimes
11598897Swollman	if (logging || pflag) {
11698897Swollman		sval = sizeof(ss);
11798897Swollman		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
11898897Swollman			logerr("getpeername: %s", strerror(errno));
11998897Swollman		realhostname_sa(rhost, sizeof rhost - 1,
12098897Swollman				(struct sockaddr *)&ss, sval);
12198897Swollman		rhost[sizeof(rhost) - 1] = '\0';
12298897Swollman		if (pflag)
12398897Swollman			setenv("FINGERD_REMOTE_HOST", rhost, 1);
12498897Swollman	}
12598897Swollman
12631168Ssef	if (logging) {
12731168Ssef		char *t;
12831168Ssef		char *end;
12931168Ssef
13031168Ssef		end = memchr(line, 0, sizeof(line));
13131168Ssef		if (end == NULL) {
13271274Sjedgar			if ((t = malloc(sizeof(line) + 1)) == NULL)
13371274Sjedgar				logerr("malloc: %s", strerror(errno));
13431168Ssef			memcpy(t, line, sizeof(line));
13531168Ssef			t[sizeof(line)] = 0;
13631168Ssef		} else {
13771274Sjedgar			if ((t = strdup(line)) == NULL)
13871274Sjedgar				logerr("strdup: %s", strerror(errno));
13931168Ssef		}
14031168Ssef		for (end = t; *end; end++)
14131168Ssef			if (*end == '\n' || *end == '\r')
14231168Ssef				*end = ' ';
14345393Sbrian		syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
14431168Ssef	}
14531168Ssef
1461592Srgrimes	comp = &av[1];
14712728Speter	av[2] = "--";
14812728Speter	for (lp = line, ap = &av[3];;) {
1491592Srgrimes		*ap = strtok(lp, " \t\r\n");
1501592Srgrimes		if (!*ap) {
15112728Speter			if (secure && ap == &av[3]) {
1521592Srgrimes				puts("must provide username\r\n");
1531592Srgrimes				exit(1);
1541592Srgrimes			}
1551592Srgrimes			break;
1561592Srgrimes		}
1571592Srgrimes		if (secure && strchr(*ap, '@')) {
1586180Sphk			puts("forwarding service denied\r\n");
1591592Srgrimes			exit(1);
1601592Srgrimes		}
1611592Srgrimes
1621592Srgrimes		/* RFC742: "/[Ww]" == "-l" */
1631592Srgrimes		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
1641592Srgrimes			av[1] = "-l";
1651592Srgrimes			comp = &av[0];
1661592Srgrimes		}
16784454Sru		else if (++ap == av + ENTRIES) {
16884454Sru			*ap = NULL;
1691592Srgrimes			break;
17084454Sru		}
1711592Srgrimes		lp = NULL;
1721592Srgrimes	}
1731592Srgrimes
174127560Spjd	if ((lp = strrchr(prog, '/')) != NULL)
1751592Srgrimes		*comp = ++lp;
1761592Srgrimes	else
1771592Srgrimes		*comp = prog;
1781592Srgrimes	if (pipe(p) < 0)
17912908Swollman		logerr("pipe: %s", strerror(errno));
1801592Srgrimes
1811592Srgrimes	switch(vfork()) {
1821592Srgrimes	case 0:
1831592Srgrimes		(void)close(p[0]);
18498897Swollman		if (p[1] != STDOUT_FILENO) {
18598897Swollman			(void)dup2(p[1], STDOUT_FILENO);
1861592Srgrimes			(void)close(p[1]);
1871592Srgrimes		}
18898897Swollman		dup2(STDOUT_FILENO, STDERR_FILENO);
18998897Swollman
1901592Srgrimes		execv(prog, comp);
19198897Swollman		write(STDERR_FILENO, prog, strlen(prog));
19298897Swollman#define MSG ": cannot execute\n"
19398897Swollman		write(STDERR_FILENO, MSG, strlen(MSG));
19498897Swollman#undef MSG
1951592Srgrimes		_exit(1);
1961592Srgrimes	case -1:
19712908Swollman		logerr("fork: %s", strerror(errno));
1981592Srgrimes	}
1991592Srgrimes	(void)close(p[1]);
2001592Srgrimes	if (!(fp = fdopen(p[0], "r")))
20112908Swollman		logerr("fdopen: %s", strerror(errno));
2021592Srgrimes	while ((ch = getc(fp)) != EOF) {
2031592Srgrimes		if (ch == '\n')
2041592Srgrimes			putchar('\r');
2051592Srgrimes		putchar(ch);
2061592Srgrimes	}
2071592Srgrimes	exit(0);
2081592Srgrimes}
2091592Srgrimes
2101592Srgrimes#include <stdarg.h>
2111592Srgrimes
2121592Srgrimesvoid
21312908Swollmanlogerr(const char *fmt, ...)
2141592Srgrimes{
2151592Srgrimes	va_list ap;
2161592Srgrimes	va_start(ap, fmt);
2171592Srgrimes	(void)vsyslog(LOG_ERR, fmt, ap);
2181592Srgrimes	va_end(ap);
2191592Srgrimes	exit(1);
2201592Srgrimes	/* NOTREACHED */
2211592Srgrimes}
222