Debian Codegraph @

src/resolve/test-dnssec-complex.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/socket.h>
4
5 #include "sd-bus.h"
6
7 #include "af-list.h"
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "bus-locator.h"
11 #include "dns-type.h"
12 #include "random-util.h"
13 #include "resolved-def.h"
14 #include "string-util.h"
15 #include "tests.h"
16
17 static void prefix_random(const char *name, char **ret) {
18 uint64_t i, u;
19 char *m = NULL;
20
21 assert(ret);
22
23 u = 1 + (random_u64() & 3);
24
25 for (i = 0; i < u; i++) {
26 _cleanup_free_ char *b = NULL;
27 char *x;
28
29 assert_se(asprintf(&b, "x%" PRIu64 "x", random_u64()));
30 x = strjoin(b, ".", name);
31 assert_se(x);
32
33 free(m);
34 m = x;
35 }
36
37 *ret = m;
38 }
39
40 static void test_rr_lookup(sd_bus *bus, const char *name, uint16_t type, const char *result) {
41 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
42 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
43 _cleanup_free_ char *m = NULL;
44 int r;
45
46 /* If the name starts with a dot, we prefix one to three random labels */
47 if (startswith(name, ".")) {
48 prefix_random(name + 1, &m);
49 name = m;
50 }
51
52 assert_se(bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord") >= 0);
53
54 assert_se(sd_bus_message_append(req, "isqqt", 0, name, DNS_CLASS_IN, type, UINT64_C(0)) >= 0);
55
56 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
57
58 if (r < 0) {
59 assert_se(result);
60 assert_se(sd_bus_error_has_name(&error, result));
61 log_info("[OK] %s/%s resulted in <%s>.", name, dns_type_to_string(type), error.name);
62 } else {
63 assert_se(!result);
64 log_info("[OK] %s/%s succeeded.", name, dns_type_to_string(type));
65 }
66 }
67
68 static void test_hostname_lookup(sd_bus *bus, const char *name, int family, const char *result) {
69 _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
70 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
71 _cleanup_free_ char *m = NULL;
72 const char *af;
73 int r;
74
75 af = family == AF_UNSPEC ? "AF_UNSPEC" : af_to_name(family);
76
77 /* If the name starts with a dot, we prefix one to three random labels */
78 if (startswith(name, ".")) {
79 prefix_random(name + 1, &m);
80 name = m;
81 }
82
83 assert_se(bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname") >= 0);
84
85 assert_se(sd_bus_message_append(req, "isit", 0, name, family, UINT64_C(0)) >= 0);
86
87 r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
88
89 if (r < 0) {
90 assert_se(result);
91 assert_se(sd_bus_error_has_name(&error, result));
92 log_info("[OK] %s/%s resulted in <%s>.", name, af, error.name);
93 } else {
94 assert_se(!result);
95 log_info("[OK] %s/%s succeeded.", name, af);
96 }
97 }
98
99 int main(int argc, char* argv[]) {
100 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
101
102 /* Note that this is a manual test as it requires:
103 *
104 * Full network access
105 * A DNSSEC capable DNS server
106 * That zones contacted are still set up as they were when I wrote this.
107 */
108
109 test_setup_logging(LOG_DEBUG);
110
111 assert_se(sd_bus_open_system(&bus) >= 0);
112
113 /* Normally signed */
114 test_rr_lookup(bus, "www.eurid.eu", DNS_TYPE_A, NULL);
115 test_hostname_lookup(bus, "www.eurid.eu", AF_UNSPEC, NULL);
116
117 test_rr_lookup(bus, "sigok.verteiltesysteme.net", DNS_TYPE_A, NULL);
118 test_hostname_lookup(bus, "sigok.verteiltesysteme.net", AF_UNSPEC, NULL);
119
120 /* Normally signed, NODATA */
121 test_rr_lookup(bus, "www.eurid.eu", DNS_TYPE_RP, BUS_ERROR_NO_SUCH_RR);
122 test_rr_lookup(bus, "sigok.verteiltesysteme.net", DNS_TYPE_RP, BUS_ERROR_NO_SUCH_RR);
123
124 /* Invalid signature */
125 test_rr_lookup(bus, "sigfail.verteiltesysteme.net", DNS_TYPE_A, BUS_ERROR_DNSSEC_FAILED);
126 test_hostname_lookup(bus, "sigfail.verteiltesysteme.net", AF_INET, BUS_ERROR_DNSSEC_FAILED);
127
128 /* Invalid signature, RSA, wildcard */
129 test_rr_lookup(bus, ".wilda.rhybar.0skar.cz", DNS_TYPE_A, BUS_ERROR_DNSSEC_FAILED);
130 test_hostname_lookup(bus, ".wilda.rhybar.0skar.cz", AF_INET, BUS_ERROR_DNSSEC_FAILED);
131
132 /* Invalid signature, ECDSA, wildcard */
133 test_rr_lookup(bus, ".wilda.rhybar.ecdsa.0skar.cz", DNS_TYPE_A, BUS_ERROR_DNSSEC_FAILED);
134 test_hostname_lookup(bus, ".wilda.rhybar.ecdsa.0skar.cz", AF_INET, BUS_ERROR_DNSSEC_FAILED);
135
136 /* Missing DS for DNSKEY */
137 test_rr_lookup(bus, "www.dnssec-bogus.sg", DNS_TYPE_A, BUS_ERROR_DNSSEC_FAILED);
138 test_hostname_lookup(bus, "www.dnssec-bogus.sg", AF_INET, BUS_ERROR_DNSSEC_FAILED);
139
140 /* NXDOMAIN in NSEC domain */
141 test_rr_lookup(bus, "hhh.nasa.gov", DNS_TYPE_A, BUS_ERROR_DNS_NXDOMAIN);
142 test_hostname_lookup(bus, "hhh.nasa.gov", AF_UNSPEC, BUS_ERROR_DNS_NXDOMAIN);
143 test_rr_lookup(bus, "_pgpkey-https._tcp.hkps.pool.sks-keyservers.net", DNS_TYPE_SRV, BUS_ERROR_DNS_NXDOMAIN);
144
145 /* wildcard, NSEC zone */
146 test_rr_lookup(bus, ".wilda.nsec.0skar.cz", DNS_TYPE_A, NULL);
147 test_hostname_lookup(bus, ".wilda.nsec.0skar.cz", AF_INET, NULL);
148
149 /* wildcard, NSEC zone, NODATA */
150 test_rr_lookup(bus, ".wilda.nsec.0skar.cz", DNS_TYPE_RP, BUS_ERROR_NO_SUCH_RR);
151
152 /* wildcard, NSEC3 zone */
153 test_rr_lookup(bus, ".wilda.0skar.cz", DNS_TYPE_A, NULL);
154 test_hostname_lookup(bus, ".wilda.0skar.cz", AF_INET, NULL);
155
156 /* wildcard, NSEC3 zone, NODATA */
157 test_rr_lookup(bus, ".wilda.0skar.cz", DNS_TYPE_RP, BUS_ERROR_NO_SUCH_RR);
158
159 /* wildcard, NSEC zone, CNAME */
160 test_rr_lookup(bus, ".wild.nsec.0skar.cz", DNS_TYPE_A, NULL);
161 test_hostname_lookup(bus, ".wild.nsec.0skar.cz", AF_UNSPEC, NULL);
162 test_hostname_lookup(bus, ".wild.nsec.0skar.cz", AF_INET, NULL);
163
164 /* wildcard, NSEC zone, NODATA, CNAME */
165 test_rr_lookup(bus, ".wild.nsec.0skar.cz", DNS_TYPE_RP, BUS_ERROR_NO_SUCH_RR);
166
167 /* wildcard, NSEC3 zone, CNAME */
168 test_rr_lookup(bus, ".wild.0skar.cz", DNS_TYPE_A, NULL);
169 test_hostname_lookup(bus, ".wild.0skar.cz", AF_UNSPEC, NULL);
170 test_hostname_lookup(bus, ".wild.0skar.cz", AF_INET, NULL);
171
172 /* wildcard, NSEC3 zone, NODATA, CNAME */
173 test_rr_lookup(bus, ".wild.0skar.cz", DNS_TYPE_RP, BUS_ERROR_NO_SUCH_RR);
174
175 /* NODATA due to empty non-terminal in NSEC domain */
176 test_rr_lookup(bus, "herndon.nasa.gov", DNS_TYPE_A, BUS_ERROR_NO_SUCH_RR);
177 test_hostname_lookup(bus, "herndon.nasa.gov", AF_UNSPEC, BUS_ERROR_NO_SUCH_RR);
178 test_hostname_lookup(bus, "herndon.nasa.gov", AF_INET, BUS_ERROR_NO_SUCH_RR);
179 test_hostname_lookup(bus, "herndon.nasa.gov", AF_INET6, BUS_ERROR_NO_SUCH_RR);
180
181 /* NXDOMAIN in NSEC root zone: */
182 test_rr_lookup(bus, "jasdhjas.kjkfgjhfjg", DNS_TYPE_A, BUS_ERROR_DNS_NXDOMAIN);
183 test_hostname_lookup(bus, "jasdhjas.kjkfgjhfjg", AF_UNSPEC, BUS_ERROR_DNS_NXDOMAIN);
184 test_hostname_lookup(bus, "jasdhjas.kjkfgjhfjg", AF_INET, BUS_ERROR_DNS_NXDOMAIN);
185 test_hostname_lookup(bus, "jasdhjas.kjkfgjhfjg", AF_INET6, BUS_ERROR_DNS_NXDOMAIN);
186
187 /* NXDOMAIN in NSEC3 .com zone: */
188 test_rr_lookup(bus, "kjkfgjhfjgsdfdsfd.com", DNS_TYPE_A, BUS_ERROR_DNS_NXDOMAIN);
189 test_hostname_lookup(bus, "kjkfgjhfjgsdfdsfd.com", AF_INET, BUS_ERROR_DNS_NXDOMAIN);
190 test_hostname_lookup(bus, "kjkfgjhfjgsdfdsfd.com", AF_INET6, BUS_ERROR_DNS_NXDOMAIN);
191 test_hostname_lookup(bus, "kjkfgjhfjgsdfdsfd.com", AF_UNSPEC, BUS_ERROR_DNS_NXDOMAIN);
192
193 /* Unsigned A */
194 test_rr_lookup(bus, "poettering.de", DNS_TYPE_A, NULL);
195 test_rr_lookup(bus, "poettering.de", DNS_TYPE_AAAA, NULL);
196 test_hostname_lookup(bus, "poettering.de", AF_UNSPEC, NULL);
197 test_hostname_lookup(bus, "poettering.de", AF_INET, NULL);
198 test_hostname_lookup(bus, "poettering.de", AF_INET6, NULL);
199
200 #if HAVE_LIBIDN2
201 /* Unsigned A with IDNA conversion necessary */
202 test_hostname_lookup(bus, "pöttering.de", AF_UNSPEC, NULL);
203 test_hostname_lookup(bus, "pöttering.de", AF_INET, NULL);
204 test_hostname_lookup(bus, "pöttering.de", AF_INET6, NULL);
205 #endif
206
207 /* DNAME, pointing to NXDOMAIN */
208 test_rr_lookup(bus, ".ireallyhpoethisdoesnexist.xn--kprw13d.", DNS_TYPE_A, BUS_ERROR_DNS_NXDOMAIN);
209 test_rr_lookup(bus, ".ireallyhpoethisdoesnexist.xn--kprw13d.", DNS_TYPE_RP, BUS_ERROR_DNS_NXDOMAIN);
210 test_hostname_lookup(bus, ".ireallyhpoethisdoesntexist.xn--kprw13d.", AF_UNSPEC, BUS_ERROR_DNS_NXDOMAIN);
211 test_hostname_lookup(bus, ".ireallyhpoethisdoesntexist.xn--kprw13d.", AF_INET, BUS_ERROR_DNS_NXDOMAIN);
212 test_hostname_lookup(bus, ".ireallyhpoethisdoesntexist.xn--kprw13d.", AF_INET6, BUS_ERROR_DNS_NXDOMAIN);
213
214 return 0;
215 }