diff options
Diffstat (limited to 'src/lib/libc/net/getrrsetbyname.c')
-rw-r--r-- | src/lib/libc/net/getrrsetbyname.c | 514 |
1 files changed, 514 insertions, 0 deletions
diff --git a/src/lib/libc/net/getrrsetbyname.c b/src/lib/libc/net/getrrsetbyname.c new file mode 100644 index 0000000000..89aa592ba0 --- /dev/null +++ b/src/lib/libc/net/getrrsetbyname.c | |||
@@ -0,0 +1,514 @@ | |||
1 | /* $OpenBSD: getrrsetbyname.c,v 1.11 2007/10/11 18:36:41 jakob Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2001 Jakob Schlyter. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
27 | */ | ||
28 | |||
29 | /* | ||
30 | * Portions Copyright (c) 1999-2001 Internet Software Consortium. | ||
31 | * | ||
32 | * Permission to use, copy, modify, and distribute this software for any | ||
33 | * purpose with or without fee is hereby granted, provided that the above | ||
34 | * copyright notice and this permission notice appear in all copies. | ||
35 | * | ||
36 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM | ||
37 | * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | ||
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL | ||
39 | * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
40 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING | ||
41 | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | ||
42 | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | ||
43 | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
44 | */ | ||
45 | |||
46 | #include <sys/types.h> | ||
47 | #include <netinet/in.h> | ||
48 | #include <arpa/nameser.h> | ||
49 | #include <netdb.h> | ||
50 | #include <resolv.h> | ||
51 | #include <stdlib.h> | ||
52 | #include <string.h> | ||
53 | |||
54 | #include "thread_private.h" | ||
55 | |||
56 | #define ANSWER_BUFFER_SIZE 1024*64 | ||
57 | |||
58 | struct dns_query { | ||
59 | char *name; | ||
60 | u_int16_t type; | ||
61 | u_int16_t class; | ||
62 | struct dns_query *next; | ||
63 | }; | ||
64 | |||
65 | struct dns_rr { | ||
66 | char *name; | ||
67 | u_int16_t type; | ||
68 | u_int16_t class; | ||
69 | u_int16_t ttl; | ||
70 | u_int16_t size; | ||
71 | void *rdata; | ||
72 | struct dns_rr *next; | ||
73 | }; | ||
74 | |||
75 | struct dns_response { | ||
76 | HEADER header; | ||
77 | struct dns_query *query; | ||
78 | struct dns_rr *answer; | ||
79 | struct dns_rr *authority; | ||
80 | struct dns_rr *additional; | ||
81 | }; | ||
82 | |||
83 | static struct dns_response *parse_dns_response(const u_char *, int); | ||
84 | static struct dns_query *parse_dns_qsection(const u_char *, int, | ||
85 | const u_char **, int); | ||
86 | static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **, | ||
87 | int); | ||
88 | |||
89 | static void free_dns_query(struct dns_query *); | ||
90 | static void free_dns_rr(struct dns_rr *); | ||
91 | static void free_dns_response(struct dns_response *); | ||
92 | |||
93 | static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t); | ||
94 | |||
95 | int | ||
96 | getrrsetbyname(const char *hostname, unsigned int rdclass, | ||
97 | unsigned int rdtype, unsigned int flags, | ||
98 | struct rrsetinfo **res) | ||
99 | { | ||
100 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
101 | int result; | ||
102 | struct rrsetinfo *rrset = NULL; | ||
103 | struct dns_response *response = NULL; | ||
104 | struct dns_rr *rr; | ||
105 | struct rdatainfo *rdata; | ||
106 | int length; | ||
107 | unsigned int index_ans, index_sig; | ||
108 | u_char answer[ANSWER_BUFFER_SIZE]; | ||
109 | |||
110 | /* check for invalid class and type */ | ||
111 | if (rdclass > 0xffff || rdtype > 0xffff) { | ||
112 | result = ERRSET_INVAL; | ||
113 | goto fail; | ||
114 | } | ||
115 | |||
116 | /* don't allow queries of class or type ANY */ | ||
117 | if (rdclass == 0xff || rdtype == 0xff) { | ||
118 | result = ERRSET_INVAL; | ||
119 | goto fail; | ||
120 | } | ||
121 | |||
122 | /* don't allow flags yet, unimplemented */ | ||
123 | if (flags) { | ||
124 | result = ERRSET_INVAL; | ||
125 | goto fail; | ||
126 | } | ||
127 | |||
128 | /* initialize resolver */ | ||
129 | if (_res_init(0) == -1) { | ||
130 | result = ERRSET_FAIL; | ||
131 | goto fail; | ||
132 | } | ||
133 | |||
134 | #ifdef DEBUG | ||
135 | _resp->options |= RES_DEBUG; | ||
136 | #endif /* DEBUG */ | ||
137 | |||
138 | #ifdef RES_USE_DNSSEC | ||
139 | /* turn on DNSSEC if EDNS0 is configured */ | ||
140 | if (_resp->options & RES_USE_EDNS0) | ||
141 | _resp->options |= RES_USE_DNSSEC; | ||
142 | #endif /* RES_USE_DNSEC */ | ||
143 | |||
144 | /* make query */ | ||
145 | length = res_query(hostname, (signed int) rdclass, (signed int) rdtype, | ||
146 | answer, sizeof(answer)); | ||
147 | if (length < 0) { | ||
148 | switch(h_errno) { | ||
149 | case HOST_NOT_FOUND: | ||
150 | result = ERRSET_NONAME; | ||
151 | goto fail; | ||
152 | case NO_DATA: | ||
153 | result = ERRSET_NODATA; | ||
154 | goto fail; | ||
155 | default: | ||
156 | result = ERRSET_FAIL; | ||
157 | goto fail; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /* parse result */ | ||
162 | response = parse_dns_response(answer, length); | ||
163 | if (response == NULL) { | ||
164 | result = ERRSET_FAIL; | ||
165 | goto fail; | ||
166 | } | ||
167 | |||
168 | if (response->header.qdcount != 1) { | ||
169 | result = ERRSET_FAIL; | ||
170 | goto fail; | ||
171 | } | ||
172 | |||
173 | /* initialize rrset */ | ||
174 | rrset = calloc(1, sizeof(struct rrsetinfo)); | ||
175 | if (rrset == NULL) { | ||
176 | result = ERRSET_NOMEMORY; | ||
177 | goto fail; | ||
178 | } | ||
179 | rrset->rri_rdclass = response->query->class; | ||
180 | rrset->rri_rdtype = response->query->type; | ||
181 | rrset->rri_ttl = response->answer->ttl; | ||
182 | rrset->rri_nrdatas = response->header.ancount; | ||
183 | |||
184 | /* check for authenticated data */ | ||
185 | if (response->header.ad == 1) | ||
186 | rrset->rri_flags |= RRSET_VALIDATED; | ||
187 | |||
188 | /* copy name from answer section */ | ||
189 | rrset->rri_name = strdup(response->answer->name); | ||
190 | if (rrset->rri_name == NULL) { | ||
191 | result = ERRSET_NOMEMORY; | ||
192 | goto fail; | ||
193 | } | ||
194 | |||
195 | /* count answers */ | ||
196 | rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass, | ||
197 | rrset->rri_rdtype); | ||
198 | rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass, | ||
199 | T_RRSIG); | ||
200 | |||
201 | /* allocate memory for answers */ | ||
202 | rrset->rri_rdatas = calloc(rrset->rri_nrdatas, | ||
203 | sizeof(struct rdatainfo)); | ||
204 | if (rrset->rri_rdatas == NULL) { | ||
205 | result = ERRSET_NOMEMORY; | ||
206 | goto fail; | ||
207 | } | ||
208 | |||
209 | /* allocate memory for signatures */ | ||
210 | rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo)); | ||
211 | if (rrset->rri_sigs == NULL) { | ||
212 | result = ERRSET_NOMEMORY; | ||
213 | goto fail; | ||
214 | } | ||
215 | |||
216 | /* copy answers & signatures */ | ||
217 | for (rr = response->answer, index_ans = 0, index_sig = 0; | ||
218 | rr; rr = rr->next) { | ||
219 | |||
220 | rdata = NULL; | ||
221 | |||
222 | if (rr->class == rrset->rri_rdclass && | ||
223 | rr->type == rrset->rri_rdtype) | ||
224 | rdata = &rrset->rri_rdatas[index_ans++]; | ||
225 | |||
226 | if (rr->class == rrset->rri_rdclass && | ||
227 | rr->type == T_RRSIG) | ||
228 | rdata = &rrset->rri_sigs[index_sig++]; | ||
229 | |||
230 | if (rdata) { | ||
231 | rdata->rdi_length = rr->size; | ||
232 | rdata->rdi_data = malloc(rr->size); | ||
233 | |||
234 | if (rdata->rdi_data == NULL) { | ||
235 | result = ERRSET_NOMEMORY; | ||
236 | goto fail; | ||
237 | } | ||
238 | memcpy(rdata->rdi_data, rr->rdata, rr->size); | ||
239 | } | ||
240 | } | ||
241 | free_dns_response(response); | ||
242 | |||
243 | *res = rrset; | ||
244 | return (ERRSET_SUCCESS); | ||
245 | |||
246 | fail: | ||
247 | if (rrset != NULL) | ||
248 | freerrset(rrset); | ||
249 | if (response != NULL) | ||
250 | free_dns_response(response); | ||
251 | return (result); | ||
252 | } | ||
253 | |||
254 | void | ||
255 | freerrset(struct rrsetinfo *rrset) | ||
256 | { | ||
257 | u_int16_t i; | ||
258 | |||
259 | if (rrset == NULL) | ||
260 | return; | ||
261 | |||
262 | if (rrset->rri_rdatas) { | ||
263 | for (i = 0; i < rrset->rri_nrdatas; i++) { | ||
264 | if (rrset->rri_rdatas[i].rdi_data == NULL) | ||
265 | break; | ||
266 | free(rrset->rri_rdatas[i].rdi_data); | ||
267 | } | ||
268 | free(rrset->rri_rdatas); | ||
269 | } | ||
270 | |||
271 | if (rrset->rri_sigs) { | ||
272 | for (i = 0; i < rrset->rri_nsigs; i++) { | ||
273 | if (rrset->rri_sigs[i].rdi_data == NULL) | ||
274 | break; | ||
275 | free(rrset->rri_sigs[i].rdi_data); | ||
276 | } | ||
277 | free(rrset->rri_sigs); | ||
278 | } | ||
279 | |||
280 | if (rrset->rri_name) | ||
281 | free(rrset->rri_name); | ||
282 | free(rrset); | ||
283 | } | ||
284 | |||
285 | /* | ||
286 | * DNS response parsing routines | ||
287 | */ | ||
288 | static struct dns_response * | ||
289 | parse_dns_response(const u_char *answer, int size) | ||
290 | { | ||
291 | struct dns_response *resp; | ||
292 | const u_char *cp; | ||
293 | |||
294 | /* allocate memory for the response */ | ||
295 | resp = calloc(1, sizeof(*resp)); | ||
296 | if (resp == NULL) | ||
297 | return (NULL); | ||
298 | |||
299 | /* initialize current pointer */ | ||
300 | cp = answer; | ||
301 | |||
302 | /* copy header */ | ||
303 | memcpy(&resp->header, cp, HFIXEDSZ); | ||
304 | cp += HFIXEDSZ; | ||
305 | |||
306 | /* fix header byte order */ | ||
307 | resp->header.qdcount = ntohs(resp->header.qdcount); | ||
308 | resp->header.ancount = ntohs(resp->header.ancount); | ||
309 | resp->header.nscount = ntohs(resp->header.nscount); | ||
310 | resp->header.arcount = ntohs(resp->header.arcount); | ||
311 | |||
312 | /* there must be at least one query */ | ||
313 | if (resp->header.qdcount < 1) { | ||
314 | free_dns_response(resp); | ||
315 | return (NULL); | ||
316 | } | ||
317 | |||
318 | /* parse query section */ | ||
319 | resp->query = parse_dns_qsection(answer, size, &cp, | ||
320 | resp->header.qdcount); | ||
321 | if (resp->header.qdcount && resp->query == NULL) { | ||
322 | free_dns_response(resp); | ||
323 | return (NULL); | ||
324 | } | ||
325 | |||
326 | /* parse answer section */ | ||
327 | resp->answer = parse_dns_rrsection(answer, size, &cp, | ||
328 | resp->header.ancount); | ||
329 | if (resp->header.ancount && resp->answer == NULL) { | ||
330 | free_dns_response(resp); | ||
331 | return (NULL); | ||
332 | } | ||
333 | |||
334 | /* parse authority section */ | ||
335 | resp->authority = parse_dns_rrsection(answer, size, &cp, | ||
336 | resp->header.nscount); | ||
337 | if (resp->header.nscount && resp->authority == NULL) { | ||
338 | free_dns_response(resp); | ||
339 | return (NULL); | ||
340 | } | ||
341 | |||
342 | /* parse additional section */ | ||
343 | resp->additional = parse_dns_rrsection(answer, size, &cp, | ||
344 | resp->header.arcount); | ||
345 | if (resp->header.arcount && resp->additional == NULL) { | ||
346 | free_dns_response(resp); | ||
347 | return (NULL); | ||
348 | } | ||
349 | |||
350 | return (resp); | ||
351 | } | ||
352 | |||
353 | static struct dns_query * | ||
354 | parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count) | ||
355 | { | ||
356 | struct dns_query *head, *curr, *prev; | ||
357 | int i, length; | ||
358 | char name[MAXDNAME]; | ||
359 | |||
360 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { | ||
361 | |||
362 | /* allocate and initialize struct */ | ||
363 | curr = calloc(1, sizeof(struct dns_query)); | ||
364 | if (curr == NULL) { | ||
365 | free_dns_query(head); | ||
366 | return (NULL); | ||
367 | } | ||
368 | if (head == NULL) | ||
369 | head = curr; | ||
370 | if (prev != NULL) | ||
371 | prev->next = curr; | ||
372 | |||
373 | /* name */ | ||
374 | length = dn_expand(answer, answer + size, *cp, name, | ||
375 | sizeof(name)); | ||
376 | if (length < 0) { | ||
377 | free_dns_query(head); | ||
378 | return (NULL); | ||
379 | } | ||
380 | curr->name = strdup(name); | ||
381 | if (curr->name == NULL) { | ||
382 | free_dns_query(head); | ||
383 | return (NULL); | ||
384 | } | ||
385 | *cp += length; | ||
386 | |||
387 | /* type */ | ||
388 | curr->type = _getshort(*cp); | ||
389 | *cp += INT16SZ; | ||
390 | |||
391 | /* class */ | ||
392 | curr->class = _getshort(*cp); | ||
393 | *cp += INT16SZ; | ||
394 | } | ||
395 | |||
396 | return (head); | ||
397 | } | ||
398 | |||
399 | static struct dns_rr * | ||
400 | parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, | ||
401 | int count) | ||
402 | { | ||
403 | struct dns_rr *head, *curr, *prev; | ||
404 | int i, length; | ||
405 | char name[MAXDNAME]; | ||
406 | |||
407 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { | ||
408 | |||
409 | /* allocate and initialize struct */ | ||
410 | curr = calloc(1, sizeof(struct dns_rr)); | ||
411 | if (curr == NULL) { | ||
412 | free_dns_rr(head); | ||
413 | return (NULL); | ||
414 | } | ||
415 | if (head == NULL) | ||
416 | head = curr; | ||
417 | if (prev != NULL) | ||
418 | prev->next = curr; | ||
419 | |||
420 | /* name */ | ||
421 | length = dn_expand(answer, answer + size, *cp, name, | ||
422 | sizeof(name)); | ||
423 | if (length < 0) { | ||
424 | free_dns_rr(head); | ||
425 | return (NULL); | ||
426 | } | ||
427 | curr->name = strdup(name); | ||
428 | if (curr->name == NULL) { | ||
429 | free_dns_rr(head); | ||
430 | return (NULL); | ||
431 | } | ||
432 | *cp += length; | ||
433 | |||
434 | /* type */ | ||
435 | curr->type = _getshort(*cp); | ||
436 | *cp += INT16SZ; | ||
437 | |||
438 | /* class */ | ||
439 | curr->class = _getshort(*cp); | ||
440 | *cp += INT16SZ; | ||
441 | |||
442 | /* ttl */ | ||
443 | curr->ttl = _getlong(*cp); | ||
444 | *cp += INT32SZ; | ||
445 | |||
446 | /* rdata size */ | ||
447 | curr->size = _getshort(*cp); | ||
448 | *cp += INT16SZ; | ||
449 | |||
450 | /* rdata itself */ | ||
451 | curr->rdata = malloc(curr->size); | ||
452 | if (curr->rdata == NULL) { | ||
453 | free_dns_rr(head); | ||
454 | return (NULL); | ||
455 | } | ||
456 | memcpy(curr->rdata, *cp, curr->size); | ||
457 | *cp += curr->size; | ||
458 | } | ||
459 | |||
460 | return (head); | ||
461 | } | ||
462 | |||
463 | static void | ||
464 | free_dns_query(struct dns_query *p) | ||
465 | { | ||
466 | if (p == NULL) | ||
467 | return; | ||
468 | |||
469 | if (p->name) | ||
470 | free(p->name); | ||
471 | free_dns_query(p->next); | ||
472 | free(p); | ||
473 | } | ||
474 | |||
475 | static void | ||
476 | free_dns_rr(struct dns_rr *p) | ||
477 | { | ||
478 | if (p == NULL) | ||
479 | return; | ||
480 | |||
481 | if (p->name) | ||
482 | free(p->name); | ||
483 | if (p->rdata) | ||
484 | free(p->rdata); | ||
485 | free_dns_rr(p->next); | ||
486 | free(p); | ||
487 | } | ||
488 | |||
489 | static void | ||
490 | free_dns_response(struct dns_response *p) | ||
491 | { | ||
492 | if (p == NULL) | ||
493 | return; | ||
494 | |||
495 | free_dns_query(p->query); | ||
496 | free_dns_rr(p->answer); | ||
497 | free_dns_rr(p->authority); | ||
498 | free_dns_rr(p->additional); | ||
499 | free(p); | ||
500 | } | ||
501 | |||
502 | static int | ||
503 | count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type) | ||
504 | { | ||
505 | int n = 0; | ||
506 | |||
507 | while(p) { | ||
508 | if (p->class == class && p->type == type) | ||
509 | n++; | ||
510 | p = p->next; | ||
511 | } | ||
512 | |||
513 | return (n); | ||
514 | } | ||