aboutsummaryrefslogtreecommitdiff
path: root/src/service/dns/gnunet-zonewalk.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dns/gnunet-zonewalk.c')
-rw-r--r--src/service/dns/gnunet-zonewalk.c616
1 files changed, 616 insertions, 0 deletions
diff --git a/src/service/dns/gnunet-zonewalk.c b/src/service/dns/gnunet-zonewalk.c
new file mode 100644
index 000000000..f4b676d6c
--- /dev/null
+++ b/src/service/dns/gnunet-zonewalk.c
@@ -0,0 +1,616 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2018 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file src/dns/gnunet-zoneimport.c
23 * @brief import a DNS zone for analysis, brute force
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include <gnunet_util_lib.h>
28
29/**
30 * Request we should make.
31 */
32struct Request
33{
34 /**
35 * Requests are kept in a DLL.
36 */
37 struct Request *next;
38
39 /**
40 * Requests are kept in a DLL.
41 */
42 struct Request *prev;
43
44 /**
45 * Socket used to make the request, NULL if not active.
46 */
47 struct GNUNET_DNSSTUB_RequestSocket *rs;
48
49 /**
50 * Raw DNS query.
51 */
52 void *raw;
53
54 /**
55 * Number of bytes in @e raw.
56 */
57 size_t raw_len;
58
59 /**
60 * Hostname we are resolving.
61 */
62 char *hostname;
63
64 /**
65 * When did we last issue this request?
66 */
67 time_t time;
68
69 /**
70 * How often did we issue this query?
71 */
72 int issue_num;
73
74 /**
75 * random 16-bit DNS query identifier.
76 */
77 uint16_t id;
78};
79
80
81/**
82 * Context for DNS resolution.
83 */
84static struct GNUNET_DNSSTUB_Context *ctx;
85
86/**
87 * The number of queries that are outstanding
88 */
89static unsigned int pending;
90
91/**
92 * Number of lookups we performed overall.
93 */
94static unsigned int lookups;
95
96/**
97 * Number of lookups that failed.
98 */
99static unsigned int failures;
100
101/**
102 * Number of records we found.
103 */
104static unsigned int records;
105
106/**
107 * Head of DLL of all requests to perform.
108 */
109static struct Request *req_head;
110
111/**
112 * Tail of DLL of all requests to perform.
113 */
114static struct Request *req_tail;
115
116/**
117 * Main task.
118 */
119static struct GNUNET_SCHEDULER_Task *t;
120
121/**
122 * Maximum number of queries pending at the same time.
123 */
124#define THRESH 20
125
126/**
127 * TIME_THRESH is in usecs. How quickly do we submit fresh queries.
128 * Used as an additional throttle.
129 */
130#define TIME_THRESH 10
131
132/**
133 * How often do we retry a query before giving up for good?
134 */
135#define MAX_RETRIES 5
136
137
138/**
139 * We received @a rec for @a req. Remember the answer.
140 *
141 * @param req request
142 * @param rec response
143 */
144static void
145process_record (struct Request *req,
146 struct GNUNET_DNSPARSER_Record *rec)
147{
148 char buf[INET6_ADDRSTRLEN];
149
150 records++;
151 switch (rec->type)
152 {
153 case GNUNET_DNSPARSER_TYPE_A:
154 fprintf (stdout,
155 "%s A %s\n",
156 req->hostname,
157 inet_ntop (AF_INET,
158 rec->data.raw.data,
159 buf,
160 sizeof(buf)));
161 break;
162
163 case GNUNET_DNSPARSER_TYPE_AAAA:
164 fprintf (stdout,
165 "%s AAAA %s\n",
166 req->hostname,
167 inet_ntop (AF_INET6,
168 rec->data.raw.data,
169 buf,
170 sizeof(buf)));
171 break;
172
173 case GNUNET_DNSPARSER_TYPE_NS:
174 fprintf (stdout,
175 "%s NS %s\n",
176 req->hostname,
177 rec->data.hostname);
178 break;
179
180 case GNUNET_DNSPARSER_TYPE_CNAME:
181 fprintf (stdout,
182 "%s CNAME %s\n",
183 req->hostname,
184 rec->data.hostname);
185 break;
186
187 case GNUNET_DNSPARSER_TYPE_MX:
188 fprintf (stdout,
189 "%s MX %u %s\n",
190 req->hostname,
191 (unsigned int) rec->data.mx->preference,
192 rec->data.mx->mxhost);
193 break;
194
195 case GNUNET_DNSPARSER_TYPE_SOA:
196 fprintf (stdout,
197 "%s SOA %s %s %u %u %u %u %u\n",
198 req->hostname,
199 rec->data.soa->mname,
200 rec->data.soa->rname,
201 (unsigned int) rec->data.soa->serial,
202 (unsigned int) rec->data.soa->refresh,
203 (unsigned int) rec->data.soa->retry,
204 (unsigned int) rec->data.soa->expire,
205 (unsigned int) rec->data.soa->minimum_ttl);
206 break;
207
208 case GNUNET_DNSPARSER_TYPE_SRV:
209 fprintf (stdout,
210 "%s SRV %s %u %u %u\n",
211 req->hostname,
212 rec->data.srv->target,
213 rec->data.srv->priority,
214 rec->data.srv->weight,
215 rec->data.srv->port);
216 break;
217
218 case GNUNET_DNSPARSER_TYPE_URI:
219 fprintf (stdout,
220 "%s URI \"%s\" %u %u\n",
221 req->hostname,
222 rec->data.uri->target,
223 rec->data.uri->priority,
224 rec->data.uri->weight);
225 break;
226
227 case GNUNET_DNSPARSER_TYPE_PTR:
228 fprintf (stdout,
229 "%s PTR %s\n",
230 req->hostname,
231 rec->data.hostname);
232 break;
233
234 case GNUNET_DNSPARSER_TYPE_TXT:
235 fprintf (stdout,
236 "%s TXT %.*s\n",
237 req->hostname,
238 (int) rec->data.raw.data_len,
239 (char *) rec->data.raw.data);
240 break;
241
242 case GNUNET_DNSPARSER_TYPE_DNAME:
243 fprintf (stdout,
244 "%s DNAME %s\n",
245 req->hostname,
246 rec->data.hostname);
247 break;
248
249 /* obscure records */
250 case GNUNET_DNSPARSER_TYPE_AFSDB:
251 case GNUNET_DNSPARSER_TYPE_NAPTR:
252 case GNUNET_DNSPARSER_TYPE_APL:
253 case GNUNET_DNSPARSER_TYPE_DHCID:
254 case GNUNET_DNSPARSER_TYPE_HIP:
255 case GNUNET_DNSPARSER_TYPE_LOC:
256 case GNUNET_DNSPARSER_TYPE_RP:
257 case GNUNET_DNSPARSER_TYPE_TKEY:
258 case GNUNET_DNSPARSER_TYPE_TSIG:
259 case GNUNET_DNSPARSER_TYPE_TA:
260
261 /* DNSSEC */
262 case GNUNET_DNSPARSER_TYPE_DS:
263 case GNUNET_DNSPARSER_TYPE_RRSIG:
264 case GNUNET_DNSPARSER_TYPE_NSEC:
265 case GNUNET_DNSPARSER_TYPE_DNSKEY:
266 case GNUNET_DNSPARSER_TYPE_NSEC3:
267 case GNUNET_DNSPARSER_TYPE_NSEC3PARAM:
268 case GNUNET_DNSPARSER_TYPE_CDS:
269 case GNUNET_DNSPARSER_TYPE_CDNSKEY:
270
271 /* DNSSEC payload */
272 case GNUNET_DNSPARSER_TYPE_CERT:
273 case GNUNET_DNSPARSER_TYPE_SSHFP:
274 case GNUNET_DNSPARSER_TYPE_IPSECKEY:
275 case GNUNET_DNSPARSER_TYPE_TLSA:
276 case GNUNET_DNSPARSER_TYPE_SMIMEA:
277 case GNUNET_DNSPARSER_TYPE_OPENPGPKEY:
278
279 /* obsolete records */
280 case GNUNET_DNSPARSER_TYPE_SIG:
281 case GNUNET_DNSPARSER_TYPE_KEY:
282 case GNUNET_DNSPARSER_TYPE_KX:
283 {
284 char *base32;
285
286 base32 = GNUNET_STRINGS_data_to_string_alloc (rec->data.raw.data,
287 rec->data.raw.data_len);
288 fprintf (stdout,
289 "%s (%u) %s\n",
290 req->hostname,
291 rec->type,
292 base32);
293 GNUNET_free (base32);
294 }
295 break;
296
297 default:
298 fprintf (stderr,
299 "Unsupported type %u\n",
300 (unsigned int) rec->type);
301 break;
302 }
303}
304
305
306/**
307 * Function called with the result of a DNS resolution.
308 *
309 * @param cls closure with the `struct Request`
310 * @param dns dns response, never NULL
311 * @param dns_len number of bytes in @a dns
312 */
313static void
314process_result (void *cls,
315 const struct GNUNET_TUN_DnsHeader *dns,
316 size_t dns_len)
317{
318 struct Request *req = cls;
319 struct GNUNET_DNSPARSER_Packet *p;
320
321 if (NULL == dns)
322 {
323 /* stub gave up */
324 pending--;
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 "Stub gave up on DNS reply for `%s'\n",
327 req->hostname);
328 GNUNET_CONTAINER_DLL_remove (req_head,
329 req_tail,
330 req);
331 if (req->issue_num > MAX_RETRIES)
332 {
333 failures++;
334 GNUNET_free (req->hostname);
335 GNUNET_free (req->raw);
336 GNUNET_free (req);
337 return;
338 }
339 GNUNET_CONTAINER_DLL_insert_tail (req_head,
340 req_tail,
341 req);
342 req->rs = NULL;
343 return;
344 }
345 if (req->id != dns->id)
346 return;
347 pending--;
348 GNUNET_DNSSTUB_resolve_cancel (req->rs);
349 req->rs = NULL;
350 GNUNET_CONTAINER_DLL_remove (req_head,
351 req_tail,
352 req);
353 p = GNUNET_DNSPARSER_parse ((const char *) dns,
354 dns_len);
355 if (NULL == p)
356 {
357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358 "Failed to parse DNS reply for `%s'\n",
359 req->hostname);
360 if (req->issue_num > MAX_RETRIES)
361 {
362 failures++;
363 GNUNET_free (req->hostname);
364 GNUNET_free (req->raw);
365 GNUNET_free (req);
366 return;
367 }
368 GNUNET_CONTAINER_DLL_insert_tail (req_head,
369 req_tail,
370 req);
371 return;
372 }
373 for (unsigned int i = 0; i < p->num_answers; i++)
374 {
375 struct GNUNET_DNSPARSER_Record *rs = &p->answers[i];
376
377 process_record (req,
378 rs);
379 }
380 for (unsigned int i = 0; i < p->num_authority_records; i++)
381 {
382 struct GNUNET_DNSPARSER_Record *rs = &p->authority_records[i];
383
384 process_record (req,
385 rs);
386 }
387 for (unsigned int i = 0; i < p->num_additional_records; i++)
388 {
389 struct GNUNET_DNSPARSER_Record *rs = &p->additional_records[i];
390
391 process_record (req,
392 rs);
393 }
394 GNUNET_DNSPARSER_free_packet (p);
395 GNUNET_free (req->hostname);
396 GNUNET_free (req->raw);
397 GNUNET_free (req);
398}
399
400
401/**
402 * Submit a request to DNS unless we need to slow down because
403 * we are at the rate limit.
404 *
405 * @param req request to submit
406 * @return #GNUNET_OK if request was submitted
407 * #GNUNET_NO if request was already submitted
408 * #GNUNET_SYSERR if we are at the rate limit
409 */
410static int
411submit_req (struct Request *req)
412{
413 static struct timeval last_request;
414 struct timeval now;
415
416 if (NULL != req->rs)
417 return GNUNET_NO; /* already submitted */
418 gettimeofday (&now,
419 NULL);
420 if ((((now.tv_sec - last_request.tv_sec) == 0) &&
421 ((now.tv_usec - last_request.tv_usec) < TIME_THRESH)) ||
422 (pending >= THRESH))
423 return GNUNET_SYSERR;
424 GNUNET_assert (NULL == req->rs);
425 req->rs = GNUNET_DNSSTUB_resolve (ctx,
426 req->raw,
427 req->raw_len,
428 &process_result,
429 req);
430 GNUNET_assert (NULL != req->rs);
431 req->issue_num++;
432 last_request = now;
433 lookups++;
434 pending++;
435 req->time = time (NULL);
436 return GNUNET_OK;
437}
438
439
440/**
441 * Process as many requests as possible from the queue.
442 *
443 * @param cls NULL
444 */
445static void
446process_queue (void *cls)
447{
448 (void) cls;
449 t = NULL;
450 for (struct Request *req = req_head;
451 NULL != req;
452 req = req->next)
453 {
454 if (GNUNET_SYSERR == submit_req (req))
455 break;
456 }
457 if (NULL != req_head)
458 t = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
459 &process_queue,
460 NULL);
461 else
462 GNUNET_SCHEDULER_shutdown ();
463}
464
465
466/**
467 * Clean up and terminate the process.
468 *
469 * @param cls NULL
470 */
471static void
472do_shutdown (void *cls)
473{
474 (void) cls;
475 if (NULL != t)
476 {
477 GNUNET_SCHEDULER_cancel (t);
478 t = NULL;
479 }
480 GNUNET_DNSSTUB_stop (ctx);
481 ctx = NULL;
482}
483
484
485/**
486 * Process requests from the queue, then if the queue is
487 * not empty, try again.
488 *
489 * @param cls NULL
490 */
491static void
492run (void *cls)
493{
494 (void) cls;
495
496 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
497 NULL);
498 t = GNUNET_SCHEDULER_add_now (&process_queue,
499 NULL);
500}
501
502
503/**
504 * Add @a hostname to the list of requests to be made.
505 *
506 * @param hostname name to resolve
507 */
508static void
509queue (const char *hostname)
510{
511 struct GNUNET_DNSPARSER_Packet p;
512 struct GNUNET_DNSPARSER_Query q;
513 struct Request *req;
514 char *raw;
515 size_t raw_size;
516 int ret;
517
518 if (GNUNET_OK !=
519 GNUNET_DNSPARSER_check_name (hostname))
520 {
521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
522 "Refusing invalid hostname `%s'\n",
523 hostname);
524 return;
525 }
526 q.name = (char *) hostname;
527 q.type = GNUNET_DNSPARSER_TYPE_NS;
528 q.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
529
530 memset (&p,
531 0,
532 sizeof(p));
533 p.num_queries = 1;
534 p.queries = &q;
535 p.id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
536 UINT16_MAX);
537 ret = GNUNET_DNSPARSER_pack (&p,
538 UINT16_MAX,
539 &raw,
540 &raw_size);
541 if (GNUNET_OK != ret)
542 {
543 if (GNUNET_NO == ret)
544 GNUNET_free (raw);
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 "Failed to pack query for hostname `%s'\n",
547 hostname);
548 return;
549 }
550
551 req = GNUNET_new (struct Request);
552 req->hostname = strdup (hostname);
553 req->raw = raw;
554 req->raw_len = raw_size;
555 req->id = p.id;
556 GNUNET_CONTAINER_DLL_insert_tail (req_head,
557 req_tail,
558 req);
559}
560
561
562/**
563 * Call with IP address of resolver to query.
564 *
565 * @param argc should be 2
566 * @param argv[1] should contain IP address
567 * @return 0 on success
568 */
569int
570main (int argc,
571 char **argv)
572{
573 char hn[256];
574
575 if (2 != argc)
576 {
577 fprintf (stderr,
578 "Missing required configuration argument\n");
579 return -1;
580 }
581 ctx = GNUNET_DNSSTUB_start (256);
582 if (NULL == ctx)
583 {
584 fprintf (stderr,
585 "Failed to initialize GNUnet DNS STUB\n");
586 return 1;
587 }
588 if (GNUNET_OK !=
589 GNUNET_DNSSTUB_add_dns_ip (ctx,
590 argv[1]))
591 {
592 fprintf (stderr,
593 "Failed to use `%s' for DNS resolver\n",
594 argv[1]);
595 return 1;
596 }
597
598 while (NULL !=
599 fgets (hn,
600 sizeof(hn),
601 stdin))
602 {
603 if (strlen (hn) > 0)
604 hn[strlen (hn) - 1] = '\0'; /* eat newline */
605 queue (hn);
606 }
607 GNUNET_SCHEDULER_run (&run,
608 NULL);
609 fprintf (stderr,
610 "Did %u lookups, found %u records, %u lookups failed, %u pending on shutdown\n",
611 lookups,
612 records,
613 failures,
614 pending);
615 return 0;
616}