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