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