summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_init.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_init.c')
-rw-r--r--src/lib/libc/net/res_init.c504
1 files changed, 380 insertions, 124 deletions
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c
index 33cc8d39f1..9c73958883 100644
--- a/src/lib/libc/net/res_init.c
+++ b/src/lib/libc/net/res_init.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $ */ 1/* $OpenBSD: res_init.c,v 1.29 2003/06/02 20:18:36 millert Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1989, 1993
5 * -
4 * Copyright (c) 1985, 1989, 1993 6 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -12,14 +14,10 @@
12 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. Neither the name of the University nor the names of its contributors
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 18 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 19 * without specific prior written permission.
22 * 20 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -53,80 +51,181 @@
53 * --Copyright-- 51 * --Copyright--
54 */ 52 */
55 53
54#ifndef INET6
55#define INET6
56#endif
57
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 60static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
59static char rcsid[] = "$Id: res_init.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; 61static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $"; 63static char rcsid[] = "$OpenBSD: res_init.c,v 1.29 2003/06/02 20:18:36 millert Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67#include <sys/types.h>
65#include <sys/param.h> 68#include <sys/param.h>
66#include <sys/socket.h> 69#include <sys/socket.h>
70#include <sys/time.h>
67#include <netinet/in.h> 71#include <netinet/in.h>
68#include <arpa/inet.h> 72#include <arpa/inet.h>
69#include <arpa/nameser.h> 73#include <arpa/nameser.h>
74
75#include <stdio.h>
76#include <ctype.h>
70#include <resolv.h> 77#include <resolv.h>
71#include <unistd.h> 78#include <unistd.h>
72#include <stdio.h>
73#include <stdlib.h> 79#include <stdlib.h>
74#include <string.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
110static void res_setoptions(char *, char *);
75 111
76static void res_setoptions __P((char *, char *)); 112#ifdef RESOLVSORT
77static u_int32_t net_mask __P((struct in_addr)); 113static const char sort_mask[] = "/&";
114#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
115static u_int32_t net_mask(struct in_addr);
116#endif
78 117
79/* 118/*
80 * Resolver state default settings 119 * Resolver state default settings.
81 */ 120 */
121volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res) = {
122 PTHREAD_ONCE_INIT, 0
123};
82 124
83struct __res_state _res = { 125struct __res_state _res
84 RES_TIMEOUT, /* retransmition time interval */ 126# if defined(__BIND_RES_TEXT)
85 4, /* number of times to retransmit */ 127 = { RES_TIMEOUT, } /* Motorola, et al. */
86 RES_DEFAULT, /* options flags */ 128# endif
87 1, /* number of name servers */ 129 ;
130#ifdef INET6
131volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res_ext) = {
132 PTHREAD_ONCE_INIT, 0
88}; 133};
89 134
135struct __res_state_ext _res_ext;
136#endif /* INET6 */
137
90/* 138/*
91 * Set up default settings. If the configuration file exist, the values 139 * Set up default settings. If the configuration file exist, the values
92 * there will have precedence. Otherwise, the server address is set to 140 * there will have precedence. Otherwise, the server address is set to
93 * INADDR_ANY and the default domain name comes from the gethostname(). 141 * INADDR_ANY and the default domain name comes from the gethostname().
94 * 142 *
95 * The configuration file should only be used if you want to redefine your 143 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
96 * domain or run without a server on your machine. 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.
97 * 156 *
98 * Return 0 if completes successfully, -1 on error 157 * Return 0 if completes successfully, -1 on error
99 */ 158 */
159int
100res_init() 160res_init()
101{ 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
102 register FILE *fp; 167 register FILE *fp;
103 register char *cp, **pp, *net; 168 register char *cp, **pp;
104 register int n; 169 register int n;
105 char buf[BUFSIZ], buf2[BUFSIZ]; 170 char buf[BUFSIZ];
106 int nserv = 0; /* number of nameserver records read from file */ 171 int nserv = 0; /* number of nameserver records read from file */
107 int haveenv = 0; 172 int haveenv = 0;
108 int havesearch = 0; 173 int havesearch = 0;
174 size_t len;
175#ifdef RESOLVSORT
109 int nsort = 0; 176 int nsort = 0;
110 u_long mask; 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;
111 208
112 _res.nsaddr.sin_len = sizeof(struct sockaddr_in);
113 _res.nsaddr.sin_family = AF_INET;
114 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
115#ifdef USELOOPBACK 209#ifdef USELOOPBACK
116 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 210 _resp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
117#else 211#else
118 _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 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);
119#endif 220#endif
120 _res.nscount = 1; 221 _resp->nscount = 1;
121 _res.ndots = 1; 222 _resp->ndots = 1;
122 _res.pfcode = 0; 223 _resp->pfcode = 0;
123 strncpy(_res.lookups, "f", sizeof _res.lookups); 224 strlcpy(_resp->lookups, "f", sizeof _resp->lookups);
124 225
125 /* Allow user to override the local domain definition */ 226 /* Allow user to override the local domain definition */
126 if ((cp = getenv("LOCALDOMAIN")) != NULL) { 227 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
127 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); 228 strlcpy(_resp->defdname, cp, sizeof(_resp->defdname));
128 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
129 *cp = '\0';
130 haveenv++; 229 haveenv++;
131 230
132 /* 231 /*
@@ -136,11 +235,11 @@ res_init()
136 * one that they want to use as an individual (even more 235 * one that they want to use as an individual (even more
137 * important now that the rfc1535 stuff restricts searches) 236 * important now that the rfc1535 stuff restricts searches)
138 */ 237 */
139 cp = _res.defdname; 238 cp = _resp->defdname;
140 pp = _res.dnsrch; 239 pp = _resp->dnsrch;
141 *pp++ = cp; 240 *pp++ = cp;
142 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { 241 for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) {
143 if (*cp == '\n') /* silly backwards compat */ 242 if (*cp == '\n') /* silly backwards compat */
144 break; 243 break;
145 else if (*cp == ' ' || *cp == '\t') { 244 else if (*cp == ' ' || *cp == '\t') {
146 *cp = 0; 245 *cp = 0;
@@ -158,16 +257,29 @@ res_init()
158 *pp++ = 0; 257 *pp++ = 0;
159 } 258 }
160 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
161 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 265 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
162 strncpy(_res.lookups, "bf", sizeof _res.lookups); 266 strlcpy(_resp->lookups, "bf", sizeof _resp->lookups);
163 267
164 /* read the config file */ 268 /* read the config file */
165 while (fgets(buf, sizeof(buf), fp) != NULL) { 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';
166 /* skip comments */ 276 /* skip comments */
167 if ((*buf == ';') || (*buf == '#')) 277 if ((cp = strpbrk(buf, ";#")) != NULL)
278 *cp = '\0';
279 if (buf[0] == '\0')
168 continue; 280 continue;
169 /* read default domain name */ 281 /* read default domain name */
170 if (!strncmp(buf, "domain", sizeof("domain") - 1)) { 282 if (MATCH(buf, "domain")) {
171 if (haveenv) /* skip if have from environ */ 283 if (haveenv) /* skip if have from environ */
172 continue; 284 continue;
173 cp = buf + sizeof("domain") - 1; 285 cp = buf + sizeof("domain") - 1;
@@ -175,18 +287,17 @@ res_init()
175 cp++; 287 cp++;
176 if ((*cp == '\0') || (*cp == '\n')) 288 if ((*cp == '\0') || (*cp == '\n'))
177 continue; 289 continue;
178 (void)strncpy(_res.defdname, cp, 290 strlcpy(_resp->defdname, cp, sizeof(_resp->defdname));
179 sizeof(_res.defdname) - 1); 291 if ((cp = strpbrk(_resp->defdname, " \t\n")) != NULL)
180 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
181 *cp = '\0'; 292 *cp = '\0';
182 havesearch = 0; 293 havesearch = 0;
183 continue; 294 continue;
184 } 295 }
185 /* lookup types */ 296 /* lookup types */
186 if (!strncmp(buf, "lookup", sizeof("lookup") -1)) { 297 if (MATCH(buf, "lookup")) {
187 char *sp = NULL; 298 char *sp = NULL;
188 299
189 bzero(_res.lookups, sizeof _res.lookups); 300 bzero(_resp->lookups, sizeof _resp->lookups);
190 cp = buf + sizeof("lookup") - 1; 301 cp = buf + sizeof("lookup") - 1;
191 for (n = 0;; cp++) { 302 for (n = 0;; cp++) {
192 if (n == MAXDNSLUS) 303 if (n == MAXDNSLUS)
@@ -194,14 +305,14 @@ res_init()
194 if ((*cp == '\0') || (*cp == '\n')) { 305 if ((*cp == '\0') || (*cp == '\n')) {
195 if (sp) { 306 if (sp) {
196 if (*sp=='y' || *sp=='b' || *sp=='f') 307 if (*sp=='y' || *sp=='b' || *sp=='f')
197 _res.lookups[n++] = *sp; 308 _resp->lookups[n++] = *sp;
198 sp = NULL; 309 sp = NULL;
199 } 310 }
200 break; 311 break;
201 } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) { 312 } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) {
202 if (sp) { 313 if (sp) {
203 if (*sp=='y' || *sp=='b' || *sp=='f') 314 if (*sp=='y' || *sp=='b' || *sp=='f')
204 _res.lookups[n++] = *sp; 315 _resp->lookups[n++] = *sp;
205 sp = NULL; 316 sp = NULL;
206 } 317 }
207 } else if (sp == NULL) 318 } else if (sp == NULL)
@@ -210,7 +321,7 @@ res_init()
210 continue; 321 continue;
211 } 322 }
212 /* set search list */ 323 /* set search list */
213 if (!strncmp(buf, "search", sizeof("search") - 1)) { 324 if (MATCH(buf, "search")) {
214 if (haveenv) /* skip if have from environ */ 325 if (haveenv) /* skip if have from environ */
215 continue; 326 continue;
216 cp = buf + sizeof("search") - 1; 327 cp = buf + sizeof("search") - 1;
@@ -218,18 +329,17 @@ res_init()
218 cp++; 329 cp++;
219 if ((*cp == '\0') || (*cp == '\n')) 330 if ((*cp == '\0') || (*cp == '\n'))
220 continue; 331 continue;
221 (void)strncpy(_res.defdname, cp, 332 strlcpy(_resp->defdname, cp, sizeof(_resp->defdname));
222 sizeof(_res.defdname) - 1); 333 if ((cp = strchr(_resp->defdname, '\n')) != NULL)
223 if ((cp = strchr(_res.defdname, '\n')) != NULL)
224 *cp = '\0'; 334 *cp = '\0';
225 /* 335 /*
226 * Set search list to be blank-separated strings 336 * Set search list to be blank-separated strings
227 * on rest of line. 337 * on rest of line.
228 */ 338 */
229 cp = _res.defdname; 339 cp = _resp->defdname;
230 pp = _res.dnsrch; 340 pp = _resp->dnsrch;
231 *pp++ = cp; 341 *pp++ = cp;
232 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { 342 for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) {
233 if (*cp == ' ' || *cp == '\t') { 343 if (*cp == ' ' || *cp == '\t') {
234 *cp = 0; 344 *cp = 0;
235 n = 1; 345 n = 1;
@@ -247,130 +357,273 @@ res_init()
247 continue; 357 continue;
248 } 358 }
249 /* read nameservers to query */ 359 /* read nameservers to query */
250 if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && 360 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
251 nserv < MAXNS) { 361#ifdef INET6
252 struct in_addr a; 362 char *q;
363 struct addrinfo hints, *res;
364 char pbuf[NI_MAXSERV];
365#else
366 struct in_addr a;
367#endif /* INET6 */
253 368
254 cp = buf + sizeof("nameserver") - 1; 369 cp = buf + sizeof("nameserver") - 1;
255 while (*cp == ' ' || *cp == '\t') 370 while (*cp == ' ' || *cp == '\t')
256 cp++; 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 */
257 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { 407 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
258 _res.nsaddr_list[nserv].sin_len = sizeof(struct sockaddr_in); 408 _resp->nsaddr_list[nserv].sin_addr = a;
259 _res.nsaddr_list[nserv].sin_family = AF_INET; 409 _resp->nsaddr_list[nserv].sin_family = AF_INET;
260 _res.nsaddr_list[nserv].sin_port = 410 _resp->nsaddr_list[nserv].sin_port =
261 htons(NAMESERVER_PORT); 411 htons(NAMESERVER_PORT);
262 _res.nsaddr_list[nserv].sin_addr = a; 412 _resp->nsaddr_list[nserv].sin_len =
413 sizeof(struct sockaddr_in);
263 nserv++; 414 nserv++;
264 } 415 }
416#endif /* INET6 */
265 continue; 417 continue;
266 } 418 }
267 if (!strncmp(buf, "sortlist", sizeof("sortlist") - 1)) { 419#ifdef RESOLVSORT
420 if (MATCH(buf, "sortlist")) {
268 struct in_addr a; 421 struct in_addr a;
422#ifdef INET6
423 struct in6_addr a6;
424 int m, i;
425 u_char *u;
426#endif /* INET6 */
269 427
270 cp = buf + sizeof("sortlist") - 1; 428 cp = buf + sizeof("sortlist") - 1;
271 while (*cp == ' ' || *cp == '\t') 429 while (nsort < MAXRESOLVSORT) {
272 cp++; 430 while (*cp == ' ' || *cp == '\t')
273 while (sscanf(cp,"%[0-9./]s", buf2) && nsort < MAXRESOLVSORT) { 431 cp++;
274 if (net = strchr(buf2, '/')) 432 if (*cp == '\0' || *cp == '\n' || *cp == ';')
275 *net = '\0'; 433 break;
276 if (inet_aton(buf2, &a)) { 434 net = cp;
277 _res.sort_list[nsort].addr = a; 435 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
278 if (net && inet_aton(net+1, &a)) { 436 isascii(*cp) && !isspace(*cp))
279 _res.sort_list[nsort].mask = a.s_addr; 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 }
280 } else { 456 } else {
281 _res.sort_list[nsort].mask = 457 _resp->sort_list[nsort].mask =
282 net_mask(_res.sort_list[nsort].addr); 458 net_mask(_resp->sort_list[nsort].addr);
283 } 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 */
284 nsort++; 467 nsort++;
285 } 468 }
286 if (net) 469#ifdef INET6
287 *net = '/'; 470 else if (inet_pton(AF_INET6, net, &a6) == 1) {
288 cp += strlen(buf2); 471 _res_extp->sort_list[nsort].af = AF_INET6;
289 while (*cp == ' ' || *cp == '\t') 472 _res_extp->sort_list[nsort].addr.in6a = a6;
290 cp++; 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;
291 } 513 }
292 continue; 514 continue;
293 } 515 }
294 if (!strncmp(buf, "options", sizeof("options") -1)) { 516#endif
517 if (MATCH(buf, "options")) {
295 res_setoptions(buf + sizeof("options") - 1, "conf"); 518 res_setoptions(buf + sizeof("options") - 1, "conf");
296 continue; 519 continue;
297 } 520 }
298 } 521 }
299 if (nserv > 1) 522 if (nserv > 1)
300 _res.nscount = nserv; 523 _resp->nscount = nserv;
301 _res.nsort = nsort; 524#ifdef RESOLVSORT
525 _resp->nsort = nsort;
526#endif
302 (void) fclose(fp); 527 (void) fclose(fp);
303 } 528 }
304 if (_res.defdname[0] == 0) { 529 if (_resp->defdname[0] == 0 &&
305 if (gethostname(buf, sizeof(_res.defdname) - 1) == 0 && 530 gethostname(buf, sizeof(_resp->defdname) - 1) == 0 &&
306 (cp = strchr(buf, '.'))) 531 (cp = strchr(buf, '.')) != NULL)
307 (void)strcpy(_res.defdname, cp + 1); 532 {
533 strlcpy(_resp->defdname, cp + 1,
534 sizeof(_resp->defdname));
308 } 535 }
309 536
310 /* find components of local domain that might be searched */ 537 /* find components of local domain that might be searched */
311 if (havesearch == 0) { 538 if (havesearch == 0) {
312 pp = _res.dnsrch; 539 pp = _resp->dnsrch;
313 *pp++ = _res.defdname; 540 *pp++ = _resp->defdname;
314#ifndef SEARCH_LOCAL_DOMAINS
315 *pp = NULL; 541 *pp = NULL;
316#else 542
317 for (cp = _res.defdname, n = 0; *cp; cp++) 543#ifndef RFC1535
318 if (*cp == '.') 544 dots = 0;
319 n++; 545 for (cp = _resp->defdname; *cp; cp++)
320 cp = _res.defdname; 546 dots += (*cp == '.');
321 for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; 547
322 n--) { 548 cp = _resp->defdname;
323 cp = strchr(cp, '.'); 549 while (pp < _resp->dnsrch + MAXDFLSRCH) {
324 *pp++ = ++cp; 550 if (dots < LOCALDOMAINPARTS)
551 break;
552 cp = strchr(cp, '.') + 1; /* we know there is one */
553 *pp++ = cp;
554 dots--;
325 } 555 }
326 *pp++ = 0; 556 *pp = NULL;
327#endif 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 */
328 } 566 }
329 567
330 if ((cp = getenv("RES_OPTIONS")) != NULL) 568 if (issetugid())
569 _resp->options |= RES_NOALIASES;
570 else if ((cp = getenv("RES_OPTIONS")) != NULL)
331 res_setoptions(cp, "env"); 571 res_setoptions(cp, "env");
332 _res.options |= RES_INIT; 572 _resp->options |= RES_INIT;
333 return (0); 573 return (0);
334} 574}
335 575
576/* ARGSUSED */
336static void 577static void
337res_setoptions(options, source) 578res_setoptions(options, source)
338 char *options, *source; 579 char *options, *source;
339{ 580{
581 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
340 char *cp = options; 582 char *cp = options;
341 int i; 583 char *endp;
584 long l;
342 585
343#ifdef DEBUG 586#ifdef DEBUG
344 if (_res.options & RES_DEBUG) { 587 if (_resp->options & RES_DEBUG)
345 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 588 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
346 options, source); 589 options, source);
347 }
348#endif 590#endif
349 while (*cp) { 591 while (*cp) {
350 /* skip leading and inner runs of spaces */ 592 /* skip leading and inner runs of spaces */
351 while (*cp == ' ' || *cp == '\t') 593 while (*cp == ' ' || *cp == '\t')
352 cp++; 594 cp++;
353 /* search for and process individual options */ 595 /* search for and process individual options */
354 if (!strncmp(cp, "ndots:", sizeof("ndots:")-1)) { 596 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
355 i = atoi(cp + sizeof("ndots:") - 1); 597 char *p = cp + sizeof("ndots:") - 1;
356 if (i <= RES_MAXNDOTS) 598 l = strtol(p, &endp, 10);
357 _res.ndots = i; 599 if (l >= 0 && endp != p &&
358 else 600 (*endp = '\0' || isspace(*endp))) {
359 _res.ndots = RES_MAXNDOTS; 601 if (l <= RES_MAXNDOTS)
602 _resp->ndots = l;
603 else
604 _resp->ndots = RES_MAXNDOTS;
360#ifdef DEBUG 605#ifdef DEBUG
361 if (_res.options & RES_DEBUG) { 606 if (_resp->options & RES_DEBUG)
362 printf(";;\tndots=%d\n", _res.ndots); 607 printf(";;\tndots=%u\n", _resp->ndots);
363 }
364#endif 608#endif
365 } else if (!strncmp(cp, "debug", sizeof("debug")-1)) { 609 }
610 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
366#ifdef DEBUG 611#ifdef DEBUG
367 if (!(_res.options & RES_DEBUG)) { 612 if (!(_resp->options & RES_DEBUG)) {
368 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 613 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
369 options, source); 614 options, source);
370 _res.options |= RES_DEBUG; 615 _resp->options |= RES_DEBUG;
371 } 616 }
372 printf(";;\tdebug\n"); 617 printf(";;\tdebug\n");
373#endif 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;
374 } else { 627 } else {
375 /* XXX - print a warning here? */ 628 /* XXX - print a warning here? */
376 } 629 }
@@ -380,6 +633,8 @@ res_setoptions(options, source)
380 } 633 }
381} 634}
382 635
636#ifdef RESOLVSORT
637/* XXX - should really support CIDR which means explicit masks always. */
383static u_int32_t 638static u_int32_t
384net_mask(in) /* XXX - should really use system's version of this */ 639net_mask(in) /* XXX - should really use system's version of this */
385 struct in_addr in; 640 struct in_addr in;
@@ -388,7 +643,8 @@ net_mask(in) /* XXX - should really use system's version of this */
388 643
389 if (IN_CLASSA(i)) 644 if (IN_CLASSA(i))
390 return (htonl(IN_CLASSA_NET)); 645 return (htonl(IN_CLASSA_NET));
391 if (IN_CLASSB(i)) 646 else if (IN_CLASSB(i))
392 return (htonl(IN_CLASSB_NET)); 647 return (htonl(IN_CLASSB_NET));
393 return (htonl(IN_CLASSC_NET)); 648 return (htonl(IN_CLASSC_NET));
394} 649}
650#endif