resolv: Implement CNAME faking
authorJakub Hrozek <jhrozek@redhat.com>
Sat, 4 Oct 2014 16:10:42 +0000 (18:10 +0200)
committerMichael Adam <obnox@samba.org>
Tue, 21 Oct 2014 11:39:39 +0000 (13:39 +0200)
Signed-off-by: Jakub Hrozek <jhrozek@redhat.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
src/resolv_wrapper.c
tests/fake_hosts.in
tests/test_dns_fake.c

index ab741b3..dab26b3 100644 (file)
@@ -478,6 +478,39 @@ static int rwrap_fake_soa(const char *key,
        return 0;
 }
 
+static int rwrap_fake_cname(const char *key,
+                           const char *value,
+                           uint8_t *answer,
+                           size_t anslen)
+{
+       uint8_t *a = answer;
+       int rv;
+       unsigned char hostname_compressed[MAXDNAME];
+       ssize_t rdata_size;
+
+       if (value == NULL) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
+               return -1;
+       }
+
+       /* Prepare the data to write */
+       rdata_size = ns_name_compress(value,
+                                     hostname_compressed, MAXDNAME,
+                                     NULL, NULL);
+       if (rdata_size < 0) {
+               return -1;
+       }
+
+       rv = rwrap_fake_common(ns_t_cname, key, rdata_size, &a, anslen);
+       if (rv < 0) {
+               return -1;
+       }
+
+       memcpy(a, hostname_compressed, rdata_size);
+
+       return 0;
+}
+
 static int rwrap_fake_empty_query(const char *key,
                                  uint16_t type,
                                  uint8_t *answer,
@@ -571,6 +604,10 @@ static int rwrap_res_fake_hosts(const char *hostfile,
                                      rec_type, "SOA", key, query)) {
                        rc = rwrap_fake_soa(key, value, answer, anslen);
                        break;
+               } else if (TYPE_MATCH(type, ns_t_cname,
+                                     rec_type, "CNAME", key, query)) {
+                       rc = rwrap_fake_cname(key, value, answer, anslen);
+                       break;
                }
        }
 
index ed82fd6..5e1e008 100644 (file)
@@ -3,3 +3,4 @@ AAAA cwrap6.org 2a00:1450:4013:c01::63
 SRV _ldap._tcp.cwrap.org ldap.cwrap.org 389 1 5
 SRV _krb5._tcp.cwrap.org krb5.cwrap.org 88
 SOA cwrap.org ns1.cwrap.org admin.cwrap.org 2014100457 3600 300 1814400 600
+CNAME cwrap.org therealcwrap.org
index eec0548..260a8e6 100644 (file)
@@ -339,6 +339,48 @@ static void test_res_fake_soa_query(void **state)
        assert_int_equal(minimum, 600);
 }
 
+static void test_res_fake_cname_query(void **state)
+{
+       int rv;
+       struct __res_state dnsstate;
+       unsigned char answer[ANSIZE];
+       ns_msg handle;
+       ns_rr rr;   /* expanded resource record */
+       const uint8_t *rrdata;
+       char cname[MAXDNAME];
+
+       (void) state; /* unused */
+
+       memset(&dnsstate, 0, sizeof(struct __res_state));
+       rv = res_ninit(&dnsstate);
+       assert_int_equal(rv, 0);
+
+       rv = res_nquery(&dnsstate, "cwrap.org", ns_c_in, ns_t_cname,
+                       answer, ANSIZE);
+       assert_int_not_equal(rv, -1);
+
+       ns_initparse(answer, 256, &handle);
+
+       /*
+        * The query must finish w/o an error, have one answer and the answer
+        * must be a parseable RR of type CNAME and have the cname as in the
+        * fake hosts file
+        */
+       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_cname);
+
+       rrdata = ns_rr_rdata(rr);
+
+       rv = ns_name_uncompress(ns_msg_base(handle),
+                               ns_msg_end(handle),
+                               rrdata,
+                               cname, MAXDNAME);
+       assert_int_not_equal(rv, -1);
+
+       assert_string_equal(cname, "therealcwrap.org");
+}
 
 int main(void)
 {
@@ -352,6 +394,7 @@ int main(void)
                unit_test(test_res_fake_srv_query),
                unit_test(test_res_fake_srv_query_minimal),
                unit_test(test_res_fake_soa_query),
+               unit_test(test_res_fake_cname_query),
        };
 
        rc = run_tests(tests);