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