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