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