faithd.c revision 1.29 1 1.29 itojun /* $NetBSD: faithd.c,v 1.29 2003/05/15 00:23:54 itojun Exp $ */
2 1.29 itojun /* $KAME: faithd.c,v 1.60 2003/05/15 00:21:08 itojun Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1997 and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.10 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.10 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun /*
34 1.1 itojun * User level translator from IPv6 to IPv4.
35 1.1 itojun *
36 1.1 itojun * Usage: faithd [<port> <progpath> <arg1(progname)> <arg2> ...]
37 1.1 itojun * e.g. faithd telnet /usr/local/v6/sbin/telnetd telnetd
38 1.1 itojun */
39 1.1 itojun
40 1.1 itojun #include <sys/param.h>
41 1.1 itojun #include <sys/types.h>
42 1.1 itojun #include <sys/sysctl.h>
43 1.1 itojun #include <sys/socket.h>
44 1.1 itojun #include <sys/wait.h>
45 1.1 itojun #include <sys/stat.h>
46 1.1 itojun #include <sys/time.h>
47 1.1 itojun #include <sys/ioctl.h>
48 1.1 itojun #ifdef __FreeBSD__
49 1.1 itojun #include <libutil.h>
50 1.1 itojun #endif
51 1.1 itojun
52 1.1 itojun #include <stdio.h>
53 1.1 itojun #include <stdlib.h>
54 1.1 itojun #include <stdarg.h>
55 1.1 itojun #include <string.h>
56 1.1 itojun #include <syslog.h>
57 1.1 itojun #include <unistd.h>
58 1.1 itojun #include <errno.h>
59 1.1 itojun #include <signal.h>
60 1.1 itojun #include <fcntl.h>
61 1.1 itojun #include <termios.h>
62 1.1 itojun
63 1.1 itojun #include <net/if_types.h>
64 1.1 itojun #ifdef IFT_FAITH
65 1.1 itojun # define USE_ROUTE
66 1.1 itojun # include <net/if.h>
67 1.1 itojun # include <net/route.h>
68 1.1 itojun # include <net/if_dl.h>
69 1.1 itojun #endif
70 1.1 itojun
71 1.1 itojun #include <netinet/in.h>
72 1.1 itojun #include <arpa/inet.h>
73 1.1 itojun #include <netdb.h>
74 1.9 itojun #include <ifaddrs.h>
75 1.1 itojun
76 1.1 itojun #include "faithd.h"
77 1.17 itojun #include "prefix.h"
78 1.1 itojun
79 1.1 itojun char *serverpath = NULL;
80 1.1 itojun char *serverarg[MAXARGV + 1];
81 1.1 itojun static char *faithdname = NULL;
82 1.1 itojun char logname[BUFSIZ];
83 1.7 itojun char procname[BUFSIZ];
84 1.1 itojun struct myaddrs {
85 1.1 itojun struct myaddrs *next;
86 1.1 itojun struct sockaddr *addr;
87 1.1 itojun };
88 1.1 itojun struct myaddrs *myaddrs = NULL;
89 1.20 itojun static const char *service;
90 1.1 itojun #ifdef USE_ROUTE
91 1.1 itojun static int sockfd = 0;
92 1.1 itojun #endif
93 1.1 itojun int dflag = 0;
94 1.1 itojun static int pflag = 0;
95 1.12 itojun static int inetd = 0;
96 1.17 itojun static char *configfile = NULL;
97 1.1 itojun
98 1.1 itojun int main __P((int, char **));
99 1.12 itojun static int inetd_main __P((int, char **));
100 1.12 itojun static int daemon_main __P((int, char **));
101 1.1 itojun static void play_service __P((int));
102 1.1 itojun static void play_child __P((int, struct sockaddr *));
103 1.1 itojun static int faith_prefix __P((struct sockaddr *));
104 1.1 itojun static int map6to4 __P((struct sockaddr_in6 *, struct sockaddr_in *));
105 1.1 itojun static void sig_child __P((int));
106 1.1 itojun static void sig_terminate __P((int));
107 1.1 itojun static void start_daemon __P((void));
108 1.15 itojun static void exit_stderr __P((const char *, ...))
109 1.15 itojun __attribute__((__format__(__printf__, 1, 2)));
110 1.1 itojun static void grab_myaddrs __P((void));
111 1.1 itojun static void free_myaddrs __P((void));
112 1.1 itojun static void update_myaddrs __P((void));
113 1.1 itojun static void usage __P((void));
114 1.1 itojun
115 1.1 itojun int
116 1.12 itojun main(int argc, char **argv)
117 1.1 itojun {
118 1.1 itojun
119 1.1 itojun /*
120 1.1 itojun * Initializing stuff
121 1.1 itojun */
122 1.1 itojun
123 1.1 itojun faithdname = strrchr(argv[0], '/');
124 1.1 itojun if (faithdname)
125 1.1 itojun faithdname++;
126 1.1 itojun else
127 1.1 itojun faithdname = argv[0];
128 1.1 itojun
129 1.12 itojun if (strcmp(faithdname, "faithd") != 0) {
130 1.12 itojun inetd = 1;
131 1.12 itojun return inetd_main(argc, argv);
132 1.12 itojun } else
133 1.12 itojun return daemon_main(argc, argv);
134 1.12 itojun }
135 1.12 itojun
136 1.12 itojun static int
137 1.12 itojun inetd_main(int argc, char **argv)
138 1.12 itojun {
139 1.12 itojun char path[MAXPATHLEN];
140 1.12 itojun struct sockaddr_storage me;
141 1.12 itojun struct sockaddr_storage from;
142 1.28 itojun socklen_t melen, fromlen;
143 1.12 itojun int i;
144 1.12 itojun int error;
145 1.12 itojun const int on = 1;
146 1.12 itojun char sbuf[NI_MAXSERV], snum[NI_MAXSERV];
147 1.12 itojun
148 1.17 itojun if (config_load(configfile) < 0 && configfile) {
149 1.17 itojun exit_failure("could not load config file");
150 1.17 itojun /*NOTREACHED*/
151 1.17 itojun }
152 1.17 itojun
153 1.12 itojun if (strrchr(argv[0], '/') == NULL)
154 1.12 itojun snprintf(path, sizeof(path), "%s/%s", DEFAULT_DIR, argv[0]);
155 1.12 itojun else
156 1.12 itojun snprintf(path, sizeof(path), "%s", argv[0]);
157 1.12 itojun
158 1.12 itojun #ifdef USE_ROUTE
159 1.12 itojun grab_myaddrs();
160 1.12 itojun
161 1.12 itojun sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
162 1.12 itojun if (sockfd < 0) {
163 1.20 itojun exit_failure("socket(PF_ROUTE): %s", strerror(errno));
164 1.12 itojun /*NOTREACHED*/
165 1.12 itojun }
166 1.12 itojun #endif
167 1.12 itojun
168 1.12 itojun melen = sizeof(me);
169 1.17 itojun if (getsockname(STDIN_FILENO, (struct sockaddr *)&me, &melen) < 0) {
170 1.20 itojun exit_failure("getsockname: %s", strerror(errno));
171 1.17 itojun /*NOTREACHED*/
172 1.17 itojun }
173 1.12 itojun fromlen = sizeof(from);
174 1.17 itojun if (getpeername(STDIN_FILENO, (struct sockaddr *)&from, &fromlen) < 0) {
175 1.20 itojun exit_failure("getpeername: %s", strerror(errno));
176 1.17 itojun /*NOTREACHED*/
177 1.17 itojun }
178 1.12 itojun if (getnameinfo((struct sockaddr *)&me, melen, NULL, 0,
179 1.12 itojun sbuf, sizeof(sbuf), NI_NUMERICHOST) == 0)
180 1.12 itojun service = sbuf;
181 1.12 itojun else
182 1.12 itojun service = DEFAULT_PORT_NAME;
183 1.12 itojun if (getnameinfo((struct sockaddr *)&me, melen, NULL, 0,
184 1.12 itojun snum, sizeof(snum), NI_NUMERICHOST) != 0)
185 1.12 itojun snprintf(snum, sizeof(snum), "?");
186 1.12 itojun
187 1.12 itojun snprintf(logname, sizeof(logname), "faithd %s", snum);
188 1.12 itojun snprintf(procname, sizeof(procname), "accepting port %s", snum);
189 1.12 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
190 1.12 itojun
191 1.17 itojun if (argc >= MAXARGV) {
192 1.14 itojun exit_failure("too many arguments");
193 1.17 itojun /*NOTREACHED*/
194 1.17 itojun }
195 1.12 itojun serverarg[0] = serverpath = path;
196 1.12 itojun for (i = 1; i < argc; i++)
197 1.12 itojun serverarg[i] = argv[i];
198 1.12 itojun serverarg[i] = NULL;
199 1.12 itojun
200 1.12 itojun error = setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &on,
201 1.12 itojun sizeof(on));
202 1.17 itojun if (error < 0) {
203 1.20 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
204 1.17 itojun /*NOTREACHED*/
205 1.17 itojun }
206 1.12 itojun
207 1.12 itojun play_child(STDIN_FILENO, (struct sockaddr *)&from);
208 1.12 itojun exit_failure("should not reach here");
209 1.12 itojun return 0; /*dummy!*/
210 1.12 itojun }
211 1.12 itojun
212 1.12 itojun static int
213 1.12 itojun daemon_main(int argc, char **argv)
214 1.12 itojun {
215 1.12 itojun struct addrinfo hints, *res;
216 1.12 itojun int s_wld, error, i, serverargc, on = 1;
217 1.12 itojun int family = AF_INET6;
218 1.12 itojun int c;
219 1.12 itojun
220 1.26 itojun while ((c = getopt(argc, argv, "df:p")) != -1) {
221 1.1 itojun switch (c) {
222 1.1 itojun case 'd':
223 1.1 itojun dflag++;
224 1.1 itojun break;
225 1.17 itojun case 'f':
226 1.17 itojun configfile = optarg;
227 1.17 itojun break;
228 1.1 itojun case 'p':
229 1.1 itojun pflag++;
230 1.1 itojun break;
231 1.1 itojun default:
232 1.1 itojun usage();
233 1.14 itojun /*NOTREACHED*/
234 1.1 itojun }
235 1.1 itojun }
236 1.1 itojun argc -= optind;
237 1.1 itojun argv += optind;
238 1.1 itojun
239 1.17 itojun if (config_load(configfile) < 0 && configfile) {
240 1.17 itojun exit_failure("could not load config file");
241 1.17 itojun /*NOTREACHED*/
242 1.17 itojun }
243 1.17 itojun
244 1.1 itojun
245 1.1 itojun #ifdef USE_ROUTE
246 1.1 itojun grab_myaddrs();
247 1.1 itojun #endif
248 1.1 itojun
249 1.1 itojun switch (argc) {
250 1.1 itojun case 0:
251 1.14 itojun usage();
252 1.14 itojun /*NOTREACHED*/
253 1.1 itojun default:
254 1.1 itojun serverargc = argc - NUMARG;
255 1.12 itojun if (serverargc >= MAXARGV)
256 1.14 itojun exit_stderr("too many arguments");
257 1.1 itojun
258 1.29 itojun serverpath = strdup(argv[NUMPRG]);
259 1.21 itojun if (!serverpath)
260 1.21 itojun exit_stderr("not enough core");
261 1.1 itojun for (i = 0; i < serverargc; i++) {
262 1.29 itojun serverarg[i] = strdup(argv[i + NUMARG]);
263 1.21 itojun if (!serverarg[i])
264 1.21 itojun exit_stderr("not enough core");
265 1.1 itojun }
266 1.1 itojun serverarg[i] = NULL;
267 1.1 itojun /* fall throuth */
268 1.1 itojun case 1: /* no local service */
269 1.1 itojun service = argv[NUMPRT];
270 1.1 itojun break;
271 1.1 itojun }
272 1.1 itojun
273 1.23 itojun start_daemon();
274 1.23 itojun
275 1.1 itojun /*
276 1.1 itojun * Opening wild card socket for this service.
277 1.1 itojun */
278 1.1 itojun
279 1.1 itojun memset(&hints, 0, sizeof(hints));
280 1.1 itojun hints.ai_flags = AI_PASSIVE;
281 1.1 itojun hints.ai_family = family;
282 1.1 itojun hints.ai_socktype = SOCK_STREAM;
283 1.29 itojun hints.ai_protocol = IPPROTO_TCP; /* SCTP? */
284 1.1 itojun error = getaddrinfo(NULL, service, &hints, &res);
285 1.10 itojun if (error)
286 1.23 itojun exit_failure("getaddrinfo: %s", gai_strerror(error));
287 1.1 itojun
288 1.1 itojun s_wld = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
289 1.1 itojun if (s_wld == -1)
290 1.23 itojun exit_failure("socket: %s", strerror(errno));
291 1.1 itojun
292 1.1 itojun #ifdef IPV6_FAITH
293 1.1 itojun if (res->ai_family == AF_INET6) {
294 1.1 itojun error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_FAITH, &on, sizeof(on));
295 1.1 itojun if (error == -1)
296 1.23 itojun exit_failure("setsockopt(IPV6_FAITH): %s",
297 1.20 itojun strerror(errno));
298 1.1 itojun }
299 1.1 itojun #endif
300 1.1 itojun
301 1.1 itojun error = setsockopt(s_wld, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
302 1.1 itojun if (error == -1)
303 1.23 itojun exit_failure("setsockopt(SO_REUSEADDR): %s", strerror(errno));
304 1.1 itojun
305 1.1 itojun error = setsockopt(s_wld, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
306 1.1 itojun if (error == -1)
307 1.23 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
308 1.1 itojun
309 1.27 itojun #ifdef IPV6_V6ONLY
310 1.27 itojun error = setsockopt(s_wld, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
311 1.27 itojun if (error == -1)
312 1.27 itojun exit_failure("setsockopt(IPV6_V6ONLY): %s", strerror(errno));
313 1.27 itojun #endif
314 1.27 itojun
315 1.1 itojun error = bind(s_wld, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
316 1.1 itojun if (error == -1)
317 1.23 itojun exit_failure("bind: %s", strerror(errno));
318 1.1 itojun
319 1.1 itojun error = listen(s_wld, 5);
320 1.1 itojun if (error == -1)
321 1.23 itojun exit_failure("listen: %s", strerror(errno));
322 1.1 itojun
323 1.1 itojun #ifdef USE_ROUTE
324 1.1 itojun sockfd = socket(PF_ROUTE, SOCK_RAW, PF_UNSPEC);
325 1.1 itojun if (sockfd < 0) {
326 1.23 itojun exit_failure("socket(PF_ROUTE): %s", strerror(errno));
327 1.1 itojun /*NOTREACHED*/
328 1.1 itojun }
329 1.1 itojun #endif
330 1.1 itojun
331 1.1 itojun /*
332 1.1 itojun * Everything is OK.
333 1.1 itojun */
334 1.1 itojun
335 1.7 itojun snprintf(logname, sizeof(logname), "faithd %s", service);
336 1.7 itojun snprintf(procname, sizeof(procname), "accepting port %s", service);
337 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
338 1.17 itojun syslog(LOG_INFO, "Staring faith daemon for %s port", service);
339 1.1 itojun
340 1.1 itojun play_service(s_wld);
341 1.14 itojun /* NOTREACHED */
342 1.1 itojun exit(1); /*pacify gcc*/
343 1.1 itojun }
344 1.1 itojun
345 1.1 itojun static void
346 1.1 itojun play_service(int s_wld)
347 1.1 itojun {
348 1.1 itojun struct sockaddr_storage srcaddr;
349 1.28 itojun socklen_t len;
350 1.1 itojun int s_src;
351 1.1 itojun pid_t child_pid;
352 1.1 itojun fd_set rfds;
353 1.1 itojun int error;
354 1.1 itojun int maxfd;
355 1.1 itojun
356 1.1 itojun /*
357 1.1 itojun * Wait, accept, fork, faith....
358 1.1 itojun */
359 1.1 itojun again:
360 1.13 itojun setproctitle("%s", procname);
361 1.1 itojun
362 1.1 itojun FD_ZERO(&rfds);
363 1.27 itojun if (s_wld >= FD_SETSIZE)
364 1.27 itojun exit_failure("descriptor too big");
365 1.1 itojun FD_SET(s_wld, &rfds);
366 1.1 itojun maxfd = s_wld;
367 1.1 itojun #ifdef USE_ROUTE
368 1.1 itojun if (sockfd) {
369 1.27 itojun if (sockfd >= FD_SETSIZE)
370 1.27 itojun exit_failure("descriptor too big");
371 1.1 itojun FD_SET(sockfd, &rfds);
372 1.1 itojun maxfd = (maxfd < sockfd) ? sockfd : maxfd;
373 1.1 itojun }
374 1.1 itojun #endif
375 1.1 itojun
376 1.1 itojun error = select(maxfd + 1, &rfds, NULL, NULL, NULL);
377 1.1 itojun if (error < 0) {
378 1.1 itojun if (errno == EINTR)
379 1.1 itojun goto again;
380 1.20 itojun exit_failure("select: %s", strerror(errno));
381 1.1 itojun /*NOTREACHED*/
382 1.1 itojun }
383 1.1 itojun
384 1.1 itojun #ifdef USE_ROUTE
385 1.1 itojun if (FD_ISSET(sockfd, &rfds)) {
386 1.1 itojun update_myaddrs();
387 1.1 itojun }
388 1.1 itojun #endif
389 1.1 itojun if (FD_ISSET(s_wld, &rfds)) {
390 1.1 itojun len = sizeof(srcaddr);
391 1.27 itojun s_src = accept(s_wld, (struct sockaddr *)&srcaddr, &len);
392 1.24 itojun if (s_src < 0) {
393 1.24 itojun if (errno == ECONNABORTED)
394 1.24 itojun goto again;
395 1.20 itojun exit_failure("socket: %s", strerror(errno));
396 1.17 itojun /*NOTREACHED*/
397 1.27 itojun }
398 1.27 itojun if (srcaddr.ss_family == AF_INET6 &&
399 1.27 itojun IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&srcaddr)->sin6_addr)) {
400 1.27 itojun close(s_src);
401 1.27 itojun syslog(LOG_ERR, "connection from IPv4 mapped address?");
402 1.27 itojun goto again;
403 1.17 itojun }
404 1.1 itojun
405 1.1 itojun child_pid = fork();
406 1.26 itojun
407 1.1 itojun if (child_pid == 0) {
408 1.1 itojun /* child process */
409 1.1 itojun close(s_wld);
410 1.1 itojun closelog();
411 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
412 1.1 itojun play_child(s_src, (struct sockaddr *)&srcaddr);
413 1.1 itojun exit_failure("should never reach here");
414 1.17 itojun /*NOTREACHED*/
415 1.1 itojun } else {
416 1.1 itojun /* parent process */
417 1.1 itojun close(s_src);
418 1.1 itojun if (child_pid == -1)
419 1.1 itojun syslog(LOG_ERR, "can't fork");
420 1.1 itojun }
421 1.1 itojun }
422 1.1 itojun goto again;
423 1.1 itojun }
424 1.1 itojun
425 1.1 itojun static void
426 1.1 itojun play_child(int s_src, struct sockaddr *srcaddr)
427 1.1 itojun {
428 1.10 itojun struct sockaddr_storage dstaddr6;
429 1.1 itojun struct sockaddr_storage dstaddr4;
430 1.24 itojun char src[NI_MAXHOST];
431 1.24 itojun char dst6[NI_MAXHOST];
432 1.24 itojun char dst4[NI_MAXHOST];
433 1.28 itojun socklen_t len = sizeof(dstaddr6);
434 1.1 itojun int s_dst, error, hport, nresvport, on = 1;
435 1.1 itojun struct timeval tv;
436 1.2 itojun struct sockaddr *sa4;
437 1.17 itojun const struct config *conf;
438 1.1 itojun
439 1.1 itojun tv.tv_sec = 1;
440 1.1 itojun tv.tv_usec = 0;
441 1.1 itojun
442 1.1 itojun getnameinfo(srcaddr, srcaddr->sa_len,
443 1.1 itojun src, sizeof(src), NULL, 0, NI_NUMERICHOST);
444 1.1 itojun syslog(LOG_INFO, "accepted a client from %s", src);
445 1.1 itojun
446 1.1 itojun error = getsockname(s_src, (struct sockaddr *)&dstaddr6, &len);
447 1.17 itojun if (error == -1) {
448 1.20 itojun exit_failure("getsockname: %s", strerror(errno));
449 1.17 itojun /*NOTREACHED*/
450 1.17 itojun }
451 1.1 itojun
452 1.2 itojun getnameinfo((struct sockaddr *)&dstaddr6, len,
453 1.1 itojun dst6, sizeof(dst6), NULL, 0, NI_NUMERICHOST);
454 1.1 itojun syslog(LOG_INFO, "the client is connecting to %s", dst6);
455 1.1 itojun
456 1.1 itojun if (!faith_prefix((struct sockaddr *)&dstaddr6)) {
457 1.1 itojun if (serverpath) {
458 1.1 itojun /*
459 1.1 itojun * Local service
460 1.1 itojun */
461 1.1 itojun syslog(LOG_INFO, "executing local %s", serverpath);
462 1.12 itojun if (!inetd) {
463 1.12 itojun dup2(s_src, 0);
464 1.12 itojun close(s_src);
465 1.12 itojun dup2(0, 1);
466 1.12 itojun dup2(0, 2);
467 1.12 itojun }
468 1.1 itojun execv(serverpath, serverarg);
469 1.20 itojun syslog(LOG_ERR, "execv %s: %s", serverpath,
470 1.20 itojun strerror(errno));
471 1.1 itojun _exit(EXIT_FAILURE);
472 1.1 itojun } else {
473 1.1 itojun close(s_src);
474 1.1 itojun exit_success("no local service for %s", service);
475 1.1 itojun }
476 1.1 itojun }
477 1.1 itojun
478 1.1 itojun /*
479 1.1 itojun * Act as a translator
480 1.1 itojun */
481 1.1 itojun
482 1.2 itojun switch (((struct sockaddr *)&dstaddr6)->sa_family) {
483 1.2 itojun case AF_INET6:
484 1.1 itojun if (!map6to4((struct sockaddr_in6 *)&dstaddr6,
485 1.1 itojun (struct sockaddr_in *)&dstaddr4)) {
486 1.1 itojun close(s_src);
487 1.14 itojun exit_failure("map6to4 failed");
488 1.17 itojun /*NOTREACHED*/
489 1.1 itojun }
490 1.1 itojun syslog(LOG_INFO, "translating from v6 to v4");
491 1.2 itojun break;
492 1.2 itojun default:
493 1.1 itojun close(s_src);
494 1.14 itojun exit_failure("family not supported");
495 1.2 itojun /*NOTREACHED*/
496 1.1 itojun }
497 1.1 itojun
498 1.2 itojun sa4 = (struct sockaddr *)&dstaddr4;
499 1.2 itojun getnameinfo(sa4, sa4->sa_len,
500 1.1 itojun dst4, sizeof(dst4), NULL, 0, NI_NUMERICHOST);
501 1.17 itojun
502 1.17 itojun conf = config_match(srcaddr, sa4);
503 1.17 itojun if (!conf || !conf->permit) {
504 1.17 itojun close(s_src);
505 1.19 itojun if (conf) {
506 1.19 itojun exit_failure("translation to %s not permitted for %s",
507 1.19 itojun dst4, prefix_string(&conf->match));
508 1.19 itojun /*NOTREACHED*/
509 1.19 itojun } else {
510 1.19 itojun exit_failure("translation to %s not permitted", dst4);
511 1.19 itojun /*NOTREACHED*/
512 1.19 itojun }
513 1.17 itojun }
514 1.17 itojun
515 1.1 itojun syslog(LOG_INFO, "the translator is connecting to %s", dst4);
516 1.1 itojun
517 1.1 itojun setproctitle("port %s, %s -> %s", service, src, dst4);
518 1.1 itojun
519 1.2 itojun if (sa4->sa_family == AF_INET6)
520 1.1 itojun hport = ntohs(((struct sockaddr_in6 *)&dstaddr4)->sin6_port);
521 1.1 itojun else /* AF_INET */
522 1.1 itojun hport = ntohs(((struct sockaddr_in *)&dstaddr4)->sin_port);
523 1.1 itojun
524 1.25 itojun if (pflag)
525 1.2 itojun s_dst = rresvport_af(&nresvport, sa4->sa_family);
526 1.25 itojun else
527 1.25 itojun s_dst = socket(sa4->sa_family, SOCK_STREAM, 0);
528 1.17 itojun if (s_dst < 0) {
529 1.20 itojun exit_failure("socket: %s", strerror(errno));
530 1.17 itojun /*NOTREACHED*/
531 1.17 itojun }
532 1.17 itojun
533 1.17 itojun if (conf->src.a.ss_family) {
534 1.20 itojun if (bind(s_dst, (const struct sockaddr *)&conf->src.a,
535 1.17 itojun conf->src.a.ss_len) < 0) {
536 1.20 itojun exit_failure("bind: %s", strerror(errno));
537 1.17 itojun /*NOTREACHED*/
538 1.17 itojun }
539 1.17 itojun }
540 1.1 itojun
541 1.1 itojun error = setsockopt(s_dst, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
542 1.17 itojun if (error < 0) {
543 1.20 itojun exit_failure("setsockopt(SO_OOBINLINE): %s", strerror(errno));
544 1.17 itojun /*NOTREACHED*/
545 1.17 itojun }
546 1.1 itojun
547 1.1 itojun error = setsockopt(s_src, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
548 1.17 itojun if (error < 0) {
549 1.20 itojun exit_failure("setsockopt(SO_SNDTIMEO): %s", strerror(errno));
550 1.17 itojun /*NOTREACHED*/
551 1.17 itojun }
552 1.1 itojun error = setsockopt(s_dst, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
553 1.17 itojun if (error < 0) {
554 1.20 itojun exit_failure("setsockopt(SO_SNDTIMEO): %s", strerror(errno));
555 1.17 itojun /*NOTREACHED*/
556 1.17 itojun }
557 1.1 itojun
558 1.8 itojun error = connect(s_dst, sa4, sa4->sa_len);
559 1.17 itojun if (error < 0) {
560 1.20 itojun exit_failure("connect: %s", strerror(errno));
561 1.17 itojun /*NOTREACHED*/
562 1.17 itojun }
563 1.1 itojun
564 1.1 itojun switch (hport) {
565 1.1 itojun case FTP_PORT:
566 1.1 itojun ftp_relay(s_src, s_dst);
567 1.1 itojun break;
568 1.1 itojun default:
569 1.1 itojun tcp_relay(s_src, s_dst, service);
570 1.1 itojun break;
571 1.1 itojun }
572 1.1 itojun
573 1.1 itojun /* NOTREACHED */
574 1.1 itojun }
575 1.1 itojun
576 1.1 itojun /* 0: non faith, 1: faith */
577 1.1 itojun static int
578 1.1 itojun faith_prefix(struct sockaddr *dst)
579 1.1 itojun {
580 1.1 itojun #ifndef USE_ROUTE
581 1.1 itojun int mib[4], size;
582 1.1 itojun struct in6_addr faith_prefix;
583 1.1 itojun struct sockaddr_in6 *dst6 = (struct sockaddr_in *)dst;
584 1.1 itojun
585 1.1 itojun if (dst->sa_family != AF_INET6)
586 1.1 itojun return 0;
587 1.1 itojun
588 1.1 itojun mib[0] = CTL_NET;
589 1.1 itojun mib[1] = PF_INET6;
590 1.1 itojun mib[2] = IPPROTO_IPV6;
591 1.1 itojun mib[3] = IPV6CTL_FAITH_PREFIX;
592 1.1 itojun size = sizeof(struct in6_addr);
593 1.17 itojun if (sysctl(mib, 4, &faith_prefix, &size, NULL, 0) < 0) {
594 1.20 itojun exit_failure("sysctl: %s", strerror(errno));
595 1.17 itojun /*NOTREACHED*/
596 1.17 itojun }
597 1.1 itojun
598 1.2 itojun if (memcmp(dst, &faith_prefix,
599 1.2 itojun sizeof(struct in6_addr) - sizeof(struct in_addr) == 0) {
600 1.1 itojun return 1;
601 1.2 itojun }
602 1.1 itojun return 0;
603 1.1 itojun #else
604 1.1 itojun struct myaddrs *p;
605 1.1 itojun struct sockaddr_in6 *sin6;
606 1.1 itojun struct sockaddr_in *sin4;
607 1.3 itojun struct sockaddr_in6 *dst6;
608 1.3 itojun struct sockaddr_in *dst4;
609 1.3 itojun struct sockaddr_in dstmap;
610 1.3 itojun
611 1.4 itojun dst6 = (struct sockaddr_in6 *)dst;
612 1.3 itojun if (dst->sa_family == AF_INET6
613 1.3 itojun && IN6_IS_ADDR_V4MAPPED(&dst6->sin6_addr)) {
614 1.3 itojun /* ugly... */
615 1.3 itojun memset(&dstmap, 0, sizeof(dstmap));
616 1.3 itojun dstmap.sin_family = AF_INET;
617 1.3 itojun dstmap.sin_len = sizeof(dstmap);
618 1.3 itojun memcpy(&dstmap.sin_addr, &dst6->sin6_addr.s6_addr[12],
619 1.3 itojun sizeof(dstmap.sin_addr));
620 1.3 itojun dst = (struct sockaddr *)&dstmap;
621 1.3 itojun }
622 1.3 itojun
623 1.3 itojun dst6 = (struct sockaddr_in6 *)dst;
624 1.3 itojun dst4 = (struct sockaddr_in *)dst;
625 1.1 itojun
626 1.1 itojun for (p = myaddrs; p; p = p->next) {
627 1.1 itojun sin6 = (struct sockaddr_in6 *)p->addr;
628 1.1 itojun sin4 = (struct sockaddr_in *)p->addr;
629 1.1 itojun
630 1.3 itojun if (p->addr->sa_len != dst->sa_len
631 1.3 itojun || p->addr->sa_family != dst->sa_family)
632 1.3 itojun continue;
633 1.3 itojun
634 1.3 itojun switch (dst->sa_family) {
635 1.3 itojun case AF_INET6:
636 1.3 itojun if (sin6->sin6_scope_id == dst6->sin6_scope_id
637 1.5 itojun && IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &dst6->sin6_addr))
638 1.1 itojun return 0;
639 1.3 itojun break;
640 1.3 itojun case AF_INET:
641 1.3 itojun if (sin4->sin_addr.s_addr == dst4->sin_addr.s_addr)
642 1.6 itojun return 0;
643 1.3 itojun break;
644 1.1 itojun }
645 1.1 itojun }
646 1.1 itojun return 1;
647 1.1 itojun #endif
648 1.1 itojun }
649 1.1 itojun
650 1.1 itojun /* 0: non faith, 1: faith */
651 1.1 itojun static int
652 1.1 itojun map6to4(struct sockaddr_in6 *dst6, struct sockaddr_in *dst4)
653 1.1 itojun {
654 1.1 itojun memset(dst4, 0, sizeof(*dst4));
655 1.1 itojun dst4->sin_len = sizeof(*dst4);
656 1.1 itojun dst4->sin_family = AF_INET;
657 1.1 itojun dst4->sin_port = dst6->sin6_port;
658 1.2 itojun memcpy(&dst4->sin_addr, &dst6->sin6_addr.s6_addr[12],
659 1.2 itojun sizeof(dst4->sin_addr));
660 1.1 itojun
661 1.1 itojun if (dst4->sin_addr.s_addr == INADDR_ANY
662 1.1 itojun || dst4->sin_addr.s_addr == INADDR_BROADCAST
663 1.14 itojun || IN_MULTICAST(ntohl(dst4->sin_addr.s_addr)))
664 1.1 itojun return 0;
665 1.1 itojun
666 1.1 itojun return 1;
667 1.1 itojun }
668 1.1 itojun
669 1.1 itojun
670 1.1 itojun static void
671 1.1 itojun sig_child(int sig)
672 1.1 itojun {
673 1.1 itojun int status;
674 1.1 itojun pid_t pid;
675 1.1 itojun
676 1.21 itojun while ((pid = wait3(&status, WNOHANG, (struct rusage *)0)) > 0)
677 1.21 itojun if (WEXITSTATUS(status))
678 1.26 itojun syslog(LOG_WARNING, "child %ld exit status 0x%x",
679 1.26 itojun (long)pid, status);
680 1.1 itojun }
681 1.1 itojun
682 1.1 itojun void
683 1.1 itojun sig_terminate(int sig)
684 1.1 itojun {
685 1.1 itojun syslog(LOG_INFO, "Terminating faith daemon");
686 1.1 itojun exit(EXIT_SUCCESS);
687 1.1 itojun }
688 1.1 itojun
689 1.1 itojun static void
690 1.1 itojun start_daemon(void)
691 1.1 itojun {
692 1.18 itojun #ifdef SA_NOCLDWAIT
693 1.18 itojun struct sigaction sa;
694 1.18 itojun #endif
695 1.18 itojun
696 1.1 itojun if (daemon(0, 0) == -1)
697 1.20 itojun exit_stderr("daemon: %s", strerror(errno));
698 1.1 itojun
699 1.18 itojun #ifdef SA_NOCLDWAIT
700 1.18 itojun memset(&sa, 0, sizeof(sa));
701 1.18 itojun sa.sa_handler = sig_child;
702 1.18 itojun sa.sa_flags = SA_NOCLDWAIT;
703 1.18 itojun sigemptyset(&sa.sa_mask);
704 1.18 itojun sigaction(SIGCHLD, &sa, (struct sigaction *)0);
705 1.18 itojun #else
706 1.17 itojun if (signal(SIGCHLD, sig_child) == SIG_ERR) {
707 1.20 itojun exit_failure("signal CHLD: %s", strerror(errno));
708 1.17 itojun /*NOTREACHED*/
709 1.17 itojun }
710 1.18 itojun #endif
711 1.1 itojun
712 1.17 itojun if (signal(SIGTERM, sig_terminate) == SIG_ERR) {
713 1.20 itojun exit_failure("signal TERM: %s", strerror(errno));
714 1.17 itojun /*NOTREACHED*/
715 1.17 itojun }
716 1.1 itojun }
717 1.1 itojun
718 1.14 itojun static void
719 1.14 itojun exit_stderr(const char *fmt, ...)
720 1.1 itojun {
721 1.1 itojun va_list ap;
722 1.1 itojun char buf[BUFSIZ];
723 1.1 itojun
724 1.1 itojun va_start(ap, fmt);
725 1.7 itojun vsnprintf(buf, sizeof(buf), fmt, ap);
726 1.1 itojun va_end(ap);
727 1.17 itojun fprintf(stderr, "%s\n", buf);
728 1.1 itojun exit(EXIT_FAILURE);
729 1.1 itojun }
730 1.1 itojun
731 1.1 itojun void
732 1.1 itojun exit_failure(const char *fmt, ...)
733 1.1 itojun {
734 1.1 itojun va_list ap;
735 1.1 itojun char buf[BUFSIZ];
736 1.1 itojun
737 1.1 itojun va_start(ap, fmt);
738 1.7 itojun vsnprintf(buf, sizeof(buf), fmt, ap);
739 1.1 itojun va_end(ap);
740 1.11 itojun syslog(LOG_ERR, "%s", buf);
741 1.1 itojun exit(EXIT_FAILURE);
742 1.1 itojun }
743 1.1 itojun
744 1.1 itojun void
745 1.1 itojun exit_success(const char *fmt, ...)
746 1.1 itojun {
747 1.1 itojun va_list ap;
748 1.1 itojun char buf[BUFSIZ];
749 1.1 itojun
750 1.1 itojun va_start(ap, fmt);
751 1.7 itojun vsnprintf(buf, sizeof(buf), fmt, ap);
752 1.1 itojun va_end(ap);
753 1.11 itojun syslog(LOG_INFO, "%s", buf);
754 1.1 itojun exit(EXIT_SUCCESS);
755 1.1 itojun }
756 1.1 itojun
757 1.1 itojun #ifdef USE_ROUTE
758 1.1 itojun static void
759 1.1 itojun grab_myaddrs()
760 1.1 itojun {
761 1.9 itojun struct ifaddrs *ifap, *ifa;
762 1.9 itojun struct myaddrs *p;
763 1.9 itojun struct sockaddr_in6 *sin6;
764 1.9 itojun
765 1.9 itojun if (getifaddrs(&ifap) != 0) {
766 1.9 itojun exit_failure("getifaddrs");
767 1.9 itojun /*NOTREACHED*/
768 1.9 itojun }
769 1.9 itojun
770 1.9 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
771 1.9 itojun switch (ifa->ifa_addr->sa_family) {
772 1.9 itojun case AF_INET:
773 1.9 itojun case AF_INET6:
774 1.9 itojun break;
775 1.9 itojun default:
776 1.9 itojun continue;
777 1.9 itojun }
778 1.9 itojun
779 1.9 itojun p = (struct myaddrs *)malloc(sizeof(struct myaddrs) +
780 1.9 itojun ifa->ifa_addr->sa_len);
781 1.9 itojun if (!p) {
782 1.9 itojun exit_failure("not enough core");
783 1.9 itojun /*NOTREACHED*/
784 1.9 itojun }
785 1.9 itojun memcpy(p + 1, ifa->ifa_addr, ifa->ifa_addr->sa_len);
786 1.9 itojun p->next = myaddrs;
787 1.9 itojun p->addr = (struct sockaddr *)(p + 1);
788 1.9 itojun #ifdef __KAME__
789 1.9 itojun if (ifa->ifa_addr->sa_family == AF_INET6) {
790 1.9 itojun sin6 = (struct sockaddr_in6 *)p->addr;
791 1.9 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)
792 1.9 itojun || IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
793 1.9 itojun sin6->sin6_scope_id =
794 1.9 itojun ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
795 1.9 itojun sin6->sin6_addr.s6_addr[2] = 0;
796 1.9 itojun sin6->sin6_addr.s6_addr[3] = 0;
797 1.9 itojun }
798 1.9 itojun }
799 1.9 itojun #endif
800 1.9 itojun myaddrs = p;
801 1.9 itojun if (dflag) {
802 1.9 itojun char hbuf[NI_MAXHOST];
803 1.9 itojun getnameinfo(p->addr, p->addr->sa_len,
804 1.9 itojun hbuf, sizeof(hbuf), NULL, 0,
805 1.9 itojun NI_NUMERICHOST);
806 1.9 itojun syslog(LOG_INFO, "my interface: %s %s", hbuf,
807 1.9 itojun ifa->ifa_name);
808 1.9 itojun }
809 1.9 itojun }
810 1.9 itojun
811 1.9 itojun freeifaddrs(ifap);
812 1.1 itojun }
813 1.1 itojun
814 1.1 itojun static void
815 1.1 itojun free_myaddrs()
816 1.1 itojun {
817 1.1 itojun struct myaddrs *p, *q;
818 1.1 itojun
819 1.1 itojun p = myaddrs;
820 1.1 itojun while (p) {
821 1.1 itojun q = p->next;
822 1.1 itojun free(p);
823 1.1 itojun p = q;
824 1.1 itojun }
825 1.1 itojun myaddrs = NULL;
826 1.1 itojun }
827 1.1 itojun
828 1.1 itojun static void
829 1.1 itojun update_myaddrs()
830 1.1 itojun {
831 1.1 itojun char msg[BUFSIZ];
832 1.1 itojun int len;
833 1.1 itojun struct rt_msghdr *rtm;
834 1.1 itojun
835 1.1 itojun len = read(sockfd, msg, sizeof(msg));
836 1.1 itojun if (len < 0) {
837 1.1 itojun syslog(LOG_ERR, "read(PF_ROUTE) failed");
838 1.1 itojun return;
839 1.1 itojun }
840 1.1 itojun rtm = (struct rt_msghdr *)msg;
841 1.1 itojun if (len < 4 || len < rtm->rtm_msglen) {
842 1.1 itojun syslog(LOG_ERR, "read(PF_ROUTE) short read");
843 1.1 itojun return;
844 1.1 itojun }
845 1.1 itojun if (rtm->rtm_version != RTM_VERSION) {
846 1.1 itojun syslog(LOG_ERR, "routing socket version mismatch");
847 1.1 itojun close(sockfd);
848 1.1 itojun sockfd = 0;
849 1.1 itojun return;
850 1.1 itojun }
851 1.1 itojun switch (rtm->rtm_type) {
852 1.1 itojun case RTM_NEWADDR:
853 1.1 itojun case RTM_DELADDR:
854 1.1 itojun case RTM_IFINFO:
855 1.1 itojun break;
856 1.1 itojun default:
857 1.1 itojun return;
858 1.1 itojun }
859 1.1 itojun /* XXX more filters here? */
860 1.1 itojun
861 1.1 itojun syslog(LOG_INFO, "update interface address list");
862 1.1 itojun free_myaddrs();
863 1.1 itojun grab_myaddrs();
864 1.1 itojun }
865 1.1 itojun #endif /*USE_ROUTE*/
866 1.1 itojun
867 1.1 itojun static void
868 1.1 itojun usage()
869 1.1 itojun {
870 1.17 itojun fprintf(stderr, "usage: %s [-dp] [-f conf] service [serverpath [serverargs]]\n",
871 1.1 itojun faithdname);
872 1.1 itojun exit(0);
873 1.1 itojun }
874