-fix check for DANE and ftbfs
[oweals/gnunet.git] / src / gns / gnunet-service-gns_interceptor.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009-2013 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  * @file gns/gnunet-service-gns_interceptor.c
22  * @brief GNUnet GNS interceptor logic
23  * @author Martin Schanzenbach
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet-service-gns_resolver.h"
31 #include "gnunet-service-gns_interceptor.h"
32 #include "gns.h"
33
34
35 /**
36  * Handle to a DNS intercepted
37  * reslution request
38  */
39 struct InterceptLookupHandle
40 {
41
42   /**
43    * We keep these in a DLL.
44    */
45   struct InterceptLookupHandle *next;
46
47   /**
48    * We keep these in a DLL.
49    */
50   struct InterceptLookupHandle *prev;
51
52   /**
53    * the request handle to reply to
54    */
55   struct GNUNET_DNS_RequestHandle *request_handle;
56
57   /**
58    * the dns parser packet received
59    */
60   struct GNUNET_DNSPARSER_Packet *packet;
61
62   /**
63    * Handle for the lookup operation.
64    */
65   struct GNS_ResolverHandle *lookup;
66
67 };
68
69
70 /**
71  * Our handle to the DNS handler library
72  */
73 static struct GNUNET_DNS_Handle *dns_handle;
74
75 /**
76  * Key of the zone we start lookups in.
77  */
78 static struct GNUNET_CRYPTO_EcdsaPublicKey zone;
79
80 /**
81  * Head of the DLL.
82  */
83 static struct InterceptLookupHandle *ilh_head;
84
85 /**
86  * Tail of the DLL.
87  */
88 static struct InterceptLookupHandle *ilh_tail;
89
90
91 /**
92  * Reply to dns request with the result from our lookup.
93  *
94  * @param cls the closure to the request (an InterceptLookupHandle)
95  * @param rd_count the number of records to return
96  * @param rd the record data
97  */
98 static void
99 reply_to_dns (void *cls, uint32_t rd_count,
100               const struct GNUNET_GNSRECORD_Data *rd)
101 {
102   struct InterceptLookupHandle *ilh = cls;
103   struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
104   struct GNUNET_DNSPARSER_Query *query = &packet->queries[0];
105   uint32_t i;
106   size_t len;
107   int ret;
108   char *buf;
109   unsigned int num_answers;
110   unsigned int skip_answers;
111   unsigned int skip_additional;
112   size_t off;
113
114   /* Put records in the DNS packet */
115   num_answers = 0;
116   for (i=0; i < rd_count; i++)
117     if (rd[i].record_type == query->type)
118       num_answers++;
119   skip_answers = 0;
120   skip_additional = 0;
121
122   {
123     struct GNUNET_DNSPARSER_Record answer_records[num_answers];
124     struct GNUNET_DNSPARSER_Record additional_records[rd_count - num_answers];
125
126     packet->answers = answer_records;
127     packet->additional_records = additional_records;
128     /* FIXME: need to handle #GNUNET_GNSRECORD_RF_SHADOW_RECORD option
129        (by ignoring records where this flag is set if there is any
130        other record of that type in the result set) */
131     for (i=0; i < rd_count; i++)
132     {
133       if (rd[i].record_type == query->type)
134       {
135         answer_records[i - skip_answers].name = query->name;
136         answer_records[i - skip_answers].type = rd[i].record_type;
137         switch(rd[i].record_type)
138         {
139         case GNUNET_DNSPARSER_TYPE_NS:
140         case GNUNET_DNSPARSER_TYPE_CNAME:
141         case GNUNET_DNSPARSER_TYPE_PTR:
142           answer_records[i - skip_answers].data.hostname
143             = GNUNET_DNSPARSER_parse_name (rd[i].data,
144                                            rd[i].data_size,
145                                            &off);
146           if ( (off != rd[i].data_size) ||
147                (NULL == answer_records[i].data.hostname) )
148           {
149             GNUNET_break_op (0);
150             skip_answers++;
151           }
152           break;
153         case GNUNET_DNSPARSER_TYPE_SOA:
154           answer_records[i - skip_answers].data.soa
155             = GNUNET_DNSPARSER_parse_soa (rd[i].data,
156                                           rd[i].data_size,
157                                           &off);
158           if ( (off != rd[i].data_size) ||
159                (NULL == answer_records[i].data.soa) )
160           {
161             GNUNET_break_op (0);
162             skip_answers++;
163           }
164           break;
165         case GNUNET_DNSPARSER_TYPE_SRV:
166           /* FIXME: SRV is not yet supported */
167           skip_answers++;
168           break;
169         case GNUNET_DNSPARSER_TYPE_MX:
170           answer_records[i - skip_answers].data.mx
171             = GNUNET_DNSPARSER_parse_mx (rd[i].data,
172                                          rd[i].data_size,
173                                          &off);
174           if ( (off != rd[i].data_size) ||
175                (NULL == answer_records[i].data.hostname) )
176           {
177             GNUNET_break_op (0);
178             skip_answers++;
179           }
180           break;
181         default:
182           answer_records[i - skip_answers].data.raw.data_len = rd[i].data_size;
183           answer_records[i - skip_answers].data.raw.data = (char*)rd[i].data;
184           break;
185         }
186         GNUNET_break (0 == (rd[i - skip_answers].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION));
187         answer_records[i - skip_answers].expiration_time.abs_value_us = rd[i].expiration_time;
188         answer_records[i - skip_answers].dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
189       }
190       else
191       {
192         additional_records[i - skip_additional].name = query->name;
193         additional_records[i - skip_additional].type = rd[i].record_type;
194         switch(rd[i].record_type)
195         {
196         case GNUNET_DNSPARSER_TYPE_NS:
197         case GNUNET_DNSPARSER_TYPE_CNAME:
198         case GNUNET_DNSPARSER_TYPE_PTR:
199           additional_records[i - skip_additional].data.hostname
200             = GNUNET_DNSPARSER_parse_name (rd[i].data,
201                                            rd[i].data_size,
202                                            &off);
203           if ( (off != rd[i].data_size) ||
204                (NULL == additional_records[i].data.hostname) )
205           {
206             GNUNET_break_op (0);
207             skip_additional++;
208           }
209           break;
210         case GNUNET_DNSPARSER_TYPE_SOA:
211           additional_records[i - skip_additional].data.soa
212             = GNUNET_DNSPARSER_parse_soa (rd[i].data,
213                                           rd[i].data_size,
214                                           &off);
215           if ( (off != rd[i].data_size) ||
216                (NULL == additional_records[i].data.hostname) )
217           {
218             GNUNET_break_op (0);
219             skip_additional++;
220           }
221           break;
222         case GNUNET_DNSPARSER_TYPE_MX:
223           additional_records[i - skip_additional].data.mx
224             = GNUNET_DNSPARSER_parse_mx (rd[i].data,
225                                          rd[i].data_size,
226                                          &off);
227           if ( (off != rd[i].data_size) ||
228                (NULL == additional_records[i].data.hostname) )
229           {
230             GNUNET_break_op (0);
231             skip_additional++;
232           }
233           break;
234         case GNUNET_DNSPARSER_TYPE_SRV:
235           /* FIXME: SRV is not yet supported */
236           skip_answers++;
237           break;
238         default:
239           additional_records[i - skip_additional].data.raw.data_len = rd[i].data_size;
240           additional_records[i - skip_additional].data.raw.data = (char*)rd[i].data;
241           break;
242         }
243         GNUNET_break (0 == (rd[i - skip_additional].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION));
244         additional_records[i - skip_additional].expiration_time.abs_value_us = rd[i].expiration_time;
245         additional_records[i - skip_additional].dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
246       }
247     }
248     packet->num_answers = num_answers - skip_answers;
249     packet->num_additional_records = rd_count - num_answers - skip_additional;
250     packet->flags.authoritative_answer = 1;
251     if (NULL == rd)
252       packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NAME_ERROR;
253     else
254       packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
255     packet->flags.query_or_response = 1;
256     ret = GNUNET_DNSPARSER_pack (packet,
257                                  1024, /* maximum allowed size for DNS reply */
258                                  &buf,
259                                  &len);
260     if (GNUNET_OK != ret)
261     {
262       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
263                   _("Error converting GNS response to DNS response!\n"));
264     }
265     else
266     {
267       GNUNET_DNS_request_answer (ilh->request_handle,
268                                  len,
269                                  buf);
270       GNUNET_free (buf);
271     }
272     packet->num_answers = 0;
273     packet->answers = NULL;
274     packet->num_additional_records = 0;
275     packet->additional_records = NULL;
276     GNUNET_DNSPARSER_free_packet (packet);
277   }
278   GNUNET_CONTAINER_DLL_remove (ilh_head, ilh_tail, ilh);
279   GNUNET_free (ilh);
280 }
281
282
283 /**
284  * The DNS request handler.  Called for every incoming DNS request.
285  *
286  * @param cls closure, unused
287  * @param rh request handle to user for reply
288  * @param request_length number of bytes in @a request
289  * @param request UDP payload of the DNS request
290  */
291 static void
292 handle_dns_request (void *cls,
293                     struct GNUNET_DNS_RequestHandle *rh,
294                     size_t request_length,
295                     const char *request)
296 {
297   struct GNUNET_DNSPARSER_Packet *p;
298   struct InterceptLookupHandle *ilh;
299
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301               "Hijacked a DNS request. Processing.\n");
302   if (NULL == (p = GNUNET_DNSPARSER_parse (request, request_length)))
303   {
304     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305                 "Received malformed DNS packet, leaving it untouched.\n");
306     GNUNET_DNS_request_forward (rh);
307     GNUNET_DNSPARSER_free_packet (p);
308     return;
309   }
310
311   /* Check TLD and decide if we or legacy dns is responsible */
312   if (1 != p->num_queries)
313   {
314     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315                 "Not exactly one query in DNS packet. Forwarding untouched.\n");
316     GNUNET_DNS_request_forward (rh);
317     GNUNET_DNSPARSER_free_packet(p);
318     return;
319   }
320
321   /* Check for GNS TLDs. */
322   if ( (GNUNET_YES == is_gnu_tld (p->queries[0].name)) ||
323        (GNUNET_YES == is_zkey_tld (p->queries[0].name)) ||
324        (0 == strcmp (p->queries[0].name, GNUNET_GNS_TLD)) )
325   {
326     /* Start resolution in GNS */
327     ilh = GNUNET_new (struct InterceptLookupHandle);
328     GNUNET_CONTAINER_DLL_insert (ilh_head, ilh_tail, ilh);
329     ilh->packet = p;
330     ilh->request_handle = rh;
331     ilh->lookup = GNS_resolver_lookup (&zone,
332                                        p->queries[0].type,
333                                        p->queries[0].name,
334                                        NULL /* FIXME: enable shorten for DNS intercepts? */,
335                                        GNUNET_NO,
336                                        &reply_to_dns, ilh);
337     return;
338   }
339   /* This request does not concern us. Forward to real DNS. */
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341               "Request for `%s' is forwarded to DNS untouched.\n",
342               p->queries[0].name);
343   GNUNET_DNS_request_forward (rh);
344   GNUNET_DNSPARSER_free_packet (p);
345 }
346
347
348 /**
349  * Initialized the interceptor
350  *
351  * @param gnu_zone the zone to work in
352  * @param c the configuration
353  * @return #GNUNET_OK on success
354  */
355 int
356 GNS_interceptor_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *gnu_zone,
357                       const struct GNUNET_CONFIGURATION_Handle *c)
358 {
359   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
360               "DNS hijacking enabled. Connecting to DNS service.\n");
361   zone = *gnu_zone;
362   dns_handle = GNUNET_DNS_connect (c,
363                                    GNUNET_DNS_FLAG_PRE_RESOLUTION,
364                                    &handle_dns_request,
365                                    NULL);
366   if (NULL == dns_handle)
367   {
368     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369                 _("Failed to connect to the DNS service!\n"));
370     return GNUNET_SYSERR;
371   }
372   return GNUNET_YES;
373 }
374
375
376 /**
377  * Disconnect from interceptor
378  */
379 void
380 GNS_interceptor_done ()
381 {
382   struct InterceptLookupHandle *ilh;
383
384   while (NULL != (ilh = ilh_head))
385   {
386     GNUNET_CONTAINER_DLL_remove (ilh_head, ilh_tail, ilh);
387     GNS_resolver_lookup_cancel (ilh->lookup);
388     GNUNET_DNS_request_drop (ilh->request_handle);
389     GNUNET_DNSPARSER_free_packet (ilh->packet);
390     GNUNET_free (ilh);
391   }
392   if (NULL != dns_handle)
393   {
394     GNUNET_DNS_disconnect (dns_handle);
395     dns_handle = NULL;
396   }
397 }
398
399 /* end of gnunet-service-gns_interceptor.c */