(_nl_C_LC_CTYPE_class): Clear ISCNTRL bit for character >= 0x80. (_nl_C_LC_CTYPE_clas...
[glibc.git] / resolv / res_init.c
blob893072f24e93e5632f79aa705522408ed8ce2083
1 /*
2 * ++Copyright++ 1985, 1989, 1993
3 * -
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
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
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 * -
53 * --Copyright--
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <sys/time.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <arpa/nameser.h>
69 #include <stdio.h>
70 #include <ctype.h>
71 #include <resolv.h>
72 #if defined(BSD) && (BSD >= 199103)
73 # include <unistd.h>
74 # include <stdlib.h>
75 # include <string.h>
76 #else
77 # include "../conf/portability.h"
78 #endif
80 /*-------------------------------------- info about "sortlist" --------------
81 * Marc Majka 1994/04/16
82 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
84 * NetInfo resolver configuration directory support.
86 * Allow a NetInfo directory to be created in the hierarchy which
87 * contains the same information as the resolver configuration file.
89 * - The local domain name is stored as the value of the "domain" property.
90 * - The Internet address(es) of the name server(s) are stored as values
91 * of the "nameserver" property.
92 * - The name server addresses are stored as values of the "nameserver"
93 * property.
94 * - The search list for host-name lookup is stored as values of the
95 * "search" property.
96 * - The sortlist comprised of IP address netmask pairs are stored as
97 * values of the "sortlist" property. The IP address and optional netmask
98 * should be separated by a slash (/) or ampersand (&) character.
99 * - Internal resolver variables can be set from the value of the "options"
100 * property.
102 #if defined(NeXT)
103 # include <netinfo/ni.h>
104 # define NI_PATH_RESCONF "/locations/resolver"
105 # define NI_TIMEOUT 10
106 static int netinfo_res_init __P((int *haveenv, int *havesearch));
107 #endif
109 #if defined(USE_OPTIONS_H)
110 # include "../conf/options.h"
111 #endif
113 static void res_setoptions __P((char *, char *)) internal_function;
115 #ifdef RESOLVSORT
116 static const char sort_mask[] = "/&";
117 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
118 static u_int32_t net_mask __P((struct in_addr));
119 #endif
121 #if !defined(isascii) /* XXX - could be a function */
122 # define isascii(c) (!(c & 0200))
123 #endif
126 * Resolver state default settings.
129 struct __res_state _res
130 # if defined(__BIND_RES_TEXT)
131 = { RES_TIMEOUT, } /* Motorola, et al. */
132 # endif
136 * Set up default settings. If the configuration file exist, the values
137 * there will have precedence. Otherwise, the server address is set to
138 * INADDR_ANY and the default domain name comes from the gethostname().
140 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
141 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
142 * since it was noted that INADDR_ANY actually meant ``the first interface
143 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
144 * it had to be "up" in order for you to reach your own name server. It
145 * was later decided that since the recommended practice is to always
146 * install local static routes through 127.0.0.1 for all your network
147 * interfaces, that we could solve this problem without a code change.
149 * The configuration file should always be used, since it is the only way
150 * to specify a default domain. If you are running a server on your local
151 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
152 * in the configuration file.
154 * Return 0 if completes successfully, -1 on error
157 res_init()
159 register FILE *fp;
160 register char *cp, **pp;
161 register int n;
162 char buf[MAXDNAME];
163 int nserv = 0; /* number of nameserver records read from file */
164 int haveenv = 0;
165 int havesearch = 0;
166 #ifdef RESOLVSORT
167 int nsort = 0;
168 char *net;
169 #endif
170 #ifndef RFC1535
171 int dots;
172 #endif
175 * These three fields used to be statically initialized. This made
176 * it hard to use this code in a shared library. It is necessary,
177 * now that we're doing dynamic initialization here, that we preserve
178 * the old semantics: if an application modifies one of these three
179 * fields of _res before res_init() is called, res_init() will not
180 * alter them. Of course, if an application is setting them to
181 * _zero_ before calling res_init(), hoping to override what used
182 * to be the static default, we can't detect it and unexpected results
183 * will follow. Zero for any of these fields would make no sense,
184 * so one can safely assume that the applications were already getting
185 * unexpected results.
187 * _res.options is tricky since some apps were known to diddle the bits
188 * before res_init() was first called. We can't replicate that semantic
189 * with dynamic initialization (they may have turned bits off that are
190 * set in RES_DEFAULT). Our solution is to declare such applications
191 * "broken". They could fool us by setting RES_INIT but none do (yet).
193 if (!_res.retrans)
194 _res.retrans = RES_TIMEOUT;
195 if (!_res.retry)
196 _res.retry = 4;
197 if (!(_res.options & RES_INIT))
198 _res.options = RES_DEFAULT;
201 * This one used to initialize implicitly to zero, so unless the app
202 * has set it to something in particular, we can randomize it now.
204 if (!_res.id)
205 _res.id = res_randomid();
207 #ifdef USELOOPBACK
208 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
209 _res.nscount = 1;
210 #else
211 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
212 _res.nscount = 0;
213 #endif
214 _res.nsaddr.sin_family = AF_INET;
215 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
216 _res.ndots = 1;
217 _res.pfcode = 0;
219 /* Allow user to override the local domain definition */
220 if ((cp = __secure_getenv("LOCALDOMAIN")) != NULL) {
221 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
222 haveenv++;
225 * Set search list to be blank-separated strings
226 * from rest of env value. Permits users of LOCALDOMAIN
227 * to still have a search list, and anyone to set the
228 * one that they want to use as an individual (even more
229 * important now that the rfc1535 stuff restricts searches)
231 cp = _res.defdname;
232 pp = _res.dnsrch;
233 *pp++ = cp;
234 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
235 if (*cp == '\n') /* silly backwards compat */
236 break;
237 else if (*cp == ' ' || *cp == '\t') {
238 *cp = 0;
239 n = 1;
240 } else if (n) {
241 *pp++ = cp;
242 n = 0;
243 havesearch = 1;
246 /* null terminate last domain if there are excess */
247 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
248 cp++;
249 *cp = '\0';
250 *pp++ = 0;
253 #define MATCH(line, name) \
254 (!strncmp(line, name, sizeof(name) - 1) && \
255 (line[sizeof(name) - 1] == ' ' || \
256 line[sizeof(name) - 1] == '\t'))
258 #ifdef NeXT
259 if (netinfo_res_init(&haveenv, &havesearch) == 0)
260 #endif
261 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
262 /* read the config file */
263 while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
264 /* skip comments */
265 if (*buf == ';' || *buf == '#')
266 continue;
267 /* read default domain name */
268 if (MATCH(buf, "domain")) {
269 if (haveenv) /* skip if have from environ */
270 continue;
271 cp = buf + sizeof("domain") - 1;
272 while (*cp == ' ' || *cp == '\t')
273 cp++;
274 if ((*cp == '\0') || (*cp == '\n'))
275 continue;
276 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
277 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
278 *cp = '\0';
279 havesearch = 0;
280 continue;
282 /* set search list */
283 if (MATCH(buf, "search")) {
284 if (haveenv) /* skip if have from environ */
285 continue;
286 cp = buf + sizeof("search") - 1;
287 while (*cp == ' ' || *cp == '\t')
288 cp++;
289 if ((*cp == '\0') || (*cp == '\n'))
290 continue;
291 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
292 if ((cp = strchr(_res.defdname, '\n')) != NULL)
293 *cp = '\0';
295 * Set search list to be blank-separated strings
296 * on rest of line.
298 cp = _res.defdname;
299 pp = _res.dnsrch;
300 *pp++ = cp;
301 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
302 if (*cp == ' ' || *cp == '\t') {
303 *cp = 0;
304 n = 1;
305 } else if (n) {
306 *pp++ = cp;
307 n = 0;
310 /* null terminate last domain if there are excess */
311 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
312 cp++;
313 *cp = '\0';
314 *pp++ = 0;
315 havesearch = 1;
316 continue;
318 /* read nameservers to query */
319 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
320 struct in_addr a;
322 cp = buf + sizeof("nameserver") - 1;
323 while (*cp == ' ' || *cp == '\t')
324 cp++;
325 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
326 _res.nsaddr_list[nserv].sin_addr = a;
327 _res.nsaddr_list[nserv].sin_family = AF_INET;
328 _res.nsaddr_list[nserv].sin_port =
329 htons(NAMESERVER_PORT);
330 nserv++;
332 continue;
334 #ifdef RESOLVSORT
335 if (MATCH(buf, "sortlist")) {
336 struct in_addr a;
338 cp = buf + sizeof("sortlist") - 1;
339 while (nsort < MAXRESOLVSORT) {
340 while (*cp == ' ' || *cp == '\t')
341 cp++;
342 if (*cp == '\0' || *cp == '\n' || *cp == ';')
343 break;
344 net = cp;
345 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
346 isascii(*cp) && !isspace(*cp))
347 cp++;
348 n = *cp;
349 *cp = 0;
350 if (inet_aton(net, &a)) {
351 _res.sort_list[nsort].addr = a;
352 if (ISSORTMASK(n)) {
353 *cp++ = n;
354 net = cp;
355 while (*cp && *cp != ';' &&
356 isascii(*cp) && !isspace(*cp))
357 cp++;
358 n = *cp;
359 *cp = 0;
360 if (inet_aton(net, &a)) {
361 _res.sort_list[nsort].mask = a.s_addr;
362 } else {
363 _res.sort_list[nsort].mask =
364 net_mask(_res.sort_list[nsort].addr);
366 } else {
367 _res.sort_list[nsort].mask =
368 net_mask(_res.sort_list[nsort].addr);
370 nsort++;
372 *cp = n;
374 continue;
376 #endif
377 if (MATCH(buf, "options")) {
378 res_setoptions(buf + sizeof("options") - 1, "conf");
379 continue;
382 if (nserv > _res.nscount)
383 _res.nscount = nserv;
384 #ifdef RESOLVSORT
385 _res.nsort = nsort;
386 #endif
387 (void) fclose(fp);
389 if (_res.defdname[0] == 0 &&
390 __gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
391 (cp = strchr(buf, '.')) != NULL)
392 strcpy(_res.defdname, cp + 1);
394 /* find components of local domain that might be searched */
395 if (havesearch == 0) {
396 pp = _res.dnsrch;
397 *pp++ = _res.defdname;
398 *pp = NULL;
400 #ifndef RFC1535
401 dots = 0;
402 for (cp = _res.defdname; *cp; cp++)
403 dots += (*cp == '.');
405 cp = _res.defdname;
406 while (pp < _res.dnsrch + MAXDFLSRCH) {
407 if (dots < LOCALDOMAINPARTS)
408 break;
409 cp = strchr(cp, '.') + 1; /* we know there is one */
410 *pp++ = cp;
411 dots--;
413 *pp = NULL;
414 #ifdef DEBUG
415 if (_res.options & RES_DEBUG) {
416 printf(";; res_init()... default dnsrch list:\n");
417 for (pp = _res.dnsrch; *pp; pp++)
418 printf(";;\t%s\n", *pp);
419 printf(";;\t..END..\n");
421 #endif /* DEBUG */
422 #endif /* !RFC1535 */
425 if ((cp = __secure_getenv("RES_OPTIONS")) != NULL)
426 res_setoptions(cp, "env");
427 _res.options |= RES_INIT;
428 return (0);
431 static void
432 internal_function
433 res_setoptions(options, source)
434 char *options, *source;
436 char *cp = options;
437 int i;
439 #ifdef DEBUG
440 if (_res.options & RES_DEBUG)
441 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
442 options, source);
443 #endif
444 while (*cp) {
445 /* skip leading and inner runs of spaces */
446 while (*cp == ' ' || *cp == '\t')
447 cp++;
448 /* search for and process individual options */
449 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
450 i = atoi(cp + sizeof("ndots:") - 1);
451 if (i <= RES_MAXNDOTS)
452 _res.ndots = i;
453 else
454 _res.ndots = RES_MAXNDOTS;
455 #ifdef DEBUG
456 if (_res.options & RES_DEBUG)
457 printf(";;\tndots=%d\n", _res.ndots);
458 #endif
459 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
460 #ifdef DEBUG
461 if (!(_res.options & RES_DEBUG)) {
462 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
463 options, source);
464 _res.options |= RES_DEBUG;
466 printf(";;\tdebug\n");
467 #endif
468 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
469 _res.options |= RES_USE_INET6;
470 } else {
471 /* XXX - print a warning here? */
473 /* skip to next run of spaces */
474 while (*cp && *cp != ' ' && *cp != '\t')
475 cp++;
479 #ifdef RESOLVSORT
480 /* XXX - should really support CIDR which means explicit masks always. */
481 static u_int32_t
482 net_mask(in) /* XXX - should really use system's version of this */
483 struct in_addr in;
485 register u_int32_t i = ntohl(in.s_addr);
487 if (IN_CLASSA(i))
488 return (htonl(IN_CLASSA_NET));
489 else if (IN_CLASSB(i))
490 return (htonl(IN_CLASSB_NET));
491 return (htonl(IN_CLASSC_NET));
493 #endif
495 #ifdef NeXT
496 static int
497 netinfo_res_init(haveenv, havesearch)
498 int *haveenv;
499 int *havesearch;
501 register int n;
502 void *domain, *parent;
503 ni_id dir;
504 ni_status status;
505 ni_namelist nl;
506 int nserv = 0;
507 #ifdef RESOLVSORT
508 int nsort = 0;
509 #endif
511 status = ni_open(NULL, ".", &domain);
512 if (status == NI_OK) {
513 ni_setreadtimeout(domain, NI_TIMEOUT);
514 ni_setabort(domain, 1);
516 /* climb the NetInfo hierarchy to find a resolver directory */
517 while (status == NI_OK) {
518 status = ni_pathsearch(domain, &dir, NI_PATH_RESCONF);
519 if (status == NI_OK) {
520 /* found a resolver directory */
522 if (*haveenv == 0) {
523 /* get the default domain name */
524 status = ni_lookupprop(domain, &dir, "domain", &nl);
525 if (status == NI_OK && nl.ni_namelist_len > 0) {
526 (void)strncpy(_res.defdname,
527 nl.ni_namelist_val[0],
528 sizeof(_res.defdname) - 1);
529 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
530 ni_namelist_free(&nl);
531 *havesearch = 0;
534 /* get search list */
535 status = ni_lookupprop(domain, &dir, "search", &nl);
536 if (status == NI_OK && nl.ni_namelist_len > 0) {
537 (void)strncpy(_res.defdname,
538 nl.ni_namelist_val[0],
539 sizeof(_res.defdname) - 1);
540 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
541 /* copy */
542 for (n = 0;
543 n < nl.ni_namelist_len && n < MAXDNSRCH;
544 n++) {
545 /* duplicate up to MAXDNSRCH servers */
546 char *cp = nl.ni_namelist_val[n];
547 _res.dnsrch[n] =
548 strcpy((char *)malloc(strlen(cp) + 1), cp);
550 ni_namelist_free(&nl);
551 *havesearch = 1;
555 /* get list of nameservers */
556 status = ni_lookupprop(domain, &dir, "nameserver", &nl);
557 if (status == NI_OK && nl.ni_namelist_len > 0) {
558 /* copy up to MAXNS servers */
559 for (n = 0;
560 n < nl.ni_namelist_len && nserv < MAXNS;
561 n++) {
562 struct in_addr a;
564 if (inet_aton(nl.ni_namelist_val[n], &a)) {
565 _res.nsaddr_list[nserv].sin_addr = a;
566 _res.nsaddr_list[nserv].sin_family = AF_INET;
567 _res.nsaddr_list[nserv].sin_port =
568 htons(NAMESERVER_PORT);
569 nserv++;
572 ni_namelist_free(&nl);
575 if (nserv > _res.nscount)
576 _res.nscount = nserv;
578 #ifdef RESOLVSORT
579 /* get sort order */
580 status = ni_lookupprop(domain, &dir, "sortlist", &nl);
581 if (status == NI_OK && nl.ni_namelist_len > 0) {
583 /* copy up to MAXRESOLVSORT address/netmask pairs */
584 for (n = 0;
585 n < nl.ni_namelist_len && nsort < MAXRESOLVSORT;
586 n++) {
587 char ch;
588 char *cp;
589 const char *sp;
590 struct in_addr a;
592 cp = NULL;
593 for (sp = sort_mask; *sp; sp++) {
594 char *cp1;
595 cp1 = strchr(nl.ni_namelist_val[n], *sp);
596 if (cp && cp1)
597 cp = (cp < cp1)? cp : cp1;
598 else if (cp1)
599 cp = cp1;
601 if (cp != NULL) {
602 ch = *cp;
603 *cp = '\0';
604 break;
606 if (inet_aton(nl.ni_namelist_val[n], &a)) {
607 _res.sort_list[nsort].addr = a;
608 if (*cp && ISSORTMASK(ch)) {
609 *cp++ = ch;
610 if (inet_aton(cp, &a)) {
611 _res.sort_list[nsort].mask = a.s_addr;
612 } else {
613 _res.sort_list[nsort].mask =
614 net_mask(_res.sort_list[nsort].addr);
616 } else {
617 _res.sort_list[nsort].mask =
618 net_mask(_res.sort_list[nsort].addr);
620 nsort++;
623 ni_namelist_free(&nl);
626 _res.nsort = nsort;
627 #endif
629 /* get resolver options */
630 status = ni_lookupprop(domain, &dir, "options", &nl);
631 if (status == NI_OK && nl.ni_namelist_len > 0) {
632 res_setoptions(nl.ni_namelist_val[0], "conf");
633 ni_namelist_free(&nl);
636 ni_free(domain);
637 return(1); /* using DNS configuration from NetInfo */
640 status = ni_open(domain, "..", &parent);
641 ni_free(domain);
642 if (status == NI_OK)
643 domain = parent;
646 return(0); /* if not using DNS configuration from NetInfo */
648 #endif /* NeXT */
650 u_int
651 res_randomid()
653 struct timeval now;
655 __gettimeofday(&now, NULL);
656 return (0xffff & (now.tv_sec ^ now.tv_usec ^ __getpid()));