-fix mx records postprocessing
[oweals/gnunet.git] / src / gns / gnunet-service-gns_interceptor.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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  *
23  * @file gns/gns_interceptor.c
24  * @brief GNUnet GNS interceptor logic
25  * @author Martin Schanzenbach
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet-service-gns_resolver.h"
33 #include "gns.h"
34
35 #define MAX_DNS_LABEL_LENGTH 63
36
37 /**
38  * Handle to a DNS intercepted
39  * reslution request
40  */
41 struct InterceptLookupHandle
42 {
43   /* the request handle to reply to */
44   struct GNUNET_DNS_RequestHandle *request_handle;
45   
46   /* the dns parser packet received */
47   struct GNUNET_DNSPARSER_Packet *packet;
48   
49   /* the query parsed from the packet */
50   struct GNUNET_DNSPARSER_Query *query;
51 };
52
53
54 /**
55  * Our handle to the DNS handler library
56  */
57 static struct GNUNET_DNS_Handle *dns_handle;
58
59 /**
60  * The root zone for this interceptor
61  */
62 static GNUNET_HashCode our_zone;
63
64 /**
65  * Our priv key
66  */
67 static struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
68
69 /**
70  * Reply to dns request with the result from our lookup.
71  *
72  * @param cls the closure to the request (an InterceptLookupHandle)
73  * @param rh the request handle of the lookup
74  * @param rd_count the number of records to return
75  * @param rd the record data
76  */
77 static void
78 reply_to_dns(void* cls, uint32_t rd_count,
79              const struct GNUNET_NAMESTORE_RecordData *rd)
80 {
81   int i;
82   size_t len;
83   int ret;
84   char *buf;
85   struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
86   struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
87   unsigned int num_answers = 0;
88   
89   
90   /**
91    * Put records in the DNS packet and modify it
92    * to a response
93    */
94   for (i=0; i < rd_count; i++)
95   {
96     if (rd[i].record_type == ilh->query->type)
97       num_answers++;
98   }
99
100   struct GNUNET_DNSPARSER_Record answer_records[num_answers];
101   struct GNUNET_DNSPARSER_Record additional_records[rd_count-(num_answers)];
102   packet->answers = answer_records;
103   packet->additional_records = additional_records;
104
105   for (i=0; i < rd_count; i++)
106   {
107     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
108                "Adding type %d to DNS response\n", rd[i].record_type);
109     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", ilh->query->name);
110     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
111     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
112     
113     if (rd[i].record_type == ilh->query->type)
114     {
115       answer_records[i].name = ilh->query->name;
116       answer_records[i].type = rd[i].record_type;
117       switch(rd[i].record_type)
118       {
119        case GNUNET_GNS_RECORD_TYPE_NS:
120        case GNUNET_GNS_RECORD_TYPE_CNAME:
121        case GNUNET_GNS_RECORD_TYPE_PTR:
122          answer_records[i].data.hostname = (char*)rd[i].data;
123          break;
124        case GNUNET_GNS_RECORD_TYPE_SOA:
125          answer_records[i].data.soa =
126            (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
127          break;
128        case GNUNET_GNS_RECORD_MX:
129          answer_records[i].data.mx =
130            (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
131          break;
132        default:
133         answer_records[i].data.raw.data_len = rd[i].data_size;
134         answer_records[i].data.raw.data = (char*)rd[i].data;
135       }
136       answer_records[i].expiration_time = rd[i].expiration;
137       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
138     }
139     else
140     {
141       additional_records[i].name = ilh->query->name;
142       additional_records[i].type = rd[i].record_type;
143       additional_records[i].data.raw.data_len = rd[i].data_size;
144       additional_records[i].data.raw.data = (char*)rd[i].data;
145       additional_records[i].expiration_time = rd[i].expiration;
146       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
147     }
148   }
149   
150   packet->num_answers = num_answers;
151   packet->num_additional_records = rd_count-(num_answers);
152   
153   packet->flags.authoritative_answer = 1;
154
155   if (rd == NULL)
156     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
157   else
158     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
159   
160   packet->flags.query_or_response = 1;
161
162   
163   /**
164    * Reply to DNS
165    */
166   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167              "Building DNS response\n");
168   ret = GNUNET_DNSPARSER_pack (packet,
169                                1024, /* FIXME magic from dns redirector */
170                                &buf,
171                                &len);
172   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
173              "Built DNS response! (ret=%d,len=%d)\n", ret, len);
174   if (ret == GNUNET_OK)
175   {
176     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
177                "Answering DNS request\n");
178     GNUNET_DNS_request_answer(ilh->request_handle,
179                               len,
180                               buf);
181
182     GNUNET_free(buf);
183     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
184   }
185   else
186   {
187     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
188                "Error building DNS response! (ret=%d)", ret);
189   }
190   
191   packet->num_answers = 0;
192   packet->answers = NULL;
193   packet->num_additional_records = 0;
194   packet->additional_records = NULL;
195   GNUNET_DNSPARSER_free_packet(packet);
196   //FIXME free resolver handle in resp functions in resolver!
197   //GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
198   //free_resolver_handle(rh);
199   GNUNET_free(ilh);
200 }
201
202
203 /**
204  * Entry point for name resolution
205  * Setup a new query and try to resolve
206  *
207  * @param request the request handle of the DNS request from a client
208  * @param p the DNS query packet we received
209  * @param q the DNS query we received parsed from p
210  */
211 static void
212 start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
213                           struct GNUNET_DNSPARSER_Packet *p,
214                           struct GNUNET_DNSPARSER_Query *q)
215 {
216   struct InterceptLookupHandle* ilh;
217   
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "Starting resolution for %s (type=%d)!\n",
220               q->name, q->type);
221   
222   ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
223   ilh->packet = p;
224   ilh->query = q;
225   ilh->request_handle = request;
226   
227   /* Start resolution in our zone */
228   gns_resolver_lookup_record(our_zone, q->type, q->name,
229                              our_key,
230                              &reply_to_dns, ilh);
231 }
232
233
234
235 /**
236  * The DNS request handler
237  * Called for every incoming DNS request.
238  *
239  * @param cls closure
240  * @param rh request handle to user for reply
241  * @param request_length number of bytes in request
242  * @param request udp payload of the DNS request
243  */
244 static void
245 handle_dns_request(void *cls,
246                    struct GNUNET_DNS_RequestHandle *rh,
247                    size_t request_length,
248                    const char *request)
249 {
250   struct GNUNET_DNSPARSER_Packet *p;
251   int i;
252   char *tldoffset;
253
254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
255   p = GNUNET_DNSPARSER_parse (request, request_length);
256   
257   if (NULL == p)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
260                 "Received malformed DNS packet, leaving it untouched\n");
261     GNUNET_DNS_request_forward (rh);
262     GNUNET_DNSPARSER_free_packet (p);
263     return;
264   }
265   
266   /**
267    * Check tld and decide if we or
268    * legacy dns is responsible
269    *
270    * FIXME now in theory there could be more than 1 query in the request
271    * but if this is case we get into trouble:
272    * either we query the GNS or the DNS. We cannot do both!
273    * So I suggest to either only allow a single query per request or
274    * only allow GNS or DNS requests.
275    * The way it is implemented here now is buggy and will lead to erratic
276    * behaviour (if multiple queries are present).
277    */
278   if (p->num_queries == 0)
279   {
280     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281                 "No Queries in DNS packet... forwarding\n");
282     GNUNET_DNS_request_forward (rh);
283     GNUNET_DNSPARSER_free_packet(p);
284     return;
285   }
286
287   if (p->num_queries > 1)
288   {
289     /* Note: We could also look for .gnunet */
290     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291                 ">1 queriy in DNS packet... odd. We only process #1\n");
292   }
293
294   
295   /**
296    * Check for .gnunet
297    */
298   tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
299   
300   for (i=0; i<strlen(p->queries[0].name); i++)
301   {
302     if (*(tldoffset-i) == '.')
303       break;
304   }
305
306   i--;
307   
308   if ((i==strlen(GNUNET_GNS_TLD)-1)
309       && (0 == strcmp(tldoffset-i, GNUNET_GNS_TLD)))
310   {
311     start_resolution_for_dns(rh, p, p->queries);
312   }
313   else
314   {
315     /**
316      * This request does not concern us. Forward to real DNS.
317      */
318     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
319                "Request for %s is forwarded to DNS\n", p->queries[0].name);
320     GNUNET_DNS_request_forward (rh);
321     GNUNET_DNSPARSER_free_packet (p);
322   }
323
324 }
325
326
327 /**
328  * Initialized the interceptor
329  *
330  * @param zone the zone to work in
331  * @param the prov key of the zone (can be null, needed for caching)
332  * @param c the configuration
333  * @return GNUNET_OK on success
334  */
335 int
336 gns_interceptor_init(GNUNET_HashCode zone,
337                      struct GNUNET_CRYPTO_RsaPrivateKey *key,
338                      const struct GNUNET_CONFIGURATION_Handle *c)
339 {
340   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
341              "DNS hijacking enabled... connecting to service.\n");
342
343   our_zone = zone;
344   our_key = key;
345   /**
346    * Do gnunet dns init here
347    */
348   dns_handle = GNUNET_DNS_connect(c,
349                                   GNUNET_DNS_FLAG_PRE_RESOLUTION,
350                                   &handle_dns_request, /* rh */
351                                   NULL); /* Closure */
352   if (NULL == dns_handle)
353   {
354     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
355              "Failed to connect to the dnsservice!\n");
356     return GNUNET_SYSERR;
357   }
358
359   return GNUNET_YES;
360 }
361
362 /* end of gns_interceptor.c */