fingerd.c revision 98897
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 98897 2002-06-26 21:46:56Z wollman $";
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
6590148Simpvoid logerr(const char *, ...);
661592Srgrimes
671592Srgrimesint
6890148Simpmain(int argc, char *argv[])
691592Srgrimes{
7090148Simp	FILE *fp;
7190148Simp	int ch;
7290148Simp	char *lp;
7357313Sshin	struct sockaddr_storage ss;
7498897Swollman	int p[2], logging, pflag, secure, sval;
751592Srgrimes#define	ENTRIES	50
761592Srgrimes	char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
7745418Sbrian	char rhost[MAXHOSTNAMELEN];
781592Srgrimes
791592Srgrimes	prog = _PATH_FINGER;
8098897Swollman	logging = pflag = secure = 0;
811592Srgrimes	openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
821592Srgrimes	opterr = 0;
8398897Swollman	while ((ch = getopt(argc, argv, "lp:s")) != -1)
841592Srgrimes		switch (ch) {
851592Srgrimes		case 'l':
861592Srgrimes			logging = 1;
871592Srgrimes			break;
881592Srgrimes		case 'p':
891592Srgrimes			prog = optarg;
9098897Swollman			pflag = 1;
911592Srgrimes			break;
921592Srgrimes		case 's':
931592Srgrimes			secure = 1;
941592Srgrimes			break;
951592Srgrimes		case '?':
961592Srgrimes		default:
9747291Speter			logerr("illegal option -- %c", optopt);
981592Srgrimes		}
991592Srgrimes
10012908Swollman	/*
10112908Swollman	 * Enable server-side Transaction TCP.
10212908Swollman	 */
10312908Swollman	{
10412908Swollman		int one = 1;
10512908Swollman		if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one,
10612908Swollman			       sizeof one) < 0) {
10712908Swollman			logerr("setsockopt(TCP_NOPUSH) failed: %m");
10812908Swollman		}
10912908Swollman	}
11012908Swollman
1111592Srgrimes	if (!fgets(line, sizeof(line), stdin))
1121592Srgrimes		exit(1);
1138870Srgrimes
11498897Swollman	if (logging || pflag) {
11598897Swollman		sval = sizeof(ss);
11698897Swollman		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
11798897Swollman			logerr("getpeername: %s", strerror(errno));
11898897Swollman		realhostname_sa(rhost, sizeof rhost - 1,
11998897Swollman				(struct sockaddr *)&ss, sval);
12098897Swollman		rhost[sizeof(rhost) - 1] = '\0';
12198897Swollman		if (pflag)
12298897Swollman			setenv("FINGERD_REMOTE_HOST", rhost, 1);
12398897Swollman	}
12498897Swollman
12531168Ssef	if (logging) {
12631168Ssef		char *t;
12731168Ssef		char *end;
12831168Ssef
12931168Ssef		end = memchr(line, 0, sizeof(line));
13031168Ssef		if (end == NULL) {
13171274Sjedgar			if ((t = malloc(sizeof(line) + 1)) == NULL)
13271274Sjedgar				logerr("malloc: %s", strerror(errno));
13331168Ssef			memcpy(t, line, sizeof(line));
13431168Ssef			t[sizeof(line)] = 0;
13531168Ssef		} else {
13671274Sjedgar			if ((t = strdup(line)) == NULL)
13771274Sjedgar				logerr("strdup: %s", strerror(errno));
13831168Ssef		}
13931168Ssef		for (end = t; *end; end++)
14031168Ssef			if (*end == '\n' || *end == '\r')
14131168Ssef				*end = ' ';
14245393Sbrian		syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t);
14331168Ssef	}
14431168Ssef
1451592Srgrimes	comp = &av[1];
14612728Speter	av[2] = "--";
14712728Speter	for (lp = line, ap = &av[3];;) {
1481592Srgrimes		*ap = strtok(lp, " \t\r\n");
1491592Srgrimes		if (!*ap) {
15012728Speter			if (secure && ap == &av[3]) {
1511592Srgrimes				puts("must provide username\r\n");
1521592Srgrimes				exit(1);
1531592Srgrimes			}
1541592Srgrimes			break;
1551592Srgrimes		}
1561592Srgrimes		if (secure && strchr(*ap, '@')) {
1576180Sphk			puts("forwarding service denied\r\n");
1581592Srgrimes			exit(1);
1591592Srgrimes		}
1601592Srgrimes
1611592Srgrimes		/* RFC742: "/[Ww]" == "-l" */
1621592Srgrimes		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
1631592Srgrimes			av[1] = "-l";
1641592Srgrimes			comp = &av[0];
1651592Srgrimes		}
16684454Sru		else if (++ap == av + ENTRIES) {
16784454Sru			*ap = NULL;
1681592Srgrimes			break;
16984454Sru		}
1701592Srgrimes		lp = NULL;
1711592Srgrimes	}
1721592Srgrimes
1731592Srgrimes	if (lp = strrchr(prog, '/'))
1741592Srgrimes		*comp = ++lp;
1751592Srgrimes	else
1761592Srgrimes		*comp = prog;
1771592Srgrimes	if (pipe(p) < 0)
17812908Swollman		logerr("pipe: %s", strerror(errno));
1791592Srgrimes
1801592Srgrimes	switch(vfork()) {
1811592Srgrimes	case 0:
1821592Srgrimes		(void)close(p[0]);
18398897Swollman		if (p[1] != STDOUT_FILENO) {
18498897Swollman			(void)dup2(p[1], STDOUT_FILENO);
1851592Srgrimes			(void)close(p[1]);
1861592Srgrimes		}
18798897Swollman		dup2(STDOUT_FILENO, STDERR_FILENO);
18898897Swollman
1891592Srgrimes		execv(prog, comp);
19098897Swollman		write(STDERR_FILENO, prog, strlen(prog));
19198897Swollman#define MSG ": cannot execute\n"
19298897Swollman		write(STDERR_FILENO, MSG, strlen(MSG));
19398897Swollman#undef MSG
1941592Srgrimes		_exit(1);
1951592Srgrimes	case -1:
19612908Swollman		logerr("fork: %s", strerror(errno));
1971592Srgrimes	}
1981592Srgrimes	(void)close(p[1]);
1991592Srgrimes	if (!(fp = fdopen(p[0], "r")))
20012908Swollman		logerr("fdopen: %s", strerror(errno));
2011592Srgrimes	while ((ch = getc(fp)) != EOF) {
2021592Srgrimes		if (ch == '\n')
2031592Srgrimes			putchar('\r');
2041592Srgrimes		putchar(ch);
2051592Srgrimes	}
2061592Srgrimes	exit(0);
2071592Srgrimes}
2081592Srgrimes
2091592Srgrimes#include <stdarg.h>
2101592Srgrimes
2111592Srgrimesvoid
21212908Swollmanlogerr(const char *fmt, ...)
2131592Srgrimes{
2141592Srgrimes	va_list ap;
2151592Srgrimes	va_start(ap, fmt);
2161592Srgrimes	(void)vsyslog(LOG_ERR, fmt, ap);
2171592Srgrimes	va_end(ap);
2181592Srgrimes	exit(1);
2191592Srgrimes	/* NOTREACHED */
2201592Srgrimes}
221