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