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 struct netent *ne; 272 struct netent_data *ned; 273 unsigned int netbr[4]; 274 int nn, anslen, error; 275 querybuf *buf; 276 char qbuf[MAXDNAME]; 277 uint32_t net2; 278 res_state statp; 279 280 net = va_arg(ap, uint32_t); 281 net_type = va_arg(ap, int); 282 ne = va_arg(ap, struct netent *); 283 ned = va_arg(ap, struct netent_data *); 284 285 if (net_type != AF_INET) 286 return NS_UNAVAIL; 287 288 statp = __res_state(); 289 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 290 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 291 return NS_UNAVAIL; 292 } 293 294 for (nn = 4, net2 = net; net2; net2 >>= 8) 295 netbr[--nn] = net2 & 0xff; 296 switch (nn) { 297 case 3: /* Class A */ 298 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 299 break; 300 case 2: /* Class B */ 301 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 302 break; 303 case 1: /* Class C */ 304 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 305 netbr[1]); 306 break; 307 case 0: /* Class D - E */ 308 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 309 netbr[1], netbr[0]); 310 break; 311 } 312 if ((buf = malloc(sizeof(*buf))) == NULL) { 313 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 314 return NS_NOTFOUND; 315 } 316 anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf, 317 sizeof(*buf)); 318 if (anslen < 0) { 319 free(buf); 320 #ifdef DEBUG 321 if (statp->options & RES_DEBUG) 322 printf("res_nsearch failed\n"); 323 #endif 324 return NS_UNAVAIL; 325 } else if (anslen > sizeof(*buf)) { 326 free(buf); 327 #ifdef DEBUG 328 if (statp->options & RES_DEBUG) 329 printf("res_nsearch static buffer too small\n"); 330 #endif 331 return NS_UNAVAIL; 332 } 333 error = getnetanswer(buf, anslen, BYADDR, ne, ned, statp); 334 free(buf); 335 if (error == 0) { 336 /* Strip trailing zeros */ 337 while ((net & 0xff) == 0 && net != 0) 338 net >>= 8; 339 ne->n_net = net; 340 return NS_SUCCESS; 341 } 342 return NS_NOTFOUND; 343 } 344 345 int 346 _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 347 { 348 const char *net; 349 struct netent *ne; 350 struct netent_data *ned; 351 int anslen, error; 352 querybuf *buf; 353 char qbuf[MAXDNAME]; 354 res_state statp; 355 356 net = va_arg(ap, const char *); 357 ne = va_arg(ap, struct netent *); 358 ned = va_arg(ap, struct netent_data *); 359 360 statp = __res_state(); 361 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) { 362 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 363 return NS_UNAVAIL; 364 } 365 if ((buf = malloc(sizeof(*buf))) == NULL) { 366 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 367 return NS_NOTFOUND; 368 } 369 strncpy(qbuf, net, sizeof(qbuf) - 1); 370 qbuf[sizeof(qbuf) - 1] = '\0'; 371 anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf, 372 sizeof(*buf)); 373 if (anslen < 0) { 374 free(buf); 375 #ifdef DEBUG 376 if (statp->options & RES_DEBUG) 377 printf("res_nsearch failed\n"); 378 #endif 379 return NS_UNAVAIL; 380 } else if (anslen > sizeof(*buf)) { 381 free(buf); 382 #ifdef DEBUG 383 if (statp->options & RES_DEBUG) 384 printf("res_search static buffer too small\n"); 385 #endif 386 return NS_UNAVAIL; 387 } 388 error = getnetanswer(buf, anslen, BYNAME, ne, ned, statp); 389 free(buf); 390 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND; 391 } 392 393 void 394 _setnetdnsent(stayopen) 395 int stayopen; 396 { 397 res_state statp; 398 399 statp = __res_state(); 400 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) 401 return; 402 if (stayopen) 403 statp->options |= RES_STAYOPEN | RES_USEVC; 404 } 405 406 void 407 _endnetdnsent() 408 { 409 res_state statp; 410 411 statp = __res_state(); 412 statp->options &= ~(RES_STAYOPEN | RES_USEVC); 413 res_nclose(statp); 414 } 415