-try with finished
[oweals/gnunet.git] / src / dns / gnunet-helper-dns.c
index 07cc9647c4d819b337374289ac7bf11940e01304..5e603ceb8de05c1f13288d3e56c7f7413f0fbe3c 100644 (file)
@@ -95,12 +95,12 @@ struct in6_ifreq
 /**
  * Name and full path of IPTABLES binary.
  */
-#define SBIN_IPTABLES "/sbin/iptables"
+static const char *sbin_iptables;
 
 /**
  * Name and full path of IPTABLES binary.
  */
-#define SBIN_IP "/sbin/ip"
+static const char *sbin_ip;
 
 /**
  * Port for DNS traffic.
@@ -173,6 +173,10 @@ fork_and_exec (const char *file,
   if (0 == pid)
   {
     /* we are the child process */
+    /* close stdin/stdout to not cause interference
+       with the helper's main protocol! */
+    (void) close (0); 
+    (void) close (1); 
     (void) execv (file, cmd);
     /* can only get here on error */
     fprintf (stderr, 
@@ -573,6 +577,7 @@ run (int fd_tun)
         bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
         if (-1 == bufin_size)
         {
+         bufin_read = NULL;
          if ( (errno == EINTR) ||
               (errno == EAGAIN) )
            continue;
@@ -581,6 +586,7 @@ run (int fd_tun)
         }
        if (0 == bufin_size)
         {
+         bufin_read = NULL;
           fprintf (stderr, "EOF on stdin\n");
          return;
         }
@@ -685,19 +691,25 @@ main (int argc, char *const*argv)
   }
 
   /* verify that the binaries were care about are executable */
-  if (0 != access (SBIN_IPTABLES, X_OK))
+  if (0 == access ("/sbin/iptables", X_OK))
+    sbin_iptables = "/sbin/iptables";
+  else if (0 == access ("/usr/sbin/iptables", X_OK))
+    sbin_iptables = "/usr/sbin/iptables";
+  else
   {
     fprintf (stderr, 
-            "`%s' is not executable: %s\n", 
-            SBIN_IPTABLES,
+            "Fatal: executable iptables not found in approved directories: %s\n",
             strerror (errno));
     return 3;
   }
-  if (0 != access (SBIN_IP, X_OK))
+  if (0 == access ("/sbin/ip", X_OK))
+    sbin_ip = "/sbin/ip";
+  else if (0 == access ("/usr/sbin/ip", X_OK))
+    sbin_ip = "/usr/sbin/ip";
+  else
   {
-    fprintf (stderr, 
-            "`%s' is not executable: %s\n", 
-            SBIN_IP,
+    fprintf (stderr,
+            "Fatal: executable ip not found in approved directories: %s\n",
             strerror (errno));
     return 4;
   }
@@ -760,7 +772,9 @@ main (int argc, char *const*argv)
       return 6;
     }
   }
