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