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