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