-  if (SIG_ERR == signal (SIGTERM, &signal_handler))
+  if ( (SIG_ERR == signal (SIGTERM, &signal_handler)) ||
+       (SIG_ERR == signal (SIGINT, &signal_handler)) ||
+       (SIG_ERR == signal (SIGHUP, &signal_handler)) )       
   { 
     fprintf (stderr, 
             "Fatal: could not initialize signal handler: %s\n",
@@ -779,7 +793,9 @@ main (int argc, char *const*argv)
   if (-1 == (fd_tun = init_tun (dev)))
   {
     fprintf (stderr, "Fatal: could not initialize tun-interface\n");
+    (void) signal (SIGTERM, SIG_IGN);
     (void) signal (SIGINT, SIG_IGN);
+    (void) signal (SIGHUP, SIG_IGN);
     (void) close (cpipe[0]);
     (void) close (cpipe[1]);
     return 5;
@@ -793,7 +809,9 @@ main (int argc, char *const*argv)
     if ((prefix_len < 1) || (prefix_len > 127))
     {
       fprintf (stderr, "Fatal: prefix_len out of range\n");
+      (void) signal (SIGTERM, SIG_IGN);
       (void) signal (SIGINT, SIG_IGN);
+      (void) signal (SIGHUP, SIG_IGN);
       (void) close (cpipe[0]);
       (void) close (cpipe[1]);
       return 2;
@@ -819,7 +837,7 @@ main (int argc, char *const*argv)
        "udp", "--sport", localport, "--dport", DNS_PORT, "-j",
        "ACCEPT", NULL
       };
-    if (0 != fork_and_exec (SBIN_IPTABLES, mangle_args))
+    if (0 != fork_and_exec (sbin_iptables, mangle_args))
       goto cleanup_rest;
   }    
   /* Mark all of the other DNS traffic using our mark DNS_MARK */
@@ -830,7 +848,7 @@ main (int argc, char *const*argv)
        "udp", "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK,
        NULL
       };
-    if (0 != fork_and_exec (SBIN_IPTABLES, mark_args))
+    if (0 != fork_and_exec (sbin_iptables, mark_args))
       goto cleanup_mangle_1;
   }
   /* Forward all marked DNS traffic to our DNS_TABLE */
@@ -839,7 +857,7 @@ main (int argc, char *const*argv)
       {
        "ip", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
       };
-    if (0 != fork_and_exec (SBIN_IP, forward_args))
+    if (0 != fork_and_exec (sbin_ip, forward_args))
       goto cleanup_mark_2;
   }
   /* Finally, add rule in our forwarding table to pass to our virtual interface */
@@ -849,7 +867,7 @@ main (int argc, char *const*argv)
        "ip", "route", "add", "default", "dev", dev,
        "table", DNS_TABLE, NULL
       };
-    if (0 != fork_and_exec (SBIN_IP, route_args))
+    if (0 != fork_and_exec (sbin_ip, route_args))
       goto cleanup_forward_3;
   }
 
@@ -904,7 +922,7 @@ main (int argc, char *const*argv)
        "ip", "route", "del", "default", "dev", dev,
        "table", DNS_TABLE, NULL
       };
-    if (0 != fork_and_exec (SBIN_IP, route_clean_args))
+    if (0 != fork_and_exec (sbin_ip, route_clean_args))
       r += 1;
   }
  cleanup_forward_3:
@@ -913,7 +931,7 @@ main (int argc, char *const*argv)
       {
        "ip", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
       };
-    if (0 != fork_and_exec (SBIN_IP, forward_clean_args))
+    if (0 != fork_and_exec (sbin_ip, forward_clean_args))
       r += 2;  
   }
  cleanup_mark_2:
@@ -923,7 +941,7 @@ main (int argc, char *const*argv)
        "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
        "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, NULL
       };
-    if (0 != fork_and_exec (SBIN_IPTABLES, mark_clean_args))
+    if (0 != fork_and_exec (sbin_iptables, mark_clean_args))
       r += 4;
   }    
  cleanup_mangle_1:
@@ -934,15 +952,17 @@ main (int argc, char *const*argv)
        "--sport", localport, "--dport", DNS_PORT, "-j", "ACCEPT",
        NULL
       };
-    if (0 != fork_and_exec (SBIN_IPTABLES, mangle_clean_args))
+    if (0 != fork_and_exec (sbin_iptables, mangle_clean_args))
       r += 8;
   }
 
  cleanup_rest:
   /* close virtual interface */
   (void) close (fd_tun);
-  /* remove SIGINT handler so we can close the pipes */
+  /* remove signal handler so we can close the pipes */
+  (void) signal (SIGTERM, SIG_IGN);
   (void) signal (SIGINT, SIG_IGN);
+  (void) signal (SIGHUP, SIG_IGN);
   (void) close (cpipe[0]);
   (void) close (cpipe[1]);
   return r;