dns_core.c 17 KB

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