- fixing compile error
[oweals/gnunet.git] / src / dns / gnunet-helper-dns.c
1 /*
2    This file is part of GNUnet.
3    (C) 2010, 2011, 2012 Christian Grothoff
4
5    GNUnet is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published
7    by the Free Software Foundation; either version 3, or (at your
8    option) any later version.
9
10    GNUnet is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GNUnet; see the file COPYING.  If not, write to the
17    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file dns/gnunet-helper-dns.c
23  * @brief helper to install firewall rules to hijack all DNS traffic
24  *        and send it to our virtual interface (except for DNS traffic
25  *        that originates on the specified port).  We then
26  *        allow interacting with our virtual interface via stdin/stdout. 
27  * @author Philipp Tölke
28  * @author Christian Grothoff
29  *
30  * This program alters the Linux firewall rules so that DNS traffic
31  * that ordinarily exits the system can be intercepted and managed by
32  * a virtual interface.  In order to achieve this, DNS traffic is
33  * marked with the DNS_MARK given in below and re-routed to a custom
34  * table with the DNS_TABLE ID given below.  Systems and
35  * administrators must take care to not cause conflicts with these
36  * values (it was deemed safest to hardcode them as passing these
37  * values as arguments might permit messing with arbitrary firewall
38  * rules, which would be dangerous).
39  *
40  * The code first sets up the virtual interface, then begins to
41  * redirect the DNS traffic to it, and then on errors or SIGTERM shuts
42  * down the virtual interface and removes the rules for the traffic
43  * redirection.
44  *
45  *
46  * Note that having this binary SUID is only partially safe: it will
47  * allow redirecting (and intercepting / mangling) of all DNS traffic
48  * originating from this system by any user who is able to run it.
49  * Furthermore, this code will make it trivial to DoS all DNS traffic
50  * originating from the current system, simply by sending it to
51  * nowhere (redirect stdout to /dev/null).
52  *
53  * Naturally, neither of these problems can be helped as this is the
54  * fundamental purpose of the binary.  Certifying that this code is
55  * "safe" thus only means that it doesn't allow anything else (such
56  * as local priv. escalation, etc.). 
57  *
58  * The following list of people have reviewed this code and considered
59  * it safe (within specifications) since the last modification (if you
60  * reviewed it, please have your name added to the list):
61  *
62  * - Christian Grothoff 
63  */
64 #include "platform.h"
65
66 #include <linux/if_tun.h>
67
68 /**
69  * Need 'struct GNUNET_MessageHeader'.
70  */
71 #include "gnunet_common.h"
72
73 /**
74  * Need DNS message types.
75  */
76 #include "gnunet_protocols.h"
77
78 /**
79  * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
80  */
81 #define MAX_SIZE 65536
82
83 #ifndef _LINUX_IN6_H
84 /**
85  * This is in linux/include/net/ipv6.h, but not always exported...
86  */
87 struct in6_ifreq
88 {
89   struct in6_addr ifr6_addr;
90   uint32_t ifr6_prefixlen;
91   unsigned int ifr6_ifindex;
92 };
93 #endif
94
95 /**
96  * Name and full path of IPTABLES binary.
97  */
98 static const char *sbin_iptables;
99
100 /**
101  * Name and full path of IPTABLES binary.
102  */
103 static const char *sbin_ip;
104
105 /**
106  * Port for DNS traffic.
107  */
108 #define DNS_PORT "53"
109
110 /**
111  * Marker we set for our hijacked DNS traffic.  We use GNUnet's
112  * port (2086) plus the DNS port (53) in HEX to make a 32-bit mark
113  * (which is hopefully long enough to not collide); so
114  * 0x08260035 = 136708149 (hopefully unique enough...).
115  */
116 #define DNS_MARK "136708149"
117
118 /**
119  * Table we use for our DNS rules.  0-255 is the range and
120  * 0, 253, 254 and 255 are already reserved.  As this is about
121  * DNS and as "53" is likely (fingers crossed!) high enough to
122  * not usually conflict with a normal user's setup, we use 53
123  * to give a hint that this has something to do with DNS.
124  */
125 #define DNS_TABLE "53"
126
127
128 /**
129  * Control pipe for shutdown via signal. [0] is the read end,
130  * [1] is the write end.
131  */
132 static int cpipe[2];
133
134
135 /**
136  * Signal handler called to initiate "nice" shutdown.  Signals select
137  * loop via non-bocking pipe 'cpipe'.
138  *
139  * @param signal signal number of the signal (not used)
140  */
141 static void
142 signal_handler (int signal)
143 {
144   /* ignore return value, as the signal handler could theoretically
145      be called many times before the shutdown can actually happen */
146   (void) write (cpipe[1], "K", 1);
147 }
148
149
150 /**
151  * Run the given command and wait for it to complete.
152  * 
153  * @param file name of the binary to run
154  * @param cmd command line arguments (as given to 'execv')
155  * @return 0 on success, 1 on any error
156  */
157 static int
158 fork_and_exec (const char *file, 
159                char *const cmd[])
160 {
161   int status;
162   pid_t pid;
163   pid_t ret;
164
165   pid = fork ();
166   if (-1 == pid)
167   {
168     fprintf (stderr, 
169              "fork failed: %s\n", 
170              strerror (errno));
171     return 1;
172   }
173   if (0 == pid)
174   {
175     /* we are the child process */
176     /* close stdin/stdout to not cause interference
177        with the helper's main protocol! */
178     (void) close (0); 
179     (void) close (1); 
180     (void) execv (file, cmd);
181     /* can only get here on error */
182     fprintf (stderr, 
183              "exec `%s' failed: %s\n", 
184              file,
185              strerror (errno));
186     _exit (1);
187   }
188   /* keep running waitpid as long as the only error we get is 'EINTR' */
189   while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
190           (errno == EINTR) ); 
191   if (-1 == ret)
192   {
193     fprintf (stderr, 
194              "waitpid failed: %s\n", 
195              strerror (errno));
196     return 1;
197   }
198   if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
199     return 1;
200   /* child process completed and returned success, we're happy */
201   return 0;
202 }
203
204
205 /**
206  * Creates a tun-interface called dev;
207  *
208  * @param dev is asumed to point to a char[IFNAMSIZ]
209  *        if *dev == '\\0', uses the name supplied by the kernel;
210  * @return the fd to the tun or -1 on error
211  */
212 static int
213 init_tun (char *dev)
214 {
215   struct ifreq ifr;
216   int fd;
217
218   if (NULL == dev)
219   {
220     errno = EINVAL;
221     return -1;
222   }
223
224   if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
225   {
226     fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
227              strerror (errno));
228     return -1;
229   }
230
231   if (fd >= FD_SETSIZE)
232   {
233     fprintf (stderr, "File descriptor to large: %d", fd);
234     return -1;
235   }
236
237   memset (&ifr, 0, sizeof (ifr));
238   ifr.ifr_flags = IFF_TUN;
239
240   if ('\0' != *dev)
241     strncpy (ifr.ifr_name, dev, IFNAMSIZ);
242
243   if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
244   {
245     fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
246              strerror (errno));
247     (void) close (fd);
248     return -1;
249   }
250   strcpy (dev, ifr.ifr_name);
251   return fd;
252 }
253
254
255 /**
256  * @brief Sets the IPv6-Address given in address on the interface dev
257  *
258  * @param dev the interface to configure
259  * @param address the IPv6-Address
260  * @param prefix_len the length of the network-prefix
261  */
262 static void
263 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
264 {
265   struct ifreq ifr;
266   struct in6_ifreq ifr6;
267   struct sockaddr_in6 sa6;
268   int fd;
269
270   /*
271    * parse the new address
272    */
273   memset (&sa6, 0, sizeof (struct sockaddr_in6));
274   sa6.sin6_family = AF_INET6;
275   if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
276   {
277     fprintf (stderr, "Failed to parse address `%s': %s\n", address,
278              strerror (errno));
279     exit (1);
280   }
281
282   if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
283   {
284     fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
285     exit (1);
286   }
287
288   memset (&ifr, 0, sizeof (struct ifreq));
289   /*
290    * Get the index of the if
291    */
292   strncpy (ifr.ifr_name, dev, IFNAMSIZ);
293   if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
294   {
295     fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
296     (void) close (fd);
297     exit (1);
298   }
299
300   memset (&ifr6, 0, sizeof (struct in6_ifreq));
301   ifr6.ifr6_addr = sa6.sin6_addr;
302   ifr6.ifr6_ifindex = ifr.ifr_ifindex;
303   ifr6.ifr6_prefixlen = prefix_len;
304
305   /*
306    * Set the address
307    */
308   if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
309   {
310     fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
311              strerror (errno));
312     (void) close (fd);
313     exit (1);
314   }
315
316   /*
317    * Get the flags
318    */
319   if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
320   {
321     fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
322              strerror (errno));
323     (void) close (fd);
324     exit (1);
325   }
326
327   /*
328    * Add the UP and RUNNING flags
329    */
330   ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
331   if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
332   {
333     fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
334              strerror (errno));
335     (void) close (fd);
336     exit (1);
337   }
338
339   if (0 != close (fd))
340   {
341     fprintf (stderr, "close failed: %s\n", strerror (errno));
342     exit (1);
343   }
344 }
345
346
347 /**
348  * @brief Sets the IPv4-Address given in address on the interface dev
349  *
350  * @param dev the interface to configure
351  * @param address the IPv4-Address
352  * @param mask the netmask
353  */
354 static void
355 set_address4 (const char *dev, const char *address, const char *mask)
356 {
357   int fd;
358   struct sockaddr_in *addr;
359   struct ifreq ifr;
360
361   memset (&ifr, 0, sizeof (struct ifreq));
362   addr = (struct sockaddr_in *) &(ifr.ifr_addr);
363   addr->sin_family = AF_INET;
364
365   /*
366    * Parse the address
367    */
368   if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
369   {
370     fprintf (stderr, "Failed to parse address `%s': %s\n", address,
371              strerror (errno));
372     exit (1);
373   }
374
375   if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
376   {
377     fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
378     exit (1);
379   }
380
381   strncpy (ifr.ifr_name, dev, IFNAMSIZ);
382
383   /*
384    * Set the address
385    */
386   if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
387   {
388     fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
389     (void) close (fd);
390     exit (1);
391   }
392
393   /*
394    * Parse the netmask
395    */
396   addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
397   if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
398   {
399     fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
400              strerror (errno));
401     (void) close (fd);
402     exit (1);
403   }
404
405   /*
406    * Set the netmask
407    */
408   if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
409   {
410     fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
411              strerror (errno));
412     (void) close (fd);
413     exit (1);
414   }
415
416   /*
417    * Get the flags
418    */
419   if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
420   {
421     fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
422              strerror (errno));
423     (void) close (fd);
424     exit (1);
425   }
426
427   /*
428    * Add the UP and RUNNING flags
429    */
430   ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
431   if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
432   {
433     fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
434              strerror (errno));
435     (void) close (fd);
436     exit (1);
437   }
438
439   if (0 != close (fd))
440   {
441     fprintf (stderr, "close failed: %s\n", strerror (errno));
442     (void) close (fd);
443     exit (1);
444   }
445 }
446
447
448 /**
449  * Start forwarding to and from the tunnel.  This function runs with
450  * "reduced" priviledges (saved UID is still 0, but effective UID is
451  * the real user ID).
452  *
453  * @param fd_tun tunnel FD
454  */
455 static void
456 run (int fd_tun)
457 {
458   /*
459    * The buffer filled by reading from fd_tun
460    */
461   unsigned char buftun[MAX_SIZE];
462   ssize_t buftun_size = 0;
463   unsigned char *buftun_read = NULL;
464
465   /*
466    * The buffer filled by reading from stdin
467    */
468   unsigned char bufin[MAX_SIZE];
469   ssize_t bufin_size = 0;
470   size_t bufin_rpos = 0;
471   unsigned char *bufin_read = NULL;
472   fd_set fds_w;
473   fd_set fds_r;
474   int max;
475
476   while (1)
477   {
478     FD_ZERO (&fds_w);
479     FD_ZERO (&fds_r);
480
481     /*
482      * We are supposed to read and the buffer is empty
483      * -> select on read from tun
484      */
485     if (0 == buftun_size)
486       FD_SET (fd_tun, &fds_r);
487
488     /*
489      * We are supposed to read and the buffer is not empty
490      * -> select on write to stdout
491      */
492     if (0 != buftun_size)
493       FD_SET (1, &fds_w);
494
495     /*
496      * We are supposed to write and the buffer is empty
497      * -> select on read from stdin
498      */
499     if (NULL == bufin_read)
500       FD_SET (0, &fds_r);
501
502     /*
503      * We are supposed to write and the buffer is not empty
504      * -> select on write to tun
505      */
506     if (NULL != bufin_read)
507       FD_SET (fd_tun, &fds_w);
508
509     FD_SET (cpipe[0], &fds_r);
510     max = (fd_tun > cpipe[0]) ? fd_tun : cpipe[0];
511
512     int r = select (max + 1, &fds_r, &fds_w, NULL, NULL);
513
514     if (-1 == r)
515     {
516       if (EINTR == errno)
517         continue;
518       fprintf (stderr, "select failed: %s\n", strerror (errno));
519       return;
520     }
521
522     if (r > 0)
523     {
524       if (FD_ISSET (cpipe[0], &fds_r))
525         return; /* aborted by signal */
526
527       if (FD_ISSET (fd_tun, &fds_r))
528       {
529         buftun_size =
530             read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
531                   MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
532         if (-1 == buftun_size)
533         {
534           if ( (errno == EINTR) ||
535                (errno == EAGAIN) )
536             continue;
537           fprintf (stderr, "read-error: %s\n", strerror (errno));
538           return;
539         }
540         if (0 == buftun_size)
541         {
542           fprintf (stderr, "EOF on tun\n");
543           return;
544         }
545         buftun_read = buftun;
546         {
547           struct GNUNET_MessageHeader *hdr =
548               (struct GNUNET_MessageHeader *) buftun;
549           buftun_size += sizeof (struct GNUNET_MessageHeader);
550           hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
551           hdr->size = htons (buftun_size);
552         }
553       }
554       else if (FD_ISSET (1, &fds_w))
555       {
556         ssize_t written = write (1, buftun_read, buftun_size);
557
558         if (-1 == written)
559         {
560           if ( (errno == EINTR) ||
561                (errno == EAGAIN) )
562             continue;
563           fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
564           return;
565         }
566         if (0 == written)
567         {
568           fprintf (stderr, "write returned 0\n");
569           return;
570         }
571         buftun_size -= written;
572         buftun_read += written;        
573       }
574
575       if (FD_ISSET (0, &fds_r))
576       {
577         bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
578         if (-1 == bufin_size)
579         {
580           if ( (errno == EINTR) ||
581                (errno == EAGAIN) )
582             continue;
583           fprintf (stderr, "read-error: %s\n", strerror (errno));
584           return;
585         }
586         if (0 == bufin_size)
587         {
588           fprintf (stderr, "EOF on stdin\n");
589           return;
590         }
591         {
592           struct GNUNET_MessageHeader *hdr;
593
594 PROCESS_BUFFER:
595           bufin_rpos += bufin_size;
596           if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
597             continue;
598           hdr = (struct GNUNET_MessageHeader *) bufin;
599           if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_DNS_HELPER)
600           {
601             fprintf (stderr, "protocol violation!\n");
602             return;
603           }
604           if (ntohs (hdr->size) > bufin_rpos)
605             continue;
606           bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
607           bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
608           bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
609         }
610       }
611       else if (FD_ISSET (fd_tun, &fds_w))
612       {
613         ssize_t written = write (fd_tun, bufin_read, bufin_size);
614
615         if (-1 == written)
616         {
617           if ( (errno == EINTR) ||
618                (errno == EAGAIN) )
619             continue;
620           fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
621           return;
622         }
623         if (0 == written)
624         {
625           fprintf (stderr, "write returned 0\n");
626           return;
627         }
628         {
629           bufin_size -= written;
630           bufin_read += written;
631           if (0 == bufin_size)
632           {
633             memmove (bufin, bufin_read, bufin_rpos);
634             bufin_read = NULL;  /* start reading again */
635             bufin_size = 0;
636             goto PROCESS_BUFFER;
637           }
638         }
639       }
640     }
641   }
642 }
643
644
645 /**
646  * Main function of "gnunet-helper-dns", which opens a VPN tunnel interface,
647  * redirects all outgoing DNS traffic (except from the specified port) to that
648  * interface and then passes traffic from and to the interface via stdin/stdout.
649  *
650  * Once stdin/stdout close or have other errors, the tunnel is closed and the
651  * DNS traffic redirection is stopped.
652  *
653  * @param argc number of arguments
654  * @param argv 0: binary name (should be "gnunet-helper-vpn")
655  *             1: tunnel interface name (typically "gnunet-dns")
656  *             2: IPv6 address for the tunnel ("FE80::1")
657  *             3: IPv6 netmask length in bits ("64")
658  *             4: IPv4 address for the tunnel ("1.2.3.4")
659  *             5: IPv4 netmask ("255.255.0.0")
660  *             6: PORT to not hijack ("55533")
661  * @return 0 on success, otherwise code indicating type of error:
662  *         1 wrong number of arguments
663  *         2 invalid arguments (i.e. port number / prefix length wrong)
664  *         3 iptables not executable
665  *         4 ip not executable
666  *         5 failed to initialize tunnel interface
667  *         6 failed to initialize control pipe
668  *         8 failed to change routing table, cleanup successful
669  *         9-23 failed to change routing table and failed to undo some changes to routing table
670  *         24 failed to drop privs
671  *         25-39 failed to drop privs and then failed to undo some changes to routing table
672  *         40 failed to regain privs
673  *         41-55 failed to regain prisv and then failed to undo some changes to routing table
674  *         255 failed to handle kill signal properly
675  */
676 int
677 main (int argc, char *const*argv)
678 {
679   unsigned int port;
680   char localport[6];
681   int r;
682   char dev[IFNAMSIZ];
683   int fd_tun;
684
685   if (7 != argc)
686   {
687     fprintf (stderr, "Fatal: must supply 6 arguments!\n");
688     return 1;
689   }
690
691   /* verify that the binaries were care about are executable */
692   if (0 == access ("/sbin/iptables", X_OK))
693     sbin_iptables = "/sbin/iptables";
694   else if (0 == access ("/usr/sbin/iptables", X_OK))
695     sbin_iptables = "/usr/sbin/iptables";
696   else
697   {
698     fprintf (stderr, 
699              "Fatal: executable iptables not found in approved directories: %s\n",
700              strerror (errno));
701     return 3;
702   }
703   if (0 == access ("/sbin/ip", X_OK))
704     sbin_ip = "/sbin/ip";
705   else if (0 == access ("/usr/sbin/ip", X_OK))
706     sbin_ip = "/usr/sbin/ip";
707   else
708   {
709     fprintf (stderr,
710              "Fatal: executable ip not found in approved directories: %s\n",
711              strerror (errno));
712     return 4;
713   }
714
715   /* validate port number */
716   port = atoi (argv[6]);
717   if ( (port == 0) || (port >= 65536) )
718   {
719     fprintf (stderr, 
720              "Port `%u' is invalid\n",
721              port);
722     return 2;
723   }
724   /* print port number to string for command-line use*/
725   (void) snprintf (localport,
726                    sizeof (localport), 
727                    "%u", 
728                    port);
729
730   /* do not die on SIGPIPE */
731   if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
732   {
733     fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
734              strerror (errno));
735     return 7;
736   }
737
738   /* setup pipe to shutdown nicely on SIGINT */
739   if (0 != pipe (cpipe))
740   {
741     fprintf (stderr, 
742              "Fatal: could not setup control pipe: %s\n",
743              strerror (errno));
744     return 6;
745   }
746   if (cpipe[0] >= FD_SETSIZE)
747   {
748     fprintf (stderr, "Pipe file descriptor to large: %d", cpipe[0]);
749     (void) close (cpipe[0]);
750     (void) close (cpipe[1]);
751     return 6;
752   }
753   {
754     /* make pipe non-blocking, as we theoretically could otherwise block
755        in the signal handler */
756     int flags = fcntl (cpipe[1], F_GETFL);
757     if (-1 == flags)
758     {
759       fprintf (stderr, "Failed to read flags for pipe: %s", strerror (errno));
760       (void) close (cpipe[0]);
761       (void) close (cpipe[1]);
762       return 6;
763     }
764     flags |= O_NONBLOCK;
765     if (0 != fcntl (cpipe[1], F_SETFL, flags))
766     {
767       fprintf (stderr, "Failed to make pipe non-blocking: %s", strerror (errno));
768       (void) close (cpipe[0]);
769       (void) close (cpipe[1]);
770       return 6;
771     }
772   }
773   if ( (SIG_ERR == signal (SIGTERM, &signal_handler)) ||
774        (SIG_ERR == signal (SIGINT, &signal_handler)) ||
775        (SIG_ERR == signal (SIGHUP, &signal_handler)) )       
776   { 
777     fprintf (stderr, 
778              "Fatal: could not initialize signal handler: %s\n",
779              strerror (errno));
780     (void) close (cpipe[0]);
781     (void) close (cpipe[1]);
782     return 7;   
783   }
784
785
786   /* get interface name */
787   strncpy (dev, argv[1], IFNAMSIZ);
788   dev[IFNAMSIZ - 1] = '\0';
789
790   /* now open virtual interface (first part that requires root) */
791   if (-1 == (fd_tun = init_tun (dev)))
792   {
793     fprintf (stderr, "Fatal: could not initialize tun-interface\n");
794     (void) signal (SIGTERM, SIG_IGN);
795     (void) signal (SIGINT, SIG_IGN);
796     (void) signal (SIGHUP, SIG_IGN);
797     (void) close (cpipe[0]);
798     (void) close (cpipe[1]);
799     return 5;
800   }
801
802   /* now set interface addresses */
803   {
804     const char *address = argv[2];
805     long prefix_len = atol (argv[3]);
806
807     if ((prefix_len < 1) || (prefix_len > 127))
808     {
809       fprintf (stderr, "Fatal: prefix_len out of range\n");
810       (void) signal (SIGTERM, SIG_IGN);
811       (void) signal (SIGINT, SIG_IGN);
812       (void) signal (SIGHUP, SIG_IGN);
813       (void) close (cpipe[0]);
814       (void) close (cpipe[1]);
815       return 2;
816     }
817     set_address6 (dev, address, prefix_len);
818   }
819
820   {
821     const char *address = argv[4];
822     const char *mask = argv[5];
823
824     set_address4 (dev, address, mask);
825   }
826   
827   /* update routing tables -- next part why we need SUID! */
828   /* Forward everything from the given local port (with destination
829      to port 53, and only for UDP) without hijacking */
830   r = 8; /* failed to fully setup routing table */
831   {
832     char *const mangle_args[] = 
833       {
834         "iptables", "-t", "mangle", "-I", "OUTPUT", "1", "-p",
835         "udp", "--sport", localport, "--dport", DNS_PORT, "-j",
836         "ACCEPT", NULL
837       };
838     if (0 != fork_and_exec (sbin_iptables, mangle_args))
839       goto cleanup_rest;
840   }    
841   /* Mark all of the other DNS traffic using our mark DNS_MARK */
842   {
843     char *const mark_args[] =
844       {
845         "iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p",
846         "udp", "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK,
847         NULL
848       };
849     if (0 != fork_and_exec (sbin_iptables, mark_args))
850       goto cleanup_mangle_1;
851   }
852   /* Forward all marked DNS traffic to our DNS_TABLE */
853   {
854     char *const forward_args[] =
855       {
856         "ip", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
857       };
858     if (0 != fork_and_exec (sbin_ip, forward_args))
859       goto cleanup_mark_2;
860   }
861   /* Finally, add rule in our forwarding table to pass to our virtual interface */
862   {
863     char *const route_args[] =
864       {
865         "ip", "route", "add", "default", "dev", dev,
866         "table", DNS_TABLE, NULL
867       };
868     if (0 != fork_and_exec (sbin_ip, route_args))
869       goto cleanup_forward_3;
870   }
871
872   /* drop privs *except* for the saved UID; this is not perfect, but better
873      than doing nothing */
874   uid_t uid = getuid ();
875 #ifdef HAVE_SETRESUID
876   if (0 != setresuid (uid, uid, 0))
877   {
878     fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
879     r = 24;
880     goto cleanup_route_4;
881   }
882 #else
883   /* Note: no 'setuid' here as we must keep our saved UID as root */
884   if (0 != seteuid (uid)) 
885   {
886     fprintf (stderr, "Failed to seteuid: %s\n", strerror (errno));
887     r = 24;
888     goto cleanup_route_4;
889   }
890 #endif
891
892   r = 0; /* did fully setup routing table (if nothing else happens, we were successful!) */
893
894   /* now forward until we hit a problem */
895    run (fd_tun);
896   
897   /* now need to regain privs so we can remove the firewall rules we added! */
898 #ifdef HAVE_SETRESUID
899   if (0 != setresuid (uid, 0, 0))
900   {
901     fprintf (stderr, "Failed to setresuid back to root: %s\n", strerror (errno));
902     r = 40;
903     goto cleanup_route_4;
904   }
905 #else
906   if (0 != seteuid (0)) 
907   {
908     fprintf (stderr, "Failed to seteuid back to root: %s\n", strerror (errno));
909     r = 40;
910     goto cleanup_route_4;
911   }
912 #endif
913  
914   /* update routing tables again -- this is why we could not fully drop privs */
915   /* now undo updating of routing tables; normal exit or clean-up-on-error case */
916  cleanup_route_4:
917   {
918     char *const route_clean_args[] =                     
919       {
920         "ip", "route", "del", "default", "dev", dev,
921         "table", DNS_TABLE, NULL
922       };
923     if (0 != fork_and_exec (sbin_ip, route_clean_args))
924       r += 1;
925   }
926  cleanup_forward_3:
927   {
928     char *const forward_clean_args[] =
929       {
930         "ip", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
931       };
932     if (0 != fork_and_exec (sbin_ip, forward_clean_args))
933       r += 2;   
934   }
935  cleanup_mark_2:
936   {
937     char *const mark_clean_args[] =
938       {
939         "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
940         "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, NULL
941       };
942     if (0 != fork_and_exec (sbin_iptables, mark_clean_args))
943       r += 4;
944   }     
945  cleanup_mangle_1:
946   {
947     char *const mangle_clean_args[] =
948       {
949         "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
950         "--sport", localport, "--dport", DNS_PORT, "-j", "ACCEPT",
951         NULL
952       };
953     if (0 != fork_and_exec (sbin_iptables, mangle_clean_args))
954       r += 8;
955   }
956
957  cleanup_rest:
958   /* close virtual interface */
959   (void) close (fd_tun);
960   /* remove signal handler so we can close the pipes */
961   (void) signal (SIGTERM, SIG_IGN);
962   (void) signal (SIGINT, SIG_IGN);
963   (void) signal (SIGHUP, SIG_IGN);
964   (void) close (cpipe[0]);
965   (void) close (cpipe[1]);
966   return r;
967 }
968
969 /* end of gnunet-helper-dns.c */