xref: /freebsd/contrib/ldns/zone.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /* zone.c
2  *
3  * Functions for ldns_zone structure
4  * a Net::DNS like library for C
5  *
6  * (c) NLnet Labs, 2005-2006
7  * See the file LICENSE for the license
8  */
9 #include <ldns/config.h>
10 
11 #include <ldns/ldns.h>
12 #include <ldns/internal.h>
13 
14 #include <strings.h>
15 #include <limits.h>
16 
17 ldns_rr *
18 ldns_zone_soa(const ldns_zone *z)
19 {
20         return z->_soa;
21 }
22 
23 size_t
24 ldns_zone_rr_count(const ldns_zone *z)
25 {
26 	return ldns_rr_list_rr_count(z->_rrs);
27 }
28 
29 void
30 ldns_zone_set_soa(ldns_zone *z, ldns_rr *soa)
31 {
32 	z->_soa = soa;
33 }
34 
35 ldns_rr_list *
36 ldns_zone_rrs(const ldns_zone *z)
37 {
38 	return z->_rrs;
39 }
40 
41 void
42 ldns_zone_set_rrs(ldns_zone *z, ldns_rr_list *rrlist)
43 {
44 	z->_rrs = rrlist;
45 }
46 
47 bool
48 ldns_zone_push_rr_list(ldns_zone *z, const ldns_rr_list *list)
49 {
50 	return ldns_rr_list_cat(ldns_zone_rrs(z), list);
51 }
52 
53 bool
54 ldns_zone_push_rr(ldns_zone *z, ldns_rr *rr)
55 {
56 	return ldns_rr_list_push_rr(ldns_zone_rrs(z), rr);
57 }
58 
59 
60 /*
61  * Get the list of glue records in a zone
62  * XXX: there should be a way for this to return error, other than NULL,
63  *      since NULL is a valid return
64  */
65 ldns_rr_list *
66 ldns_zone_glue_rr_list(const ldns_zone *z)
67 {
68 	/* when do we find glue? It means we find an IP address
69 	 * (AAAA/A) for a nameserver listed in the zone
70 	 *
71 	 * Alg used here:
72 	 * first find all the zonecuts (NS records)
73 	 * find all the AAAA or A records (can be done it the
74 	 * above loop).
75 	 *
76 	 * Check if the aaaa/a list are subdomains under the
77 	 * NS domains.
78 	 * If yes -> glue, if no -> not glue
79 	 */
80 
81 	ldns_rr_list *zone_cuts;
82 	ldns_rr_list *addr;
83 	ldns_rr_list *glue;
84 	ldns_rr *r, *ns, *a;
85 	ldns_rdf *dname_a, *ns_owner;
86 	size_t i,j;
87 
88 	zone_cuts = NULL;
89 	addr = NULL;
90 	glue = NULL;
91 
92 	/* we cannot determine glue in a 'zone' without a SOA */
93 	if (!ldns_zone_soa(z)) {
94 		return NULL;
95 	}
96 
97 	zone_cuts = ldns_rr_list_new();
98 	if (!zone_cuts) goto memory_error;
99 	addr = ldns_rr_list_new();
100 	if (!addr) goto memory_error;
101 	glue = ldns_rr_list_new();
102 	if (!glue) goto memory_error;
103 
104 	for(i = 0; i < ldns_zone_rr_count(z); i++) {
105 		r = ldns_rr_list_rr(ldns_zone_rrs(z), i);
106 		if (ldns_rr_get_type(r) == LDNS_RR_TYPE_A ||
107 				ldns_rr_get_type(r) == LDNS_RR_TYPE_AAAA) {
108 			/* possibly glue */
109 			if (!ldns_rr_list_push_rr(addr, r)) goto memory_error;
110 			continue;
111 		}
112 		if (ldns_rr_get_type(r) == LDNS_RR_TYPE_NS) {
113 			/* multiple zones will end up here -
114 			 * for now; not a problem
115 			 */
116 			/* don't add NS records for the current zone itself */
117 			if (ldns_rdf_compare(ldns_rr_owner(r),
118 						ldns_rr_owner(ldns_zone_soa(z))) != 0) {
119 				if (!ldns_rr_list_push_rr(zone_cuts, r)) goto memory_error;
120 			}
121 			continue;
122 		}
123 	}
124 
125 	/* will sorting make it quicker ?? */
126 	for(i = 0; i < ldns_rr_list_rr_count(zone_cuts); i++) {
127 		ns = ldns_rr_list_rr(zone_cuts, i);
128 		ns_owner = ldns_rr_owner(ns);
129 
130 		for(j = 0; j < ldns_rr_list_rr_count(addr); j++) {
131 			a = ldns_rr_list_rr(addr, j);
132 			dname_a = ldns_rr_owner(a);
133 
134 			if (ldns_dname_is_subdomain(dname_a, ns_owner) ||
135 				ldns_dname_compare(dname_a, ns_owner) == 0) {
136 				/* GLUE! */
137 				if (!ldns_rr_list_push_rr(glue, a)) goto memory_error;
138 			}
139 		}
140 	}
141 
142 	ldns_rr_list_free(addr);
143 	ldns_rr_list_free(zone_cuts);
144 
145 	if (ldns_rr_list_rr_count(glue) == 0) {
146 		ldns_rr_list_free(glue);
147 		return NULL;
148 	} else {
149 		return glue;
150 	}
151 
152 memory_error:
153 	if (zone_cuts) {
154 		LDNS_FREE(zone_cuts);
155 	}
156 	if (addr) {
157 		ldns_rr_list_free(addr);
158 	}
159 	if (glue) {
160 		ldns_rr_list_free(glue);
161 	}
162 	return NULL;
163 }
164 
165 ldns_zone *
166 ldns_zone_new(void)
167 {
168 	ldns_zone *z;
169 
170 	z = LDNS_MALLOC(ldns_zone);
171 	if (!z) {
172 		return NULL;
173 	}
174 
175 	z->_rrs = ldns_rr_list_new();
176 	if (!z->_rrs) {
177 		LDNS_FREE(z);
178 		return NULL;
179 	}
180 	ldns_zone_set_soa(z, NULL);
181 	return z;
182 }
183 
184 /* we recognize:
185  * $TTL, $ORIGIN
186  */
187 ldns_status
188 ldns_zone_new_frm_fp(ldns_zone **z, FILE *fp, const ldns_rdf *origin, uint32_t ttl, ldns_rr_class c)
189 {
190 	return ldns_zone_new_frm_fp_l(z, fp, origin, ttl, c, NULL);
191 }
192 
193 /* XXX: class is never used */
194 ldns_status
195 ldns_zone_new_frm_fp_l(ldns_zone **z, FILE *fp, const ldns_rdf *origin,
196 	uint32_t default_ttl, ldns_rr_class ATTR_UNUSED(c), int *line_nr)
197 {
198 	ldns_zone *newzone;
199 	ldns_rr *rr, *prev_rr = NULL;
200 	uint32_t my_ttl;
201 	ldns_rdf *my_origin;
202 	ldns_rdf *my_prev;
203 	bool soa_seen = false; 	/* 2 soa are an error */
204 	ldns_status s;
205 	ldns_status ret;
206 	/* RFC 1035 Section 5.1, says 'Omitted class and TTL values are default
207 	 * to the last explicitly stated values.'
208 	 */
209 	bool ttl_from_TTL = false;
210 	bool explicit_ttl = false;
211 
212 	/* most cases of error are memory problems */
213 	ret = LDNS_STATUS_MEM_ERR;
214 
215 	newzone = NULL;
216 	my_origin = NULL;
217 	my_prev = NULL;
218 
219 	my_ttl    = default_ttl;
220 
221 	if (origin) {
222 		my_origin = ldns_rdf_clone(origin);
223 		if (!my_origin) goto error;
224 		/* also set the prev */
225 		my_prev   = ldns_rdf_clone(origin);
226 		if (!my_prev) goto error;
227 	}
228 
229 	newzone = ldns_zone_new();
230 	if (!newzone) goto error;
231 
232 	while(!feof(fp)) {
233 		/* If ttl came from $TTL line, then it should be the default.
234 		 * (RFC 2308 Section 4)
235 		 * Otherwise it "defaults to the last explicitly stated value"
236 		 * (RFC 1035 Section 5.1)
237 		 */
238 		if (ttl_from_TTL)
239 			my_ttl = default_ttl;
240 		s = _ldns_rr_new_frm_fp_l_internal(&rr, fp, &my_ttl, &my_origin,
241 				&my_prev, line_nr, &explicit_ttl);
242 		switch (s) {
243 		case LDNS_STATUS_OK:
244 			if (explicit_ttl) {
245 				if (!ttl_from_TTL) {
246 					/* No $TTL, so ttl "defaults to the
247 					 * last explicitly stated value"
248 					 * (RFC 1035 Section 5.1)
249 					 */
250 					my_ttl = ldns_rr_ttl(rr);
251 				}
252 			/* When ttl is implicit, try to adhere to the rules as
253 			 * much as possible. (also for compatibility with bind)
254 			 * This was changed when fixing an issue with ZONEMD
255 			 * which hashes the TTL too.
256 			 */
257 			} else if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SIG
258 			       ||  ldns_rr_get_type(rr) == LDNS_RR_TYPE_RRSIG) {
259 				if (ldns_rr_rd_count(rr) >= 4
260 				&&  ldns_rdf_get_type(ldns_rr_rdf(rr, 3)) == LDNS_RDF_TYPE_INT32)
261 
262 					/* SIG without explicit ttl get ttl
263 					 * from the original_ttl field
264 					 * (RFC 2535 Section 7.2)
265 					 *
266 					 * Similarly for RRSIG, but stated less
267 					 * specifically in the spec.
268 					 * (RFC 4034 Section 3)
269 					 */
270 					ldns_rr_set_ttl(rr,
271 					    ldns_rdf2native_int32(
272 					        ldns_rr_rdf(rr, 3)));
273 
274 			} else if (prev_rr
275 			       &&  ldns_rr_get_type(prev_rr) == ldns_rr_get_type(rr)
276 			       &&  ldns_dname_compare( ldns_rr_owner(prev_rr)
277 			                             , ldns_rr_owner(rr)) == 0)
278 
279 				/* "TTLs of all RRs in an RRSet must be the same"
280 				 * (RFC 2881 Section 5.2)
281 				 */
282 				ldns_rr_set_ttl(rr, ldns_rr_ttl(prev_rr));
283 
284 			prev_rr = rr;
285 			if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) {
286 				if (soa_seen) {
287 					/* second SOA
288 					 * just skip, maybe we want to say
289 					 * something??? */
290 					ldns_rr_free(rr);
291 					continue;
292 				}
293 				soa_seen = true;
294 				ldns_zone_set_soa(newzone, rr);
295 				/* set origin to soa if not specified */
296 				if (!my_origin) {
297 					my_origin = ldns_rdf_clone(ldns_rr_owner(rr));
298 				}
299 				continue;
300 			}
301 
302 			/* a normal RR - as sofar the DNS is normal */
303 			if (!ldns_zone_push_rr(newzone, rr)) {
304 				ldns_rr_free(rr);
305 				goto error;
306 			}
307 			break;
308 
309 		case LDNS_STATUS_SYNTAX_EMPTY:
310 			/* empty line was seen */
311 		case LDNS_STATUS_SYNTAX_TTL:
312 			/* the function set the ttl */
313 			default_ttl = my_ttl;
314 			ttl_from_TTL = true;
315 			break;
316 		case LDNS_STATUS_SYNTAX_ORIGIN:
317 			/* the function set the origin */
318 			break;
319 		case LDNS_STATUS_SYNTAX_INCLUDE:
320 			ret = LDNS_STATUS_SYNTAX_INCLUDE_ERR_NOTIMPL;
321 			goto error;
322 		default:
323 			ret = s;
324 			goto error;
325 		}
326 	}
327 
328 	if (my_origin) {
329 		ldns_rdf_deep_free(my_origin);
330 	}
331 	if (my_prev) {
332 		ldns_rdf_deep_free(my_prev);
333 	}
334 	if (z) {
335 		*z = newzone;
336 	} else {
337 		ldns_zone_free(newzone);
338 	}
339 
340 	return LDNS_STATUS_OK;
341 
342 error:
343 	if (my_origin) {
344 		ldns_rdf_deep_free(my_origin);
345 	}
346 	if (my_prev) {
347 		ldns_rdf_deep_free(my_prev);
348 	}
349 	if (newzone) {
350 		ldns_zone_free(newzone);
351 	}
352 	return ret;
353 }
354 
355 void
356 ldns_zone_sort(ldns_zone *zone)
357 {
358 	ldns_rr_list *zrr;
359 	assert(zone != NULL);
360 
361 	zrr = ldns_zone_rrs(zone);
362 	ldns_rr_list_sort(zrr);
363 }
364 
365 void
366 ldns_zone_free(ldns_zone *zone)
367 {
368 	ldns_rr_list_free(zone->_rrs);
369 	LDNS_FREE(zone);
370 }
371 
372 void
373 ldns_zone_deep_free(ldns_zone *zone)
374 {
375 	ldns_rr_free(zone->_soa);
376 	ldns_rr_list_deep_free(zone->_rrs);
377 	LDNS_FREE(zone);
378 }
379