diff options
Diffstat (limited to 'src/lib/libc/net/res_init.c')
| -rw-r--r-- | src/lib/libc/net/res_init.c | 707 |
1 files changed, 0 insertions, 707 deletions
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c deleted file mode 100644 index 18de3550e6..0000000000 --- a/src/lib/libc/net/res_init.c +++ /dev/null | |||
| @@ -1,707 +0,0 @@ | |||
| 1 | /* $OpenBSD: res_init.c,v 1.40 2009/06/05 09:52:26 pyr 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 | #include <sys/types.h> | ||
| 59 | #include <sys/param.h> | ||
| 60 | #include <sys/socket.h> | ||
| 61 | #include <sys/time.h> | ||
| 62 | #include <sys/stat.h> | ||
| 63 | #include <netinet/in.h> | ||
| 64 | #include <arpa/inet.h> | ||
| 65 | #include <arpa/nameser.h> | ||
| 66 | |||
| 67 | #include <stdio.h> | ||
| 68 | #include <ctype.h> | ||
| 69 | #include <resolv.h> | ||
| 70 | #include <unistd.h> | ||
| 71 | #include <stdlib.h> | ||
| 72 | #include <string.h> | ||
| 73 | #ifdef INET6 | ||
| 74 | #include <netdb.h> | ||
| 75 | #endif /* INET6 */ | ||
| 76 | |||
| 77 | #include "thread_private.h" | ||
| 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 | ||
| 109 | |||
| 110 | /* | ||
| 111 | * Resolver state default settings. | ||
| 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); | ||
| 122 | |||
| 123 | struct __res_state_ext _res_ext; | ||
| 124 | #endif /* INET6 */ | ||
| 125 | |||
| 126 | int __res_chktime = 30; | ||
| 127 | |||
| 128 | /* | ||
| 129 | * Set up default settings. If the configuration file exist, the values | ||
| 130 | * there will have precedence. Otherwise, the server address is set to | ||
| 131 | * INADDR_ANY and the default domain name comes from the gethostname(). | ||
| 132 | * | ||
| 133 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 | ||
| 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. | ||
| 146 | * | ||
| 147 | * Return 0 if completes successfully, -1 on error | ||
| 148 | */ | ||
| 149 | int | ||
| 150 | res_init(void) | ||
| 151 | { | ||
| 152 | |||
| 153 | return (_res_init(1)); | ||
| 154 | } | ||
| 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]; | ||
| 170 | int nserv = 0; /* number of nameserver records read from file */ | ||
| 171 | int haveenv = 0; | ||
| 172 | int havesearch = 0; | ||
| 173 | size_t len; | ||
| 174 | #ifdef RESOLVSORT | ||
| 175 | int nsort = 0; | ||
| 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; | ||
| 227 | |||
| 228 | #ifdef USELOOPBACK | ||
| 229 | _resp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); | ||
| 230 | #else | ||
| 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); | ||
| 239 | #endif | ||
| 240 | _resp->nscount = 1; | ||
| 241 | _resp->ndots = 1; | ||
| 242 | _resp->pfcode = 0; | ||
| 243 | strlcpy(_resp->lookups, "f", sizeof _resp->lookups); | ||
| 244 | |||
| 245 | /* Allow user to override the local domain definition */ | ||
| 246 | if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { | ||
| 247 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
| 248 | haveenv++; | ||
| 249 | |||
| 250 | /* | ||
| 251 | * Set search list to be blank-separated strings | ||
| 252 | * from rest of env value. Permits users of LOCALDOMAIN | ||
| 253 | * to still have a search list, and anyone to set the | ||
| 254 | * one that they want to use as an individual (even more | ||
| 255 | * important now that the rfc1535 stuff restricts searches) | ||
| 256 | */ | ||
| 257 | cp = _resp->defdname; | ||
| 258 | pp = _resp->dnsrch; | ||
| 259 | *pp++ = cp; | ||
| 260 | for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) { | ||
| 261 | if (*cp == '\n') /* silly backwards compat */ | ||
| 262 | break; | ||
| 263 | else if (*cp == ' ' || *cp == '\t') { | ||
| 264 | *cp = 0; | ||
| 265 | n = 1; | ||
| 266 | } else if (n) { | ||
| 267 | *pp++ = cp; | ||
| 268 | n = 0; | ||
| 269 | havesearch = 1; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | /* null terminate last domain if there are excess */ | ||
| 273 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') | ||
| 274 | cp++; | ||
| 275 | *cp = '\0'; | ||
| 276 | *pp++ = 0; | ||
| 277 | } | ||
| 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; | ||
| 287 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { | ||
| 288 | strlcpy(_resp->lookups, "bf", sizeof _resp->lookups); | ||
| 289 | |||
| 290 | /* read the config file */ | ||
| 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'; | ||
| 298 | /* skip comments */ | ||
| 299 | if ((cp = strpbrk(buf, ";#")) != NULL) | ||
| 300 | *cp = '\0'; | ||
| 301 | if (buf[0] == '\0') | ||
| 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 | } | ||
| 338 | /* read default domain name */ | ||
| 339 | if (MATCH(buf, "domain")) { | ||
| 340 | if (haveenv) /* skip if have from environ */ | ||
| 341 | continue; | ||
| 342 | cp = buf + sizeof("domain") - 1; | ||
| 343 | while (*cp == ' ' || *cp == '\t') | ||
| 344 | cp++; | ||
| 345 | if ((*cp == '\0') || (*cp == '\n')) | ||
| 346 | continue; | ||
| 347 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
| 348 | if ((cp = strpbrk(_resp->defdname, " \t\n")) != NULL) | ||
| 349 | *cp = '\0'; | ||
| 350 | havesearch = 0; | ||
| 351 | continue; | ||
| 352 | } | ||
| 353 | /* lookup types */ | ||
| 354 | if (MATCH(buf, "lookup")) { | ||
| 355 | char *sp = NULL; | ||
| 356 | |||
| 357 | bzero(_resp->lookups, sizeof _resp->lookups); | ||
| 358 | cp = buf + sizeof("lookup") - 1; | ||
| 359 | for (n = 0;; cp++) { | ||
| 360 | if (n == MAXDNSLUS) | ||
| 361 | break; | ||
| 362 | if ((*cp == '\0') || (*cp == '\n')) { | ||
| 363 | if (sp) { | ||
| 364 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
| 365 | _resp->lookups[n++] = *sp; | ||
| 366 | sp = NULL; | ||
| 367 | } | ||
| 368 | break; | ||
| 369 | } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) { | ||
| 370 | if (sp) { | ||
| 371 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
| 372 | _resp->lookups[n++] = *sp; | ||
| 373 | sp = NULL; | ||
| 374 | } | ||
| 375 | } else if (sp == NULL) | ||
| 376 | sp = cp; | ||
| 377 | } | ||
| 378 | continue; | ||
| 379 | } | ||
| 380 | /* set search list */ | ||
| 381 | if (MATCH(buf, "search")) { | ||
| 382 | if (haveenv) /* skip if have from environ */ | ||
| 383 | continue; | ||
| 384 | cp = buf + sizeof("search") - 1; | ||
| 385 | while (*cp == ' ' || *cp == '\t') | ||
| 386 | cp++; | ||
| 387 | if ((*cp == '\0') || (*cp == '\n')) | ||
| 388 | continue; | ||
| 389 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
| 390 | if ((cp = strchr(_resp->defdname, '\n')) != NULL) | ||
| 391 | *cp = '\0'; | ||
| 392 | /* | ||
| 393 | * Set search list to be blank-separated strings | ||
| 394 | * on rest of line. | ||
| 395 | */ | ||
| 396 | cp = _resp->defdname; | ||
| 397 | pp = _resp->dnsrch; | ||
| 398 | *pp++ = cp; | ||
| 399 | for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) { | ||
| 400 | if (*cp == ' ' || *cp == '\t') { | ||
| 401 | *cp = 0; | ||
| 402 | n = 1; | ||
| 403 | } else if (n) { | ||
| 404 | *pp++ = cp; | ||
| 405 | n = 0; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | /* null terminate last domain if there are excess */ | ||
| 409 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') | ||
| 410 | cp++; | ||
| 411 | *cp = '\0'; | ||
| 412 | *pp++ = 0; | ||
| 413 | havesearch = 1; | ||
| 414 | continue; | ||
| 415 | } | ||
| 416 | /* read nameservers to query */ | ||
| 417 | if (MATCH(buf, "nameserver") && nserv < MAXNS) { | ||
| 418 | char *q; | ||
| 419 | struct addrinfo hints, *res; | ||
| 420 | char pbuf[NI_MAXSERV]; | ||
| 421 | |||
| 422 | cp = buf + sizeof("nameserver") - 1; | ||
| 423 | while (*cp == ' ' || *cp == '\t') | ||
| 424 | cp++; | ||
| 425 | if ((*cp == '\0') || (*cp == '\n')) | ||
| 426 | continue; | ||
| 427 | for (q = cp; *q; q++) { | ||
| 428 | if (isspace(*q)) { | ||
| 429 | *q = '\0'; | ||
| 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 | } | ||
| 470 | nserv++; | ||
| 471 | } | ||
| 472 | if (res) | ||
| 473 | freeaddrinfo(res); | ||
| 474 | continue; | ||
| 475 | } | ||
| 476 | #ifdef RESOLVSORT | ||
| 477 | if (MATCH(buf, "sortlist")) { | ||
| 478 | struct in_addr a; | ||
| 479 | #ifdef INET6 | ||
| 480 | struct in6_addr a6; | ||
| 481 | int m, i; | ||
| 482 | u_char *u; | ||
| 483 | #endif /* INET6 */ | ||
| 484 | |||
| 485 | cp = buf + sizeof("sortlist") - 1; | ||
| 486 | while (nsort < MAXRESOLVSORT) { | ||
| 487 | while (*cp == ' ' || *cp == '\t') | ||
| 488 | cp++; | ||
| 489 | if (*cp == '\0' || *cp == '\n' || *cp == ';') | ||
| 490 | break; | ||
| 491 | net = cp; | ||
| 492 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && | ||
| 493 | isascii(*cp) && !isspace(*cp)) | ||
| 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 | } | ||
| 513 | } else { | ||
| 514 | _resp->sort_list[nsort].mask = | ||
| 515 | net_mask(_resp->sort_list[nsort].addr); | ||
| 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 */ | ||
| 524 | nsort++; | ||
| 525 | } | ||
| 526 | #ifdef INET6 | ||
| 527 | else if (inet_pton(AF_INET6, net, &a6) == 1) { | ||
| 528 | _res_extp->sort_list[nsort].af = AF_INET6; | ||
| 529 | _res_extp->sort_list[nsort].addr.in6a = a6; | ||
| 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; | ||
| 570 | } | ||
| 571 | continue; | ||
| 572 | } | ||
| 573 | #endif | ||
| 574 | if (MATCH(buf, "options")) { | ||
| 575 | res_setoptions(buf + sizeof("options") - 1, "conf"); | ||
| 576 | continue; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | if (nserv > 1) | ||
| 580 | _resp->nscount = nserv; | ||
| 581 | #ifdef RESOLVSORT | ||
| 582 | _resp->nsort = nsort; | ||
| 583 | #endif | ||
| 584 | (void) fclose(fp); | ||
| 585 | } | ||
| 586 | if (_resp->defdname[0] == 0 && | ||
| 587 | gethostname(buf, sizeof(_resp->defdname) - 1) == 0 && | ||
| 588 | (cp = strchr(buf, '.')) != NULL) | ||
| 589 | { | ||
| 590 | strlcpy(_resp->defdname, cp + 1, | ||
| 591 | sizeof(_resp->defdname)); | ||
| 592 | } | ||
| 593 | |||
| 594 | /* find components of local domain that might be searched */ | ||
| 595 | if (havesearch == 0) { | ||
| 596 | pp = _resp->dnsrch; | ||
| 597 | *pp++ = _resp->defdname; | ||
| 598 | *pp = NULL; | ||
| 599 | |||
| 600 | #ifndef RFC1535 | ||
| 601 | dots = 0; | ||
| 602 | for (cp = _resp->defdname; *cp; cp++) | ||
| 603 | dots += (*cp == '.'); | ||
| 604 | |||
| 605 | cp = _resp->defdname; | ||
| 606 | while (pp < _resp->dnsrch + MAXDFLSRCH) { | ||
| 607 | if (dots < LOCALDOMAINPARTS) | ||
| 608 | break; | ||
| 609 | cp = strchr(cp, '.') + 1; /* we know there is one */ | ||
| 610 | *pp++ = cp; | ||
| 611 | dots--; | ||
| 612 | } | ||
| 613 | *pp = NULL; | ||
| 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 */ | ||
| 623 | } | ||
| 624 | |||
| 625 | if (issetugid()) | ||
| 626 | _resp->options |= RES_NOALIASES; | ||
| 627 | else if ((cp = getenv("RES_OPTIONS")) != NULL) | ||
| 628 | res_setoptions(cp, "env"); | ||
| 629 | _resp->options |= RES_INIT; | ||
| 630 | return (0); | ||
| 631 | } | ||
| 632 | |||
| 633 | /* ARGSUSED */ | ||
| 634 | static void | ||
| 635 | res_setoptions(char *options, char *source) | ||
| 636 | { | ||
| 637 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
| 638 | char *cp = options; | ||
| 639 | char *endp; | ||
| 640 | long l; | ||
| 641 | |||
| 642 | #ifdef DEBUG | ||
| 643 | if (_resp->options & RES_DEBUG) | ||
| 644 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", | ||
| 645 | options, source); | ||
| 646 | #endif | ||
| 647 | while (*cp) { | ||
| 648 | /* skip leading and inner runs of spaces */ | ||
| 649 | while (*cp == ' ' || *cp == '\t') | ||
| 650 | cp++; | ||
| 651 | /* search for and process individual options */ | ||
| 652 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { | ||
| 653 | char *p = cp + sizeof("ndots:") - 1; | ||
| 654 | l = strtol(p, &endp, 10); | ||
| 655 | if (l >= 0 && endp != p && | ||
| 656 | (*endp = '\0' || isspace(*endp))) { | ||
| 657 | if (l <= RES_MAXNDOTS) | ||
| 658 | _resp->ndots = l; | ||
| 659 | else | ||
| 660 | _resp->ndots = RES_MAXNDOTS; | ||
| 661 | #ifdef DEBUG | ||
| 662 | if (_resp->options & RES_DEBUG) | ||
| 663 | printf(";;\tndots=%u\n", _resp->ndots); | ||
| 664 | #endif | ||
| 665 | } | ||
| 666 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { | ||
| 667 | #ifdef DEBUG | ||
| 668 | if (!(_resp->options & RES_DEBUG)) { | ||
| 669 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", | ||
| 670 | options, source); | ||
| 671 | _resp->options |= RES_DEBUG; | ||
| 672 | } | ||
| 673 | printf(";;\tdebug\n"); | ||
| 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; | ||
| 685 | } else { | ||
| 686 | /* XXX - print a warning here? */ | ||
| 687 | } | ||
| 688 | /* skip to next run of spaces */ | ||
| 689 | while (*cp && *cp != ' ' && *cp != '\t') | ||
| 690 | cp++; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | |||
| 694 | #ifdef RESOLVSORT | ||
| 695 | /* XXX - should really support CIDR which means explicit masks always. */ | ||
| 696 | static u_int32_t | ||
| 697 | net_mask(struct in_addr in) /* XXX - should really use system's version of this */ | ||
| 698 | { | ||
| 699 | u_int32_t i = ntohl(in.s_addr); | ||
| 700 | |||
| 701 | if (IN_CLASSA(i)) | ||
| 702 | return (htonl(IN_CLASSA_NET)); | ||
| 703 | else if (IN_CLASSB(i)) | ||
| 704 | return (htonl(IN_CLASSB_NET)); | ||
| 705 | return (htonl(IN_CLASSC_NET)); | ||
| 706 | } | ||
| 707 | #endif | ||
