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