Thu Oct 26 23:11:11 1995 Ulrich Drepper <drepper@ipd.info.uni-karlsruhe.de>
[glibc.git] / resolv / res_init.c
blob42c7c2ef8cd1a5e33b43813877b541e85a06c648
1 /*
2 * ++Copyright++ 1985, 1989, 1993
3 * -
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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/param.h>
62 #include <sys/socket.h>
63 #include <sys/time.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
68 #include <stdio.h>
69 #include <ctype.h>
70 #include <resolv.h>
71 #if defined(BSD) && (BSD >= 199103)
72 # include <unistd.h>
73 # include <stdlib.h>
74 # include <string.h>
75 #else
76 # include "../conf/portability.h"
77 #endif
79 /*-------------------------------------- info about "sortlist" --------------
80 * Marc Majka 1994/04/16
81 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
83 * NetInfo resolver configuration directory support.
85 * Allow a NetInfo directory to be created in the hierarchy which
86 * contains the same information as the resolver configuration file.
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 seperated by a slash (/) or ampersand (&) character.
98 * - Internal resolver variables can be set from the value of the "options"
99 * property.
101 #if defined(NeXT)
102 # include <netinfo/ni.h>
103 # define NI_PATH_RESCONF "/locations/resolver"
104 # define NI_TIMEOUT 10
105 static int netinfo_res_init __P((int *haveenv, int *havesearch));
106 #endif
108 #if defined(USE_OPTIONS_H)
109 # include "../conf/options.h"
110 #endif
112 static void res_setoptions __P((char *, char *));
114 #ifdef RESOLVSORT
115 static const char sort_mask[] = "/&";
116 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
117 static u_int32_t net_mask __P((struct in_addr));
118 #endif
120 #if !defined(isascii) /* XXX - could be a function */
121 # define isascii(c) (!(c & 0200))
122 #endif
125 * Resolver state default settings.
128 struct __res_state _res;
131 * Set up default settings. If the configuration file exist, the values
132 * there will have precedence. Otherwise, the server address is set to
133 * INADDR_ANY and the default domain name comes from the gethostname().
135 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
136 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
137 * since it was noted that INADDR_ANY actually meant ``the first interface
138 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
139 * it had to be "up" in order for you to reach your own name server. It
140 * was later decided that since the recommended practice is to always
141 * install local static routes through 127.0.0.1 for all your network
142 * interfaces, that we could solve this problem without a code change.
144 * The configuration file should always be used, since it is the only way
145 * to specify a default domain. If you are running a server on your local
146 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
147 * in the configuration file.
149 * Return 0 if completes successfully, -1 on error
152 res_init()
154 register FILE *fp;
155 register char *cp, **pp;
156 register int n;
157 char buf[BUFSIZ];
158 int nserv = 0; /* number of nameserver records read from file */
159 int haveenv = 0;
160 int havesearch = 0;
161 #ifdef RESOLVSORT
162 int nsort = 0;
163 char *net;
164 #endif
165 #ifndef RFC1535
166 int dots;
167 #endif
170 * These three fields used to be statically initialized. This made
171 * it hard to use this code in a shared library. It is necessary,
172 * now that we're doing dynamic initialization here, that we preserve
173 * the old semantics: if an application modifies one of these three
174 * fields of _res before res_init() is called, res_init() will not
175 * alter them. Of course, if an application is setting them to
176 * _zero_ before calling res_init(), hoping to override what used
177 * to be the static default, we can't detect it and unexpected results
178 * will follow. Zero for any of these fields would make no sense,
179 * so one can safely assume that the applications were already getting
180 * unexpected results.
182 * _res.options is tricky since some apps were known to diddle the bits
183 * before res_init() was first called. We can't replicate that semantic
184 * with dynamic initialization (they may have turned bits off that are
185 * set in RES_DEFAULT). Our solution is to declare such applications
186 * "broken". They could fool us by setting RES_INIT but none do (yet).
188 if (!_res.retrans)
189 _res.retrans = RES_TIMEOUT;
190 if (!_res.retry)
191 _res.retry = 4;
192 if (!(_res.options & RES_INIT))
193 _res.options = RES_DEFAULT;
196 * This one used to initialize implicitly to zero, so unless the app
197 * has set it to something in particular, we can randomize it now.
199 if (!_res.id)
200 _res.id = res_randomid();
202 #ifdef USELOOPBACK
203 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
204 #else
205 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
206 #endif
207 _res.nsaddr.sin_family = AF_INET;
208 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
209 _res.nscount = 1;
210 _res.ndots = 1;
211 _res.pfcode = 0;
213 /* Allow user to override the local domain definition */
214 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
215 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
216 haveenv++;
219 * Set search list to be blank-separated strings
220 * from rest of env value. Permits users of LOCALDOMAIN
221 * to still have a search list, and anyone to set the
222 * one that they want to use as an individual (even more
223 * important now that the rfc1535 stuff restricts searches)
225 cp = _res.defdname;
226 pp = _res.dnsrch;
227 *pp++ = cp;
228 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
229 if (*cp == '\n') /* silly backwards compat */
230 break;
231 else if (*cp == ' ' || *cp == '\t') {
232 *cp = 0;
233 n = 1;
234 } else if (n) {
235 *pp++ = cp;
236 n = 0;
237 havesearch = 1;
240 /* null terminate last domain if there are excess */
241 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
242 cp++;
243 *cp = '\0';
244 *pp++ = 0;
247 #define MATCH(line, name) \
248 (!strncmp(line, name, sizeof(name) - 1) && \
249 (line[sizeof(name) - 1] == ' ' || \
250 line[sizeof(name) - 1] == '\t'))
252 #ifdef NeXT
253 if (netinfo_res_init(&haveenv, &havesearch) == 0)
254 #endif
255 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
256 /* read the config file */
257 while (fgets(buf, sizeof(buf), fp) != NULL) {
258 /* skip comments */
259 if (*buf == ';' || *buf == '#')
260 continue;
261 /* read default domain name */
262 if (MATCH(buf, "domain")) {
263 if (haveenv) /* skip if have from environ */
264 continue;
265 cp = buf + sizeof("domain") - 1;
266 while (*cp == ' ' || *cp == '\t')
267 cp++;
268 if ((*cp == '\0') || (*cp == '\n'))
269 continue;
270 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
271 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
272 *cp = '\0';
273 havesearch = 0;
274 continue;
276 /* set search list */
277 if (MATCH(buf, "search")) {
278 if (haveenv) /* skip if have from environ */
279 continue;
280 cp = buf + sizeof("search") - 1;
281 while (*cp == ' ' || *cp == '\t')
282 cp++;
283 if ((*cp == '\0') || (*cp == '\n'))
284 continue;
285 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
286 if ((cp = strchr(_res.defdname, '\n')) != NULL)
287 *cp = '\0';
289 * Set search list to be blank-separated strings
290 * on rest of line.
292 cp = _res.defdname;
293 pp = _res.dnsrch;
294 *pp++ = cp;
295 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
296 if (*cp == ' ' || *cp == '\t') {
297 *cp = 0;
298 n = 1;
299 } else if (n) {
300 *pp++ = cp;
301 n = 0;
304 /* null terminate last domain if there are excess */
305 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
306 cp++;
307 *cp = '\0';
308 *pp++ = 0;
309 havesearch = 1;
310 continue;
312 /* read nameservers to query */
313 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
314 struct in_addr a;
316 cp = buf + sizeof("nameserver") - 1;
317 while (*cp == ' ' || *cp == '\t')
318 cp++;
319 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
320 _res.nsaddr_list[nserv].sin_addr = a;
321 _res.nsaddr_list[nserv].sin_family = AF_INET;
322 _res.nsaddr_list[nserv].sin_port =
323 htons(NAMESERVER_PORT);
324 nserv++;
326 continue;
328 #ifdef RESOLVSORT
329 if (MATCH(buf, "sortlist")) {
330 struct in_addr a;
332 cp = buf + sizeof("sortlist") - 1;
333 while (nsort < MAXRESOLVSORT) {
334 while (*cp == ' ' || *cp == '\t')
335 cp++;
336 if (*cp == '\0' || *cp == '\n' || *cp == ';')
337 break;
338 net = cp;
339 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
340 isascii(*cp) && !isspace(*cp))
341 cp++;
342 n = *cp;
343 *cp = 0;
344 if (inet_aton(net, &a)) {
345 _res.sort_list[nsort].addr = a;
346 if (ISSORTMASK(n)) {
347 *cp++ = n;
348 net = cp;
349 while (*cp && *cp != ';' &&
350 isascii(*cp) && !isspace(*cp))
351 cp++;
352 n = *cp;
353 *cp = 0;
354 if (inet_aton(net, &a)) {
355 _res.sort_list[nsort].mask = a.s_addr;
356 } else {
357 _res.sort_list[nsort].mask =
358 net_mask(_res.sort_list[nsort].addr);
360 } else {
361 _res.sort_list[nsort].mask =
362 net_mask(_res.sort_list[nsort].addr);
364 nsort++;
366 *cp = n;
368 continue;
370 #endif
371 if (MATCH(buf, "options")) {
372 res_setoptions(buf + sizeof("options") - 1, "conf");
373 continue;
376 if (nserv > 1)
377 _res.nscount = nserv;
378 #ifdef RESOLVSORT
379 _res.nsort = nsort;
380 #endif
381 (void) fclose(fp);
383 if (_res.defdname[0] == 0 &&
384 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
385 (cp = strchr(buf, '.')) != NULL)
386 strcpy(_res.defdname, cp + 1);
388 /* find components of local domain that might be searched */
389 if (havesearch == 0) {
390 pp = _res.dnsrch;
391 *pp++ = _res.defdname;
392 *pp = NULL;
394 #ifndef RFC1535
395 dots = 0;
396 for (cp = _res.defdname; *cp; cp++)
397 dots += (*cp == '.');
399 cp = _res.defdname;
400 while (pp < _res.dnsrch + MAXDFLSRCH) {
401 if (dots < LOCALDOMAINPARTS)
402 break;
403 cp = strchr(cp, '.') + 1; /* we know there is one */
404 *pp++ = cp;
405 dots--;
407 *pp = NULL;
408 #ifdef DEBUG
409 if (_res.options & RES_DEBUG) {
410 printf(";; res_init()... default dnsrch list:\n");
411 for (pp = _res.dnsrch; *pp; pp++)
412 printf(";;\t%s\n", *pp);
413 printf(";;\t..END..\n");
415 #endif /* DEBUG */
416 #endif /* !RFC1535 */
419 if ((cp = getenv("RES_OPTIONS")) != NULL)
420 res_setoptions(cp, "env");
421 _res.options |= RES_INIT;
422 return (0);
425 static void
426 res_setoptions(options, source)
427 char *options, *source;
429 char *cp = options;
430 int i;
432 #ifdef DEBUG
433 if (_res.options & RES_DEBUG)
434 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
435 options, source);
436 #endif
437 while (*cp) {
438 /* skip leading and inner runs of spaces */
439 while (*cp == ' ' || *cp == '\t')
440 cp++;
441 /* search for and process individual options */
442 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
443 i = atoi(cp + sizeof("ndots:") - 1);
444 if (i <= RES_MAXNDOTS)
445 _res.ndots = i;
446 else
447 _res.ndots = RES_MAXNDOTS;
448 #ifdef DEBUG
449 if (_res.options & RES_DEBUG)
450 printf(";;\tndots=%d\n", _res.ndots);
451 #endif
452 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
453 #ifdef DEBUG
454 if (!(_res.options & RES_DEBUG)) {
455 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
456 options, source);
457 _res.options |= RES_DEBUG;
459 printf(";;\tdebug\n");
460 #endif
461 } else {
462 /* XXX - print a warning here? */
464 /* skip to next run of spaces */
465 while (*cp && *cp != ' ' && *cp != '\t')
466 cp++;
470 #ifdef RESOLVSORT
471 /* XXX - should really support CIDR which means explicit masks always. */
472 static u_int32_t
473 net_mask(in) /* XXX - should really use system's version of this */
474 struct in_addr in;
476 register u_int32_t i = ntohl(in.s_addr);
478 if (IN_CLASSA(i))
479 return (htonl(IN_CLASSA_NET));
480 else if (IN_CLASSB(i))
481 return (htonl(IN_CLASSB_NET));
482 return (htonl(IN_CLASSC_NET));
484 #endif
486 #ifdef NeXT
487 static int
488 netinfo_res_init(haveenv, havesearch)
489 int *haveenv;
490 int *havesearch;
492 register int n;
493 void *domain, *parent;
494 ni_id dir;
495 ni_status status;
496 ni_namelist nl;
497 int nserv = 0;
498 #ifdef RESOLVSORT
499 int nsort = 0;
500 #endif
502 status = ni_open(NULL, ".", &domain);
503 if (status == NI_OK) {
504 ni_setreadtimeout(domain, NI_TIMEOUT);
505 ni_setabort(domain, 1);
507 /* climb the NetInfo hierarchy to find a resolver directory */
508 while (status == NI_OK) {
509 status = ni_pathsearch(domain, &dir, NI_PATH_RESCONF);
510 if (status == NI_OK) {
511 /* found a resolver directory */
513 if (*haveenv == 0) {
514 /* get the default domain name */
515 status = ni_lookupprop(domain, &dir, "domain", &nl);
516 if (status == NI_OK && nl.ni_namelist_len > 0) {
517 (void)strncpy(_res.defdname,
518 nl.ni_namelist_val[0],
519 sizeof(_res.defdname) - 1);
520 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
521 ni_namelist_free(&nl);
522 *havesearch = 0;
525 /* get search list */
526 status = ni_lookupprop(domain, &dir, "search", &nl);
527 if (status == NI_OK && nl.ni_namelist_len > 0) {
528 (void)strncpy(_res.defdname,
529 nl.ni_namelist_val[0],
530 sizeof(_res.defdname) - 1);
531 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
532 /* copy */
533 for (n = 0;
534 n < nl.ni_namelist_len && n < MAXDNSRCH;
535 n++) {
536 /* duplicate up to MAXDNSRCH servers */
537 char *cp = nl.ni_namelist_val[n];
538 _res.dnsrch[n] =
539 strcpy((char *)malloc(strlen(cp) + 1), cp);
541 ni_namelist_free(&nl);
542 *havesearch = 1;
546 /* get list of nameservers */
547 status = ni_lookupprop(domain, &dir, "nameserver", &nl);
548 if (status == NI_OK && nl.ni_namelist_len > 0) {
549 /* copy up to MAXNS servers */
550 for (n = 0;
551 n < nl.ni_namelist_len && nserv < MAXNS;
552 n++) {
553 struct in_addr a;
555 if (inet_aton(nl.ni_namelist_val[n], &a)) {
556 _res.nsaddr_list[nserv].sin_addr = a;
557 _res.nsaddr_list[nserv].sin_family = AF_INET;
558 _res.nsaddr_list[nserv].sin_port =
559 htons(NAMESERVER_PORT);
560 nserv++;
563 ni_namelist_free(&nl);
566 if (nserv > 1)
567 _res.nscount = nserv;
569 #ifdef RESOLVSORT
570 /* get sort order */
571 status = ni_lookupprop(domain, &dir, "sortlist", &nl);
572 if (status == NI_OK && nl.ni_namelist_len > 0) {
574 /* copy up to MAXRESOLVSORT address/netmask pairs */
575 for (n = 0;
576 n < nl.ni_namelist_len && nsort < MAXRESOLVSORT;
577 n++) {
578 char ch;
579 char *cp;
580 const char *sp;
581 struct in_addr a;
583 cp = NULL;
584 for (sp = sort_mask; *sp; sp++) {
585 char *cp1;
586 cp1 = strchr(nl.ni_namelist_val[n], *sp);
587 if (cp && cp1)
588 cp = (cp < cp1)? cp : cp1;
589 else if (cp1)
590 cp = cp1;
592 if (cp != NULL) {
593 ch = *cp;
594 *cp = '\0';
595 break;
597 if (inet_aton(nl.ni_namelist_val[n], &a)) {
598 _res.sort_list[nsort].addr = a;
599 if (*cp && ISSORTMASK(ch)) {
600 *cp++ = ch;
601 if (inet_aton(cp, &a)) {
602 _res.sort_list[nsort].mask = a.s_addr;
603 } else {
604 _res.sort_list[nsort].mask =
605 net_mask(_res.sort_list[nsort].addr);
607 } else {
608 _res.sort_list[nsort].mask =
609 net_mask(_res.sort_list[nsort].addr);
611 nsort++;
614 ni_namelist_free(&nl);
617 _res.nsort = nsort;
618 #endif
620 /* get resolver options */
621 status = ni_lookupprop(domain, &dir, "options", &nl);
622 if (status == NI_OK && nl.ni_namelist_len > 0) {
623 res_setoptions(nl.ni_namelist_val[0], "conf");
624 ni_namelist_free(&nl);
627 ni_free(domain);
628 return(1); /* using DNS configuration from NetInfo */
631 status = ni_open(domain, "..", &parent);
632 ni_free(domain);
633 if (status == NI_OK)
634 domain = parent;
637 return(0); /* if not using DNS configuration from NetInfo */
639 #endif /* NeXT */
641 u_int16_t
642 res_randomid()
644 struct timeval now;
646 gettimeofday(&now, NULL);
647 return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));