indentation
[oweals/gnunet.git] / src / vpn / gnunet-helper-hijack-dns.c
1 /*
2    This file is part of GNUnet.
3    (C) 2010 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 vpn/gnunet-helper-hijack-dns.c
23  * @brief
24  * @author Philipp Tölke
25  */
26 #include <platform.h>
27
28 #include "gnunet_common.h"
29
30 int
31 fork_and_exec (char *file, char *cmd[])
32 {
33   pid_t pid = fork ();
34
35   if (pid < 0)
36   {
37     fprintf (stderr, "could not fork: %s\n", strerror (errno));
38     return GNUNET_SYSERR;
39   }
40
41   int st = 0;
42
43   if (pid == 0)
44   {
45     execv (file, cmd);
46   }
47   else
48   {
49     waitpid (pid, &st, 0);
50   }
51   return WIFEXITED (st) && (WEXITSTATUS (st) == 0);
52 }
53
54 int
55 main (int argc, char **argv)
56 {
57   int delete = 0;
58   int port = 0;
59   char *virt_dns;
60
61   if (argc < 3)
62     return GNUNET_SYSERR;
63
64   if (strncmp (argv[1], "-d", 2) == 0)
65   {
66     if (argc < 3)
67       return GNUNET_SYSERR;
68     delete = 1;
69     port = atoi (argv[2]);
70     virt_dns = argv[3];
71   }
72   else
73   {
74     port = atoi (argv[1]);
75     virt_dns = argv[2];
76   }
77
78   if (port == 0)
79     return GNUNET_SYSERR;
80
81   struct stat s;
82
83   if (stat ("/sbin/iptables", &s) < 0)
84   {
85     fprintf (stderr, "stat on /sbin/iptables failed: %s\n", strerror (errno));
86     return GNUNET_SYSERR;
87   }
88   if (stat ("/sbin/ip", &s) < 0)
89   {
90     fprintf (stderr, "stat on /sbin/ip failed: %s\n", strerror (errno));
91     return GNUNET_SYSERR;
92   }
93
94   char localport[7];
95
96   snprintf (localport, 7, "%d", port);
97
98   int r;
99
100   if (delete)
101   {
102 e4:
103     r = fork_and_exec ("/sbin/ip", (char *[])
104                        {
105                        "ip", "route", "del", "default", "via", virt_dns,
106                        "table", "2", NULL});
107 e3:
108     r = fork_and_exec ("/sbin/ip", (char *[])
109                        {
110                        "ip", "rule", "del", "fwmark", "3", "table", "2", NULL});
111 e2:
112     r = fork_and_exec ("/sbin/iptables", (char *[])
113                        {
114                        "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
115                        "--dport", "53", "-j", "MARK", "--set-mark", "3", NULL});
116 e1:
117     r = fork_and_exec ("/sbin/iptables", (char *[])
118                        {
119                        "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
120                        "--sport", localport, "--dport", "53", "-j", "ACCEPT",
121                        NULL});
122     if (!delete)
123       r = 0;
124   }
125   else
126   {
127     r = fork_and_exec ("/sbin/iptables", (char *[])
128                        {
129                        "iptables", "-t", "mangle", "-I", "OUTPUT", "1", "-p",
130                        "udp", "--sport", localport, "--dport", "53", "-j",
131                        "ACCEPT", NULL});
132     if (!r)
133       goto e1;
134     r = fork_and_exec ("/sbin/iptables", (char *[])
135                        {
136                        "iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p",
137                        "udp", "--dport", "53", "-j", "MARK", "--set-mark", "3",
138                        NULL});
139     if (!r)
140       goto e2;
141     r = fork_and_exec ("/sbin/ip", (char *[])
142                        {
143                        "ip", "rule", "add", "fwmark", "3", "table", "2", NULL});
144     if (!r)
145       goto e3;
146     r = fork_and_exec ("/sbin/ip", (char *[])
147                        {
148                        "ip", "route", "add", "default", "via", virt_dns,
149                        "table", "2", NULL});
150     if (!r)
151       goto e4;
152   }
153   if (r)
154     return GNUNET_YES;
155   return GNUNET_SYSERR;
156 }