blob: 2307337a76f7039d1c4bb6ccee904230ac373356 [file] [log] [blame]
Damien Millere323df62003-05-18 22:24:09 +10001/* $OpenBSD: getrrsetbyname.c,v 1.7 2003/03/07 07:34:14 itojun Exp $ */
Damien Millerd9ec3702003-05-15 12:27:08 +10002
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 "includes.h"
47
Darren Tucker5f88d342003-10-15 16:57:57 +100048#ifndef HAVE_GETRRSETBYNAME
Damien Millerd9ec3702003-05-15 12:27:08 +100049
50#include "getrrsetbyname.h"
51
Damien Millere323df62003-05-18 22:24:09 +100052/* #include "thread_private.h" */
53
Damien Millerd9ec3702003-05-15 12:27:08 +100054#define ANSWER_BUFFER_SIZE 1024*64
55
56struct dns_query {
57 char *name;
58 u_int16_t type;
59 u_int16_t class;
60 struct dns_query *next;
61};
62
63struct 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
73struct 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
Damien Millere323df62003-05-18 22:24:09 +100081static struct dns_response *parse_dns_response(const u_char *, int);
82static struct dns_query *parse_dns_qsection(const u_char *, int,
83 const u_char **, int);
84static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
Damien Millerd9ec3702003-05-15 12:27:08 +100085 int);
86
87static void free_dns_query(struct dns_query *);
88static void free_dns_rr(struct dns_rr *);
89static void free_dns_response(struct dns_response *);
90
91static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
92
93/*
94 * Inline versions of get/put short/long. Pointer is advanced.
95 *
96 * These macros demonstrate the property of C whereby it can be
97 * portable or it can be elegant but rarely both.
98 */
99
100#ifndef INT32SZ
101# define INT32SZ 4
102#endif
103#ifndef INT16SZ
104# define INT16SZ 2
105#endif
106
107#ifndef GETSHORT
108#define GETSHORT(s, cp) { \
109 register u_char *t_cp = (u_char *)(cp); \
110 (s) = ((u_int16_t)t_cp[0] << 8) \
111 | ((u_int16_t)t_cp[1]) \
112 ; \
113 (cp) += INT16SZ; \
114}
115#endif
116
117#ifndef GETLONG
118#define GETLONG(l, cp) { \
119 register u_char *t_cp = (u_char *)(cp); \
120 (l) = ((u_int32_t)t_cp[0] << 24) \
121 | ((u_int32_t)t_cp[1] << 16) \
122 | ((u_int32_t)t_cp[2] << 8) \
123 | ((u_int32_t)t_cp[3]) \
124 ; \
125 (cp) += INT32SZ; \
126}
127#endif
128
129/*
130 * Routines to insert/extract short/long's.
131 */
132
Tim Rice0ac16a42003-09-08 06:33:33 -0700133#ifndef HAVE__GETSHORT
Damien Millerd9ec3702003-05-15 12:27:08 +1000134static u_int16_t
135_getshort(msgp)
136 register const u_char *msgp;
137{
138 register u_int16_t u;
139
140 GETSHORT(u, msgp);
141 return (u);
142}
Tim Rice0ac16a42003-09-08 06:33:33 -0700143#endif
Damien Millerd9ec3702003-05-15 12:27:08 +1000144
Tim Rice0ac16a42003-09-08 06:33:33 -0700145#ifndef HAVE__GETLONG
Damien Millerd9ec3702003-05-15 12:27:08 +1000146static u_int32_t
147_getlong(msgp)
148 register const u_char *msgp;
149{
150 register u_int32_t u;
151
152 GETLONG(u, msgp);
153 return (u);
154}
Tim Rice0ac16a42003-09-08 06:33:33 -0700155#endif
Damien Millerd9ec3702003-05-15 12:27:08 +1000156
157int
158getrrsetbyname(const char *hostname, unsigned int rdclass,
159 unsigned int rdtype, unsigned int flags,
160 struct rrsetinfo **res)
161{
Damien Millere323df62003-05-18 22:24:09 +1000162 struct __res_state *_resp = &_res;
Damien Millerd9ec3702003-05-15 12:27:08 +1000163 int result;
164 struct rrsetinfo *rrset = NULL;
165 struct dns_response *response;
166 struct dns_rr *rr;
167 struct rdatainfo *rdata;
Damien Millere323df62003-05-18 22:24:09 +1000168 int length;
169 unsigned int index_ans, index_sig;
170 u_char answer[ANSWER_BUFFER_SIZE];
Damien Millerd9ec3702003-05-15 12:27:08 +1000171
172 /* check for invalid class and type */
173 if (rdclass > 0xffff || rdtype > 0xffff) {
174 result = ERRSET_INVAL;
175 goto fail;
176 }
177
178 /* don't allow queries of class or type ANY */
179 if (rdclass == 0xff || rdtype == 0xff) {
180 result = ERRSET_INVAL;
181 goto fail;
182 }
183
184 /* don't allow flags yet, unimplemented */
185 if (flags) {
186 result = ERRSET_INVAL;
187 goto fail;
188 }
189
190 /* initialize resolver */
Damien Millere323df62003-05-18 22:24:09 +1000191 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
Damien Millerd9ec3702003-05-15 12:27:08 +1000192 result = ERRSET_FAIL;
193 goto fail;
194 }
195
196#ifdef DEBUG
Damien Millere323df62003-05-18 22:24:09 +1000197 _resp->options |= RES_DEBUG;
Damien Millerd9ec3702003-05-15 12:27:08 +1000198#endif /* DEBUG */
199
200#ifdef RES_USE_DNSSEC
201 /* turn on DNSSEC if EDNS0 is configured */
Damien Millere323df62003-05-18 22:24:09 +1000202 if (_resp->options & RES_USE_EDNS0)
203 _resp->options |= RES_USE_DNSSEC;
Damien Millerd9ec3702003-05-15 12:27:08 +1000204#endif /* RES_USE_DNSEC */
205
206 /* make query */
Damien Millere323df62003-05-18 22:24:09 +1000207 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
208 answer, sizeof(answer));
Damien Millerd9ec3702003-05-15 12:27:08 +1000209 if (length < 0) {
210 switch(h_errno) {
211 case HOST_NOT_FOUND:
212 result = ERRSET_NONAME;
213 goto fail;
214 case NO_DATA:
215 result = ERRSET_NODATA;
216 goto fail;
217 default:
218 result = ERRSET_FAIL;
219 goto fail;
220 }
221 }
222
223 /* parse result */
224 response = parse_dns_response(answer, length);
225 if (response == NULL) {
226 result = ERRSET_FAIL;
227 goto fail;
228 }
229
230 if (response->header.qdcount != 1) {
231 result = ERRSET_FAIL;
232 goto fail;
233 }
234
235 /* initialize rrset */
236 rrset = calloc(1, sizeof(struct rrsetinfo));
237 if (rrset == NULL) {
238 result = ERRSET_NOMEMORY;
239 goto fail;
240 }
241 rrset->rri_rdclass = response->query->class;
242 rrset->rri_rdtype = response->query->type;
243 rrset->rri_ttl = response->answer->ttl;
244 rrset->rri_nrdatas = response->header.ancount;
245
Tim Riceb284e162003-09-08 14:35:16 -0700246#ifdef HAVE_HEADER_AD
Damien Millerd9ec3702003-05-15 12:27:08 +1000247 /* check for authenticated data */
248 if (response->header.ad == 1)
249 rrset->rri_flags |= RRSET_VALIDATED;
Tim Riceb284e162003-09-08 14:35:16 -0700250#endif
Damien Millerd9ec3702003-05-15 12:27:08 +1000251
252 /* copy name from answer section */
253 length = strlen(response->answer->name);
254 rrset->rri_name = malloc(length + 1);
255 if (rrset->rri_name == NULL) {
256 result = ERRSET_NOMEMORY;
257 goto fail;
258 }
259 strlcpy(rrset->rri_name, response->answer->name, length + 1);
260
261 /* count answers */
262 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
263 rrset->rri_rdtype);
264 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
265 T_SIG);
266
267 /* allocate memory for answers */
268 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
269 sizeof(struct rdatainfo));
270 if (rrset->rri_rdatas == NULL) {
271 result = ERRSET_NOMEMORY;
272 goto fail;
273 }
274
275 /* allocate memory for signatures */
276 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
277 if (rrset->rri_sigs == NULL) {
278 result = ERRSET_NOMEMORY;
279 goto fail;
280 }
281
282 /* copy answers & signatures */
283 for (rr = response->answer, index_ans = 0, index_sig = 0;
284 rr; rr = rr->next) {
285
286 rdata = NULL;
287
288 if (rr->class == rrset->rri_rdclass &&
289 rr->type == rrset->rri_rdtype)
290 rdata = &rrset->rri_rdatas[index_ans++];
291
292 if (rr->class == rrset->rri_rdclass &&
293 rr->type == T_SIG)
294 rdata = &rrset->rri_sigs[index_sig++];
295
296 if (rdata) {
297 rdata->rdi_length = rr->size;
298 rdata->rdi_data = malloc(rr->size);
299
300 if (rdata->rdi_data == NULL) {
301 result = ERRSET_NOMEMORY;
302 goto fail;
303 }
304 memcpy(rdata->rdi_data, rr->rdata, rr->size);
305 }
306 }
307
308 *res = rrset;
309 return (ERRSET_SUCCESS);
310
311fail:
312 if (rrset != NULL)
313 freerrset(rrset);
314 return (result);
315}
316
317void
318freerrset(struct rrsetinfo *rrset)
319{
320 u_int16_t i;
321
322 if (rrset == NULL)
323 return;
324
325 if (rrset->rri_rdatas) {
326 for (i = 0; i < rrset->rri_nrdatas; i++) {
327 if (rrset->rri_rdatas[i].rdi_data == NULL)
328 break;
329 free(rrset->rri_rdatas[i].rdi_data);
330 }
331 free(rrset->rri_rdatas);
332 }
333
334 if (rrset->rri_sigs) {
335 for (i = 0; i < rrset->rri_nsigs; i++) {
336 if (rrset->rri_sigs[i].rdi_data == NULL)
337 break;
338 free(rrset->rri_sigs[i].rdi_data);
339 }
340 free(rrset->rri_sigs);
341 }
342
343 if (rrset->rri_name)
344 free(rrset->rri_name);
345 free(rrset);
346}
347
348/*
349 * DNS response parsing routines
350 */
351static struct dns_response *
Damien Millere323df62003-05-18 22:24:09 +1000352parse_dns_response(const u_char *answer, int size)
Damien Millerd9ec3702003-05-15 12:27:08 +1000353{
354 struct dns_response *resp;
Damien Millere323df62003-05-18 22:24:09 +1000355 const u_char *cp;
Damien Millerd9ec3702003-05-15 12:27:08 +1000356
357 /* allocate memory for the response */
358 resp = calloc(1, sizeof(*resp));
359 if (resp == NULL)
360 return (NULL);
361
362 /* initialize current pointer */
363 cp = answer;
364
365 /* copy header */
366 memcpy(&resp->header, cp, HFIXEDSZ);
367 cp += HFIXEDSZ;
368
369 /* fix header byte order */
370 resp->header.qdcount = ntohs(resp->header.qdcount);
371 resp->header.ancount = ntohs(resp->header.ancount);
372 resp->header.nscount = ntohs(resp->header.nscount);
373 resp->header.arcount = ntohs(resp->header.arcount);
374
375 /* there must be at least one query */
376 if (resp->header.qdcount < 1) {
377 free_dns_response(resp);
378 return (NULL);
379 }
380
381 /* parse query section */
382 resp->query = parse_dns_qsection(answer, size, &cp,
383 resp->header.qdcount);
384 if (resp->header.qdcount && resp->query == NULL) {
385 free_dns_response(resp);
386 return (NULL);
387 }
388
389 /* parse answer section */
390 resp->answer = parse_dns_rrsection(answer, size, &cp,
391 resp->header.ancount);
392 if (resp->header.ancount && resp->answer == NULL) {
393 free_dns_response(resp);
394 return (NULL);
395 }
396
397 /* parse authority section */
398 resp->authority = parse_dns_rrsection(answer, size, &cp,
399 resp->header.nscount);
400 if (resp->header.nscount && resp->authority == NULL) {
401 free_dns_response(resp);
402 return (NULL);
403 }
404
405 /* parse additional section */
406 resp->additional = parse_dns_rrsection(answer, size, &cp,
407 resp->header.arcount);
408 if (resp->header.arcount && resp->additional == NULL) {
409 free_dns_response(resp);
410 return (NULL);
411 }
412
413 return (resp);
414}
415
416static struct dns_query *
Damien Millere323df62003-05-18 22:24:09 +1000417parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
Damien Millerd9ec3702003-05-15 12:27:08 +1000418{
419 struct dns_query *head, *curr, *prev;
420 int i, length;
421 char name[MAXDNAME];
422
423 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
424
425 /* allocate and initialize struct */
426 curr = calloc(1, sizeof(struct dns_query));
427 if (curr == NULL) {
428 free_dns_query(head);
429 return (NULL);
430 }
431 if (head == NULL)
432 head = curr;
433 if (prev != NULL)
434 prev->next = curr;
435
436 /* name */
437 length = dn_expand(answer, answer + size, *cp, name,
438 sizeof(name));
439 if (length < 0) {
440 free_dns_query(head);
441 return (NULL);
442 }
443 curr->name = strdup(name);
444 if (curr->name == NULL) {
445 free_dns_query(head);
446 return (NULL);
447 }
448 *cp += length;
449
450 /* type */
451 curr->type = _getshort(*cp);
452 *cp += INT16SZ;
453
454 /* class */
455 curr->class = _getshort(*cp);
456 *cp += INT16SZ;
457 }
458
459 return (head);
460}
461
462static struct dns_rr *
Damien Millere323df62003-05-18 22:24:09 +1000463parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
Damien Millerd9ec3702003-05-15 12:27:08 +1000464{
465 struct dns_rr *head, *curr, *prev;
466 int i, length;
467 char name[MAXDNAME];
468
469 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
470
471 /* allocate and initialize struct */
472 curr = calloc(1, sizeof(struct dns_rr));
473 if (curr == NULL) {
474 free_dns_rr(head);
475 return (NULL);
476 }
477 if (head == NULL)
478 head = curr;
479 if (prev != NULL)
480 prev->next = curr;
481
482 /* name */
483 length = dn_expand(answer, answer + size, *cp, name,
484 sizeof(name));
485 if (length < 0) {
486 free_dns_rr(head);
487 return (NULL);
488 }
489 curr->name = strdup(name);
490 if (curr->name == NULL) {
491 free_dns_rr(head);
492 return (NULL);
493 }
494 *cp += length;
495
496 /* type */
497 curr->type = _getshort(*cp);
498 *cp += INT16SZ;
499
500 /* class */
501 curr->class = _getshort(*cp);
502 *cp += INT16SZ;
503
504 /* ttl */
505 curr->ttl = _getlong(*cp);
506 *cp += INT32SZ;
507
508 /* rdata size */
509 curr->size = _getshort(*cp);
510 *cp += INT16SZ;
511
512 /* rdata itself */
513 curr->rdata = malloc(curr->size);
514 if (curr->rdata == NULL) {
515 free_dns_rr(head);
516 return (NULL);
517 }
518 memcpy(curr->rdata, *cp, curr->size);
519 *cp += curr->size;
520 }
521
522 return (head);
523}
524
525static void
526free_dns_query(struct dns_query *p)
527{
528 if (p == NULL)
529 return;
530
531 if (p->name)
532 free(p->name);
533 free_dns_query(p->next);
534 free(p);
535}
536
537static void
538free_dns_rr(struct dns_rr *p)
539{
540 if (p == NULL)
541 return;
542
543 if (p->name)
544 free(p->name);
545 if (p->rdata)
546 free(p->rdata);
547 free_dns_rr(p->next);
548 free(p);
549}
550
551static void
552free_dns_response(struct dns_response *p)
553{
554 if (p == NULL)
555 return;
556
557 free_dns_query(p->query);
558 free_dns_rr(p->answer);
559 free_dns_rr(p->authority);
560 free_dns_rr(p->additional);
561 free(p);
562}
563
564static int
565count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
566{
567 int n = 0;
568
569 while(p) {
570 if (p->class == class && p->type == type)
571 n++;
572 p = p->next;
573 }
574
575 return (n);
576}
577
Darren Tuckerb8b4d0b2003-10-15 17:22:37 +1000578#endif /* !defined(HAVE_GETRRSETBYNAME) */