1 /*- 2 * Copyright (c) 1985, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * - 29 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 30 * 31 * Permission to use, copy, modify, and distribute this software for any 32 * purpose with or without fee is hereby granted, provided that the above 33 * copyright notice and this permission notice appear in all copies, and that 34 * the name of Digital Equipment Corporation not be used in advertising or 35 * publicity pertaining to distribution of the document or software without 36 * specific, written prior permission. 37 * 38 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 39 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 40 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 41 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 42 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 43 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 44 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 45 * SOFTWARE. 46 * - 47 * --Copyright-- 48 * 49 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93 50 * $FreeBSD: src/lib/libc/net/getnetbydns.c,v 1.34 2007/01/09 00:28:02 imp Exp $ 51 */ 52 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro 53 * Dep. Matematica Universidade de Coimbra, Portugal, Europe 54 * 55 * Permission to use, copy, modify, and distribute this software for any 56 * purpose with or without fee is hereby granted, provided that the above 57 * copyright notice and this permission notice appear in all copies. 58 */ 59 60 #include <sys/param.h> 61 #include <sys/socket.h> 62 #include <netinet/in.h> 63 #include <arpa/inet.h> 64 #include <arpa/nameser.h> 65 66 #include <errno.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <netdb.h> 70 #include <resolv.h> 71 #include <ctype.h> 72 #include <string.h> 73 #include <unistd.h> 74 #include <syslog.h> 75 #include <stdarg.h> 76 #include <nsswitch.h> 77 78 #include "netdb_private.h" 79 #include "res_config.h" 80 81 #define BYADDR 0 82 #define BYNAME 1 83 84 #define MAXPACKET (64*1024) 85 86 typedef union { 87 HEADER hdr; 88 u_char buf[MAXPACKET]; 89 } querybuf; 90 91 typedef union { 92 long al; 93 char ac; 94 } align; 95 96 /* 97 * Reverse the order of first four dotted entries of in. 98 * Out must contain space for at least strlen(in) characters. 99 * The result does not include any leading 0s of in. 100 */ 101 static void 102 ipreverse(char *in, char *out) 103 { 104 char *pos[4]; 105 int len[4]; 106 char *p, *start; 107 int i = 0; 108 int leading = 1; 109 110 /* Fill-in element positions and lengths: pos[], len[]. */ 111 start = p = in; 112 for (;;) { 113 if (*p == '.' || *p == '\0') { 114 /* Leading 0? */ 115 if (leading && p - start == 1 && *start == '0') 116 len[i] = 0; 117 else { 118 len[i] = p - start; 119 leading = 0; 120 } 121 pos[i] = start; 122 start = p + 1; 123 i++; 124 } 125 if (i == 4) 126 break; 127 if (*p == 0) { 128 for (; i < 4; i++) { 129 pos[i] = p; 130 len[i] = 0; 131 } 132 break; 133 } 134 p++; 135 } 136 137 /* Copy the entries in reverse order */ 138 p = out; 139 leading = 1; 140 for (i = 3; i >= 0; i--) { 141 memcpy(p, pos[i], len[i]); 142 if (len[i]) 143 leading = 0; 144 p += len[i]; 145 /* Need a . separator? */ 146 if (!leading && i > 0 && len[i - 1]) 147 *p++ = '.'; 148 } 149 *p = '\0'; 150 } 151 152 static int 153 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne, 154 struct netent_data *ned, res_state statp) 155 { 156 157 HEADER *hp; 158 u_char *cp; 159 int n; 160 u_char *eom; 161 int type, class, ancount, qdcount, haveanswer; 162 char aux[MAXHOSTNAMELEN]; 163 char ans[MAXHOSTNAMELEN]; 164 char *in, *bp, *ep, **ap; 165 166 /* 167 * find first satisfactory answer 168 * 169 * answer --> +------------+ ( MESSAGE ) 170 * | Header | 171 * +------------+ 172 * | Question | the question for the name server 173 * +------------+ 174 * | Answer | RRs answering the question 175 * +------------+ 176 * | Authority | RRs pointing toward an authority 177 * | Additional | RRs holding additional information 178 * +------------+ 179 */ 180 eom = answer->buf + anslen; 181 hp = &answer->hdr; 182 ancount = ntohs(hp->ancount); /* #/records in the answer section */ 183 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 184 bp = ned->netbuf; 185 ep = ned->netbuf + sizeof(ned->netbuf); 186 cp = answer->buf + HFIXEDSZ; 187 if (!qdcount) { 188 if (hp->aa) 189 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); 190 else 191 RES_SET_H_ERRNO(statp, TRY_AGAIN); 192 return (-1); 193 } 194 while (qdcount-- > 0) 195 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 196 ap = ned->net_aliases; 197 *ap = NULL; 198 ne->n_aliases = ned->net_aliases; 199 haveanswer = 0; 200 while (--ancount >= 0 && cp < eom) { 201 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 202 if ((n < 0) || !res_dnok(bp)) 203 break; 204 cp += n; 205 ans[0] = '\0'; 206 strncpy(&ans[0], bp, sizeof(ans) - 1); 207 ans[sizeof(ans) - 1] = '\0'; 208 GETSHORT(type, cp); 209 GETSHORT(class, cp); 210 cp += INT32SZ; /* TTL */ 211 GETSHORT(n, cp); 212 if (class == C_IN && type == T_PTR) { 213 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 214 if ((n < 0) || !res_hnok(bp)) { 215 cp += n; 216 return (-1); 217 } 218 cp += n; 219 *ap++ = bp; 220 n = strlen(bp) + 1; 221 bp += n; 222 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 223 haveanswer++; 224 } 225 } 226 if (haveanswer) { 227 *ap = NULL; 228 switch (net_i) { 229 case BYADDR: 230 ne->n_name = *ne->n_aliases; 231 ne->n_net = 0L; 232 break; 233 case BYNAME: 234 in = *ne->n_aliases; 235 n = strlen(ans) + 1; 236 if (ep - bp < n) { 237 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 238 errno = ENOBUFS; 239 return (-1); 240 } 241 strlcpy(bp, ans, ep - bp); 242 ne->n_name = bp; 243 if (strlen(in) + 1 > sizeof(aux)) { 244 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 245 errno = ENOBUFS; 246 return (-1); 247 } 248 ipreverse(in, aux); 249 ne->n_net = inet_network(aux); 250 break; 251 } 252 ne->n_aliases++; 253 return (0); 254 } 255 RES_SET_H_ERRNO(statp, TRY_AGAIN); 256 return (-1); 257 } 258 259 int 260 _dns_getnetbyaddr(void *rval, void *cb_data __unused, va_list ap) 261 { 262 uint32_t net; 263 int net_type; 264 char *buffer; 265 size_t buflen; 266 int *errnop, *h_errnop; 267 struct netent *nptr, ne; 268 struct netent_data *ned; 269 unsigned int netbr[4]; 270 int nn, anslen, error; 271 querybuf *buf; 272 char qbuf[MAXDNAME]; 273 uint32_t net2; 274 res_state statp; 275 276 net = va_arg(ap, uint32_t); 277 net_type = va_arg(ap, int); 278 nptr = va_arg(ap, struct netent *); 279 buffer = va_arg(ap, char *); 280 buflen = va_arg(ap, size_t); 281 errnop = va_arg(ap, int *); 282 h_errnop = va_arg(ap, int *); 283 284 statp = __res_state(); 285 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 286 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 287 *h_errnop = statp->res_h_errno; 288 return (NS_UNAVAIL); 289 } 290 291 if ((ned = __netent_data_init()) == NULL) { 292 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 293 *h_errnop = statp->res_h_errno; 294 return (NS_UNAVAIL); 295 } 296 297 *((struct netent **)rval) = NULL; 298 299 if (net_type != AF_INET) { 300 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 301 *h_errnop = statp->res_h_errno; 302 return (NS_UNAVAIL); 303 } 304 305 for (nn = 4, net2 = net; net2; net2 >>= 8) 306 netbr[--nn] = net2 & 0xff; 307 switch (nn) { 308 case 3: /* Class A */ 309 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 310 break; 311 case 2: /* Class B */ 312 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 313 break; 314 case 1: /* Class C */ 315 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 316 netbr[1]); 317 break; 318 case 0: /* Class D - E */ 319 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 320 netbr[1], netbr[0]); 321 break; 322 } 323 if ((buf = malloc(sizeof(*buf))) == NULL) { 324 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 325 *h_errnop = statp->res_h_errno; 326 return (NS_NOTFOUND); 327 } 328 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf, 329 sizeof(*buf)); 330 if (anslen < 0) { 331 free(buf); 332 #ifdef DEBUG 333 if (statp->options & RES_DEBUG) 334 printf("res_nsearch failed\n"); 335 #endif 336 *h_errnop = statp->res_h_errno; 337 return (NS_UNAVAIL); 338 } else if (anslen > sizeof(*buf)) { 339 free(buf); 340 #ifdef DEBUG 341 if (statp->options & RES_DEBUG) 342 printf("res_nsearch static buffer too small\n"); 343 #endif 344 *h_errnop = statp->res_h_errno; 345 return (NS_UNAVAIL); 346 } 347 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp); 348 free(buf); 349 if (error == 0) { 350 /* Strip trailing zeros */ 351 while ((net & 0xff) == 0 && net != 0) 352 net >>= 8; 353 ne.n_net = net; 354 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) { 355 *h_errnop = statp->res_h_errno; 356 return (NS_NOTFOUND); 357 } 358 *((struct netent **)rval) = nptr; 359 return (NS_SUCCESS); 360 } 361 *h_errnop = statp->res_h_errno; 362 return (NS_NOTFOUND); 363 } 364 365 int 366 _dns_getnetbyname(void *rval, void *cb_data __unused, va_list ap) 367 { 368 const char *net; 369 char *buffer; 370 size_t buflen; 371 int *errnop, *h_errnop; 372 struct netent *nptr, ne; 373 struct netent_data *ned; 374 int anslen, error; 375 querybuf *buf; 376 char qbuf[MAXDNAME]; 377 res_state statp; 378 379 net = va_arg(ap, const char *); 380 nptr = va_arg(ap, struct netent *); 381 buffer = va_arg(ap, char *); 382 buflen = va_arg(ap, size_t); 383 errnop = va_arg(ap, int *); 384 h_errnop = va_arg(ap, int *); 385 386 statp = __res_state(); 387 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 388 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 389 *h_errnop = statp->res_h_errno; 390 return (NS_UNAVAIL); 391 } 392 if ((ned = __netent_data_init()) == NULL) { 393 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 394 *h_errnop = statp->res_h_errno; 395 return (NS_UNAVAIL); 396 } 397 if ((buf = malloc(sizeof(*buf))) == NULL) { 398 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 399 *h_errnop = statp->res_h_errno; 400 return (NS_NOTFOUND); 401 } 402 403 *((struct netent **)rval) = NULL; 404 405 strncpy(qbuf, net, sizeof(qbuf) - 1); 406 qbuf[sizeof(qbuf) - 1] = '\0'; 407 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf, 408 sizeof(*buf)); 409 if (anslen < 0) { 410 free(buf); 411 #ifdef DEBUG 412 if (statp->options & RES_DEBUG) 413 printf("res_nsearch failed\n"); 414 #endif 415 return (NS_UNAVAIL); 416 } else if (anslen > sizeof(*buf)) { 417 free(buf); 418 #ifdef DEBUG 419 if (statp->options & RES_DEBUG) 420 printf("res_search static buffer too small\n"); 421 #endif 422 return (NS_UNAVAIL); 423 } 424 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp); 425 free(buf); 426 if (error != 0) { 427 *h_errnop = statp->res_h_errno; 428 return (NS_NOTFOUND); 429 } 430 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) { 431 *h_errnop = statp->res_h_errno; 432 return (NS_NOTFOUND); 433 } 434 *((struct netent **)rval) = nptr; 435 return (NS_SUCCESS); 436 } 437 438 void 439 _setnetdnsent(int stayopen) 440 { 441 res_state statp; 442 443 statp = __res_state(); 444 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) 445 return; 446 if (stayopen) 447 statp->options |= RES_STAYOPEN | RES_USEVC; 448 } 449 450 void 451 _endnetdnsent(void) 452 { 453 res_state statp; 454 455 statp = __res_state(); 456 statp->options &= ~(RES_STAYOPEN | RES_USEVC); 457 res_nclose(statp); 458 } 459