diff options
Diffstat (limited to 'src/lib/libc/net/res_init.c')
-rw-r--r-- | src/lib/libc/net/res_init.c | 650 |
1 files changed, 650 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c new file mode 100644 index 0000000000..9c73958883 --- /dev/null +++ b/src/lib/libc/net/res_init.c | |||
@@ -0,0 +1,650 @@ | |||
1 | /* $OpenBSD: res_init.c,v 1.29 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1989, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1989, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. 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 | |||
54 | #ifndef INET6 | ||
55 | #define INET6 | ||
56 | #endif | ||
57 | |||
58 | #if defined(LIBC_SCCS) && !defined(lint) | ||
59 | #if 0 | ||
60 | static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; | ||
61 | static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $"; | ||
62 | #else | ||
63 | static char rcsid[] = "$OpenBSD: res_init.c,v 1.29 2003/06/02 20:18:36 millert Exp $"; | ||
64 | #endif | ||
65 | #endif /* LIBC_SCCS and not lint */ | ||
66 | |||
67 | #include <sys/types.h> | ||
68 | #include <sys/param.h> | ||
69 | #include <sys/socket.h> | ||
70 | #include <sys/time.h> | ||
71 | #include <netinet/in.h> | ||
72 | #include <arpa/inet.h> | ||
73 | #include <arpa/nameser.h> | ||
74 | |||
75 | #include <stdio.h> | ||
76 | #include <ctype.h> | ||
77 | #include <resolv.h> | ||
78 | #include <unistd.h> | ||
79 | #include <stdlib.h> | ||
80 | #include <string.h> | ||
81 | #ifdef INET6 | ||
82 | #include <netdb.h> | ||
83 | #endif /* INET6 */ | ||
84 | |||
85 | #include "thread_private.h" | ||
86 | |||
87 | /*-------------------------------------- info about "sortlist" -------------- | ||
88 | * Marc Majka 1994/04/16 | ||
89 | * Allan Nathanson 1994/10/29 (BIND 4.9.3.x) | ||
90 | * | ||
91 | * NetInfo resolver configuration directory support. | ||
92 | * | ||
93 | * Allow a NetInfo directory to be created in the hierarchy which | ||
94 | * contains the same information as the resolver configuration file. | ||
95 | * | ||
96 | * - The local domain name is stored as the value of the "domain" property. | ||
97 | * - The Internet address(es) of the name server(s) are stored as values | ||
98 | * of the "nameserver" property. | ||
99 | * - The name server addresses are stored as values of the "nameserver" | ||
100 | * property. | ||
101 | * - The search list for host-name lookup is stored as values of the | ||
102 | * "search" property. | ||
103 | * - The sortlist comprised of IP address netmask pairs are stored as | ||
104 | * values of the "sortlist" property. The IP address and optional netmask | ||
105 | * should be separated by a slash (/) or ampersand (&) character. | ||
106 | * - Internal resolver variables can be set from the value of the "options" | ||
107 | * property. | ||
108 | */ | ||
109 | |||
110 | static void res_setoptions(char *, char *); | ||
111 | |||
112 | #ifdef RESOLVSORT | ||
113 | static const char sort_mask[] = "/&"; | ||
114 | #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) | ||
115 | static u_int32_t net_mask(struct in_addr); | ||
116 | #endif | ||
117 | |||
118 | /* | ||
119 | * Resolver state default settings. | ||
120 | */ | ||
121 | volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res) = { | ||
122 | PTHREAD_ONCE_INIT, 0 | ||
123 | }; | ||
124 | |||
125 | struct __res_state _res | ||
126 | # if defined(__BIND_RES_TEXT) | ||
127 | = { RES_TIMEOUT, } /* Motorola, et al. */ | ||
128 | # endif | ||
129 | ; | ||
130 | #ifdef INET6 | ||
131 | volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res_ext) = { | ||
132 | PTHREAD_ONCE_INIT, 0 | ||
133 | }; | ||
134 | |||
135 | struct __res_state_ext _res_ext; | ||
136 | #endif /* INET6 */ | ||
137 | |||
138 | /* | ||
139 | * Set up default settings. If the configuration file exist, the values | ||
140 | * there will have precedence. Otherwise, the server address is set to | ||
141 | * INADDR_ANY and the default domain name comes from the gethostname(). | ||
142 | * | ||
143 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 | ||
144 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address | ||
145 | * since it was noted that INADDR_ANY actually meant ``the first interface | ||
146 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, | ||
147 | * it had to be "up" in order for you to reach your own name server. It | ||
148 | * was later decided that since the recommended practice is to always | ||
149 | * install local static routes through 127.0.0.1 for all your network | ||
150 | * interfaces, that we could solve this problem without a code change. | ||
151 | * | ||
152 | * The configuration file should always be used, since it is the only way | ||
153 | * to specify a default domain. If you are running a server on your local | ||
154 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" | ||
155 | * in the configuration file. | ||
156 | * | ||
157 | * Return 0 if completes successfully, -1 on error | ||
158 | */ | ||
159 | int | ||
160 | res_init() | ||
161 | { | ||
162 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
163 | #ifdef INET6 | ||
164 | struct __res_state_ext *_res_extp = _THREAD_PRIVATE(_res_ext, _res_ext, | ||
165 | &_res_ext); | ||
166 | #endif | ||
167 | register FILE *fp; | ||
168 | register char *cp, **pp; | ||
169 | register int n; | ||
170 | char buf[BUFSIZ]; | ||
171 | int nserv = 0; /* number of nameserver records read from file */ | ||
172 | int haveenv = 0; | ||
173 | int havesearch = 0; | ||
174 | size_t len; | ||
175 | #ifdef RESOLVSORT | ||
176 | int nsort = 0; | ||
177 | char *net; | ||
178 | #endif | ||
179 | #ifndef RFC1535 | ||
180 | int dots; | ||
181 | #endif | ||
182 | |||
183 | /* | ||
184 | * These three fields used to be statically initialized. This made | ||
185 | * it hard to use this code in a shared library. It is necessary, | ||
186 | * now that we're doing dynamic initialization here, that we preserve | ||
187 | * the old semantics: if an application modifies one of these three | ||
188 | * fields of _res before res_init() is called, res_init() will not | ||
189 | * alter them. Of course, if an application is setting them to | ||
190 | * _zero_ before calling res_init(), hoping to override what used | ||
191 | * to be the static default, we can't detect it and unexpected results | ||
192 | * will follow. Zero for any of these fields would make no sense, | ||
193 | * so one can safely assume that the applications were already getting | ||
194 | * unexpected results. | ||
195 | * | ||
196 | * _res.options is tricky since some apps were known to diddle the bits | ||
197 | * before res_init() was first called. We can't replicate that semantic | ||
198 | * with dynamic initialization (they may have turned bits off that are | ||
199 | * set in RES_DEFAULT). Our solution is to declare such applications | ||
200 | * "broken". They could fool us by setting RES_INIT but none do (yet). | ||
201 | */ | ||
202 | if (!_resp->retrans) | ||
203 | _resp->retrans = RES_TIMEOUT; | ||
204 | if (!_resp->retry) | ||
205 | _resp->retry = 4; | ||
206 | if (!(_resp->options & RES_INIT)) | ||
207 | _resp->options = RES_DEFAULT; | ||
208 | |||
209 | #ifdef USELOOPBACK | ||
210 | _resp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); | ||
211 | #else | ||
212 | _resp->nsaddr.sin_addr.s_addr = INADDR_ANY; | ||
213 | #endif | ||
214 | _resp->nsaddr.sin_family = AF_INET; | ||
215 | _resp->nsaddr.sin_port = htons(NAMESERVER_PORT); | ||
216 | _resp->nsaddr.sin_len = sizeof(struct sockaddr_in); | ||
217 | #ifdef INET6 | ||
218 | if (sizeof(_res_extp->nsaddr) >= _resp->nsaddr.sin_len) | ||
219 | memcpy(&_res_extp->nsaddr, &_resp->nsaddr, _resp->nsaddr.sin_len); | ||
220 | #endif | ||
221 | _resp->nscount = 1; | ||
222 | _resp->ndots = 1; | ||
223 | _resp->pfcode = 0; | ||
224 | strlcpy(_resp->lookups, "f", sizeof _resp->lookups); | ||
225 | |||
226 | /* Allow user to override the local domain definition */ | ||
227 | if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { | ||
228 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
229 | haveenv++; | ||
230 | |||
231 | /* | ||
232 | * Set search list to be blank-separated strings | ||
233 | * from rest of env value. Permits users of LOCALDOMAIN | ||
234 | * to still have a search list, and anyone to set the | ||
235 | * one that they want to use as an individual (even more | ||
236 | * important now that the rfc1535 stuff restricts searches) | ||
237 | */ | ||
238 | cp = _resp->defdname; | ||
239 | pp = _resp->dnsrch; | ||
240 | *pp++ = cp; | ||
241 | for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) { | ||
242 | if (*cp == '\n') /* silly backwards compat */ | ||
243 | break; | ||
244 | else if (*cp == ' ' || *cp == '\t') { | ||
245 | *cp = 0; | ||
246 | n = 1; | ||
247 | } else if (n) { | ||
248 | *pp++ = cp; | ||
249 | n = 0; | ||
250 | havesearch = 1; | ||
251 | } | ||
252 | } | ||
253 | /* null terminate last domain if there are excess */ | ||
254 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') | ||
255 | cp++; | ||
256 | *cp = '\0'; | ||
257 | *pp++ = 0; | ||
258 | } | ||
259 | |||
260 | #define MATCH(line, name) \ | ||
261 | (!strncmp(line, name, sizeof(name) - 1) && \ | ||
262 | (line[sizeof(name) - 1] == ' ' || \ | ||
263 | line[sizeof(name) - 1] == '\t')) | ||
264 | |||
265 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { | ||
266 | strlcpy(_resp->lookups, "bf", sizeof _resp->lookups); | ||
267 | |||
268 | /* read the config file */ | ||
269 | buf[0] = '\0'; | ||
270 | while ((cp = fgetln(fp, &len)) != NULL) { | ||
271 | /* skip lines that are too long or zero length */ | ||
272 | if (len >= sizeof(buf) || len == 0) | ||
273 | continue; | ||
274 | (void)memcpy(buf, cp, len); | ||
275 | buf[len] = '\0'; | ||
276 | /* skip comments */ | ||
277 | if ((cp = strpbrk(buf, ";#")) != NULL) | ||
278 | *cp = '\0'; | ||
279 | if (buf[0] == '\0') | ||
280 | continue; | ||
281 | /* read default domain name */ | ||
282 | if (MATCH(buf, "domain")) { | ||
283 | if (haveenv) /* skip if have from environ */ | ||
284 | continue; | ||
285 | cp = buf + sizeof("domain") - 1; | ||
286 | while (*cp == ' ' || *cp == '\t') | ||
287 | cp++; | ||
288 | if ((*cp == '\0') || (*cp == '\n')) | ||
289 | continue; | ||
290 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
291 | if ((cp = strpbrk(_resp->defdname, " \t\n")) != NULL) | ||
292 | *cp = '\0'; | ||
293 | havesearch = 0; | ||
294 | continue; | ||
295 | } | ||
296 | /* lookup types */ | ||
297 | if (MATCH(buf, "lookup")) { | ||
298 | char *sp = NULL; | ||
299 | |||
300 | bzero(_resp->lookups, sizeof _resp->lookups); | ||
301 | cp = buf + sizeof("lookup") - 1; | ||
302 | for (n = 0;; cp++) { | ||
303 | if (n == MAXDNSLUS) | ||
304 | break; | ||
305 | if ((*cp == '\0') || (*cp == '\n')) { | ||
306 | if (sp) { | ||
307 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
308 | _resp->lookups[n++] = *sp; | ||
309 | sp = NULL; | ||
310 | } | ||
311 | break; | ||
312 | } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) { | ||
313 | if (sp) { | ||
314 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
315 | _resp->lookups[n++] = *sp; | ||
316 | sp = NULL; | ||
317 | } | ||
318 | } else if (sp == NULL) | ||
319 | sp = cp; | ||
320 | } | ||
321 | continue; | ||
322 | } | ||
323 | /* set search list */ | ||
324 | if (MATCH(buf, "search")) { | ||
325 | if (haveenv) /* skip if have from environ */ | ||
326 | continue; | ||
327 | cp = buf + sizeof("search") - 1; | ||
328 | while (*cp == ' ' || *cp == '\t') | ||
329 | cp++; | ||
330 | if ((*cp == '\0') || (*cp == '\n')) | ||
331 | continue; | ||
332 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
333 | if ((cp = strchr(_resp->defdname, '\n')) != NULL) | ||
334 | *cp = '\0'; | ||
335 | /* | ||
336 | * Set search list to be blank-separated strings | ||
337 | * on rest of line. | ||
338 | */ | ||
339 | cp = _resp->defdname; | ||
340 | pp = _resp->dnsrch; | ||
341 | *pp++ = cp; | ||
342 | for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) { | ||
343 | if (*cp == ' ' || *cp == '\t') { | ||
344 | *cp = 0; | ||
345 | n = 1; | ||
346 | } else if (n) { | ||
347 | *pp++ = cp; | ||
348 | n = 0; | ||
349 | } | ||
350 | } | ||
351 | /* null terminate last domain if there are excess */ | ||
352 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') | ||
353 | cp++; | ||
354 | *cp = '\0'; | ||
355 | *pp++ = 0; | ||
356 | havesearch = 1; | ||
357 | continue; | ||
358 | } | ||
359 | /* read nameservers to query */ | ||
360 | if (MATCH(buf, "nameserver") && nserv < MAXNS) { | ||
361 | #ifdef INET6 | ||
362 | char *q; | ||
363 | struct addrinfo hints, *res; | ||
364 | char pbuf[NI_MAXSERV]; | ||
365 | #else | ||
366 | struct in_addr a; | ||
367 | #endif /* INET6 */ | ||
368 | |||
369 | cp = buf + sizeof("nameserver") - 1; | ||
370 | while (*cp == ' ' || *cp == '\t') | ||
371 | cp++; | ||
372 | #ifdef INET6 | ||
373 | if ((*cp == '\0') || (*cp == '\n')) | ||
374 | continue; | ||
375 | for (q = cp; *q; q++) { | ||
376 | if (isspace(*q)) { | ||
377 | *q = '\0'; | ||
378 | break; | ||
379 | } | ||
380 | } | ||
381 | memset(&hints, 0, sizeof(hints)); | ||
382 | hints.ai_flags = AI_NUMERICHOST; | ||
383 | hints.ai_socktype = SOCK_DGRAM; | ||
384 | snprintf(pbuf, sizeof(pbuf), "%u", NAMESERVER_PORT); | ||
385 | res = NULL; | ||
386 | if (getaddrinfo(cp, pbuf, &hints, &res) == 0 && | ||
387 | res->ai_next == NULL) { | ||
388 | if (res->ai_addrlen <= sizeof(_res_extp->nsaddr_list[nserv])) { | ||
389 | memcpy(&_res_extp->nsaddr_list[nserv], res->ai_addr, | ||
390 | res->ai_addrlen); | ||
391 | } else { | ||
392 | memset(&_res_extp->nsaddr_list[nserv], 0, | ||
393 | sizeof(_res_extp->nsaddr_list[nserv])); | ||
394 | } | ||
395 | if (res->ai_addrlen <= sizeof(_resp->nsaddr_list[nserv])) { | ||
396 | memcpy(&_resp->nsaddr_list[nserv], res->ai_addr, | ||
397 | res->ai_addrlen); | ||
398 | } else { | ||
399 | memset(&_resp->nsaddr_list[nserv], 0, | ||
400 | sizeof(_resp->nsaddr_list[nserv])); | ||
401 | } | ||
402 | nserv++; | ||
403 | } | ||
404 | if (res) | ||
405 | freeaddrinfo(res); | ||
406 | #else /* INET6 */ | ||
407 | if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { | ||
408 | _resp->nsaddr_list[nserv].sin_addr = a; | ||
409 | _resp->nsaddr_list[nserv].sin_family = AF_INET; | ||
410 | _resp->nsaddr_list[nserv].sin_port = | ||
411 | htons(NAMESERVER_PORT); | ||
412 | _resp->nsaddr_list[nserv].sin_len = | ||
413 | sizeof(struct sockaddr_in); | ||
414 | nserv++; | ||
415 | } | ||
416 | #endif /* INET6 */ | ||
417 | continue; | ||
418 | } | ||
419 | #ifdef RESOLVSORT | ||
420 | if (MATCH(buf, "sortlist")) { | ||
421 | struct in_addr a; | ||
422 | #ifdef INET6 | ||
423 | struct in6_addr a6; | ||
424 | int m, i; | ||
425 | u_char *u; | ||
426 | #endif /* INET6 */ | ||
427 | |||
428 | cp = buf + sizeof("sortlist") - 1; | ||
429 | while (nsort < MAXRESOLVSORT) { | ||
430 | while (*cp == ' ' || *cp == '\t') | ||
431 | cp++; | ||
432 | if (*cp == '\0' || *cp == '\n' || *cp == ';') | ||
433 | break; | ||
434 | net = cp; | ||
435 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && | ||
436 | isascii(*cp) && !isspace(*cp)) | ||
437 | cp++; | ||
438 | n = *cp; | ||
439 | *cp = 0; | ||
440 | if (inet_aton(net, &a)) { | ||
441 | _resp->sort_list[nsort].addr = a; | ||
442 | if (ISSORTMASK(n)) { | ||
443 | *cp++ = n; | ||
444 | net = cp; | ||
445 | while (*cp && *cp != ';' && | ||
446 | isascii(*cp) && !isspace(*cp)) | ||
447 | cp++; | ||
448 | n = *cp; | ||
449 | *cp = 0; | ||
450 | if (inet_aton(net, &a)) { | ||
451 | _resp->sort_list[nsort].mask = a.s_addr; | ||
452 | } else { | ||
453 | _resp->sort_list[nsort].mask = | ||
454 | net_mask(_resp->sort_list[nsort].addr); | ||
455 | } | ||
456 | } else { | ||
457 | _resp->sort_list[nsort].mask = | ||
458 | net_mask(_resp->sort_list[nsort].addr); | ||
459 | } | ||
460 | #ifdef INET6 | ||
461 | _res_extp->sort_list[nsort].af = AF_INET; | ||
462 | _res_extp->sort_list[nsort].addr.ina = | ||
463 | _resp->sort_list[nsort].addr; | ||
464 | _res_extp->sort_list[nsort].mask.ina.s_addr = | ||
465 | _resp->sort_list[nsort].mask; | ||
466 | #endif /* INET6 */ | ||
467 | nsort++; | ||
468 | } | ||
469 | #ifdef INET6 | ||
470 | else if (inet_pton(AF_INET6, net, &a6) == 1) { | ||
471 | _res_extp->sort_list[nsort].af = AF_INET6; | ||
472 | _res_extp->sort_list[nsort].addr.in6a = a6; | ||
473 | u = (u_char *)&_res_extp->sort_list[nsort].mask.in6a; | ||
474 | *cp++ = n; | ||
475 | net = cp; | ||
476 | while (*cp && *cp != ';' && | ||
477 | isascii(*cp) && !isspace(*cp)) | ||
478 | cp++; | ||
479 | m = n; | ||
480 | n = *cp; | ||
481 | *cp = 0; | ||
482 | switch (m) { | ||
483 | case '/': | ||
484 | m = atoi(net); | ||
485 | break; | ||
486 | case '&': | ||
487 | if (inet_pton(AF_INET6, net, u) == 1) { | ||
488 | m = -1; | ||
489 | break; | ||
490 | } | ||
491 | /*FALLTHRU*/ | ||
492 | default: | ||
493 | m = sizeof(struct in6_addr) * NBBY; | ||
494 | break; | ||
495 | } | ||
496 | if (m >= 0) { | ||
497 | for (i = 0; i < sizeof(struct in6_addr); i++) { | ||
498 | if (m <= 0) { | ||
499 | *u = 0; | ||
500 | } else { | ||
501 | m -= NBBY; | ||
502 | *u = (u_char)~0; | ||
503 | if (m < 0) | ||
504 | *u <<= -m; | ||
505 | } | ||
506 | u++; | ||
507 | } | ||
508 | } | ||
509 | nsort++; | ||
510 | } | ||
511 | #endif /* INET6 */ | ||
512 | *cp = n; | ||
513 | } | ||
514 | continue; | ||
515 | } | ||
516 | #endif | ||
517 | if (MATCH(buf, "options")) { | ||
518 | res_setoptions(buf + sizeof("options") - 1, "conf"); | ||
519 | continue; | ||
520 | } | ||
521 | } | ||
522 | if (nserv > 1) | ||
523 | _resp->nscount = nserv; | ||
524 | #ifdef RESOLVSORT | ||
525 | _resp->nsort = nsort; | ||
526 | #endif | ||
527 | (void) fclose(fp); | ||
528 | } | ||
529 | if (_resp->defdname[0] == 0 && | ||
530 | gethostname(buf, sizeof(_resp->defdname) - 1) == 0 && | ||
531 | (cp = strchr(buf, '.')) != NULL) | ||
532 | { | ||
533 | strlcpy(_resp->defdname, cp + 1, | ||
534 | sizeof(_resp->defdname)); | ||
535 | } | ||
536 | |||
537 | /* find components of local domain that might be searched */ | ||
538 | if (havesearch == 0) { | ||
539 | pp = _resp->dnsrch; | ||
540 | *pp++ = _resp->defdname; | ||
541 | *pp = NULL; | ||
542 | |||
543 | #ifndef RFC1535 | ||
544 | dots = 0; | ||
545 | for (cp = _resp->defdname; *cp; cp++) | ||
546 | dots += (*cp == '.'); | ||
547 | |||
548 | cp = _resp->defdname; | ||
549 | while (pp < _resp->dnsrch + MAXDFLSRCH) { | ||
550 | if (dots < LOCALDOMAINPARTS) | ||
551 | break; | ||
552 | cp = strchr(cp, '.') + 1; /* we know there is one */ | ||
553 | *pp++ = cp; | ||
554 | dots--; | ||
555 | } | ||
556 | *pp = NULL; | ||
557 | #ifdef DEBUG | ||
558 | if (_resp->options & RES_DEBUG) { | ||
559 | printf(";; res_init()... default dnsrch list:\n"); | ||
560 | for (pp = _resp->dnsrch; *pp; pp++) | ||
561 | printf(";;\t%s\n", *pp); | ||
562 | printf(";;\t..END..\n"); | ||
563 | } | ||
564 | #endif /* DEBUG */ | ||
565 | #endif /* !RFC1535 */ | ||
566 | } | ||
567 | |||
568 | if (issetugid()) | ||
569 | _resp->options |= RES_NOALIASES; | ||
570 | else if ((cp = getenv("RES_OPTIONS")) != NULL) | ||
571 | res_setoptions(cp, "env"); | ||
572 | _resp->options |= RES_INIT; | ||
573 | return (0); | ||
574 | } | ||
575 | |||
576 | /* ARGSUSED */ | ||
577 | static void | ||
578 | res_setoptions(options, source) | ||
579 | char *options, *source; | ||
580 | { | ||
581 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
582 | char *cp = options; | ||
583 | char *endp; | ||
584 | long l; | ||
585 | |||
586 | #ifdef DEBUG | ||
587 | if (_resp->options & RES_DEBUG) | ||
588 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", | ||
589 | options, source); | ||
590 | #endif | ||
591 | while (*cp) { | ||
592 | /* skip leading and inner runs of spaces */ | ||
593 | while (*cp == ' ' || *cp == '\t') | ||
594 | cp++; | ||
595 | /* search for and process individual options */ | ||
596 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { | ||
597 | char *p = cp + sizeof("ndots:") - 1; | ||
598 | l = strtol(p, &endp, 10); | ||
599 | if (l >= 0 && endp != p && | ||
600 | (*endp = '\0' || isspace(*endp))) { | ||
601 | if (l <= RES_MAXNDOTS) | ||
602 | _resp->ndots = l; | ||
603 | else | ||
604 | _resp->ndots = RES_MAXNDOTS; | ||
605 | #ifdef DEBUG | ||
606 | if (_resp->options & RES_DEBUG) | ||
607 | printf(";;\tndots=%u\n", _resp->ndots); | ||
608 | #endif | ||
609 | } | ||
610 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { | ||
611 | #ifdef DEBUG | ||
612 | if (!(_resp->options & RES_DEBUG)) { | ||
613 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", | ||
614 | options, source); | ||
615 | _resp->options |= RES_DEBUG; | ||
616 | } | ||
617 | printf(";;\tdebug\n"); | ||
618 | #endif | ||
619 | } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { | ||
620 | _resp->options |= RES_USE_INET6; | ||
621 | } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { | ||
622 | _resp->options |= RES_INSECURE1; | ||
623 | } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { | ||
624 | _resp->options |= RES_INSECURE2; | ||
625 | } else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { | ||
626 | _resp->options |= RES_USE_EDNS0; | ||
627 | } else { | ||
628 | /* XXX - print a warning here? */ | ||
629 | } | ||
630 | /* skip to next run of spaces */ | ||
631 | while (*cp && *cp != ' ' && *cp != '\t') | ||
632 | cp++; | ||
633 | } | ||
634 | } | ||
635 | |||
636 | #ifdef RESOLVSORT | ||
637 | /* XXX - should really support CIDR which means explicit masks always. */ | ||
638 | static u_int32_t | ||
639 | net_mask(in) /* XXX - should really use system's version of this */ | ||
640 | struct in_addr in; | ||
641 | { | ||
642 | register u_int32_t i = ntohl(in.s_addr); | ||
643 | |||
644 | if (IN_CLASSA(i)) | ||
645 | return (htonl(IN_CLASSA_NET)); | ||
646 | else if (IN_CLASSB(i)) | ||
647 | return (htonl(IN_CLASSB_NET)); | ||
648 | return (htonl(IN_CLASSC_NET)); | ||
649 | } | ||
650 | #endif | ||