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 * $DragonFly: src/lib/libc/net/getnetbydns.c,v 1.5 2005/11/13 02:04:47 swildner Exp $ 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 #include <sys/param.h> 62 #include <sys/socket.h> 63 #include <netinet/in.h> 64 #include <arpa/inet.h> 65 #include <arpa/nameser.h> 66 67 #include <errno.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <netdb.h> 71 #include <resolv.h> 72 #include <ctype.h> 73 #include <string.h> 74 #include <unistd.h> 75 #include <syslog.h> 76 #include <stdarg.h> 77 #include <nsswitch.h> 78 79 #include "netdb_private.h" 80 #include "res_config.h" 81 82 #define BYADDR 0 83 #define BYNAME 1 84 85 #define MAXPACKET (64*1024) 86 87 typedef union { 88 HEADER hdr; 89 u_char buf[MAXPACKET]; 90 } querybuf; 91 92 typedef union { 93 long al; 94 char ac; 95 } align; 96 97 /* 98 * Reverse the order of first four dotted entries of in. 99 * Out must contain space for at least strlen(in) characters. 100 * The result does not include any leading 0s of in. 101 */ 102 static void 103 ipreverse(char *in, char *out) 104 { 105 char *pos[4]; 106 int len[4]; 107 char *p, *start; 108 int i = 0; 109 int leading = 1; 110 111 /* Fill-in element positions and lengths: pos[], len[]. */ 112 start = p = in; 113 for (;;) { 114 if (*p == '.' || *p == '\0') { 115 /* Leading 0? */ 116 if (leading && p - start == 1 && *start == '0') 117 len[i] = 0; 118 else { 119 len[i] = p - start; 120 leading = 0; 121 } 122 pos[i] = start; 123 start = p + 1; 124 i++; 125 } 126 if (i == 4) 127 break; 128 if (*p == 0) { 129 for (; i < 4; i++) { 130 pos[i] = p; 131 len[i] = 0; 132 } 133 break; 134 } 135 p++; 136 } 137 138 /* Copy the entries in reverse order */ 139 p = out; 140 leading = 1; 141 for (i = 3; i >= 0; i--) { 142 memcpy(p, pos[i], len[i]); 143 if (len[i]) 144 leading = 0; 145 p += len[i]; 146 /* Need a . separator? */ 147 if (!leading && i > 0 && len[i - 1]) 148 *p++ = '.'; 149 } 150 *p = '\0'; 151 } 152 153 static int 154 getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne, 155 struct netent_data *ned, res_state statp) 156 { 157 158 HEADER *hp; 159 u_char *cp; 160 int n; 161 u_char *eom; 162 int type, class, ancount, qdcount, haveanswer; 163 char aux[MAXHOSTNAMELEN]; 164 char ans[MAXHOSTNAMELEN]; 165 char *in, *bp, *ep, **ap; 166 167 /* 168 * find first satisfactory answer 169 * 170 * answer --> +------------+ ( MESSAGE ) 171 * | Header | 172 * +------------+ 173 * | Question | the question for the name server 174 * +------------+ 175 * | Answer | RRs answering the question 176 * +------------+ 177 * | Authority | RRs pointing toward an authority 178 * | Additional | RRs holding additional information 179 * +------------+ 180 */ 181 eom = answer->buf + anslen; 182 hp = &answer->hdr; 183 ancount = ntohs(hp->ancount); /* #/records in the answer section */ 184 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 185 bp = ned->netbuf; 186 ep = ned->netbuf + sizeof(ned->netbuf); 187 cp = answer->buf + HFIXEDSZ; 188 if (!qdcount) { 189 if (hp->aa) 190 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); 191 else 192 RES_SET_H_ERRNO(statp, TRY_AGAIN); 193 return (-1); 194 } 195 while (qdcount-- > 0) 196 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 197 ap = ned->net_aliases; 198 *ap = NULL; 199 ne->n_aliases = ned->net_aliases; 200 haveanswer = 0; 201 while (--ancount >= 0 && cp < eom) { 202 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 203 if ((n < 0) || !res_dnok(bp)) 204 break; 205 cp += n; 206 ans[0] = '\0'; 207 strncpy(&ans[0], bp, sizeof(ans) - 1); 208 ans[sizeof(ans) - 1] = '\0'; 209 GETSHORT(type, cp); 210 GETSHORT(class, cp); 211 cp += INT32SZ; /* TTL */ 212 GETSHORT(n, cp); 213 if (class == C_IN && type == T_PTR) { 214 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 215 if ((n < 0) || !res_hnok(bp)) { 216 cp += n; 217 return (-1); 218 } 219 cp += n; 220 *ap++ = bp; 221 n = strlen(bp) + 1; 222 bp += n; 223 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 224 haveanswer++; 225 } 226 } 227 if (haveanswer) { 228 *ap = NULL; 229 switch (net_i) { 230 case BYADDR: 231 ne->n_name = *ne->n_aliases; 232 ne->n_net = 0L; 233 break; 234 case BYNAME: 235 in = *ne->n_aliases; 236 n = strlen(ans) + 1; 237 if (ep - bp < n) { 238 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 239 errno = ENOBUFS; 240 return (-1); 241 } 242 strlcpy(bp, ans, ep - bp); 243 ne->n_name = bp; 244 if (strlen(in) + 1 > sizeof(aux)) { 245 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 246 errno = ENOBUFS; 247 return (-1); 248 } 249 ipreverse(in, aux); 250 ne->n_net = inet_network(aux); 251 break; 252 } 253 ne->n_aliases++; 254 return (0); 255 } 256 RES_SET_H_ERRNO(statp, TRY_AGAIN); 257 return (-1); 258 } 259 260 int 261 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap) 262 { 263 uint32_t net; 264 int net_type; 265 char *buffer; 266 size_t buflen; 267 int *errnop, *h_errnop; 268 struct netent *nptr, ne; 269 struct netent_data *ned; 270 unsigned int netbr[4]; 271 int nn, anslen, error; 272 querybuf *buf; 273 char qbuf[MAXDNAME]; 274 uint32_t net2; 275 res_state statp; 276 277 net = va_arg(ap, uint32_t); 278 net_type = va_arg(ap, int); 279 nptr = va_arg(ap, struct netent *); 280 buffer = va_arg(ap, char *); 281 buflen = va_arg(ap, size_t); 282 errnop = va_arg(ap, int *); 283 h_errnop = va_arg(ap, int *); 284 285 statp = __res_state(); 286 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 287 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 288 *h_errnop = statp->res_h_errno; 289 return (NS_UNAVAIL); 290 } 291 292 if ((ned = __netent_data_init()) == NULL) { 293 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 294 *h_errnop = statp->res_h_errno; 295 return (NS_UNAVAIL); 296 } 297 298 *((struct netent **)rval) = NULL; 299 300 if (net_type != AF_INET) { 301 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 302 *h_errnop = statp->res_h_errno; 303 return (NS_UNAVAIL); 304 } 305 306 for (nn = 4, net2 = net; net2; net2 >>= 8) 307 netbr[--nn] = net2 & 0xff; 308 switch (nn) { 309 case 3: /* Class A */ 310 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 311 break; 312 case 2: /* Class B */ 313 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 314 break; 315 case 1: /* Class C */ 316 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 317 netbr[1]); 318 break; 319 case 0: /* Class D - E */ 320 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 321 netbr[1], netbr[0]); 322 break; 323 } 324 if ((buf = malloc(sizeof(*buf))) == NULL) { 325 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 326 *h_errnop = statp->res_h_errno; 327 return (NS_NOTFOUND); 328 } 329 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf, 330 sizeof(*buf)); 331 if (anslen < 0) { 332 free(buf); 333 #ifdef DEBUG 334 if (statp->options & RES_DEBUG) 335 printf("res_nsearch failed\n"); 336 #endif 337 *h_errnop = statp->res_h_errno; 338 return (NS_UNAVAIL); 339 } else if (anslen > sizeof(*buf)) { 340 free(buf); 341 #ifdef DEBUG 342 if (statp->options & RES_DEBUG) 343 printf("res_nsearch static buffer too small\n"); 344 #endif 345 *h_errnop = statp->res_h_errno; 346 return (NS_UNAVAIL); 347 } 348 error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp); 349 free(buf); 350 if (error == 0) { 351 /* Strip trailing zeros */ 352 while ((net & 0xff) == 0 && net != 0) 353 net >>= 8; 354 ne.n_net = net; 355 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) { 356 *h_errnop = statp->res_h_errno; 357 return (NS_NOTFOUND); 358 } 359 *((struct netent **)rval) = nptr; 360 return (NS_SUCCESS); 361 } 362 *h_errnop = statp->res_h_errno; 363 return (NS_NOTFOUND); 364 } 365 366 int 367 _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 368 { 369 const char *net; 370 char *buffer; 371 size_t buflen; 372 int *errnop, *h_errnop; 373 struct netent *nptr, ne; 374 struct netent_data *ned; 375 int anslen, error; 376 querybuf *buf; 377 char qbuf[MAXDNAME]; 378 res_state statp; 379 380 net = va_arg(ap, const char *); 381 nptr = va_arg(ap, struct netent *); 382 buffer = va_arg(ap, char *); 383 buflen = va_arg(ap, size_t); 384 errnop = va_arg(ap, int *); 385 h_errnop = va_arg(ap, int *); 386 387 statp = __res_state(); 388 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 389 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 390 *h_errnop = statp->res_h_errno; 391 return (NS_UNAVAIL); 392 } 393 if ((ned = __netent_data_init()) == NULL) { 394 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 395 *h_errnop = statp->res_h_errno; 396 return (NS_UNAVAIL); 397 } 398 if ((buf = malloc(sizeof(*buf))) == NULL) { 399 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 400 *h_errnop = statp->res_h_errno; 401 return (NS_NOTFOUND); 402 } 403 404 *((struct netent **)rval) = NULL; 405 406 strncpy(qbuf, net, sizeof(qbuf) - 1); 407 qbuf[sizeof(qbuf) - 1] = '\0'; 408 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf, 409 sizeof(*buf)); 410 if (anslen < 0) { 411 free(buf); 412 #ifdef DEBUG 413 if (statp->options & RES_DEBUG) 414 printf("res_nsearch failed\n"); 415 #endif 416 return (NS_UNAVAIL); 417 } else if (anslen > sizeof(*buf)) { 418 free(buf); 419 #ifdef DEBUG 420 if (statp->options & RES_DEBUG) 421 printf("res_search static buffer too small\n"); 422 #endif 423 return (NS_UNAVAIL); 424 } 425 error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp); 426 free(buf); 427 if (error != 0) { 428 *h_errnop = statp->res_h_errno; 429 return (NS_NOTFOUND); 430 } 431 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) { 432 *h_errnop = statp->res_h_errno; 433 return (NS_NOTFOUND); 434 } 435 *((struct netent **)rval) = nptr; 436 return (NS_SUCCESS); 437 } 438 439 void 440 _setnetdnsent(int stayopen) 441 { 442 res_state statp; 443 444 statp = __res_state(); 445 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) 446 return; 447 if (stayopen) 448 statp->options |= RES_STAYOPEN | RES_USEVC; 449 } 450 451 void 452 _endnetdnsent(void) 453 { 454 res_state statp; 455 456 statp = __res_state(); 457 statp->options &= ~(RES_STAYOPEN | RES_USEVC); 458 res_nclose(statp); 459 } 460