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 <stdio.h> 74 #include <stdlib.h> 75 #include <netdb.h> 76 #include <resolv.h> 77 #include <ctype.h> 78 #include <string.h> 79 #include <unistd.h> 80 #include <syslog.h> 81 #include <stdarg.h> 82 #include <nsswitch.h> 83 84 #include "res_config.h" 85 86 extern int h_errno; 87 88 #define BYADDR 0 89 #define BYNAME 1 90 #define MAXALIASES 35 91 92 #define MAXPACKET (64*1024) 93 94 typedef union { 95 HEADER hdr; 96 u_char buf[MAXPACKET]; 97 } querybuf; 98 99 typedef union { 100 long al; 101 char ac; 102 } align; 103 104 static struct netent * 105 getnetanswer(answer, anslen, net_i) 106 querybuf *answer; 107 int anslen; 108 int net_i; 109 { 110 111 HEADER *hp; 112 u_char *cp; 113 int n; 114 u_char *eom; 115 int type, class, ancount, qdcount, haveanswer, i, nchar; 116 char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; 117 char *in, *st, *pauxt, *bp, *ep, **ap; 118 char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; 119 static struct netent net_entry; 120 static char *net_aliases[MAXALIASES], netbuf[PACKETSZ]; 121 122 /* 123 * find first satisfactory answer 124 * 125 * answer --> +------------+ ( MESSAGE ) 126 * | Header | 127 * +------------+ 128 * | Question | the question for the name server 129 * +------------+ 130 * | Answer | RRs answering the question 131 * +------------+ 132 * | Authority | RRs pointing toward an authority 133 * | Additional | RRs holding additional information 134 * +------------+ 135 */ 136 eom = answer->buf + anslen; 137 hp = &answer->hdr; 138 ancount = ntohs(hp->ancount); /* #/records in the answer section */ 139 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 140 bp = netbuf; 141 ep = netbuf + sizeof(netbuf); 142 cp = answer->buf + HFIXEDSZ; 143 if (!qdcount) { 144 if (hp->aa) 145 h_errno = HOST_NOT_FOUND; 146 else 147 h_errno = TRY_AGAIN; 148 return (NULL); 149 } 150 while (qdcount-- > 0) 151 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 152 ap = net_aliases; 153 *ap = NULL; 154 net_entry.n_aliases = net_aliases; 155 haveanswer = 0; 156 while (--ancount >= 0 && cp < eom) { 157 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 158 if ((n < 0) || !res_dnok(bp)) 159 break; 160 cp += n; 161 ans[0] = '\0'; 162 (void)strncpy(&ans[0], bp, sizeof(ans) - 1); 163 ans[sizeof(ans) - 1] = '\0'; 164 GETSHORT(type, cp); 165 GETSHORT(class, cp); 166 cp += INT32SZ; /* TTL */ 167 GETSHORT(n, cp); 168 if (class == C_IN && type == T_PTR) { 169 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 170 if ((n < 0) || !res_hnok(bp)) { 171 cp += n; 172 return (NULL); 173 } 174 cp += n; 175 *ap++ = bp; 176 n = strlen(bp) + 1; 177 bp += n; 178 net_entry.n_addrtype = 179 (class == C_IN) ? AF_INET : AF_UNSPEC; 180 haveanswer++; 181 } 182 } 183 if (haveanswer) { 184 *ap = NULL; 185 switch (net_i) { 186 case BYADDR: 187 net_entry.n_name = *net_entry.n_aliases; 188 net_entry.n_net = 0L; 189 break; 190 case BYNAME: 191 in = *net_entry.n_aliases; 192 net_entry.n_name = &ans[0]; 193 aux2[0] = '\0'; 194 for (i = 0; i < 4; i++) { 195 for (st = in, nchar = 0; 196 *st != '.'; 197 st++, nchar++) 198 ; 199 if (nchar != 1 || *in != '0' || flag) { 200 flag = 1; 201 (void)strncpy(paux1, 202 (i==0) ? in : in-1, 203 (i==0) ?nchar : nchar+1); 204 paux1[(i==0) ? nchar : nchar+1] = '\0'; 205 pauxt = paux2; 206 paux2 = strcat(paux1, paux2); 207 paux1 = pauxt; 208 } 209 in = ++st; 210 } 211 net_entry.n_net = inet_network(paux2); 212 break; 213 } 214 net_entry.n_aliases++; 215 return (&net_entry); 216 } 217 h_errno = TRY_AGAIN; 218 return (NULL); 219 } 220 221 int 222 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap) 223 { 224 unsigned long net; 225 int net_type; 226 unsigned int netbr[4]; 227 int nn, anslen; 228 querybuf *buf; 229 char qbuf[MAXDNAME]; 230 unsigned long net2; 231 struct netent *net_entry; 232 233 net = va_arg(ap, unsigned long); 234 net_type = va_arg(ap, int); 235 236 *(struct netent **)rval = NULL; 237 238 if (net_type != AF_INET) 239 return NS_UNAVAIL; 240 241 for (nn = 4, net2 = net; net2; net2 >>= 8) 242 netbr[--nn] = net2 & 0xff; 243 switch (nn) { 244 case 3: /* Class A */ 245 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 246 break; 247 case 2: /* Class B */ 248 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 249 break; 250 case 1: /* Class C */ 251 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 252 netbr[1]); 253 break; 254 case 0: /* Class D - E */ 255 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 256 netbr[1], netbr[0]); 257 break; 258 } 259 if ((buf = malloc(sizeof(*buf))) == NULL) { 260 h_errno = NETDB_INTERNAL; 261 return NS_NOTFOUND; 262 } 263 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf)); 264 if (anslen < 0) { 265 free(buf); 266 #ifdef DEBUG 267 if (_res.options & RES_DEBUG) 268 printf("res_search failed\n"); 269 #endif 270 return NS_UNAVAIL; 271 } else if (anslen > sizeof(*buf)) { 272 free(buf); 273 #ifdef DEBUG 274 if (_res.options & RES_DEBUG) 275 printf("res_search static buffer too small\n"); 276 #endif 277 return NS_UNAVAIL; 278 } 279 net_entry = getnetanswer(buf, anslen, BYADDR); 280 free(buf); 281 if (net_entry) { 282 unsigned u_net = net; /* maybe net should be unsigned ? */ 283 284 /* Strip trailing zeros */ 285 while ((u_net & 0xff) == 0 && u_net != 0) 286 u_net >>= 8; 287 net_entry->n_net = u_net; 288 *(struct netent **)rval = net_entry; 289 return NS_SUCCESS; 290 } 291 return NS_NOTFOUND; 292 } 293 294 int 295 _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 296 { 297 const char *net; 298 int anslen; 299 querybuf *buf; 300 char qbuf[MAXDNAME]; 301 302 net = va_arg(ap, const char *); 303 304 *(struct netent**)rval = NULL; 305 306 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 307 h_errno = NETDB_INTERNAL; 308 return NS_UNAVAIL; 309 } 310 if ((buf = malloc(sizeof(*buf))) == NULL) { 311 h_errno = NETDB_INTERNAL; 312 return NS_NOTFOUND; 313 } 314 strncpy(qbuf, net, sizeof(qbuf) - 1); 315 qbuf[sizeof(qbuf) - 1] = '\0'; 316 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf)); 317 if (anslen < 0) { 318 free(buf); 319 #ifdef DEBUG 320 if (_res.options & RES_DEBUG) 321 printf("res_search failed\n"); 322 #endif 323 return NS_UNAVAIL; 324 } else if (anslen > sizeof(*buf)) { 325 free(buf); 326 #ifdef DEBUG 327 if (_res.options & RES_DEBUG) 328 printf("res_search static buffer too small\n"); 329 #endif 330 return NS_UNAVAIL; 331 } 332 *(struct netent**)rval = getnetanswer(buf, anslen, BYNAME); 333 free(buf); 334 return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 335 } 336 337 void 338 _setnetdnsent(stayopen) 339 int stayopen; 340 { 341 if (stayopen) 342 _res.options |= RES_STAYOPEN | RES_USEVC; 343 } 344 345 void 346 _endnetdnsent() 347 { 348 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 349 res_close(); 350 } 351