fingerd.c revision 8870
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
41static char sccsid[] = "@(#)fingerd.c	8.1 (Berkeley) 6/4/93";
42#endif /* not lint */
43
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <netinet/in.h>
47#include <arpa/inet.h>
48#include <errno.h>
49
50#include <unistd.h>
51#include <syslog.h>
52#include <netdb.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <strings.h>
56#include "pathnames.h"
57
58void err __P((const char *, ...));
59
60int
61main(argc, argv)
62	int argc;
63	char *argv[];
64{
65	register FILE *fp;
66	register int ch;
67	register char *lp;
68	struct hostent *hp;
69	struct sockaddr_in sin;
70	int p[2], logging, secure, sval;
71#define	ENTRIES	50
72	char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog;
73
74	prog = _PATH_FINGER;
75	logging = secure = 0;
76	openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON);
77	opterr = 0;
78	while ((ch = getopt(argc, argv, "slp:")) != EOF)
79		switch (ch) {
80		case 'l':
81			logging = 1;
82			break;
83		case 'p':
84			prog = optarg;
85			break;
86		case 's':
87			secure = 1;
88			break;
89		case '?':
90		default:
91			err("illegal option -- %c", ch);
92		}
93
94	if (logging) {
95		sval = sizeof(sin);
96		if (getpeername(0, (struct sockaddr *)&sin, &sval) < 0)
97			err("getpeername: %s", strerror(errno));
98		if (hp = gethostbyaddr((char *)&sin.sin_addr.s_addr,
99		    sizeof(sin.sin_addr.s_addr), AF_INET))
100			lp = hp->h_name;
101		else
102			lp = inet_ntoa(sin.sin_addr);
103		syslog(LOG_NOTICE, "query from %s", lp);
104	}
105
106	if (!fgets(line, sizeof(line), stdin))
107		exit(1);
108
109	comp = &av[1];
110	for (lp = line, ap = &av[2];;) {
111		*ap = strtok(lp, " \t\r\n");
112		if (!*ap) {
113			if (secure && ap == &av[2]) {
114				puts("must provide username\r\n");
115				exit(1);
116			}
117			break;
118		}
119		if (secure && strchr(*ap, '@')) {
120			puts("forwarding service denied\r\n");
121			exit(1);
122		}
123
124		/* RFC742: "/[Ww]" == "-l" */
125		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
126			av[1] = "-l";
127			comp = &av[0];
128		}
129		else if (++ap == av + ENTRIES)
130			break;
131		lp = NULL;
132	}
133
134	if (lp = strrchr(prog, '/'))
135		*comp = ++lp;
136	else
137		*comp = prog;
138	if (pipe(p) < 0)
139		err("pipe: %s", strerror(errno));
140
141	switch(vfork()) {
142	case 0:
143		(void)close(p[0]);
144		if (p[1] != 1) {
145			(void)dup2(p[1], 1);
146			(void)close(p[1]);
147		}
148		execv(prog, comp);
149		err("execv: %s: %s", prog, strerror(errno));
150		_exit(1);
151	case -1:
152		err("fork: %s", strerror(errno));
153	}
154	(void)close(p[1]);
155	if (!(fp = fdopen(p[0], "r")))
156		err("fdopen: %s", strerror(errno));
157	while ((ch = getc(fp)) != EOF) {
158		if (ch == '\n')
159			putchar('\r');
160		putchar(ch);
161	}
162	exit(0);
163}
164
165#if __STDC__
166#include <stdarg.h>
167#else
168#include <varargs.h>
169#endif
170
171void
172#if __STDC__
173err(const char *fmt, ...)
174#else
175err(fmt, va_alist)
176	char *fmt;
177        va_dcl
178#endif
179{
180	va_list ap;
181#if __STDC__
182	va_start(ap, fmt);
183#else
184	va_start(ap);
185#endif
186	(void)vsyslog(LOG_ERR, fmt, ap);
187	va_end(ap);
188	exit(1);
189	/* NOTREACHED */
190}
191