loader: Process dependencies for built-in modules.
[asterisk/asterisk.git] / main / dns_core.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Core DNS Functionality
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/_private.h"
33 #include "asterisk/linkedlists.h"
34 #include "asterisk/astobj2.h"
35 #include "asterisk/strings.h"
36 #include "asterisk/sched.h"
37 #include "asterisk/dns_core.h"
38 #include "asterisk/dns_srv.h"
39 #include "asterisk/dns_tlsa.h"
40 #include "asterisk/dns_recurring.h"
41 #include "asterisk/dns_resolver.h"
42 #include "asterisk/dns_internal.h"
43
44 #include <netinet/in.h>
45 #include <arpa/nameser.h>
46
47 AST_RWLIST_HEAD_STATIC(resolvers, ast_dns_resolver);
48
49 static struct ast_sched_context *sched;
50
51 struct ast_sched_context *ast_dns_get_sched(void)
52 {
53         return sched;
54 }
55
56 const char *ast_dns_query_get_name(const struct ast_dns_query *query)
57 {
58         return query->name;
59 }
60
61 int ast_dns_query_get_rr_type(const struct ast_dns_query *query)
62 {
63         return query->rr_type;
64 }
65
66 int ast_dns_query_get_rr_class(const struct ast_dns_query *query)
67 {
68         return query->rr_class;
69 }
70
71 void *ast_dns_query_get_data(const struct ast_dns_query *query)
72 {
73         return query->user_data;
74 }
75
76 struct ast_dns_result *ast_dns_query_get_result(const struct ast_dns_query *query)
77 {
78         return query->result;
79 }
80
81 unsigned int ast_dns_result_get_secure(const struct ast_dns_result *result)
82 {
83         return result->secure;
84 }
85
86 unsigned int ast_dns_result_get_bogus(const struct ast_dns_result *result)
87 {
88         return result->bogus;
89 }
90
91 unsigned int ast_dns_result_get_rcode(const struct ast_dns_result *result)
92 {
93         return result->rcode;
94 }
95
96 const char *ast_dns_result_get_canonical(const struct ast_dns_result *result)
97 {
98         return result->canonical;
99 }
100
101 const struct ast_dns_record *ast_dns_result_get_records(const struct ast_dns_result *result)
102 {
103         return AST_LIST_FIRST(&result->records);
104 }
105
106 const char *ast_dns_result_get_answer(const struct ast_dns_result *result)
107 {
108         return result->answer;
109 }
110
111 int ast_dns_result_get_lowest_ttl(const struct ast_dns_result *result)
112 {
113         int ttl = 0;
114         const struct ast_dns_record *record;
115
116         if (ast_dns_result_get_rcode(result) == NXDOMAIN) {
117                 return 0;
118         }
119
120         for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) {
121                 if (!ttl || (ast_dns_record_get_ttl(record) && (ast_dns_record_get_ttl(record) < ttl))) {
122                         ttl = ast_dns_record_get_ttl(record);
123                 }
124         }
125
126         return ttl;
127 }
128
129 void ast_dns_result_free(struct ast_dns_result *result)
130 {
131         struct ast_dns_record *record;
132
133         if (!result) {
134                 return;
135         }
136
137         while ((record = AST_LIST_REMOVE_HEAD(&result->records, list))) {
138                 ast_free(record);
139         }
140
141         ast_free(result);
142 }
143
144 int ast_dns_record_get_rr_type(const struct ast_dns_record *record)
145 {
146         return record->rr_type;
147 }
148
149 int ast_dns_record_get_rr_class(const struct ast_dns_record *record)
150 {
151         return record->rr_class;
152 }
153
154 int ast_dns_record_get_ttl(const struct ast_dns_record *record)
155 {
156         return record->ttl;
157 }
158
159 const char *ast_dns_record_get_data(const struct ast_dns_record *record)
160 {
161         return record->data_ptr;
162 }
163
164 size_t ast_dns_record_get_data_size(const struct ast_dns_record *record)
165 {
166         return record->data_len;
167 }
168
169 const struct ast_dns_record *ast_dns_record_get_next(const struct ast_dns_record *record)
170 {
171         return AST_LIST_NEXT(record, list);
172 }
173
174 /*! \brief Destructor for an active DNS query */
175 static void dns_query_active_destroy(void *data)
176 {
177         struct ast_dns_query_active *active = data;
178
179         ao2_cleanup(active->query);
180 }
181
182 /*! \brief \brief Destructor for a DNS query */
183 static void dns_query_destroy(void *data)
184 {
185         struct ast_dns_query *query = data;
186
187         ao2_cleanup(query->user_data);
188         ao2_cleanup(query->resolver_data);
189         ast_dns_result_free(query->result);
190 }
191
192 struct ast_dns_query *dns_query_alloc(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
193 {
194         struct ast_dns_query *query;
195
196         if (ast_strlen_zero(name)) {
197                 ast_log(LOG_WARNING, "Could not perform asynchronous resolution, no name provided\n");
198                 return NULL;
199         } else if (rr_type > 65536) {
200                 ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', resource record type '%d' exceeds maximum\n",
201                         name, rr_type);
202                 return NULL;
203         } else if (rr_type < 0) {
204                 ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', invalid resource record type '%d'\n",
205                         name, rr_type);
206                 return NULL;
207         } else if (rr_class > 65536) {
208                 ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', resource record class '%d' exceeds maximum\n",
209                         name, rr_class);
210                 return NULL;
211         } else if (rr_class < 0) {
212                 ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', invalid resource class '%d'\n",
213                         name, rr_class);
214                 return NULL;
215         } else if (!callback) {
216                 ast_log(LOG_WARNING, "Could not perform asynchronous resolution of '%s', no callback provided\n",
217                         name);
218                 return NULL;
219         }
220
221         query = ao2_alloc_options(sizeof(*query) + strlen(name) + 1, dns_query_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
222         if (!query) {
223                 return NULL;
224         }
225
226         query->callback = callback;
227         query->user_data = ao2_bump(data);
228         query->rr_type = rr_type;
229         query->rr_class = rr_class;
230         strcpy(query->name, name); /* SAFE */
231
232         AST_RWLIST_RDLOCK(&resolvers);
233         query->resolver = AST_RWLIST_FIRST(&resolvers);
234         AST_RWLIST_UNLOCK(&resolvers);
235
236         if (!query->resolver) {
237                 ast_log(LOG_ERROR, "Attempted to do a DNS query for '%s' of class '%d' and type '%d' but no resolver is available\n",
238                         name, rr_class, rr_type);
239                 ao2_ref(query, -1);
240                 return NULL;
241         }
242
243         return query;
244 }
245
246 struct ast_dns_query_active *ast_dns_resolve_async(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
247 {
248         struct ast_dns_query_active *active;
249
250         active = ao2_alloc_options(sizeof(*active), dns_query_active_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
251         if (!active) {
252                 return NULL;
253         }
254
255         active->query = dns_query_alloc(name, rr_type, rr_class, callback, data);
256         if (!active->query) {
257                 ao2_ref(active, -1);
258                 return NULL;
259         }
260
261         if (active->query->resolver->resolve(active->query)) {
262                 ast_log(LOG_ERROR, "Resolver '%s' returned an error when resolving '%s' of class '%d' and type '%d'\n",
263                         active->query->resolver->name, name, rr_class, rr_type);
264                 ao2_ref(active, -1);
265                 return NULL;
266         }
267
268         return active;
269 }
270
271 int ast_dns_resolve_cancel(struct ast_dns_query_active *active)
272 {
273         return active->query->resolver->cancel(active->query);
274 }
275
276 /*! \brief Structure used for signaling back for synchronous resolution completion */
277 struct dns_synchronous_resolve {
278         /*! \brief Lock used for signaling */
279         ast_mutex_t lock;
280         /*! \brief Condition used for signaling */
281         ast_cond_t cond;
282         /*! \brief Whether the query has completed */
283         unsigned int completed;
284         /*! \brief The result from the query */
285         struct ast_dns_result *result;
286 };
287
288 /*! \brief Destructor for synchronous resolution structure */
289 static void dns_synchronous_resolve_destroy(void *data)
290 {
291         struct dns_synchronous_resolve *synchronous = data;
292
293         ast_mutex_destroy(&synchronous->lock);
294         ast_cond_destroy(&synchronous->cond);
295
296         /* This purposely does not unref result as it has been passed to the caller */
297 }
298
299 /*! \brief Callback used to implement synchronous resolution */
300 static void dns_synchronous_resolve_callback(const struct ast_dns_query *query)
301 {
302         struct dns_synchronous_resolve *synchronous = ast_dns_query_get_data(query);
303
304         synchronous->result = query->result;
305         ((struct ast_dns_query *)query)->result = NULL;
306
307         ast_mutex_lock(&synchronous->lock);
308         synchronous->completed = 1;
309         ast_cond_signal(&synchronous->cond);
310         ast_mutex_unlock(&synchronous->lock);
311 }
312
313 int ast_dns_resolve(const char *name, int rr_type, int rr_class, struct ast_dns_result **result)
314 {
315         struct dns_synchronous_resolve *synchronous;
316         struct ast_dns_query_active *active;
317
318         if (ast_strlen_zero(name)) {
319                 ast_log(LOG_WARNING, "Could not perform synchronous resolution, no name provided\n");
320                 return -1;
321         } else if (rr_type > 65536) {
322                 ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', resource record type '%d' exceeds maximum\n",
323                         name, rr_type);
324                 return -1;
325         } else if (rr_type < 0) {
326                 ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', invalid resource record type '%d'\n",
327                         name, rr_type);
328                 return -1;
329         } else if (rr_class > 65536) {
330                 ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', resource record class '%d' exceeds maximum\n",
331                         name, rr_class);
332                 return -1;
333         } else if (rr_class < 0) {
334                 ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', invalid resource class '%d'\n",
335                         name, rr_class);
336                 return -1;
337         } else if (!result) {
338                 ast_log(LOG_WARNING, "Could not perform synchronous resolution of '%s', no result pointer provided for storing results\n",
339                         name);
340                 return -1;
341         }
342
343         synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
344         if (!synchronous) {
345                 return -1;
346         }
347
348         ast_mutex_init(&synchronous->lock);
349         ast_cond_init(&synchronous->cond, NULL);
350
351         active = ast_dns_resolve_async(name, rr_type, rr_class, dns_synchronous_resolve_callback, synchronous);
352         if (active) {
353                 /* Wait for resolution to complete */
354                 ast_mutex_lock(&synchronous->lock);
355                 while (!synchronous->completed) {
356                         ast_cond_wait(&synchronous->cond, &synchronous->lock);
357                 }
358                 ast_mutex_unlock(&synchronous->lock);
359                 ao2_ref(active, -1);
360         }
361
362         *result = synchronous->result;
363         ao2_ref(synchronous, -1);
364
365         return *result ? 0 : -1;
366 }
367
368 int ast_dns_resolver_set_data(struct ast_dns_query *query, void *data)
369 {
370         if (query->resolver_data) {
371                 return -1;
372         }
373
374         query->resolver_data = ao2_bump(data);
375
376         return 0;
377 }
378
379 void *ast_dns_resolver_get_data(const struct ast_dns_query *query)
380 {
381         return query->resolver_data;
382 }
383
384 int ast_dns_resolver_set_result(struct ast_dns_query *query, unsigned int secure, unsigned int bogus,
385         unsigned int rcode, const char *canonical, const char *answer, size_t answer_size)
386 {
387         char *buf_ptr;
388
389         if (secure && bogus) {
390                 ast_debug(2, "Query '%p': Could not set result information, it can not be both secure and bogus\n",
391                         query);
392                 return -1;
393         }
394
395         if (ast_strlen_zero(canonical)) {
396                 ast_debug(2, "Query '%p': Could not set result information since no canonical name was provided\n",
397                         query);
398                 return -1;
399         }
400
401         if (!answer) {
402                 answer = "";
403                 answer_size = 0;
404                 ast_debug(2, "Query '%p': Assuming zero-sized answer on NULL input\n", query);
405         }
406
407         ast_dns_result_free(query->result);
408
409         query->result = ast_calloc(1, sizeof(*query->result) + strlen(canonical) + 1 + answer_size);
410         if (!query->result) {
411                 return -1;
412         }
413
414         query->result->secure = secure;
415         query->result->bogus = bogus;
416         query->result->rcode = rcode;
417
418         buf_ptr = query->result->buf;
419         strcpy(buf_ptr, canonical); /* SAFE */
420         query->result->canonical = buf_ptr;
421
422         buf_ptr += strlen(canonical) + 1;
423         memcpy(buf_ptr, answer, answer_size); /* SAFE */
424         query->result->answer = buf_ptr;
425         query->result->answer_size = answer_size;
426
427         return 0;
428 }
429
430 static struct ast_dns_record *generic_record_alloc(struct ast_dns_query *query, const char *data, const size_t size)
431 {
432         struct ast_dns_record *record;
433
434         record = ast_calloc(1, sizeof(*record) + size);
435         if (!record) {
436                 return NULL;
437         }
438
439         record->data_ptr = record->data;
440
441         return record;
442 }
443
444 typedef struct ast_dns_record *(*dns_alloc_fn)(struct ast_dns_query *query, const char *data, const size_t size);
445
446 static dns_alloc_fn dns_alloc_table [] = {
447         [T_NAPTR] = dns_naptr_alloc,
448         [T_SRV] = dns_srv_alloc,
449 };
450
451 static struct ast_dns_record *allocate_dns_record(unsigned int rr_type, struct ast_dns_query *query, const char *data, const size_t size)
452 {
453         dns_alloc_fn allocator = generic_record_alloc;
454
455         if (rr_type < ARRAY_LEN(dns_alloc_table) && dns_alloc_table[rr_type]) {
456                 allocator = dns_alloc_table[rr_type];
457         }
458
459         return allocator(query, data, size);
460 }
461
462 int ast_dns_resolver_add_record(struct ast_dns_query *query, int rr_type, int rr_class, int ttl, const char *data, const size_t size)
463 {
464         struct ast_dns_record *record;
465
466         if (rr_type < 0) {
467                 ast_debug(2, "Query '%p': Could not add record, invalid resource record type '%d'\n",
468                         query, rr_type);
469                 return -1;
470         } else if (rr_type > 65536) {
471                 ast_debug(2, "Query '%p': Could not add record, resource record type '%d' exceeds maximum\n",
472                         query, rr_type);
473                 return -1;
474         } else if (rr_class < 0) {
475                 ast_debug(2, "Query '%p': Could not add record, invalid resource record class '%d'\n",
476                         query, rr_class);
477                 return -1;
478         } else if (rr_class > 65536) {
479                 ast_debug(2, "Query '%p': Could not add record, resource record class '%d' exceeds maximum\n",
480                         query, rr_class);
481                 return -1;
482         } else if (ttl < 0) {
483                 ast_debug(2, "Query '%p': Could not add record, invalid TTL '%d'\n",
484                         query, ttl);
485                 return -1;
486         } else if (!data || !size) {
487                 ast_debug(2, "Query '%p': Could not add record, no data specified\n",
488                         query);
489                 return -1;
490         } else if (!query->result) {
491                 ast_debug(2, "Query '%p': No result was set on the query, thus records can not be added\n",
492                         query);
493                 return -1;
494         }
495
496         record = allocate_dns_record(rr_type, query, data, size);
497         if (!record) {
498                 return -1;
499         }
500
501         record->rr_type = rr_type;
502         record->rr_class = rr_class;
503         record->ttl = ttl;
504         record->data_len = size;
505         memcpy(record->data_ptr, data, size);
506
507         AST_LIST_INSERT_TAIL(&query->result->records, record, list);
508
509         return 0;
510 }
511
512 typedef void (*dns_sort_fn)(struct ast_dns_result *result);
513
514 static dns_sort_fn dns_sort_table [] = {
515         [T_NAPTR] = dns_naptr_sort,
516         [T_SRV] = dns_srv_sort,
517 };
518
519 static void sort_result(int rr_type, struct ast_dns_result *result)
520 {
521         if (dns_sort_table[rr_type]) {
522                 dns_sort_table[rr_type](result);
523         }
524 }
525
526 void ast_dns_resolver_completed(struct ast_dns_query *query)
527 {
528         sort_result(ast_dns_query_get_rr_type(query), query->result);
529
530         query->callback(query);
531 }
532
533 static void dns_shutdown(void)
534 {
535         if (sched) {
536                 ast_sched_context_destroy(sched);
537                 sched = NULL;
538         }
539 }
540
541 int dns_core_init(void)
542 {
543         sched = ast_sched_context_create();
544         if (!sched) {
545                 return -1;
546         }
547
548         if (ast_sched_start_thread(sched)) {
549                 return -1;
550         }
551
552         ast_register_cleanup(dns_shutdown);
553
554         return 0;
555 }
556
557 int ast_dns_resolver_register(struct ast_dns_resolver *resolver)
558 {
559         struct ast_dns_resolver *iter;
560         int inserted = 0;
561
562         if (!resolver) {
563                 return -1;
564         } else if (ast_strlen_zero(resolver->name)) {
565                 ast_log(LOG_ERROR, "Registration of DNS resolver failed as it does not have a name\n");
566                 return -1;
567         } else if (!resolver->resolve) {
568                 ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the resolve callback which is required\n",
569                         resolver->name);
570                 return -1;
571         } else if (!resolver->cancel) {
572                 ast_log(LOG_ERROR, "DNS resolver '%s' does not implement the cancel callback which is required\n",
573                         resolver->name);
574                 return -1;
575         }
576
577         AST_RWLIST_WRLOCK(&resolvers);
578
579         AST_LIST_TRAVERSE(&resolvers, iter, next) {
580                 if (!strcmp(iter->name, resolver->name)) {
581                         ast_log(LOG_ERROR, "A DNS resolver with the name '%s' is already registered\n", resolver->name);
582                         AST_RWLIST_UNLOCK(&resolvers);
583                         return -1;
584                 }
585         }
586
587         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&resolvers, iter, next) {
588                 if (iter->priority > resolver->priority) {
589                         AST_RWLIST_INSERT_BEFORE_CURRENT(resolver, next);
590                         inserted = 1;
591                         break;
592                 }
593         }
594         AST_RWLIST_TRAVERSE_SAFE_END;
595
596         if (!inserted) {
597                 AST_RWLIST_INSERT_TAIL(&resolvers, resolver, next);
598         }
599
600         AST_RWLIST_UNLOCK(&resolvers);
601
602         ast_verb(2, "Registered DNS resolver '%s' with priority '%d'\n", resolver->name, resolver->priority);
603
604         return 0;
605 }
606
607 void ast_dns_resolver_unregister(struct ast_dns_resolver *resolver)
608 {
609         struct ast_dns_resolver *iter;
610
611         if (!resolver) {
612                 return;
613         }
614
615         AST_RWLIST_WRLOCK(&resolvers);
616         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&resolvers, iter, next) {
617                 if (resolver == iter) {
618                         AST_RWLIST_REMOVE_CURRENT(next);
619                         break;
620                 }
621         }
622         AST_RWLIST_TRAVERSE_SAFE_END;
623         AST_RWLIST_UNLOCK(&resolvers);
624
625         ast_verb(2, "Unregistered DNS resolver '%s'\n", resolver->name);
626 }
627
628 char *dns_find_record(const char *record, size_t record_size, const char *response, size_t response_size)
629 {
630         size_t remaining_size = response_size;
631         const char *search_base = response;
632         char *record_offset;
633
634         while (1) {
635                 record_offset = memchr(search_base, record[0], remaining_size);
636
637                 ast_assert(record_offset != NULL);
638                 ast_assert(search_base + remaining_size - record_offset >= record_size);
639
640                 if (!memcmp(record_offset, record, record_size)) {
641                         return record_offset;
642                 }
643
644                 remaining_size -= record_offset - search_base;
645                 search_base = record_offset + 1;
646         }
647 }
648
649 int dns_parse_short(unsigned char *cur, uint16_t *val)
650 {
651         /* This assignment takes a big-endian 16-bit value and stores it in the
652          * machine's native byte order. Using this method allows us to avoid potential
653          * alignment issues in case the order is not on a short-addressable boundary.
654          * See http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html for
655          * more information
656          */
657         *val = (cur[1] << 0) | (cur[0] << 8);
658         return sizeof(*val);
659 }
660
661 int dns_parse_string(char *cur, uint8_t *size, char **val)
662 {
663         *size = *cur++;
664         *val = cur;
665         return *size + 1;
666 }