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