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