fingerd.c revision 1.39
1/*	$OpenBSD: fingerd.c,v 1.39 2015/11/13 01:26:33 deraadt Exp $	*/
2
3/*
4 * Copyright (c) 1983, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <errno.h>
36
37#include <err.h>
38#include <unistd.h>
39#include <syslog.h>
40#include <netdb.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <stdarg.h>
45#include <limits.h>
46#include "pathnames.h"
47
48__dead void logerr(const char *, ...);
49__dead void usage(void);
50
51void
52usage(void)
53{
54	syslog(LOG_ERR,
55	    "usage: fingerd [-lMmpSsu] [-P filename]");
56	exit(2);
57}
58
59
60int
61main(int argc, char *argv[])
62{
63	FILE *fp;
64	int ch, ac = 2;
65	int p[2], logging, secure, user_required, short_list;
66#define	ENTRIES	50
67	char **comp, *prog;
68	char **ap, *av[ENTRIES + 1], line[8192], *lp, *hname;
69	char hostbuf[HOST_NAME_MAX+1];
70
71	if (pledge("stdio inet dns proc exec", NULL) == -1)
72		err(1, "pledge");
73
74	prog = _PATH_FINGER;
75	logging = secure = user_required = short_list = 0;
76	openlog("fingerd", LOG_PID, LOG_DAEMON);
77	opterr = 0;
78	while ((ch = getopt(argc, argv, "sluSmMpP:")) != -1)
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 'u':
90			user_required = 1;
91			break;
92		case 'S':
93			if (ac < ENTRIES) {
94				short_list = 1;
95				av[ac++] = "-s";
96			}
97			break;
98		case 'm':
99			if (ac < ENTRIES)
100				av[ac++] = "-m";
101			break;
102		case 'M':
103			if (ac < ENTRIES)
104				av[ac++] = "-M";
105			break;
106		case 'p':
107			if (ac < ENTRIES)
108				av[ac++] = "-p";
109			break;
110		default:
111			usage();
112		}
113
114	if (logging) {
115		struct sockaddr_storage ss;
116		struct sockaddr *sa;
117		socklen_t sval;
118
119		sval = sizeof(ss);
120		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0)
121			err(1, "getpeername");
122		sa = (struct sockaddr *)&ss;
123
124		if (pledge("stdio dns proc exec", NULL) == -1)
125			err(1, "pledge");
126
127		if (getnameinfo(sa, sa->sa_len, hostbuf, sizeof(hostbuf),
128		    NULL, 0, 0) != 0) {
129			strlcpy(hostbuf, "?", sizeof(hostbuf));
130		}
131		hname = hostbuf;
132	}
133
134	if (pledge("stdio proc exec", NULL) == -1)
135		err(1, "pledge");
136
137	if (fgets(line, sizeof(line), stdin) == NULL) {
138		if (logging)
139			syslog(LOG_NOTICE, "query from %s: %s", hname,
140			    feof(stdin) ? "EOF" : strerror(errno));
141		exit(1);
142	}
143
144	if (logging)
145		syslog(LOG_NOTICE, "query from %s: `%.*s'", hname,
146		    (int)strcspn(line, "\r\n"), line);
147
148	/*
149	 * Note: we assume that finger(1) will treat "--" as end of
150	 * command args (ie: that it uses getopt(3)).
151	 */
152	av[ac++] = "--";
153	comp = &av[1];
154	for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
155		size_t len;
156
157		if ((*ap = strtok(lp, " \t\r\n")) == NULL)
158			break;
159		lp = NULL;
160		if (secure && strchr(*ap, '@')) {
161			(void) puts("forwarding service denied\r");
162			exit(1);
163		}
164
165		len = strlen(*ap);
166		while (len > 0 && (*ap)[len - 1] == '@')
167			(*ap)[--len] = '\0';
168		if (**ap == '\0')
169			continue;
170
171		/* RFC1196: "/[Ww]" == "-l" */
172		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
173			if (!short_list) {
174				av[1] = "-l";
175				comp = &av[0];
176			}
177		} else {
178			ap++;
179			ac++;
180		}
181	}
182	av[ENTRIES - 1] = NULL;
183
184	if ((lp = strrchr(prog, '/')))
185		*comp = ++lp;
186	else
187		*comp = prog;
188
189	if (user_required) {
190		for (ap = comp + 1; strcmp("--", *(ap++)); )
191			;
192		if (*ap == NULL) {
193			(void) puts("must provide username\r");
194			exit(1);
195		}
196	}
197
198	if (pipe(p) < 0)
199		logerr("pipe: %s", strerror(errno));
200
201	switch (vfork()) {
202	case 0:
203		(void) close(p[0]);
204		if (p[1] != 1) {
205			(void) dup2(p[1], 1);
206			(void) close(p[1]);
207		}
208		execv(prog, comp);
209		syslog(LOG_ERR, "execv: %s: %s", prog, strerror(errno));
210		_exit(1);
211	case -1:
212		logerr("fork: %s", strerror(errno));
213	}
214	if (pledge("stdio", NULL) == -1)
215		err(1, "pledge");
216
217	(void) close(p[1]);
218	if (!(fp = fdopen(p[0], "r")))
219		logerr("fdopen: %s", strerror(errno));
220	while ((ch = getc(fp)) != EOF) {
221		if (ch == '\n')
222			putchar('\r');
223		putchar(ch);
224	}
225	exit(0);
226}
227
228void
229logerr(const char *fmt, ...)
230{
231	va_list ap;
232
233	va_start(ap, fmt);
234	(void) vsyslog(LOG_ERR, fmt, ap);
235	va_end(ap);
236	exit(1);
237}
238