initial import
[glibc.git] / resolv / res_init.c
blob0092dbcfbcfc42481edd1065dab56b0ea5873c90
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 <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
67 #include <stdio.h>
68 #include <ctype.h>
69 #include <resolv.h>
70 #if defined(BSD) && (BSD >= 199103)
71 # include <unistd.h>
72 # include <stdlib.h>
73 # include <string.h>
74 #else
75 # include "../conf/portability.h"
76 #endif
79 * Marc Majka 1994/04/16
80 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
82 * NetInfo resolver configuration directory support.
84 * Allow a NetInfo directory to be created in the hierarchy which
85 * contains the same information as the resolver configuration file.
87 * - The local domain name is stored as the value of the "domain" property.
88 * - The Internet address(es) of the name server(s) are stored as values
89 * of the "nameserver" property.
90 * - The name server addresses are stored as values of the "nameserver"
91 * property.
92 * - The search list for host-name lookup is stored as values of the
93 * "search" property.
94 * - The sortlist comprised of IP address netmask pairs are stored as
95 * values of the "sortlist" property. The IP address and optional netmask
96 * should be seperated by a slash (/) character.
97 * - Internal resolver variables can be set from the value of the "options"
98 * 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 u_int32_t net_mask __P((struct in_addr));
116 #endif
118 #if !defined(isascii) /* XXX - could be a function */
119 # define isascii(c) (!(c & 0200))
120 #endif
123 * Resolver state default settings.
126 struct __res_state _res;
129 * Set up default settings. If the configuration file exist, the values
130 * there will have precedence. Otherwise, the server address is set to
131 * INADDR_ANY and the default domain name comes from the gethostname().
133 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
134 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
135 * since it was noted that INADDR_ANY actually meant ``the first interface
136 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
137 * it had to be "up" in order for you to reach your own name server. It
138 * was later decided that since the recommended practice is to always
139 * install local static routes through 127.0.0.1 for all your network
140 * interfaces, that we could solve this problem without a code change.
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.
147 * Return 0 if completes successfully, -1 on error
149 res_init()
151 register FILE *fp;
152 register char *cp, **pp;
153 register int n, dots;
154 char buf[BUFSIZ];
155 int nserv = 0; /* number of nameserver records read from file */
156 int haveenv = 0;
157 int havesearch = 0;
158 #ifdef RESOLVSORT
159 int nsort = 0;
160 char *net;
161 #endif
164 * These three fields used to be statically initialized. This made
165 * it hard to use this code in a shared library. It is necessary,
166 * now that we're doing dynamic initialization here, that we preserve
167 * the old semantics: if an application modifies one of these three
168 * fields of _res before res_init() is called, res_init() will not
169 * alter them. Of course, if an application is setting them to
170 * _zero_ before calling res_init(), hoping to override what used
171 * to be the static default, we can't detect it and unexpected results
172 * will follow. Zero for any of these fields would make no sense,
173 * so one can safely assume that the applications were already getting
174 * unexpected results.
176 if (!_res.retrans)
177 _res.retrans = RES_TIMEOUT;
178 if (!_res.retry)
179 _res.retry = 4;
180 if (!_res.options)
181 _res.options = RES_DEFAULT;
183 #ifdef USELOOPBACK
184 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
185 #else
186 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
187 #endif
188 _res.nsaddr.sin_family = AF_INET;
189 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
190 _res.nscount = 1;
191 _res.ndots = 1;
192 _res.pfcode = 0;
194 /* Allow user to override the local domain definition */
195 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
196 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
197 haveenv++;
200 * Set search list to be blank-separated strings
201 * from rest of env value. Permits users of LOCALDOMAIN
202 * to still have a search list, and anyone to set the
203 * one that they want to use as an individual (even more
204 * important now that the rfc1535 stuff restricts searches)
206 cp = _res.defdname;
207 pp = _res.dnsrch;
208 *pp++ = cp;
209 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
210 if (*cp == '\n') /* silly backwards compat */
211 break;
212 else if (*cp == ' ' || *cp == '\t') {
213 *cp = 0;
214 n = 1;
215 } else if (n) {
216 *pp++ = cp;
217 n = 0;
218 havesearch = 1;
221 /* null terminate last domain if there are excess */
222 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
223 cp++;
224 *cp = '\0';
225 *pp++ = 0;
228 #define MATCH(line, name) \
229 (!strncmp(line, name, sizeof(name) - 1) && \
230 (line[sizeof(name) - 1] == ' ' || \
231 line[sizeof(name) - 1] == '\t'))
233 #ifdef NeXT
234 if (netinfo_res_init(&haveenv, &havesearch) == 0)
235 #endif
236 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
237 /* read the config file */
238 while (fgets(buf, sizeof(buf), fp) != NULL) {
239 /* skip comments */
240 if (*buf == ';' || *buf == '#')
241 continue;
242 /* read default domain name */
243 if (MATCH(buf, "domain")) {
244 if (haveenv) /* skip if have from environ */
245 continue;
246 cp = buf + sizeof("domain") - 1;
247 while (*cp == ' ' || *cp == '\t')
248 cp++;
249 if ((*cp == '\0') || (*cp == '\n'))
250 continue;
251 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
252 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
253 *cp = '\0';
254 havesearch = 0;
255 continue;
257 /* set search list */
258 if (MATCH(buf, "search")) {
259 if (haveenv) /* skip if have from environ */
260 continue;
261 cp = buf + sizeof("search") - 1;
262 while (*cp == ' ' || *cp == '\t')
263 cp++;
264 if ((*cp == '\0') || (*cp == '\n'))
265 continue;
266 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
267 if ((cp = strchr(_res.defdname, '\n')) != NULL)
268 *cp = '\0';
270 * Set search list to be blank-separated strings
271 * on rest of line.
273 cp = _res.defdname;
274 pp = _res.dnsrch;
275 *pp++ = cp;
276 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
277 if (*cp == ' ' || *cp == '\t') {
278 *cp = 0;
279 n = 1;
280 } else if (n) {
281 *pp++ = cp;
282 n = 0;
285 /* null terminate last domain if there are excess */
286 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
287 cp++;
288 *cp = '\0';
289 *pp++ = 0;
290 havesearch = 1;
291 continue;
293 /* read nameservers to query */
294 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
295 struct in_addr a;
297 cp = buf + sizeof("nameserver") - 1;
298 while (*cp == ' ' || *cp == '\t')
299 cp++;
300 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
301 _res.nsaddr_list[nserv].sin_addr = a;
302 _res.nsaddr_list[nserv].sin_family = AF_INET;
303 _res.nsaddr_list[nserv].sin_port =
304 htons(NAMESERVER_PORT);
305 nserv++;
307 continue;
309 #ifdef RESOLVSORT
310 if (MATCH(buf, "sortlist")) {
311 struct in_addr a;
313 cp = buf + sizeof("sortlist") - 1;
314 while (nsort < MAXRESOLVSORT) {
315 while (*cp == ' ' || *cp == '\t')
316 cp++;
317 if (*cp == '\0' || *cp == '\n' || *cp == ';')
318 break;
319 net = cp;
320 while (*cp && *cp != '/' &&
321 isascii(*cp) && !isspace(*cp))
322 cp++;
323 n = *cp;
324 *cp = 0;
325 if (inet_aton(net, &a)) {
326 _res.sort_list[nsort].addr = a;
327 if (n == '/') {
328 *cp++ = n;
329 net = cp;
330 while (*cp && isascii(*cp) && !isspace(*cp))
331 cp++;
332 n = *cp;
333 *cp = 0;
334 if (inet_aton(net, &a)) {
335 _res.sort_list[nsort].mask = a.s_addr;
336 } else {
337 _res.sort_list[nsort].mask =
338 net_mask(_res.sort_list[nsort].addr);
340 } else {
341 _res.sort_list[nsort].mask =
342 net_mask(_res.sort_list[nsort].addr);
344 nsort++;
346 *cp++ = n;
348 continue;
350 #endif
351 if (MATCH(buf, "options")) {
352 res_setoptions(buf + sizeof("options") - 1, "conf");
353 continue;
356 if (nserv > 1)
357 _res.nscount = nserv;
358 #ifdef RESOLVSORT
359 _res.nsort = nsort;
360 #endif
361 (void) fclose(fp);
363 if (_res.defdname[0] == 0 &&
364 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
365 (cp = strchr(buf, '.')) != NULL)
366 strcpy(_res.defdname, cp + 1);
368 /* find components of local domain that might be searched */
369 if (havesearch == 0) {
370 pp = _res.dnsrch;
371 *pp++ = _res.defdname;
372 *pp = NULL;
374 #ifndef RFC1535
375 dots = 0;
376 for (cp = _res.defdname; *cp; cp++)
377 dots += (*cp == '.');
379 cp = _res.defdname;
380 while (pp < _res.dnsrch + MAXDFLSRCH) {
381 if (dots < LOCALDOMAINPARTS)
382 break;
383 cp = strchr(cp, '.') + 1; /* we know there is one */
384 *pp++ = cp;
385 dots--;
387 *pp = NULL;
388 #ifdef DEBUG
389 if (_res.options & RES_DEBUG) {
390 printf(";; res_init()... default dnsrch list:\n");
391 for (pp = _res.dnsrch; *pp; pp++)
392 printf(";;\t%s\n", *pp);
393 printf(";;\t..END..\n");
395 #endif /* DEBUG */
396 #endif /* !RFC1535 */
399 if ((cp = getenv("RES_OPTIONS")) != NULL)
400 res_setoptions(cp, "env");
401 _res.options |= RES_INIT;
402 return (0);
405 static void
406 res_setoptions(options, source)
407 char *options, *source;
409 char *cp = options;
410 int i;
412 #ifdef DEBUG
413 if (_res.options & RES_DEBUG)
414 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
415 options, source);
416 #endif
417 while (*cp) {
418 /* skip leading and inner runs of spaces */
419 while (*cp == ' ' || *cp == '\t')
420 cp++;
421 /* search for and process individual options */
422 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
423 i = atoi(cp + sizeof("ndots:") - 1);
424 if (i <= RES_MAXNDOTS)
425 _res.ndots = i;
426 else
427 _res.ndots = RES_MAXNDOTS;
428 #ifdef DEBUG
429 if (_res.options & RES_DEBUG)
430 printf(";;\tndots=%d\n", _res.ndots);
431 #endif
432 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
433 #ifdef DEBUG
434 if (!(_res.options & RES_DEBUG)) {
435 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
436 options, source);
437 _res.options |= RES_DEBUG;
439 printf(";;\tdebug\n");
440 #endif
441 } else {
442 /* XXX - print a warning here? */
444 /* skip to next run of spaces */
445 while (*cp && *cp != ' ' && *cp != '\t')
446 cp++;
450 #ifdef RESOLVSORT
451 static u_int32_t
452 net_mask(in) /* XXX - should really use system's version of this */
453 struct in_addr in;
455 register u_int32_t i = ntohl(in.s_addr);
457 if (IN_CLASSA(i))
458 return (htonl(IN_CLASSA_NET));
459 else if (IN_CLASSB(i))
460 return (htonl(IN_CLASSB_NET));
461 return (htonl(IN_CLASSC_NET));
463 #endif
465 #ifdef NeXT
466 static int
467 netinfo_res_init(haveenv, havesearch)
468 int *haveenv;
469 int *havesearch;
471 register int n;
472 void *domain, *parent;
473 ni_id dir;
474 ni_status status;
475 ni_namelist nl;
476 int nserv = 0;
477 #ifdef RESOLVSORT
478 int nsort = 0;
479 #endif
481 status = ni_open(NULL, ".", &domain);
482 if (status == NI_OK) {
483 ni_setreadtimeout(domain, NI_TIMEOUT);
484 ni_setabort(domain, 1);
486 /* climb the NetInfo hierarchy to find a resolver directory */
487 while (status == NI_OK) {
488 status = ni_pathsearch(domain, &dir, NI_PATH_RESCONF);
489 if (status == NI_OK) {
490 /* found a resolver directory */
492 if (*haveenv == 0) {
493 /* get the default domain name */
494 status = ni_lookupprop(domain, &dir, "domain", &nl);
495 if (status == NI_OK && nl.ni_namelist_len > 0) {
496 (void)strncpy(_res.defdname,
497 nl.ni_namelist_val[0],
498 sizeof(_res.defdname) - 1);
499 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
500 ni_namelist_free(&nl);
501 *havesearch = 0;
504 /* get search list */
505 status = ni_lookupprop(domain, &dir, "search", &nl);
506 if (status == NI_OK && nl.ni_namelist_len > 0) {
507 (void)strncpy(_res.defdname,
508 nl.ni_namelist_val[0],
509 sizeof(_res.defdname) - 1);
510 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
511 /* copy */
512 for (n = 0;
513 n < nl.ni_namelist_len && n < MAXDNSRCH;
514 n++) {
515 /* duplicate up to MAXDNSRCH servers */
516 char *cp = nl.ni_namelist_val[n];
517 _res.dnsrch[n] =
518 strcpy((char *)malloc(strlen(cp) + 1), cp);
520 ni_namelist_free(&nl);
521 *havesearch = 1;
525 /* get list of nameservers */
526 status = ni_lookupprop(domain, &dir, "nameserver", &nl);
527 if (status == NI_OK && nl.ni_namelist_len > 0) {
528 /* copy up to MAXNS servers */
529 for (n = 0;
530 n < nl.ni_namelist_len && nserv < MAXNS;
531 n++) {
532 struct in_addr a;
534 if (inet_aton(nl.ni_namelist_val[n], &a)) {
535 _res.nsaddr_list[nserv].sin_addr = a;
536 _res.nsaddr_list[nserv].sin_family = AF_INET;
537 _res.nsaddr_list[nserv].sin_port =
538 htons(NAMESERVER_PORT);
539 nserv++;
542 ni_namelist_free(&nl);
545 if (nserv > 1)
546 _res.nscount = nserv;
548 #ifdef RESOLVSORT
549 /* get sort order */
550 status = ni_lookupprop(domain, &dir, "sortlist", &nl);
551 if (status == NI_OK && nl.ni_namelist_len > 0) {
553 /* copy up to MAXRESOLVSORT address/netmask pairs */
554 for (n = 0;
555 n < nl.ni_namelist_len && nsort < MAXRESOLVSORT;
556 n++) {
557 char ch;
558 char *cp;
559 struct in_addr a;
561 cp = strchr(nl.ni_namelist_val[n], '/');
562 if (cp != NULL) {
563 ch = *cp;
564 *cp = '\0';
567 if (inet_aton(nl.ni_namelist_val[n], &a)) {
568 _res.sort_list[nsort].addr = a;
569 if (*cp && ch == '/') {
570 *cp++ = ch;
571 if (inet_aton(cp, &a)) {
572 _res.sort_list[nsort].mask = a.s_addr;
573 } else {
574 _res.sort_list[nsort].mask =
575 net_mask(_res.sort_list[nsort].addr);
577 } else {
578 _res.sort_list[nsort].mask =
579 net_mask(_res.sort_list[nsort].addr);
581 nsort++;
584 ni_namelist_free(&nl);
587 _res.nsort = nsort;
588 #endif
590 /* get resolver options */
591 status = ni_lookupprop(domain, &dir, "options", &nl);
592 if (status == NI_OK && nl.ni_namelist_len > 0) {
593 res_setoptions(nl.ni_namelist_val[0], "conf");
594 ni_namelist_free(&nl);
597 ni_free(domain);
598 return(1); /* using DNS configuration from NetInfo */
601 status = ni_open(domain, "..", &parent);
602 ni_free(domain);
603 if (status == NI_OK)
604 domain = parent;
607 return(0); /* if not using DNS configuration from NetInfo */
609 #endif /* NeXT */