fingerd.c revision 1.32
1/*	$OpenBSD: fingerd.c,v 1.32 2006/01/17 17:51:21 millert 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#ifndef lint
33static char copyright[] =
34"@(#) Copyright (c) 1983, 1993\n\
35	The Regents of the University of California.  All rights reserved.\n";
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "from: @(#)fingerd.c	8.1 (Berkeley) 6/4/93";
41#else
42static char rcsid[] = "$OpenBSD: fingerd.c,v 1.32 2006/01/17 17:51:21 millert Exp $";
43#endif
44#endif /* not lint */
45
46#include <sys/param.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49#include <arpa/inet.h>
50#include <errno.h>
51
52#include <unistd.h>
53#include <syslog.h>
54#include <netdb.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <stdarg.h>
59#include "pathnames.h"
60
61void err(const char *, ...);
62void usage(void);
63
64void
65usage(void)
66{
67	syslog(LOG_ERR,
68	    "usage: fingerd [-lMmpSsu] [-P filename]");
69	exit(2);
70}
71
72
73int
74main(int argc, char *argv[])
75{
76	FILE *fp;
77	int ch, ac = 2;
78	int p[2], logging, secure, user_required, short_list;
79#define	ENTRIES	50
80	char **comp, *prog;
81	char **ap, *av[ENTRIES + 1], line[8192], *lp, *hname;
82	char hostbuf[MAXHOSTNAMELEN];
83
84	prog = _PATH_FINGER;
85	logging = secure = user_required = short_list = 0;
86	openlog("fingerd", LOG_PID, LOG_DAEMON);
87	opterr = 0;
88	while ((ch = getopt(argc, argv, "sluSmMpP:")) != -1)
89		switch (ch) {
90		case 'l':
91			logging = 1;
92			break;
93		case 'P':
94			prog = optarg;
95			break;
96		case 's':
97			secure = 1;
98			break;
99		case 'u':
100			user_required = 1;
101			break;
102		case 'S':
103			if (ac < ENTRIES) {
104				short_list = 1;
105				av[ac++] = "-s";
106			}
107			break;
108		case 'm':
109			if (ac < ENTRIES)
110				av[ac++] = "-m";
111			break;
112		case 'M':
113			if (ac < ENTRIES)
114				av[ac++] = "-M";
115			break;
116		case 'p':
117			if (ac < ENTRIES)
118				av[ac++] = "-p";
119			break;
120		default:
121			usage();
122		}
123
124	if (logging) {
125		struct sockaddr_storage ss;
126		struct sockaddr *sa;
127		socklen_t sval;
128
129		sval = sizeof(ss);
130		if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0) {
131			/* err("getpeername: %s", strerror(errno)); */
132			exit(1);
133		}
134		sa = (struct sockaddr *)&ss;
135		if (getnameinfo(sa, sa->sa_len, hostbuf, sizeof(hostbuf),
136		    NULL, 0, 0) != 0) {
137			strlcpy(hostbuf, "?", sizeof(hostbuf));
138		}
139		hname = hostbuf;
140	}
141
142	if (fgets(line, sizeof(line), stdin) == NULL) {
143		if (logging)
144			syslog(LOG_NOTICE, "query from %s: %s", hname,
145			    feof(stdin) ? "EOF" : strerror(errno));
146		exit(1);
147	}
148
149	if (logging)
150		syslog(LOG_NOTICE, "query from %s: `%.*s'", hname,
151		    (int)strcspn(line, "\r\n"), line);
152
153	/*
154	 * Note: we assume that finger(1) will treat "--" as end of
155	 * command args (ie: that it uses getopt(3)).
156	 */
157	av[ac++] = "--";
158	comp = &av[1];
159	for (lp = line, ap = &av[ac]; ac < ENTRIES;) {
160		if ((*ap = strtok(lp, " \t\r\n")) == NULL)
161			break;
162		lp = NULL;
163		if (secure && strchr(*ap, '@')) {
164			(void) puts("forwarding service denied\r");
165			exit(1);
166		}
167
168		ch = strlen(*ap);
169		while ((*ap)[ch-1] == '@')
170			(*ap)[--ch] = '\0';
171		if (**ap == '\0')
172			continue;
173
174		/* RFC1196: "/[Ww]" == "-l" */
175		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
176			if (!short_list) {
177				av[1] = "-l";
178				comp = &av[0];
179			}
180		} else {
181			ap++;
182			ac++;
183		}
184	}
185	av[ENTRIES - 1] = NULL;
186
187	if ((lp = strrchr(prog, '/')))
188		*comp = ++lp;
189	else
190		*comp = prog;
191
192	if (user_required) {
193		for (ap = comp + 1; strcmp("--", *(ap++)); )
194			;
195		if (*ap == NULL) {
196			(void) puts("must provide username\r");
197			exit(1);
198		}
199	}
200
201	if (pipe(p) < 0)
202		err("pipe: %s", strerror(errno));
203
204	switch (vfork()) {
205	case 0:
206		(void) close(p[0]);
207		if (p[1] != 1) {
208			(void) dup2(p[1], 1);
209			(void) close(p[1]);
210		}
211		execv(prog, comp);
212		err("execv: %s: %s", prog, strerror(errno));
213		_exit(1);
214	case -1:
215		err("fork: %s", strerror(errno));
216	}
217	(void) close(p[1]);
218	if (!(fp = fdopen(p[0], "r")))
219		err("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
229err(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	/* NOTREACHED */
238}
239