The branch, master has been updated via 0bd14b6 Add support for the PTR DNS Resource Record type via bdd1166 rwrap: improve debug error messages from 02535da Add support for the URI DNS Resource Record type
https://git.samba.org/?p=resolv_wrapper.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit 0bd14b6fc075aa2a26d605d1aacbc2b5ff8946dc Author: Jakub Hrozek <jakub.hro...@posteo.se> Date: Thu Aug 18 17:49:00 2016 +0200 Add support for the PTR DNS Resource Record type Signed-off-by: Jakub Hrozek <jakub.hro...@posteo.se> Reviewed-by: Andreas Schneider <a...@samba.org> Reviewed-by: Michael Adam <ob...@samba.org> commit bdd11668974c16a011f2dfaefe0aa233c3be1f82 Author: Michael Adam <ob...@samba.org> Date: Mon Aug 29 08:16:25 2016 +0200 rwrap: improve debug error messages Signed-off-by: Michael Adam <ob...@samba.org> ----------------------------------------------------------------------- Summary of changes: src/resolv_wrapper.c | 93 +++++++++++++++++++++++++++++++++++++++------------ tests/fake_hosts.in | 1 + tests/test_dns_fake.c | 44 ++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 21 deletions(-) Changeset truncated at 500 lines: diff --git a/src/resolv_wrapper.c b/src/resolv_wrapper.c index a07086a..455a0af 100644 --- a/src/resolv_wrapper.c +++ b/src/resolv_wrapper.c @@ -192,6 +192,7 @@ struct rwrap_fake_rr { struct rwrap_uri_rrdata uri_rec; struct rwrap_soa_rrdata soa_rec; char cname_rec[MAXDNAME]; + char ptr_rec[MAXDNAME]; } rrdata; char key[MAXDNAME]; @@ -381,6 +382,16 @@ static int rwrap_create_fake_cname_rr(const char *key, return 0; } +static int rwrap_create_fake_ptr_rr(const char *key, + const char *value, + struct rwrap_fake_rr *rr) +{ + memcpy(rr->rrdata.ptr_rec , value, strlen(value) + 1); + memcpy(rr->key, key, strlen(key) + 1); + rr->type = ns_t_ptr; + return 0; +} + /* Prepares a fake header with a single response. Advances header_blob */ static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining, size_t ancount, size_t arcount) @@ -486,9 +497,8 @@ static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr, uint8_t *a = answer_ptr; ssize_t resp_size; - if (rr == NULL || rr->type != ns_t_a) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_a) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR"); @@ -511,9 +521,8 @@ static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr, uint8_t *a = answer; ssize_t resp_size; - if (rr == NULL || rr->type != ns_t_aaaa) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_aaaa) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR"); @@ -539,9 +548,8 @@ static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr, unsigned char hostname_compressed[MAXDNAME]; ssize_t compressed_len; - if (rr == NULL || rr->type != ns_t_ns) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_ns) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR"); @@ -580,9 +588,8 @@ static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr, unsigned char hostname_compressed[MAXDNAME]; ssize_t compressed_len; - if (rr == NULL || rr->type != ns_t_srv) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_srv) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR"); @@ -621,9 +628,8 @@ static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr, unsigned char uri_compressed[MAXDNAME]; ssize_t compressed_len; - if (rr == NULL || rr->type != ns_t_uri) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_uri) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding URI RR"); @@ -663,9 +669,8 @@ static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr, unsigned char mailbox_compressed[MAXDNAME]; ssize_t compressed_mb_len; - if (rr == NULL || rr->type != ns_t_soa) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_soa) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR"); @@ -715,9 +720,8 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr, unsigned char hostname_compressed[MAXDNAME]; ssize_t rdata_size; - if (rr == NULL || rr->type != ns_t_cname) { - RWRAP_LOG(RWRAP_LOG_ERROR, - "Malformed record, no or wrong value!\n"); + if (rr->type != ns_t_cname) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); return -1; } RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR"); @@ -741,6 +745,40 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr, return resp_size; } +static ssize_t rwrap_fake_ptr(struct rwrap_fake_rr *rr, + uint8_t *answer, + size_t anslen) +{ + uint8_t *a = answer; + ssize_t rdata_size; + ssize_t resp_size; + unsigned char hostname_compressed[MAXDNAME]; + + if (rr->type != ns_t_ptr) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n"); + return -1; + } + RWRAP_LOG(RWRAP_LOG_TRACE, "Adding PTR RR"); + + /* Prepare the data to write */ + rdata_size = ns_name_compress(rr->rrdata.ptr_rec, + hostname_compressed, MAXDNAME, + NULL, NULL); + if (rdata_size < 0) { + return -1; + } + + resp_size = rwrap_fake_rdata_common(ns_t_ptr, rdata_size, + rr->key, anslen, &a); + if (resp_size < 0) { + return -1; + } + + memcpy(a, hostname_compressed, rdata_size); + + return resp_size; +} + #define RESOLV_MATCH(line, name) \ (strncmp(line, name, sizeof(name) - 1) == 0 && \ (line[sizeof(name) - 1] == ' ' || \ @@ -880,6 +918,10 @@ static int rwrap_get_record(const char *hostfile, unsigned recursion, value, rr + 1); } break; + } else if (TYPE_MATCH(type, ns_t_ptr, + rec_type, "PTR", key, query)) { + rc = rwrap_create_fake_ptr_rr(key, value, rr); + break; } } @@ -931,6 +973,7 @@ static inline bool rwrap_known_type(int type) case ns_t_uri: case ns_t_soa: case ns_t_cname: + case ns_t_ptr: return true; } @@ -980,6 +1023,11 @@ static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr, { ssize_t resp_data; + if (rr == NULL) { + RWRAP_LOG(RWRAP_LOG_ERROR, "Internal error!\n"); + return -1; + } + switch (rr->type) { case ns_t_a: resp_data = rwrap_fake_a(rr, answer, anslen); @@ -1002,6 +1050,9 @@ static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr, case ns_t_cname: resp_data = rwrap_fake_cname(rr, answer, anslen); break; + case ns_t_ptr: + resp_data = rwrap_fake_ptr(rr, answer, anslen); + break; default: return -1; } diff --git a/tests/fake_hosts.in b/tests/fake_hosts.in index d7bf9a2..1812979 100644 --- a/tests/fake_hosts.in +++ b/tests/fake_hosts.in @@ -14,3 +14,4 @@ A ns1.cwrap.org 127.0.0.24 A ns2.cwrap.org 127.0.0.25 URI _vpn.cwrap.org https://vpn.cwrap.org/VPN 2 5 URI _ftp.cwrap.org ftp://ftp.cwrap.org/public +PTR 22.0.0.127.in-addr.arpa www.cwrap.org diff --git a/tests/test_dns_fake.c b/tests/test_dns_fake.c index 0702dc8..d3e9ebe 100644 --- a/tests/test_dns_fake.c +++ b/tests/test_dns_fake.c @@ -664,6 +664,49 @@ static void test_res_fake_a_via_cname(void **state) assert_string_equal(addr, "127.0.0.22"); } +static void test_res_fake_ptr_query(void **state) +{ + int rv; + struct __res_state dnsstate; + unsigned char answer[ANSIZE]; + const uint8_t *rrdata; + char ptrname[MAXDNAME]; + ns_msg handle; + ns_rr rr; /* expanded resource record */ + + (void) state; /* unused */ + + memset(&dnsstate, 0, sizeof(struct __res_state)); + rv = res_ninit(&dnsstate); + assert_int_equal(rv, 0); + + rv = res_nquery(&dnsstate, "22.0.0.127.in-addr.arpa", ns_c_in, ns_t_ptr, + answer, sizeof(answer)); + assert_in_range(rv, 1, 100); + + ns_initparse(answer, sizeof(answer), &handle); + + /* + * The query must finish w/o an error, have one answer and the answer + * must be a parseable RR of type PTR and have the name that our + * fake hosts file contains + */ + assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror); + assert_int_equal(ns_msg_count(handle, ns_s_an), 1); + assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0); + assert_int_equal(ns_rr_type(rr), ns_t_ptr); + + rrdata = ns_rr_rdata(rr); + + rv = ns_name_uncompress(ns_msg_base(handle), + ns_msg_end(handle), + rrdata, + ptrname, MAXDNAME); + assert_int_not_equal(rv, -1); + + assert_string_equal(ptrname, "www.cwrap.org"); +} + int main(void) { int rc; @@ -682,6 +725,7 @@ int main(void) cmocka_unit_test(test_res_fake_soa_query), cmocka_unit_test(test_res_fake_cname_query), cmocka_unit_test(test_res_fake_a_via_cname), + cmocka_unit_test(test_res_fake_ptr_query), }; rc = cmocka_run_group_tests(fake_tests, NULL, NULL); -- Resolv Wrapper Repository