1 /* $NetBSD: dns_nw.c,v 1.1.1.1 2009/04/12 15:33:37 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (c) 1996-1999 by Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #if defined(LIBC_SCCS) && !defined(lint) 21 static const char rcsid[] = "Id: dns_nw.c,v 1.12 2005/04/27 04:56:22 sra Exp"; 22 #endif /* LIBC_SCCS and not lint */ 23 24 /* Imports. */ 25 26 #include "port_before.h" 27 28 #include <sys/param.h> 29 #include <sys/socket.h> 30 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 #include <arpa/nameser.h> 34 35 #include <ctype.h> 36 #include <errno.h> 37 #include <netdb.h> 38 #include <resolv.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include <isc/memcluster.h> 44 #include <irs.h> 45 46 #include "port_after.h" 47 48 #include "irs_p.h" 49 #include "dns_p.h" 50 51 #ifdef SPRINTF_CHAR 52 # define SPRINTF(x) strlen(sprintf/**/x) 53 #else 54 # define SPRINTF(x) sprintf x 55 #endif 56 57 /* Definitions. */ 58 59 #define MAXALIASES 35 60 61 #define MAXPACKET (64*1024) 62 63 struct pvt { 64 struct nwent net; 65 char * ali[MAXALIASES]; 66 char buf[BUFSIZ+1]; 67 struct __res_state * res; 68 void (*free_res)(void *); 69 }; 70 71 typedef union { 72 long al; 73 char ac; 74 } align; 75 76 enum by_what { by_addr, by_name }; 77 78 /* Forwards. */ 79 80 static void nw_close(struct irs_nw *); 81 static struct nwent * nw_byname(struct irs_nw *, const char *, int); 82 static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); 83 static struct nwent * nw_next(struct irs_nw *); 84 static void nw_rewind(struct irs_nw *); 85 static void nw_minimize(struct irs_nw *); 86 static struct __res_state * nw_res_get(struct irs_nw *this); 87 static void nw_res_set(struct irs_nw *this, 88 struct __res_state *res, 89 void (*free_res)(void *)); 90 91 static struct nwent * get1101byaddr(struct irs_nw *, u_char *, int); 92 static struct nwent * get1101byname(struct irs_nw *, const char *); 93 static struct nwent * get1101answer(struct irs_nw *, 94 u_char *ansbuf, int anslen, 95 enum by_what by_what, 96 int af, const char *name, 97 const u_char *addr, int addrlen); 98 static struct nwent * get1101mask(struct irs_nw *this, struct nwent *); 99 static int make1101inaddr(const u_char *, int, char *, int); 100 static void normalize_name(char *name); 101 static int init(struct irs_nw *this); 102 103 /* Exports. */ 104 105 struct irs_nw * 106 irs_dns_nw(struct irs_acc *this) { 107 struct irs_nw *nw; 108 struct pvt *pvt; 109 110 UNUSED(this); 111 112 if (!(pvt = memget(sizeof *pvt))) { 113 errno = ENOMEM; 114 return (NULL); 115 } 116 memset(pvt, 0, sizeof *pvt); 117 if (!(nw = memget(sizeof *nw))) { 118 memput(pvt, sizeof *pvt); 119 errno = ENOMEM; 120 return (NULL); 121 } 122 memset(nw, 0x5e, sizeof *nw); 123 nw->private = pvt; 124 nw->close = nw_close; 125 nw->byname = nw_byname; 126 nw->byaddr = nw_byaddr; 127 nw->next = nw_next; 128 nw->rewind = nw_rewind; 129 nw->minimize = nw_minimize; 130 nw->res_get = nw_res_get; 131 nw->res_set = nw_res_set; 132 return (nw); 133 } 134 135 /* Methods. */ 136 137 static void 138 nw_close(struct irs_nw *this) { 139 struct pvt *pvt = (struct pvt *)this->private; 140 141 nw_minimize(this); 142 143 if (pvt->res && pvt->free_res) 144 (*pvt->free_res)(pvt->res); 145 146 memput(pvt, sizeof *pvt); 147 memput(this, sizeof *this); 148 } 149 150 static struct nwent * 151 nw_byname(struct irs_nw *this, const char *name, int af) { 152 struct pvt *pvt = (struct pvt *)this->private; 153 154 if (init(this) == -1) 155 return (NULL); 156 157 switch (af) { 158 case AF_INET: 159 return (get1101byname(this, name)); 160 default: 161 (void)NULL; 162 } 163 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 164 errno = EAFNOSUPPORT; 165 return (NULL); 166 } 167 168 static struct nwent * 169 nw_byaddr(struct irs_nw *this, void *net, int len, int af) { 170 struct pvt *pvt = (struct pvt *)this->private; 171 172 if (init(this) == -1) 173 return (NULL); 174 175 switch (af) { 176 case AF_INET: 177 return (get1101byaddr(this, net, len)); 178 default: 179 (void)NULL; 180 } 181 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 182 errno = EAFNOSUPPORT; 183 return (NULL); 184 } 185 186 static struct nwent * 187 nw_next(struct irs_nw *this) { 188 189 UNUSED(this); 190 191 return (NULL); 192 } 193 194 static void 195 nw_rewind(struct irs_nw *this) { 196 UNUSED(this); 197 /* NOOP */ 198 } 199 200 static void 201 nw_minimize(struct irs_nw *this) { 202 struct pvt *pvt = (struct pvt *)this->private; 203 204 if (pvt->res) 205 res_nclose(pvt->res); 206 } 207 208 static struct __res_state * 209 nw_res_get(struct irs_nw *this) { 210 struct pvt *pvt = (struct pvt *)this->private; 211 212 if (!pvt->res) { 213 struct __res_state *res; 214 res = (struct __res_state *)malloc(sizeof *res); 215 if (!res) { 216 errno = ENOMEM; 217 return (NULL); 218 } 219 memset(res, 0, sizeof *res); 220 nw_res_set(this, res, free); 221 } 222 223 return (pvt->res); 224 } 225 226 static void 227 nw_res_set(struct irs_nw *this, struct __res_state *res, 228 void (*free_res)(void *)) { 229 struct pvt *pvt = (struct pvt *)this->private; 230 231 if (pvt->res && pvt->free_res) { 232 res_nclose(pvt->res); 233 (*pvt->free_res)(pvt->res); 234 } 235 236 pvt->res = res; 237 pvt->free_res = free_res; 238 } 239 240 /* Private. */ 241 242 static struct nwent * 243 get1101byname(struct irs_nw *this, const char *name) { 244 struct pvt *pvt = (struct pvt *)this->private; 245 u_char *ansbuf; 246 int anslen; 247 struct nwent *result; 248 249 ansbuf = memget(MAXPACKET); 250 if (ansbuf == NULL) { 251 errno = ENOMEM; 252 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 253 return (NULL); 254 } 255 anslen = res_nsearch(pvt->res, name, C_IN, T_PTR, ansbuf, MAXPACKET); 256 if (anslen < 0) { 257 memput(ansbuf, MAXPACKET); 258 return (NULL); 259 } 260 result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_name, 261 AF_INET, name, NULL, 0)); 262 memput(ansbuf, MAXPACKET); 263 return (result); 264 } 265 266 static struct nwent * 267 get1101byaddr(struct irs_nw *this, u_char *net, int len) { 268 struct pvt *pvt = (struct pvt *)this->private; 269 char qbuf[sizeof "255.255.255.255.in-addr.arpa"]; 270 struct nwent *result; 271 u_char *ansbuf; 272 int anslen; 273 274 if (len < 1 || len > 32) { 275 errno = EINVAL; 276 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 277 return (NULL); 278 } 279 if (make1101inaddr(net, len, qbuf, sizeof qbuf) < 0) 280 return (NULL); 281 ansbuf = memget(MAXPACKET); 282 if (ansbuf == NULL) { 283 errno = ENOMEM; 284 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 285 return (NULL); 286 } 287 anslen = res_nquery(pvt->res, qbuf, C_IN, T_PTR, ansbuf, MAXPACKET); 288 if (anslen < 0) { 289 memput(ansbuf, MAXPACKET); 290 return (NULL); 291 } 292 result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_addr, 293 AF_INET, NULL, net, len)); 294 memput(ansbuf, MAXPACKET); 295 return (result); 296 } 297 298 static struct nwent * 299 get1101answer(struct irs_nw *this, 300 u_char *ansbuf, int anslen, enum by_what by_what, 301 int af, const char *name, const u_char *addr, int addrlen) 302 { 303 struct pvt *pvt = (struct pvt *)this->private; 304 int type, class, ancount, qdcount, haveanswer; 305 char *bp, *ep, **ap; 306 u_char *cp, *eom; 307 HEADER *hp; 308 309 /* Initialize, and parse header. */ 310 eom = ansbuf + anslen; 311 if (ansbuf + HFIXEDSZ > eom) { 312 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 313 return (NULL); 314 } 315 hp = (HEADER *)ansbuf; 316 cp = ansbuf + HFIXEDSZ; 317 qdcount = ntohs(hp->qdcount); 318 while (qdcount-- > 0) { 319 int n = dn_skipname(cp, eom); 320 cp += n + QFIXEDSZ; 321 if (n < 0 || cp > eom) { 322 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 323 return (NULL); 324 } 325 } 326 ancount = ntohs(hp->ancount); 327 if (!ancount) { 328 if (hp->aa) 329 RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); 330 else 331 RES_SET_H_ERRNO(pvt->res, TRY_AGAIN); 332 return (NULL); 333 } 334 335 /* Prepare a return structure. */ 336 bp = pvt->buf; 337 ep = pvt->buf + sizeof(pvt->buf); 338 pvt->net.n_name = NULL; 339 pvt->net.n_aliases = pvt->ali; 340 pvt->net.n_addrtype = af; 341 pvt->net.n_addr = NULL; 342 pvt->net.n_length = addrlen; 343 344 /* Save input key if given. */ 345 switch (by_what) { 346 case by_name: 347 if (name != NULL) { 348 int n = strlen(name) + 1; 349 350 if (n > (ep - bp)) { 351 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 352 return (NULL); 353 } 354 pvt->net.n_name = strcpy(bp, name); /* (checked) */ 355 bp += n; 356 } 357 break; 358 case by_addr: 359 if (addr != NULL && addrlen != 0) { 360 int n = addrlen / 8 + ((addrlen % 8) != 0); 361 362 if (INADDRSZ > (ep - bp)) { 363 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 364 return (NULL); 365 } 366 memset(bp, 0, INADDRSZ); 367 memcpy(bp, addr, n); 368 pvt->net.n_addr = bp; 369 bp += INADDRSZ; 370 } 371 break; 372 default: 373 abort(); 374 } 375 376 /* Parse the answer, collect aliases. */ 377 ap = pvt->ali; 378 haveanswer = 0; 379 while (--ancount >= 0 && cp < eom) { 380 int n = dn_expand(ansbuf, eom, cp, bp, ep - bp); 381 382 cp += n; /*%< Owner */ 383 if (n < 0 || !maybe_dnok(pvt->res, bp) || 384 cp + 3 * INT16SZ + INT32SZ > eom) { 385 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 386 return (NULL); 387 } 388 GETSHORT(type, cp); /*%< Type */ 389 GETSHORT(class, cp); /*%< Class */ 390 cp += INT32SZ; /*%< TTL */ 391 GETSHORT(n, cp); /*%< RDLENGTH */ 392 if (class == C_IN && type == T_PTR) { 393 int nn; 394 395 nn = dn_expand(ansbuf, eom, cp, bp, ep - bp); 396 if (nn < 0 || !maybe_hnok(pvt->res, bp) || nn != n) { 397 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 398 return (NULL); 399 } 400 normalize_name(bp); 401 switch (by_what) { 402 case by_addr: { 403 if (pvt->net.n_name == NULL) 404 pvt->net.n_name = bp; 405 else if (ns_samename(pvt->net.n_name, bp) == 1) 406 break; 407 else 408 *ap++ = bp; 409 nn = strlen(bp) + 1; 410 bp += nn; 411 haveanswer++; 412 break; 413 } 414 case by_name: { 415 u_int b1, b2, b3, b4; 416 417 if (pvt->net.n_addr != NULL || 418 sscanf(bp, "%u.%u.%u.%u.in-addr.arpa", 419 &b1, &b2, &b3, &b4) != 4) 420 break; 421 if ((ep - bp) < INADDRSZ) { 422 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 423 return (NULL); 424 } 425 pvt->net.n_addr = bp; 426 *bp++ = b4; 427 *bp++ = b3; 428 *bp++ = b2; 429 *bp++ = b1; 430 pvt->net.n_length = INADDRSZ * 8; 431 haveanswer++; 432 } 433 } 434 } 435 cp += n; /*%< RDATA */ 436 } 437 if (!haveanswer) { 438 RES_SET_H_ERRNO(pvt->res, TRY_AGAIN); 439 return (NULL); 440 } 441 *ap = NULL; 442 443 return (&pvt->net); 444 } 445 446 static struct nwent * 447 get1101mask(struct irs_nw *this, struct nwent *nwent) { 448 struct pvt *pvt = (struct pvt *)this->private; 449 char qbuf[sizeof "255.255.255.255.in-addr.arpa"], owner[MAXDNAME]; 450 int anslen, type, class, ancount, qdcount; 451 u_char *ansbuf, *cp, *eom; 452 HEADER *hp; 453 454 if (!nwent) 455 return (NULL); 456 if (make1101inaddr(nwent->n_addr, nwent->n_length, qbuf, sizeof qbuf) 457 < 0) { 458 /* "First, do no harm." */ 459 return (nwent); 460 } 461 462 ansbuf = memget(MAXPACKET); 463 if (ansbuf == NULL) { 464 errno = ENOMEM; 465 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 466 return (NULL); 467 } 468 /* Query for the A RR that would hold this network's mask. */ 469 anslen = res_nquery(pvt->res, qbuf, C_IN, T_A, ansbuf, MAXPACKET); 470 if (anslen < HFIXEDSZ) { 471 memput(ansbuf, MAXPACKET); 472 return (nwent); 473 } 474 475 /* Initialize, and parse header. */ 476 hp = (HEADER *)ansbuf; 477 cp = ansbuf + HFIXEDSZ; 478 eom = ansbuf + anslen; 479 qdcount = ntohs(hp->qdcount); 480 while (qdcount-- > 0) { 481 int n = dn_skipname(cp, eom); 482 cp += n + QFIXEDSZ; 483 if (n < 0 || cp > eom) { 484 memput(ansbuf, MAXPACKET); 485 return (nwent); 486 } 487 } 488 ancount = ntohs(hp->ancount); 489 490 /* Parse the answer, collect aliases. */ 491 while (--ancount >= 0 && cp < eom) { 492 int n = dn_expand(ansbuf, eom, cp, owner, sizeof owner); 493 494 if (n < 0 || !maybe_dnok(pvt->res, owner)) 495 break; 496 cp += n; /*%< Owner */ 497 if (cp + 3 * INT16SZ + INT32SZ > eom) 498 break; 499 GETSHORT(type, cp); /*%< Type */ 500 GETSHORT(class, cp); /*%< Class */ 501 cp += INT32SZ; /*%< TTL */ 502 GETSHORT(n, cp); /*%< RDLENGTH */ 503 if (cp + n > eom) 504 break; 505 if (n == INADDRSZ && class == C_IN && type == T_A && 506 ns_samename(qbuf, owner) == 1) { 507 /* This A RR indicates the actual netmask. */ 508 int nn, mm; 509 510 nwent->n_length = 0; 511 for (nn = 0; nn < INADDRSZ; nn++) 512 for (mm = 7; mm >= 0; mm--) 513 if (cp[nn] & (1 << mm)) 514 nwent->n_length++; 515 else 516 break; 517 } 518 cp += n; /*%< RDATA */ 519 } 520 memput(ansbuf, MAXPACKET); 521 return (nwent); 522 } 523 524 static int 525 make1101inaddr(const u_char *net, int bits, char *name, int size) { 526 int n, m; 527 char *ep; 528 529 ep = name + size; 530 531 /* Zero fill any whole bytes left out of the prefix. */ 532 for (n = (32 - bits) / 8; n > 0; n--) { 533 if (ep - name < (int)(sizeof "0.")) 534 goto emsgsize; 535 m = SPRINTF((name, "0.")); 536 name += m; 537 } 538 539 /* Format the partial byte, if any, within the prefix. */ 540 if ((n = bits % 8) != 0) { 541 if (ep - name < (int)(sizeof "255.")) 542 goto emsgsize; 543 m = SPRINTF((name, "%u.", 544 net[bits / 8] & ~((1 << (8 - n)) - 1))); 545 name += m; 546 } 547 548 /* Format the whole bytes within the prefix. */ 549 for (n = bits / 8; n > 0; n--) { 550 if (ep - name < (int)(sizeof "255.")) 551 goto emsgsize; 552 m = SPRINTF((name, "%u.", net[n - 1])); 553 name += m; 554 } 555 556 /* Add the static text. */ 557 if (ep - name < (int)(sizeof "in-addr.arpa")) 558 goto emsgsize; 559 (void) SPRINTF((name, "in-addr.arpa")); 560 return (0); 561 562 emsgsize: 563 errno = EMSGSIZE; 564 return (-1); 565 } 566 567 static void 568 normalize_name(char *name) { 569 char *t; 570 571 /* Make lower case. */ 572 for (t = name; *t; t++) 573 if (isascii((unsigned char)*t) && isupper((unsigned char)*t)) 574 *t = tolower((*t)&0xff); 575 576 /* Remove trailing dots. */ 577 while (t > name && t[-1] == '.') 578 *--t = '\0'; 579 } 580 581 static int 582 init(struct irs_nw *this) { 583 struct pvt *pvt = (struct pvt *)this->private; 584 585 if (!pvt->res && !nw_res_get(this)) 586 return (-1); 587 if (((pvt->res->options & RES_INIT) == 0U) && 588 res_ninit(pvt->res) == -1) 589 return (-1); 590 return (0); 591 } 592 593 /*! \file */ 594