SUID comments, review
[oweals/gnunet.git] / src / transport / gnunet-nat-client.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
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 src/transport/gnunet-nat-client.c
23  * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
24  *        This code will work under GNU/Linux only.  
25  * @author Christian Grothoff
26  *
27  * This program will send ONE ICMP message using RAW sockets
28  * to the IP address specified as the second argument.  Since
29  * it uses RAW sockets, it must be installed SUID or run as 'root'.
30  * In order to keep the security risk of the resulting SUID binary
31  * minimal, the program ONLY opens the RAW socket with root
32  * priviledges, then drops them and only then starts to process
33  * command line arguments.  The code also does not link against
34  * any shared libraries (except libc) and is strictly minimal
35  * (except for checking for errors).  The following list of people
36  * have reviewed this code and considered it safe since the last
37  * modification (if you reviewed it, please have your name added
38  * to the list):
39  *
40  * - Christian Grothoff
41  */
42 #define _GNU_SOURCE
43 #include <sys/types.h> 
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <stdint.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_icmp.h>
55 #include <netinet/in.h> 
56
57 /**
58  * Must match IP given in the server.
59  */
60 #define DUMMY_IP "1.2.3.4"
61
62 struct ip_packet 
63 {
64   uint8_t vers_ihl;
65   uint8_t tos;
66   uint16_t pkt_len;
67   uint16_t id;
68   uint16_t flags_frag_offset;
69   uint8_t ttl;
70   uint8_t proto;
71   uint16_t checksum;
72   uint32_t src_ip;
73   uint32_t dst_ip;
74 };
75
76 struct icmp_packet 
77 {
78   uint8_t type;
79   uint8_t code;
80   uint16_t checksum;
81   uint32_t reserved;
82 };
83  
84 static int rawsock;
85
86 static struct in_addr dummy;
87  
88 static struct in_addr target;
89
90
91 static uint16_t 
92 calc_checksum(const uint16_t *data, 
93               unsigned int bytes)
94 {
95   uint32_t sum;
96   unsigned int i;
97
98   sum = 0;
99   for (i=0;i<bytes/2;i++) 
100     sum += data[i];        
101   sum = (sum & 0xffff) + (sum >> 16);
102   sum = htons(0xffff - sum);
103   return sum;
104 }
105
106
107 static void
108 make_echo (const struct in_addr *src_ip,
109            struct icmp_packet *echo)
110 {
111   memset(echo, 0, sizeof(struct icmp_packet));
112   echo->type = ICMP_ECHO;
113   echo->code = 0;
114   echo->reserved = 0;
115   echo->checksum = 0;
116   echo->checksum = htons(calc_checksum((uint16_t*)echo, 
117                                        sizeof (struct icmp_packet)));
118 }
119
120
121 /**
122  * Send an ICMP message to the target.
123  *
124  * @param my_ip source address
125  * @param other target address
126  */
127 static void
128 send_icmp (const struct in_addr *my_ip,
129            const struct in_addr *other)
130 {
131   struct ip_packet ip_pkt;
132   struct icmp_packet *icmp_pkt;
133   struct icmp_packet icmp_echo;
134   struct sockaddr_in dst;
135   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet)*2];
136   size_t off;
137   int err;
138
139   /* ip header: send to (known) ip address */
140   off = 0;
141   memset(&ip_pkt, 0, sizeof(ip_pkt));
142   ip_pkt.vers_ihl = 0x45;
143   ip_pkt.tos = 0;
144   ip_pkt.pkt_len = sizeof (packet); /* huh? */
145   ip_pkt.id = 1; 
146   ip_pkt.flags_frag_offset = 0;
147   ip_pkt.ttl = IPDEFTTL;
148   ip_pkt.proto = IPPROTO_ICMP;
149   ip_pkt.checksum = 0; 
150   ip_pkt.src_ip = my_ip->s_addr;
151   ip_pkt.dst_ip = other->s_addr;
152   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
153   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
154   off += sizeof (ip_pkt);
155   /* icmp reply: time exceeded */
156   icmp_pkt = (struct icmp_packet*) &packet[off];
157   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
158   icmp_pkt->type = ICMP_TIME_EXCEEDED;
159   icmp_pkt->code = 0; 
160   icmp_pkt->reserved = 0;
161   icmp_pkt->checksum = 0;
162   off += sizeof (struct icmp_packet);
163
164   /* ip header of the presumably 'lost' udp packet */
165   ip_pkt.vers_ihl = 0x45;
166   ip_pkt.tos = 0;
167   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_packet));
168   ip_pkt.id = 1; 
169   ip_pkt.flags_frag_offset = 0;
170   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
171   ip_pkt.proto = IPPROTO_ICMP;
172   ip_pkt.src_ip = other->s_addr;
173   ip_pkt.dst_ip = dummy.s_addr;
174   ip_pkt.checksum = 0;
175   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
176   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
177   off += sizeof (struct ip_packet);
178   make_echo (other, &icmp_echo);
179   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_packet));
180   off += sizeof (struct icmp_packet);
181   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt, 
182                                            sizeof (struct icmp_packet)*2 + sizeof(struct ip_packet)));
183
184   memset (&dst, 0, sizeof (dst));
185   dst.sin_family = AF_INET;
186   dst.sin_addr = *other;
187   err = sendto(rawsock, 
188                packet, 
189                off, 0, 
190                (struct sockaddr*)&dst, 
191                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
192   if (err < 0) 
193     {
194       fprintf(stderr,
195               "sendto failed: %s\n", strerror(errno));
196     }
197   else if (err != off) 
198     {
199       fprintf(stderr,
200               "Error: partial send of ICMP message\n");
201     }
202 }
203
204
205 static int
206 make_raw_socket ()
207 {
208   const int one = 1;
209   int ret;
210
211   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
212   if (-1 == ret)
213     {
214       fprintf (stderr,
215                "Error opening RAW socket: %s\n",
216                strerror (errno));
217       return -1;
218     }  
219   if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
220                  (char *)&one, sizeof(one)) == -1)
221     fprintf(stderr,
222             "setsockopt failed: %s\n",
223             strerror (errno));
224   if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
225                  (char *)&one, sizeof(one)) == -1)
226     fprintf(stderr,
227             "setsockopt failed: %s\n",
228             strerror (errno));
229   return ret;
230 }
231
232
233 int
234 main (int argc, char *const *argv)
235 {
236   struct in_addr external;
237   uid_t uid;
238
239   if (-1 == (rawsock = make_raw_socket()))
240     return 1;     
241   uid = getuid ();
242   if (0 != setresuid (uid, uid, uid))
243     fprintf (stderr,
244              "Failed to setresuid: %s\n",
245              strerror (errno));
246   if (argc != 3)
247     {
248       fprintf (stderr,
249                "This program must be started with our IP and the targets external IP as arguments.\n");
250       return 1;
251     }
252   if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
253        (1 != inet_pton (AF_INET, argv[2], &target)) )
254     {
255       fprintf (stderr,
256                "Error parsing IPv4 address: %s\n",
257                strerror (errno));
258       return 1;
259     }
260   inet_pton (AF_INET, DUMMY_IP, &dummy);
261   send_icmp (&external,
262              &target);
263   close (rawsock);
264   return 0;
265 }
266
267 /* end of gnunet-nat-client.c */