1 | /* |
---|
2 | * Copyright (c) 1985, 1989, 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 | |
---|
30 | /* |
---|
31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
---|
32 | * |
---|
33 | * Permission to use, copy, modify, and distribute this software for any |
---|
34 | * purpose with or without fee is hereby granted, provided that the above |
---|
35 | * copyright notice and this permission notice appear in all copies, and that |
---|
36 | * the name of Digital Equipment Corporation not be used in advertising or |
---|
37 | * publicity pertaining to distribution of the document or software without |
---|
38 | * specific, written prior permission. |
---|
39 | * |
---|
40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
---|
41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
---|
42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
---|
43 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
---|
44 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
---|
45 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
46 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
---|
47 | * SOFTWARE. |
---|
48 | */ |
---|
49 | |
---|
50 | /* |
---|
51 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
---|
52 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
---|
53 | * |
---|
54 | * Permission to use, copy, modify, and distribute this software for any |
---|
55 | * purpose with or without fee is hereby granted, provided that the above |
---|
56 | * copyright notice and this permission notice appear in all copies. |
---|
57 | * |
---|
58 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
---|
59 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
60 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
---|
61 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
62 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
63 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
---|
64 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
65 | */ |
---|
66 | |
---|
67 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
68 | static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; |
---|
69 | static const char rcsid[] = "$Id: res_init.c,v 1.16.18.7 2007/07/09 01:52:58 marka Exp $"; |
---|
70 | #endif /* LIBC_SCCS and not lint */ |
---|
71 | #include <sys/cdefs.h> |
---|
72 | __FBSDID("$FreeBSD$"); |
---|
73 | |
---|
74 | #include "port_before.h" |
---|
75 | |
---|
76 | #include "namespace.h" |
---|
77 | |
---|
78 | #include <rtems/bsd/sys/types.h> |
---|
79 | #include <rtems/bsd/sys/param.h> |
---|
80 | #include <sys/socket.h> |
---|
81 | #include <rtems/bsd/sys/time.h> |
---|
82 | |
---|
83 | #include <netinet/in.h> |
---|
84 | #include <arpa/inet.h> |
---|
85 | #include <arpa/nameser.h> |
---|
86 | |
---|
87 | #include <ctype.h> |
---|
88 | #include <stdio.h> |
---|
89 | #include <stdlib.h> |
---|
90 | #include <string.h> |
---|
91 | #include <unistd.h> |
---|
92 | #include <netdb.h> |
---|
93 | |
---|
94 | #include "un-namespace.h" |
---|
95 | |
---|
96 | #include "port_after.h" |
---|
97 | |
---|
98 | /* ensure that sockaddr_in6 and IN6ADDR_ANY_INIT are declared / defined */ |
---|
99 | #include <resolv.h> |
---|
100 | |
---|
101 | #include "res_private.h" |
---|
102 | |
---|
103 | /*% Options. Should all be left alone. */ |
---|
104 | #define RESOLVSORT |
---|
105 | #define DEBUG |
---|
106 | |
---|
107 | #ifdef SOLARIS2 |
---|
108 | #include <sys/systeminfo.h> |
---|
109 | #endif |
---|
110 | |
---|
111 | static void res_setoptions(res_state, const char *, const char *); |
---|
112 | |
---|
113 | #ifdef RESOLVSORT |
---|
114 | static const char sort_mask[] = "/&"; |
---|
115 | #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) |
---|
116 | static u_int32_t net_mask(struct in_addr); |
---|
117 | #endif |
---|
118 | |
---|
119 | #if !defined(isascii) /*%< XXX - could be a function */ |
---|
120 | # define isascii(c) (!(c & 0200)) |
---|
121 | #endif |
---|
122 | |
---|
123 | /* |
---|
124 | * Resolver state default settings. |
---|
125 | */ |
---|
126 | |
---|
127 | /*% |
---|
128 | * Set up default settings. If the configuration file exist, the values |
---|
129 | * there will have precedence. Otherwise, the server address is set to |
---|
130 | * INADDR_ANY and the default domain name comes from the gethostname(). |
---|
131 | * |
---|
132 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 |
---|
133 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address |
---|
134 | * since it was noted that INADDR_ANY actually meant ``the first interface |
---|
135 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, |
---|
136 | * it had to be "up" in order for you to reach your own name server. It |
---|
137 | * was later decided that since the recommended practice is to always |
---|
138 | * install local static routes through 127.0.0.1 for all your network |
---|
139 | * interfaces, that we could solve this problem without a code change. |
---|
140 | * |
---|
141 | * The configuration file should always be used, since it is the only way |
---|
142 | * to specify a default domain. If you are running a server on your local |
---|
143 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" |
---|
144 | * in the configuration file. |
---|
145 | * |
---|
146 | * Return 0 if completes successfully, -1 on error |
---|
147 | */ |
---|
148 | int |
---|
149 | res_ninit(res_state statp) { |
---|
150 | extern int __res_vinit(res_state, int); |
---|
151 | |
---|
152 | return (__res_vinit(statp, 0)); |
---|
153 | } |
---|
154 | |
---|
155 | /*% This function has to be reachable by res_data.c but not publically. */ |
---|
156 | int |
---|
157 | __res_vinit(res_state statp, int preinit) { |
---|
158 | FILE *fp; |
---|
159 | char *cp, **pp; |
---|
160 | int n; |
---|
161 | char buf[BUFSIZ]; |
---|
162 | int nserv = 0; /*%< number of nameserver records read from file */ |
---|
163 | int haveenv = 0; |
---|
164 | int havesearch = 0; |
---|
165 | #ifdef RESOLVSORT |
---|
166 | int nsort = 0; |
---|
167 | char *net; |
---|
168 | #endif |
---|
169 | int dots; |
---|
170 | union res_sockaddr_union u[2]; |
---|
171 | int maxns = MAXNS; |
---|
172 | |
---|
173 | RES_SET_H_ERRNO(statp, 0); |
---|
174 | if (statp->_u._ext.ext != NULL) |
---|
175 | res_ndestroy(statp); |
---|
176 | |
---|
177 | if (!preinit) { |
---|
178 | statp->retrans = RES_TIMEOUT; |
---|
179 | statp->retry = RES_DFLRETRY; |
---|
180 | statp->options = RES_DEFAULT; |
---|
181 | statp->id = res_randomid(); |
---|
182 | } |
---|
183 | |
---|
184 | memset(u, 0, sizeof(u)); |
---|
185 | #ifdef USELOOPBACK |
---|
186 | u[nserv].sin.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); |
---|
187 | #else |
---|
188 | u[nserv].sin.sin_addr.s_addr = INADDR_ANY; |
---|
189 | #endif |
---|
190 | u[nserv].sin.sin_family = AF_INET; |
---|
191 | u[nserv].sin.sin_port = htons(NAMESERVER_PORT); |
---|
192 | #ifdef HAVE_SA_LEN |
---|
193 | u[nserv].sin.sin_len = sizeof(struct sockaddr_in); |
---|
194 | #endif |
---|
195 | nserv++; |
---|
196 | #ifdef HAS_INET6_STRUCTS |
---|
197 | #ifdef USELOOPBACK |
---|
198 | u[nserv].sin6.sin6_addr = in6addr_loopback; |
---|
199 | #else |
---|
200 | u[nserv].sin6.sin6_addr = in6addr_any; |
---|
201 | #endif |
---|
202 | u[nserv].sin6.sin6_family = AF_INET6; |
---|
203 | u[nserv].sin6.sin6_port = htons(NAMESERVER_PORT); |
---|
204 | #ifdef HAVE_SA_LEN |
---|
205 | u[nserv].sin6.sin6_len = sizeof(struct sockaddr_in6); |
---|
206 | #endif |
---|
207 | nserv++; |
---|
208 | #endif |
---|
209 | statp->nscount = 0; |
---|
210 | statp->ndots = 1; |
---|
211 | statp->pfcode = 0; |
---|
212 | statp->_vcsock = -1; |
---|
213 | statp->_flags = 0; |
---|
214 | statp->qhook = NULL; |
---|
215 | statp->rhook = NULL; |
---|
216 | statp->_u._ext.nscount = 0; |
---|
217 | statp->_u._ext.ext = malloc(sizeof(*statp->_u._ext.ext)); |
---|
218 | if (statp->_u._ext.ext != NULL) { |
---|
219 | memset(statp->_u._ext.ext, 0, sizeof(*statp->_u._ext.ext)); |
---|
220 | statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; |
---|
221 | strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); |
---|
222 | strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); |
---|
223 | } else { |
---|
224 | /* |
---|
225 | * Historically res_init() rarely, if at all, failed. |
---|
226 | * Examples and applications exist which do not check |
---|
227 | * our return code. Furthermore several applications |
---|
228 | * simply call us to get the systems domainname. So |
---|
229 | * rather then immediately fail here we store the |
---|
230 | * failure, which is returned later, in h_errno. And |
---|
231 | * prevent the collection of 'nameserver' information |
---|
232 | * by setting maxns to 0. Thus applications that fail |
---|
233 | * to check our return code wont be able to make |
---|
234 | * queries anyhow. |
---|
235 | */ |
---|
236 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
237 | maxns = 0; |
---|
238 | } |
---|
239 | #ifdef RESOLVSORT |
---|
240 | statp->nsort = 0; |
---|
241 | #endif |
---|
242 | res_setservers(statp, u, nserv); |
---|
243 | |
---|
244 | #ifdef SOLARIS2 |
---|
245 | /* |
---|
246 | * The old libresolv derived the defaultdomain from NIS/NIS+. |
---|
247 | * We want to keep this behaviour |
---|
248 | */ |
---|
249 | { |
---|
250 | char buf[sizeof(statp->defdname)], *cp; |
---|
251 | int ret; |
---|
252 | |
---|
253 | if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 && |
---|
254 | (unsigned int)ret <= sizeof(buf)) { |
---|
255 | if (buf[0] == '+') |
---|
256 | buf[0] = '.'; |
---|
257 | cp = strchr(buf, '.'); |
---|
258 | cp = (cp == NULL) ? buf : (cp + 1); |
---|
259 | strncpy(statp->defdname, cp, |
---|
260 | sizeof(statp->defdname) - 1); |
---|
261 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
262 | } |
---|
263 | } |
---|
264 | #endif /* SOLARIS2 */ |
---|
265 | |
---|
266 | /* Allow user to override the local domain definition */ |
---|
267 | if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { |
---|
268 | (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
---|
269 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
270 | haveenv++; |
---|
271 | |
---|
272 | /* |
---|
273 | * Set search list to be blank-separated strings |
---|
274 | * from rest of env value. Permits users of LOCALDOMAIN |
---|
275 | * to still have a search list, and anyone to set the |
---|
276 | * one that they want to use as an individual (even more |
---|
277 | * important now that the rfc1535 stuff restricts searches) |
---|
278 | */ |
---|
279 | cp = statp->defdname; |
---|
280 | pp = statp->dnsrch; |
---|
281 | *pp++ = cp; |
---|
282 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
---|
283 | if (*cp == '\n') /*%< silly backwards compat */ |
---|
284 | break; |
---|
285 | else if (*cp == ' ' || *cp == '\t') { |
---|
286 | *cp = 0; |
---|
287 | n = 1; |
---|
288 | } else if (n) { |
---|
289 | *pp++ = cp; |
---|
290 | n = 0; |
---|
291 | havesearch = 1; |
---|
292 | } |
---|
293 | } |
---|
294 | /* null terminate last domain if there are excess */ |
---|
295 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') |
---|
296 | cp++; |
---|
297 | *cp = '\0'; |
---|
298 | *pp++ = 0; |
---|
299 | } |
---|
300 | |
---|
301 | #define MATCH(line, name) \ |
---|
302 | (!strncmp(line, name, sizeof(name) - 1) && \ |
---|
303 | (line[sizeof(name) - 1] == ' ' || \ |
---|
304 | line[sizeof(name) - 1] == '\t')) |
---|
305 | |
---|
306 | nserv = 0; |
---|
307 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { |
---|
308 | /* read the config file */ |
---|
309 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
---|
310 | /* skip comments */ |
---|
311 | if (*buf == ';' || *buf == '#') |
---|
312 | continue; |
---|
313 | /* read default domain name */ |
---|
314 | if (MATCH(buf, "domain")) { |
---|
315 | if (haveenv) /*%< skip if have from environ */ |
---|
316 | continue; |
---|
317 | cp = buf + sizeof("domain") - 1; |
---|
318 | while (*cp == ' ' || *cp == '\t') |
---|
319 | cp++; |
---|
320 | if ((*cp == '\0') || (*cp == '\n')) |
---|
321 | continue; |
---|
322 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
---|
323 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
324 | if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL) |
---|
325 | *cp = '\0'; |
---|
326 | havesearch = 0; |
---|
327 | continue; |
---|
328 | } |
---|
329 | /* set search list */ |
---|
330 | if (MATCH(buf, "search")) { |
---|
331 | if (haveenv) /*%< skip if have from environ */ |
---|
332 | continue; |
---|
333 | cp = buf + sizeof("search") - 1; |
---|
334 | while (*cp == ' ' || *cp == '\t') |
---|
335 | cp++; |
---|
336 | if ((*cp == '\0') || (*cp == '\n')) |
---|
337 | continue; |
---|
338 | strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); |
---|
339 | statp->defdname[sizeof(statp->defdname) - 1] = '\0'; |
---|
340 | if ((cp = strchr(statp->defdname, '\n')) != NULL) |
---|
341 | *cp = '\0'; |
---|
342 | /* |
---|
343 | * Set search list to be blank-separated strings |
---|
344 | * on rest of line. |
---|
345 | */ |
---|
346 | cp = statp->defdname; |
---|
347 | pp = statp->dnsrch; |
---|
348 | *pp++ = cp; |
---|
349 | for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) { |
---|
350 | if (*cp == ' ' || *cp == '\t') { |
---|
351 | *cp = 0; |
---|
352 | n = 1; |
---|
353 | } else if (n) { |
---|
354 | *pp++ = cp; |
---|
355 | n = 0; |
---|
356 | } |
---|
357 | } |
---|
358 | /* null terminate last domain if there are excess */ |
---|
359 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
---|
360 | cp++; |
---|
361 | *cp = '\0'; |
---|
362 | *pp++ = 0; |
---|
363 | havesearch = 1; |
---|
364 | continue; |
---|
365 | } |
---|
366 | /* read nameservers to query */ |
---|
367 | if (MATCH(buf, "nameserver") && nserv < maxns) { |
---|
368 | struct addrinfo hints, *ai; |
---|
369 | char sbuf[NI_MAXSERV]; |
---|
370 | const size_t minsiz = |
---|
371 | sizeof(statp->_u._ext.ext->nsaddrs[0]); |
---|
372 | |
---|
373 | cp = buf + sizeof("nameserver") - 1; |
---|
374 | while (*cp == ' ' || *cp == '\t') |
---|
375 | cp++; |
---|
376 | cp[strcspn(cp, ";# \t\n")] = '\0'; |
---|
377 | if ((*cp != '\0') && (*cp != '\n')) { |
---|
378 | memset(&hints, 0, sizeof(hints)); |
---|
379 | hints.ai_family = PF_UNSPEC; |
---|
380 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ |
---|
381 | hints.ai_flags = AI_NUMERICHOST; |
---|
382 | sprintf(sbuf, "%u", NAMESERVER_PORT); |
---|
383 | if (getaddrinfo(cp, sbuf, &hints, &ai) == 0 && |
---|
384 | ai->ai_addrlen <= minsiz) { |
---|
385 | if (statp->_u._ext.ext != NULL) { |
---|
386 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], |
---|
387 | ai->ai_addr, ai->ai_addrlen); |
---|
388 | } |
---|
389 | if (ai->ai_addrlen <= |
---|
390 | sizeof(statp->nsaddr_list[nserv])) { |
---|
391 | memcpy(&statp->nsaddr_list[nserv], |
---|
392 | ai->ai_addr, ai->ai_addrlen); |
---|
393 | } else |
---|
394 | statp->nsaddr_list[nserv].sin_family = 0; |
---|
395 | freeaddrinfo(ai); |
---|
396 | nserv++; |
---|
397 | } |
---|
398 | } |
---|
399 | continue; |
---|
400 | } |
---|
401 | #ifdef RESOLVSORT |
---|
402 | if (MATCH(buf, "sortlist")) { |
---|
403 | struct in_addr a; |
---|
404 | struct in6_addr a6; |
---|
405 | int m, i; |
---|
406 | u_char *u; |
---|
407 | struct __res_state_ext *ext = statp->_u._ext.ext; |
---|
408 | |
---|
409 | cp = buf + sizeof("sortlist") - 1; |
---|
410 | while (nsort < MAXRESOLVSORT) { |
---|
411 | while (*cp == ' ' || *cp == '\t') |
---|
412 | cp++; |
---|
413 | if (*cp == '\0' || *cp == '\n' || *cp == ';') |
---|
414 | break; |
---|
415 | net = cp; |
---|
416 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && |
---|
417 | isascii(*cp) && !isspace((unsigned char)*cp)) |
---|
418 | cp++; |
---|
419 | n = *cp; |
---|
420 | *cp = 0; |
---|
421 | if (inet_aton(net, &a)) { |
---|
422 | statp->sort_list[nsort].addr = a; |
---|
423 | if (ISSORTMASK(n)) { |
---|
424 | *cp++ = n; |
---|
425 | net = cp; |
---|
426 | while (*cp && *cp != ';' && |
---|
427 | isascii(*cp) && |
---|
428 | !isspace((unsigned char)*cp)) |
---|
429 | cp++; |
---|
430 | n = *cp; |
---|
431 | *cp = 0; |
---|
432 | if (inet_aton(net, &a)) { |
---|
433 | statp->sort_list[nsort].mask = a.s_addr; |
---|
434 | } else { |
---|
435 | statp->sort_list[nsort].mask = |
---|
436 | net_mask(statp->sort_list[nsort].addr); |
---|
437 | } |
---|
438 | } else { |
---|
439 | statp->sort_list[nsort].mask = |
---|
440 | net_mask(statp->sort_list[nsort].addr); |
---|
441 | } |
---|
442 | ext->sort_list[nsort].af = AF_INET; |
---|
443 | ext->sort_list[nsort].addr.ina = |
---|
444 | statp->sort_list[nsort].addr; |
---|
445 | ext->sort_list[nsort].mask.ina.s_addr = |
---|
446 | statp->sort_list[nsort].mask; |
---|
447 | nsort++; |
---|
448 | } |
---|
449 | else if (inet_pton(AF_INET6, net, &a6) == 1) { |
---|
450 | |
---|
451 | ext->sort_list[nsort].af = AF_INET6; |
---|
452 | ext->sort_list[nsort].addr.in6a = a6; |
---|
453 | u = (u_char *)&ext->sort_list[nsort].mask.in6a; |
---|
454 | *cp++ = n; |
---|
455 | net = cp; |
---|
456 | while (*cp && *cp != ';' && |
---|
457 | isascii(*cp) && !isspace(*cp)) |
---|
458 | cp++; |
---|
459 | m = n; |
---|
460 | n = *cp; |
---|
461 | *cp = 0; |
---|
462 | switch (m) { |
---|
463 | case '/': |
---|
464 | m = atoi(net); |
---|
465 | break; |
---|
466 | case '&': |
---|
467 | if (inet_pton(AF_INET6, net, u) == 1) { |
---|
468 | m = -1; |
---|
469 | break; |
---|
470 | } |
---|
471 | /*FALLTHROUGH*/ |
---|
472 | default: |
---|
473 | m = sizeof(struct in6_addr) * CHAR_BIT; |
---|
474 | break; |
---|
475 | } |
---|
476 | if (m >= 0) { |
---|
477 | for (i = 0; i < sizeof(struct in6_addr); i++) { |
---|
478 | if (m <= 0) { |
---|
479 | *u = 0; |
---|
480 | } else { |
---|
481 | m -= CHAR_BIT; |
---|
482 | *u = (u_char)~0; |
---|
483 | if (m < 0) |
---|
484 | *u <<= -m; |
---|
485 | } |
---|
486 | u++; |
---|
487 | } |
---|
488 | } |
---|
489 | statp->sort_list[nsort].addr.s_addr = |
---|
490 | (u_int32_t)0xffffffff; |
---|
491 | statp->sort_list[nsort].mask = |
---|
492 | (u_int32_t)0xffffffff; |
---|
493 | nsort++; |
---|
494 | } |
---|
495 | *cp = n; |
---|
496 | } |
---|
497 | continue; |
---|
498 | } |
---|
499 | #endif |
---|
500 | if (MATCH(buf, "options")) { |
---|
501 | res_setoptions(statp, buf + sizeof("options") - 1, "conf"); |
---|
502 | continue; |
---|
503 | } |
---|
504 | } |
---|
505 | if (nserv > 0) |
---|
506 | statp->nscount = nserv; |
---|
507 | #ifdef RESOLVSORT |
---|
508 | statp->nsort = nsort; |
---|
509 | #endif |
---|
510 | (void) fclose(fp); |
---|
511 | } |
---|
512 | /* |
---|
513 | * Last chance to get a nameserver. This should not normally |
---|
514 | * be necessary |
---|
515 | */ |
---|
516 | #ifdef NO_RESOLV_CONF |
---|
517 | if(nserv == 0) |
---|
518 | nserv = get_nameservers(statp); |
---|
519 | #endif |
---|
520 | |
---|
521 | if (statp->defdname[0] == 0 && |
---|
522 | gethostname(buf, sizeof(statp->defdname) - 1) == 0 && |
---|
523 | (cp = strchr(buf, '.')) != NULL) |
---|
524 | strcpy(statp->defdname, cp + 1); |
---|
525 | |
---|
526 | /* find components of local domain that might be searched */ |
---|
527 | if (havesearch == 0) { |
---|
528 | pp = statp->dnsrch; |
---|
529 | *pp++ = statp->defdname; |
---|
530 | *pp = NULL; |
---|
531 | |
---|
532 | dots = 0; |
---|
533 | for (cp = statp->defdname; *cp; cp++) |
---|
534 | dots += (*cp == '.'); |
---|
535 | |
---|
536 | cp = statp->defdname; |
---|
537 | while (pp < statp->dnsrch + MAXDFLSRCH) { |
---|
538 | if (dots < LOCALDOMAINPARTS) |
---|
539 | break; |
---|
540 | cp = strchr(cp, '.') + 1; /*%< we know there is one */ |
---|
541 | *pp++ = cp; |
---|
542 | dots--; |
---|
543 | } |
---|
544 | *pp = NULL; |
---|
545 | #ifdef DEBUG |
---|
546 | if (statp->options & RES_DEBUG) { |
---|
547 | printf(";; res_init()... default dnsrch list:\n"); |
---|
548 | for (pp = statp->dnsrch; *pp; pp++) |
---|
549 | printf(";;\t%s\n", *pp); |
---|
550 | printf(";;\t..END..\n"); |
---|
551 | } |
---|
552 | #endif |
---|
553 | } |
---|
554 | |
---|
555 | if (issetugid()) |
---|
556 | statp->options |= RES_NOALIASES; |
---|
557 | else if ((cp = getenv("RES_OPTIONS")) != NULL) |
---|
558 | res_setoptions(statp, cp, "env"); |
---|
559 | statp->options |= RES_INIT; |
---|
560 | return (statp->res_h_errno); |
---|
561 | } |
---|
562 | |
---|
563 | static void |
---|
564 | res_setoptions(res_state statp, const char *options, const char *source) |
---|
565 | { |
---|
566 | const char *cp = options; |
---|
567 | int i; |
---|
568 | #ifndef _LIBC |
---|
569 | struct __res_state_ext *ext = statp->_u._ext.ext; |
---|
570 | #endif |
---|
571 | |
---|
572 | #ifdef DEBUG |
---|
573 | if (statp->options & RES_DEBUG) |
---|
574 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", |
---|
575 | options, source); |
---|
576 | #endif |
---|
577 | while (*cp) { |
---|
578 | /* skip leading and inner runs of spaces */ |
---|
579 | while (*cp == ' ' || *cp == '\t') |
---|
580 | cp++; |
---|
581 | /* search for and process individual options */ |
---|
582 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { |
---|
583 | i = atoi(cp + sizeof("ndots:") - 1); |
---|
584 | if (i <= RES_MAXNDOTS) |
---|
585 | statp->ndots = i; |
---|
586 | else |
---|
587 | statp->ndots = RES_MAXNDOTS; |
---|
588 | #ifdef DEBUG |
---|
589 | if (statp->options & RES_DEBUG) |
---|
590 | printf(";;\tndots=%d\n", statp->ndots); |
---|
591 | #endif |
---|
592 | } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) { |
---|
593 | i = atoi(cp + sizeof("timeout:") - 1); |
---|
594 | if (i <= RES_MAXRETRANS) |
---|
595 | statp->retrans = i; |
---|
596 | else |
---|
597 | statp->retrans = RES_MAXRETRANS; |
---|
598 | #ifdef DEBUG |
---|
599 | if (statp->options & RES_DEBUG) |
---|
600 | printf(";;\ttimeout=%d\n", statp->retrans); |
---|
601 | #endif |
---|
602 | #ifdef SOLARIS2 |
---|
603 | } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) { |
---|
604 | /* |
---|
605 | * For backward compatibility, 'retrans' is |
---|
606 | * supported as an alias for 'timeout', though |
---|
607 | * without an imposed maximum. |
---|
608 | */ |
---|
609 | statp->retrans = atoi(cp + sizeof("retrans:") - 1); |
---|
610 | } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){ |
---|
611 | /* |
---|
612 | * For backward compatibility, 'retry' is |
---|
613 | * supported as an alias for 'attempts', though |
---|
614 | * without an imposed maximum. |
---|
615 | */ |
---|
616 | statp->retry = atoi(cp + sizeof("retry:") - 1); |
---|
617 | #endif /* SOLARIS2 */ |
---|
618 | } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){ |
---|
619 | i = atoi(cp + sizeof("attempts:") - 1); |
---|
620 | if (i <= RES_MAXRETRY) |
---|
621 | statp->retry = i; |
---|
622 | else |
---|
623 | statp->retry = RES_MAXRETRY; |
---|
624 | #ifdef DEBUG |
---|
625 | if (statp->options & RES_DEBUG) |
---|
626 | printf(";;\tattempts=%d\n", statp->retry); |
---|
627 | #endif |
---|
628 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { |
---|
629 | #ifdef DEBUG |
---|
630 | if (!(statp->options & RES_DEBUG)) { |
---|
631 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", |
---|
632 | options, source); |
---|
633 | statp->options |= RES_DEBUG; |
---|
634 | } |
---|
635 | printf(";;\tdebug\n"); |
---|
636 | #endif |
---|
637 | } else if (!strncmp(cp, "no_tld_query", |
---|
638 | sizeof("no_tld_query") - 1) || |
---|
639 | !strncmp(cp, "no-tld-query", |
---|
640 | sizeof("no-tld-query") - 1)) { |
---|
641 | statp->options |= RES_NOTLDQUERY; |
---|
642 | } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { |
---|
643 | statp->options |= RES_USE_INET6; |
---|
644 | } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { |
---|
645 | statp->options |= RES_INSECURE1; |
---|
646 | } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { |
---|
647 | statp->options |= RES_INSECURE2; |
---|
648 | } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) { |
---|
649 | statp->options |= RES_ROTATE; |
---|
650 | } else if (!strncmp(cp, "no-check-names", |
---|
651 | sizeof("no-check-names") - 1)) { |
---|
652 | statp->options |= RES_NOCHECKNAME; |
---|
653 | } |
---|
654 | #ifdef RES_USE_EDNS0 |
---|
655 | else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { |
---|
656 | statp->options |= RES_USE_EDNS0; |
---|
657 | } |
---|
658 | #endif |
---|
659 | #ifndef _LIBC |
---|
660 | else if (!strncmp(cp, "dname", sizeof("dname") - 1)) { |
---|
661 | statp->options |= RES_USE_DNAME; |
---|
662 | } |
---|
663 | else if (!strncmp(cp, "nibble:", sizeof("nibble:") - 1)) { |
---|
664 | if (ext == NULL) |
---|
665 | goto skip; |
---|
666 | cp += sizeof("nibble:") - 1; |
---|
667 | i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix) - 1); |
---|
668 | strncpy(ext->nsuffix, cp, i); |
---|
669 | ext->nsuffix[i] = '\0'; |
---|
670 | } |
---|
671 | else if (!strncmp(cp, "nibble2:", sizeof("nibble2:") - 1)) { |
---|
672 | if (ext == NULL) |
---|
673 | goto skip; |
---|
674 | cp += sizeof("nibble2:") - 1; |
---|
675 | i = MIN(strcspn(cp, " \t"), sizeof(ext->nsuffix2) - 1); |
---|
676 | strncpy(ext->nsuffix2, cp, i); |
---|
677 | ext->nsuffix2[i] = '\0'; |
---|
678 | } |
---|
679 | else if (!strncmp(cp, "v6revmode:", sizeof("v6revmode:") - 1)) { |
---|
680 | cp += sizeof("v6revmode:") - 1; |
---|
681 | /* "nibble" and "bitstring" used to be valid */ |
---|
682 | if (!strncmp(cp, "single", sizeof("single") - 1)) { |
---|
683 | statp->options |= RES_NO_NIBBLE2; |
---|
684 | } else if (!strncmp(cp, "both", sizeof("both") - 1)) { |
---|
685 | statp->options &= |
---|
686 | ~RES_NO_NIBBLE2; |
---|
687 | } |
---|
688 | } |
---|
689 | #endif |
---|
690 | else { |
---|
691 | /* XXX - print a warning here? */ |
---|
692 | } |
---|
693 | #ifndef _LIBC |
---|
694 | skip: |
---|
695 | #endif |
---|
696 | /* skip to next run of spaces */ |
---|
697 | while (*cp && *cp != ' ' && *cp != '\t') |
---|
698 | cp++; |
---|
699 | } |
---|
700 | } |
---|
701 | |
---|
702 | #ifdef RESOLVSORT |
---|
703 | /* XXX - should really support CIDR which means explicit masks always. */ |
---|
704 | static u_int32_t |
---|
705 | net_mask(in) /*!< XXX - should really use system's version of this */ |
---|
706 | struct in_addr in; |
---|
707 | { |
---|
708 | u_int32_t i = ntohl(in.s_addr); |
---|
709 | |
---|
710 | if (IN_CLASSA(i)) |
---|
711 | return (htonl(IN_CLASSA_NET)); |
---|
712 | else if (IN_CLASSB(i)) |
---|
713 | return (htonl(IN_CLASSB_NET)); |
---|
714 | return (htonl(IN_CLASSC_NET)); |
---|
715 | } |
---|
716 | #endif |
---|
717 | |
---|
718 | u_int |
---|
719 | res_randomid(void) { |
---|
720 | struct timeval now; |
---|
721 | |
---|
722 | gettimeofday(&now, NULL); |
---|
723 | return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); |
---|
724 | } |
---|
725 | |
---|
726 | /*% |
---|
727 | * This routine is for closing the socket if a virtual circuit is used and |
---|
728 | * the program wants to close it. This provides support for endhostent() |
---|
729 | * which expects to close the socket. |
---|
730 | * |
---|
731 | * This routine is not expected to be user visible. |
---|
732 | */ |
---|
733 | void |
---|
734 | res_nclose(res_state statp) { |
---|
735 | int ns; |
---|
736 | |
---|
737 | if (statp->_vcsock >= 0) { |
---|
738 | (void) _close(statp->_vcsock); |
---|
739 | statp->_vcsock = -1; |
---|
740 | statp->_flags &= ~(RES_F_VC | RES_F_CONN); |
---|
741 | } |
---|
742 | for (ns = 0; ns < statp->_u._ext.nscount; ns++) { |
---|
743 | if (statp->_u._ext.nssocks[ns] != -1) { |
---|
744 | (void) _close(statp->_u._ext.nssocks[ns]); |
---|
745 | statp->_u._ext.nssocks[ns] = -1; |
---|
746 | } |
---|
747 | } |
---|
748 | } |
---|
749 | |
---|
750 | void |
---|
751 | res_ndestroy(res_state statp) { |
---|
752 | res_nclose(statp); |
---|
753 | if (statp->_u._ext.ext != NULL) |
---|
754 | free(statp->_u._ext.ext); |
---|
755 | statp->options &= ~RES_INIT; |
---|
756 | statp->_u._ext.ext = NULL; |
---|
757 | } |
---|
758 | |
---|
759 | #ifndef _LIBC |
---|
760 | const char * |
---|
761 | res_get_nibblesuffix(res_state statp) { |
---|
762 | if (statp->_u._ext.ext) |
---|
763 | return (statp->_u._ext.ext->nsuffix); |
---|
764 | return ("ip6.arpa"); |
---|
765 | } |
---|
766 | |
---|
767 | const char * |
---|
768 | res_get_nibblesuffix2(res_state statp) { |
---|
769 | if (statp->_u._ext.ext) |
---|
770 | return (statp->_u._ext.ext->nsuffix2); |
---|
771 | return ("ip6.int"); |
---|
772 | } |
---|
773 | #endif |
---|
774 | |
---|
775 | void |
---|
776 | res_setservers(res_state statp, const union res_sockaddr_union *set, int cnt) { |
---|
777 | int i, nserv; |
---|
778 | size_t size; |
---|
779 | |
---|
780 | /* close open servers */ |
---|
781 | res_nclose(statp); |
---|
782 | |
---|
783 | /* cause rtt times to be forgotten */ |
---|
784 | statp->_u._ext.nscount = 0; |
---|
785 | |
---|
786 | nserv = 0; |
---|
787 | for (i = 0; i < cnt && nserv < MAXNS; i++) { |
---|
788 | switch (set->sin.sin_family) { |
---|
789 | case AF_INET: |
---|
790 | size = sizeof(set->sin); |
---|
791 | if (statp->_u._ext.ext) |
---|
792 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], |
---|
793 | &set->sin, size); |
---|
794 | if (size <= sizeof(statp->nsaddr_list[nserv])) |
---|
795 | memcpy(&statp->nsaddr_list[nserv], |
---|
796 | &set->sin, size); |
---|
797 | else |
---|
798 | statp->nsaddr_list[nserv].sin_family = 0; |
---|
799 | nserv++; |
---|
800 | break; |
---|
801 | |
---|
802 | #ifdef HAS_INET6_STRUCTS |
---|
803 | case AF_INET6: |
---|
804 | size = sizeof(set->sin6); |
---|
805 | if (statp->_u._ext.ext) |
---|
806 | memcpy(&statp->_u._ext.ext->nsaddrs[nserv], |
---|
807 | &set->sin6, size); |
---|
808 | if (size <= sizeof(statp->nsaddr_list[nserv])) |
---|
809 | memcpy(&statp->nsaddr_list[nserv], |
---|
810 | &set->sin6, size); |
---|
811 | else |
---|
812 | statp->nsaddr_list[nserv].sin_family = 0; |
---|
813 | nserv++; |
---|
814 | break; |
---|
815 | #endif |
---|
816 | |
---|
817 | default: |
---|
818 | break; |
---|
819 | } |
---|
820 | set++; |
---|
821 | } |
---|
822 | statp->nscount = nserv; |
---|
823 | |
---|
824 | } |
---|
825 | |
---|
826 | int |
---|
827 | res_getservers(res_state statp, union res_sockaddr_union *set, int cnt) { |
---|
828 | int i; |
---|
829 | size_t size; |
---|
830 | u_int16_t family; |
---|
831 | |
---|
832 | for (i = 0; i < statp->nscount && i < cnt; i++) { |
---|
833 | if (statp->_u._ext.ext) |
---|
834 | family = statp->_u._ext.ext->nsaddrs[i].sin.sin_family; |
---|
835 | else |
---|
836 | family = statp->nsaddr_list[i].sin_family; |
---|
837 | |
---|
838 | switch (family) { |
---|
839 | case AF_INET: |
---|
840 | size = sizeof(set->sin); |
---|
841 | if (statp->_u._ext.ext) |
---|
842 | memcpy(&set->sin, |
---|
843 | &statp->_u._ext.ext->nsaddrs[i], |
---|
844 | size); |
---|
845 | else |
---|
846 | memcpy(&set->sin, &statp->nsaddr_list[i], |
---|
847 | size); |
---|
848 | break; |
---|
849 | |
---|
850 | #ifdef HAS_INET6_STRUCTS |
---|
851 | case AF_INET6: |
---|
852 | size = sizeof(set->sin6); |
---|
853 | if (statp->_u._ext.ext) |
---|
854 | memcpy(&set->sin6, |
---|
855 | &statp->_u._ext.ext->nsaddrs[i], |
---|
856 | size); |
---|
857 | else |
---|
858 | memcpy(&set->sin6, &statp->nsaddr_list[i], |
---|
859 | size); |
---|
860 | break; |
---|
861 | #endif |
---|
862 | |
---|
863 | default: |
---|
864 | set->sin.sin_family = 0; |
---|
865 | break; |
---|
866 | } |
---|
867 | set++; |
---|
868 | } |
---|
869 | return (statp->nscount); |
---|
870 | } |
---|
871 | |
---|
872 | /*! \file */ |
---|