diff options
Diffstat (limited to 'src/lib/libc/net/getrrsetbyname.c')
-rw-r--r-- | src/lib/libc/net/getrrsetbyname.c | 507 |
1 files changed, 507 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..87ae8fc229 --- /dev/null +++ b/src/lib/libc/net/getrrsetbyname.c | |||
@@ -0,0 +1,507 @@ | |||
1 | /* $OpenBSD: getrrsetbyname.c,v 1.4 2001/08/16 18:16:43 ho 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 | #define ANSWER_BUFFER_SIZE 1024*64 | ||
55 | |||
56 | struct dns_query { | ||
57 | char *name; | ||
58 | u_int16_t type; | ||
59 | u_int16_t class; | ||
60 | struct dns_query *next; | ||
61 | }; | ||
62 | |||
63 | struct dns_rr { | ||
64 | char *name; | ||
65 | u_int16_t type; | ||
66 | u_int16_t class; | ||
67 | u_int16_t ttl; | ||
68 | u_int16_t size; | ||
69 | void *rdata; | ||
70 | struct dns_rr *next; | ||
71 | }; | ||
72 | |||
73 | struct dns_response { | ||
74 | HEADER header; | ||
75 | struct dns_query *query; | ||
76 | struct dns_rr *answer; | ||
77 | struct dns_rr *authority; | ||
78 | struct dns_rr *additional; | ||
79 | }; | ||
80 | |||
81 | static struct dns_response *parse_dns_response(const char *, int); | ||
82 | static struct dns_query *parse_dns_qsection(const char *, int, const char **, | ||
83 | int); | ||
84 | static struct dns_rr *parse_dns_rrsection(const char *, int, const char **, | ||
85 | int); | ||
86 | |||
87 | static void free_dns_query(struct dns_query *); | ||
88 | static void free_dns_rr(struct dns_rr *); | ||
89 | static void free_dns_response(struct dns_response *); | ||
90 | |||
91 | static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t); | ||
92 | |||
93 | int | ||
94 | getrrsetbyname(const char *hostname, unsigned int rdclass, | ||
95 | unsigned int rdtype, unsigned int flags, | ||
96 | struct rrsetinfo **res) | ||
97 | { | ||
98 | int result; | ||
99 | struct rrsetinfo *rrset = NULL; | ||
100 | struct dns_response *response; | ||
101 | struct dns_rr *rr; | ||
102 | struct rdatainfo *rdata; | ||
103 | unsigned int length, index_ans, index_sig; | ||
104 | char answer[ANSWER_BUFFER_SIZE]; | ||
105 | |||
106 | /* check for invalid class and type */ | ||
107 | if (rdclass > 0xffff || rdtype > 0xffff) { | ||
108 | result = ERRSET_INVAL; | ||
109 | goto fail; | ||
110 | } | ||
111 | |||
112 | /* don't allow queries of class or type ANY */ | ||
113 | if (rdclass == 0xff || rdtype == 0xff) { | ||
114 | result = ERRSET_INVAL; | ||
115 | goto fail; | ||
116 | } | ||
117 | |||
118 | /* don't allow flags yet, unimplemented */ | ||
119 | if (flags) { | ||
120 | result = ERRSET_INVAL; | ||
121 | goto fail; | ||
122 | } | ||
123 | |||
124 | /* initialize resolver */ | ||
125 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | ||
126 | result = ERRSET_FAIL; | ||
127 | goto fail; | ||
128 | } | ||
129 | |||
130 | #ifdef DEBUG | ||
131 | _res.options |= RES_DEBUG; | ||
132 | #endif /* DEBUG */ | ||
133 | |||
134 | #ifdef RES_USE_DNSSEC | ||
135 | /* turn on DNSSEC if EDNS0 is configured */ | ||
136 | if (_res.options & RES_USE_EDNS0) | ||
137 | _res.options |= RES_USE_DNSSEC; | ||
138 | #endif /* RES_USE_DNSEC */ | ||
139 | |||
140 | /* make query */ | ||
141 | length = res_query(hostname, rdclass, rdtype, answer, sizeof(answer)); | ||
142 | if (length < 0) { | ||
143 | switch(h_errno) { | ||
144 | case HOST_NOT_FOUND: | ||
145 | result = ERRSET_NONAME; | ||
146 | goto fail; | ||
147 | case NO_DATA: | ||
148 | result = ERRSET_NODATA; | ||
149 | goto fail; | ||
150 | default: | ||
151 | result = ERRSET_FAIL; | ||
152 | goto fail; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | /* parse result */ | ||
157 | response = parse_dns_response(answer, length); | ||
158 | if (response == NULL) { | ||
159 | result = ERRSET_FAIL; | ||
160 | goto fail; | ||
161 | } | ||
162 | |||
163 | if (response->header.qdcount != 1) { | ||
164 | result = ERRSET_FAIL; | ||
165 | goto fail; | ||
166 | } | ||
167 | |||
168 | /* initialize rrset */ | ||
169 | rrset = calloc(1, sizeof(struct rrsetinfo)); | ||
170 | if (rrset == NULL) { | ||
171 | result = ERRSET_NOMEMORY; | ||
172 | goto fail; | ||
173 | } | ||
174 | rrset->rri_rdclass = response->query->class; | ||
175 | rrset->rri_rdtype = response->query->type; | ||
176 | rrset->rri_ttl = response->answer->ttl; | ||
177 | rrset->rri_nrdatas = response->header.ancount; | ||
178 | |||
179 | /* check for authenticated data */ | ||
180 | if (response->header.ad == 1) | ||
181 | rrset->rri_flags |= RRSET_VALIDATED; | ||
182 | |||
183 | /* copy name from answer section */ | ||
184 | length = strlen(response->answer->name); | ||
185 | rrset->rri_name = malloc(length + 1); | ||
186 | if (rrset->rri_name == NULL) { | ||
187 | result = ERRSET_NOMEMORY; | ||
188 | goto fail; | ||
189 | } | ||
190 | strlcpy(rrset->rri_name, response->answer->name, length + 1); | ||
191 | |||
192 | /* count answers */ | ||
193 | rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass, | ||
194 | rrset->rri_rdtype); | ||
195 | rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass, | ||
196 | T_SIG); | ||
197 | |||
198 | /* allocate memory for answers */ | ||
199 | rrset->rri_rdatas = calloc(rrset->rri_nrdatas, | ||
200 | sizeof(struct rdatainfo)); | ||
201 | if (rrset->rri_rdatas == NULL) { | ||
202 | result = ERRSET_NOMEMORY; | ||
203 | goto fail; | ||
204 | } | ||
205 | |||
206 | /* allocate memory for signatures */ | ||
207 | rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo)); | ||
208 | if (rrset->rri_sigs == NULL) { | ||
209 | result = ERRSET_NOMEMORY; | ||
210 | goto fail; | ||
211 | } | ||
212 | |||
213 | /* copy answers & signatures */ | ||
214 | for (rr = response->answer, index_ans = 0, index_sig = 0; | ||
215 | rr; rr = rr->next) { | ||
216 | |||
217 | rdata = NULL; | ||
218 | |||
219 | if (rr->class == rrset->rri_rdclass && | ||
220 | rr->type == rrset->rri_rdtype) | ||
221 | rdata = &rrset->rri_rdatas[index_ans++]; | ||
222 | |||
223 | if (rr->class == rrset->rri_rdclass && | ||
224 | rr->type == T_SIG) | ||
225 | rdata = &rrset->rri_sigs[index_sig++]; | ||
226 | |||
227 | if (rdata) { | ||
228 | rdata->rdi_length = rr->size; | ||
229 | rdata->rdi_data = malloc(rr->size); | ||
230 | |||
231 | if (rdata->rdi_data == NULL) { | ||
232 | result = ERRSET_NOMEMORY; | ||
233 | goto fail; | ||
234 | } | ||
235 | memcpy(rdata->rdi_data, rr->rdata, rr->size); | ||
236 | } | ||
237 | } | ||
238 | |||
239 | *res = rrset; | ||
240 | return (ERRSET_SUCCESS); | ||
241 | |||
242 | fail: | ||
243 | if (rrset != NULL) | ||
244 | freerrset(rrset); | ||
245 | return (result); | ||
246 | } | ||
247 | |||
248 | void | ||
249 | freerrset(struct rrsetinfo *rrset) | ||
250 | { | ||
251 | u_int16_t i; | ||
252 | |||
253 | if (rrset == NULL) | ||
254 | return; | ||
255 | |||
256 | if (rrset->rri_rdatas) { | ||
257 | for (i = 0; i < rrset->rri_nrdatas; i++) { | ||
258 | if (rrset->rri_rdatas[i].rdi_data == NULL) | ||
259 | break; | ||
260 | free(rrset->rri_rdatas[i].rdi_data); | ||
261 | } | ||
262 | free(rrset->rri_rdatas); | ||
263 | } | ||
264 | |||
265 | if (rrset->rri_sigs) { | ||
266 | for (i = 0; i < rrset->rri_nsigs; i++) { | ||
267 | if (rrset->rri_sigs[i].rdi_data == NULL) | ||
268 | break; | ||
269 | free(rrset->rri_sigs[i].rdi_data); | ||
270 | } | ||
271 | free(rrset->rri_sigs); | ||
272 | } | ||
273 | |||
274 | if (rrset->rri_name) | ||
275 | free(rrset->rri_name); | ||
276 | free(rrset); | ||
277 | } | ||
278 | |||
279 | /* | ||
280 | * DNS response parsing routines | ||
281 | */ | ||
282 | static struct dns_response * | ||
283 | parse_dns_response(const char *answer, int size) | ||
284 | { | ||
285 | struct dns_response *resp; | ||
286 | const char *cp; | ||
287 | |||
288 | /* allocate memory for the response */ | ||
289 | resp = calloc(1, sizeof(*resp)); | ||
290 | if (resp == NULL) | ||
291 | return (NULL); | ||
292 | |||
293 | /* initialize current pointer */ | ||
294 | cp = answer; | ||
295 | |||
296 | /* copy header */ | ||
297 | memcpy(&resp->header, cp, HFIXEDSZ); | ||
298 | cp += HFIXEDSZ; | ||
299 | |||
300 | /* fix header byte order */ | ||
301 | resp->header.qdcount = ntohs(resp->header.qdcount); | ||
302 | resp->header.ancount = ntohs(resp->header.ancount); | ||
303 | resp->header.nscount = ntohs(resp->header.nscount); | ||
304 | resp->header.arcount = ntohs(resp->header.arcount); | ||
305 | |||
306 | /* there must be at least one query */ | ||
307 | if (resp->header.qdcount < 1) { | ||
308 | free_dns_response(resp); | ||
309 | return (NULL); | ||
310 | } | ||
311 | |||
312 | /* parse query section */ | ||
313 | resp->query = parse_dns_qsection(answer, size, &cp, | ||
314 | resp->header.qdcount); | ||
315 | if (resp->header.qdcount && resp->query == NULL) { | ||
316 | free_dns_response(resp); | ||
317 | return (NULL); | ||
318 | } | ||
319 | |||
320 | /* parse answer section */ | ||
321 | resp->answer = parse_dns_rrsection(answer, size, &cp, | ||
322 | resp->header.ancount); | ||
323 | if (resp->header.ancount && resp->answer == NULL) { | ||
324 | free_dns_response(resp); | ||
325 | return (NULL); | ||
326 | } | ||
327 | |||
328 | /* parse authority section */ | ||
329 | resp->authority = parse_dns_rrsection(answer, size, &cp, | ||
330 | resp->header.nscount); | ||
331 | if (resp->header.nscount && resp->authority == NULL) { | ||
332 | free_dns_response(resp); | ||
333 | return (NULL); | ||
334 | } | ||
335 | |||
336 | /* parse additional section */ | ||
337 | resp->additional = parse_dns_rrsection(answer, size, &cp, | ||
338 | resp->header.arcount); | ||
339 | if (resp->header.arcount && resp->additional == NULL) { | ||
340 | free_dns_response(resp); | ||
341 | return (NULL); | ||
342 | } | ||
343 | |||
344 | return (resp); | ||
345 | } | ||
346 | |||
347 | static struct dns_query * | ||
348 | parse_dns_qsection(const char *answer, int size, const char **cp, int count) | ||
349 | { | ||
350 | struct dns_query *head, *curr, *prev; | ||
351 | int i, length; | ||
352 | char name[MAXDNAME]; | ||
353 | |||
354 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { | ||
355 | |||
356 | /* allocate and initialize struct */ | ||
357 | curr = calloc(1, sizeof(struct dns_query)); | ||
358 | if (curr == NULL) { | ||
359 | free_dns_query(head); | ||
360 | return (NULL); | ||
361 | } | ||
362 | if (head == NULL) | ||
363 | head = curr; | ||
364 | if (prev != NULL) | ||
365 | prev->next = curr; | ||
366 | |||
367 | /* name */ | ||
368 | length = dn_expand(answer, answer + size, *cp, name, | ||
369 | sizeof(name)); | ||
370 | if (length < 0) { | ||
371 | free_dns_query(head); | ||
372 | return (NULL); | ||
373 | } | ||
374 | curr->name = strdup(name); | ||
375 | if (curr->name == NULL) { | ||
376 | free_dns_query(head); | ||
377 | return (NULL); | ||
378 | } | ||
379 | *cp += length; | ||
380 | |||
381 | /* type */ | ||
382 | curr->type = _getshort(*cp); | ||
383 | *cp += INT16SZ; | ||
384 | |||
385 | /* class */ | ||
386 | curr->class = _getshort(*cp); | ||
387 | *cp += INT16SZ; | ||
388 | } | ||
389 | |||
390 | return (head); | ||
391 | } | ||
392 | |||
393 | static struct dns_rr * | ||
394 | parse_dns_rrsection(const char *answer, int size, const char **cp, int count) | ||
395 | { | ||
396 | struct dns_rr *head, *curr, *prev; | ||
397 | int i, length; | ||
398 | char name[MAXDNAME]; | ||
399 | |||
400 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { | ||
401 | |||
402 | /* allocate and initialize struct */ | ||
403 | curr = calloc(1, sizeof(struct dns_rr)); | ||
404 | if (curr == NULL) { | ||
405 | free_dns_rr(head); | ||
406 | return (NULL); | ||
407 | } | ||
408 | if (head == NULL) | ||
409 | head = curr; | ||
410 | if (prev != NULL) | ||
411 | prev->next = curr; | ||
412 | |||
413 | /* name */ | ||
414 | length = dn_expand(answer, answer + size, *cp, name, | ||
415 | sizeof(name)); | ||
416 | if (length < 0) { | ||
417 | free_dns_rr(head); | ||
418 | return (NULL); | ||
419 | } | ||
420 | curr->name = strdup(name); | ||
421 | if (curr->name == NULL) { | ||
422 | free_dns_rr(head); | ||
423 | return (NULL); | ||
424 | } | ||
425 | *cp += length; | ||
426 | |||
427 | /* type */ | ||
428 | curr->type = _getshort(*cp); | ||
429 | *cp += INT16SZ; | ||
430 | |||
431 | /* class */ | ||
432 | curr->class = _getshort(*cp); | ||
433 | *cp += INT16SZ; | ||
434 | |||
435 | /* ttl */ | ||
436 | curr->ttl = _getlong(*cp); | ||
437 | *cp += INT32SZ; | ||
438 | |||
439 | /* rdata size */ | ||
440 | curr->size = _getshort(*cp); | ||
441 | *cp += INT16SZ; | ||
442 | |||
443 | /* rdata itself */ | ||
444 | curr->rdata = malloc(curr->size); | ||
445 | if (curr->rdata == NULL) { | ||
446 | free_dns_rr(head); | ||
447 | return (NULL); | ||
448 | } | ||
449 | memcpy(curr->rdata, *cp, curr->size); | ||
450 | *cp += curr->size; | ||
451 | } | ||
452 | |||
453 | return (head); | ||
454 | } | ||
455 | |||
456 | static void | ||
457 | free_dns_query(struct dns_query *p) | ||
458 | { | ||
459 | if (p == NULL) | ||
460 | return; | ||
461 | |||
462 | if (p->name) | ||
463 | free(p->name); | ||
464 | free_dns_query(p->next); | ||
465 | free(p); | ||
466 | } | ||
467 | |||
468 | static void | ||
469 | free_dns_rr(struct dns_rr *p) | ||
470 | { | ||
471 | if (p == NULL) | ||
472 | return; | ||
473 | |||
474 | if (p->name) | ||
475 | free(p->name); | ||
476 | if (p->rdata) | ||
477 | free(p->rdata); | ||
478 | free_dns_rr(p->next); | ||
479 | free(p); | ||
480 | } | ||
481 | |||
482 | static void | ||
483 | free_dns_response(struct dns_response *p) | ||
484 | { | ||
485 | if (p == NULL) | ||
486 | return; | ||
487 | |||
488 | free_dns_query(p->query); | ||
489 | free_dns_rr(p->answer); | ||
490 | free_dns_rr(p->authority); | ||
491 | free_dns_rr(p->additional); | ||
492 | free(p); | ||
493 | } | ||
494 | |||
495 | static int | ||
496 | count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type) | ||
497 | { | ||
498 | int n = 0; | ||
499 | |||
500 | while(p) { | ||
501 | if (p->class == class && p->type == type) | ||
502 | n++; | ||
503 | p = p->next; | ||
504 | } | ||
505 | |||
506 | return (n); | ||
507 | } | ||