Update.
[glibc.git] / resolv / res_init.c
blobbf6cf7f7b1f95af671b6bfbf4207885a5f020898
1 /*
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
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.
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 * SOFTWARE.
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
69 static const char rcsid[] = "$Id$";
70 #endif /* LIBC_SCCS and not lint */
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/time.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <arpa/nameser.h>
81 #include <ctype.h>
82 #include <resolv.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
88 /* Options. Should all be left alone. */
89 #define RESOLVSORT
90 #define RFC1535
91 #undef DEBUG
93 static void
94 res_setoptions (res_state statp, const char *options, const char *source)
95 internal_function;
98 #ifdef RESOLVSORT
99 static const char sort_mask[] = "/&";
100 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
101 static u_int32_t net_mask __P((struct in_addr));
102 #endif
104 #if !defined(isascii) /* XXX - could be a function */
105 # define isascii(c) (!(c & 0200))
106 #endif
109 * Resolver state default settings.
113 * Set up default settings. If the configuration file exist, the values
114 * there will have precedence. Otherwise, the server address is set to
115 * INADDR_ANY and the default domain name comes from the gethostname().
117 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
118 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
119 * since it was noted that INADDR_ANY actually meant ``the first interface
120 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
121 * it had to be "up" in order for you to reach your own name server. It
122 * was later decided that since the recommended practice is to always
123 * install local static routes through 127.0.0.1 for all your network
124 * interfaces, that we could solve this problem without a code change.
126 * The configuration file should always be used, since it is the only way
127 * to specify a default domain. If you are running a server on your local
128 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
129 * in the configuration file.
131 * Return 0 if completes successfully, -1 on error
134 res_ninit(res_state statp) {
135 extern int __res_vinit(res_state, int);
137 return (__res_vinit(statp, 0));
140 /* This function has to be reachable by res_data.c but not publically. */
142 __res_vinit(res_state statp, int preinit) {
143 register FILE *fp;
144 register char *cp, **pp;
145 register int n;
146 char buf[BUFSIZ];
147 int nserv = 0; /* number of nameserver records read from file */
148 int haveenv = 0;
149 int havesearch = 0;
150 #ifdef RESOLVSORT
151 int nsort = 0;
152 char *net;
153 #endif
154 #ifndef RFC1535
155 int dots;
156 #endif
158 if (!preinit) {
159 statp->retrans = RES_TIMEOUT;
160 statp->retry = RES_DFLRETRY;
161 statp->options = RES_DEFAULT;
162 statp->id = res_randomid();
165 #ifdef USELOOPBACK
166 statp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
167 #else
168 statp->nsaddr.sin_addr.s_addr = INADDR_ANY;
169 #endif
170 statp->nsaddr.sin_family = AF_INET;
171 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
172 statp->nscount = 1;
173 statp->ndots = 1;
174 statp->pfcode = 0;
175 statp->_sock = -1;
176 statp->_flags = 0;
177 statp->qhook = NULL;
178 statp->rhook = NULL;
180 /* Allow user to override the local domain definition */
181 if ((cp = __secure_getenv("LOCALDOMAIN")) != NULL) {
182 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
183 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
184 haveenv++;
187 * Set search list to be blank-separated strings
188 * from rest of env value. Permits users of LOCALDOMAIN
189 * to still have a search list, and anyone to set the
190 * one that they want to use as an individual (even more
191 * important now that the rfc1535 stuff restricts searches)
193 cp = statp->defdname;
194 pp = statp->dnsrch;
195 *pp++ = cp;
196 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
197 if (*cp == '\n') /* silly backwards compat */
198 break;
199 else if (*cp == ' ' || *cp == '\t') {
200 *cp = 0;
201 n = 1;
202 } else if (n) {
203 *pp++ = cp;
204 n = 0;
205 havesearch = 1;
208 /* null terminate last domain if there are excess */
209 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
210 cp++;
211 *cp = '\0';
212 *pp++ = 0;
215 #define MATCH(line, name) \
216 (!strncmp(line, name, sizeof(name) - 1) && \
217 (line[sizeof(name) - 1] == ' ' || \
218 line[sizeof(name) - 1] == '\t'))
220 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
221 /* read the config file */
222 while (fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
223 /* skip comments */
224 if (*buf == ';' || *buf == '#')
225 continue;
226 /* read default domain name */
227 if (MATCH(buf, "domain")) {
228 if (haveenv) /* skip if have from environ */
229 continue;
230 cp = buf + sizeof("domain") - 1;
231 while (*cp == ' ' || *cp == '\t')
232 cp++;
233 if ((*cp == '\0') || (*cp == '\n'))
234 continue;
235 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
236 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
237 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
238 *cp = '\0';
239 havesearch = 0;
240 continue;
242 /* set search list */
243 if (MATCH(buf, "search")) {
244 if (haveenv) /* skip if have from environ */
245 continue;
246 cp = buf + sizeof("search") - 1;
247 while (*cp == ' ' || *cp == '\t')
248 cp++;
249 if ((*cp == '\0') || (*cp == '\n'))
250 continue;
251 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
252 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
253 if ((cp = strchr(statp->defdname, '\n')) != NULL)
254 *cp = '\0';
256 * Set search list to be blank-separated strings
257 * on rest of line.
259 cp = statp->defdname;
260 pp = statp->dnsrch;
261 *pp++ = cp;
262 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
263 if (*cp == ' ' || *cp == '\t') {
264 *cp = 0;
265 n = 1;
266 } else if (n) {
267 *pp++ = cp;
268 n = 0;
271 /* null terminate last domain if there are excess */
272 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
273 cp++;
274 *cp = '\0';
275 *pp++ = 0;
276 havesearch = 1;
277 continue;
279 /* read nameservers to query */
280 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
281 struct in_addr a;
283 cp = buf + sizeof("nameserver") - 1;
284 while (*cp == ' ' || *cp == '\t')
285 cp++;
286 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
287 statp->nsaddr_list[nserv].sin_addr = a;
288 statp->nsaddr_list[nserv].sin_family = AF_INET;
289 statp->nsaddr_list[nserv].sin_port =
290 htons(NAMESERVER_PORT);
291 nserv++;
293 continue;
295 #ifdef RESOLVSORT
296 if (MATCH(buf, "sortlist")) {
297 struct in_addr a;
299 cp = buf + sizeof("sortlist") - 1;
300 while (nsort < MAXRESOLVSORT) {
301 while (*cp == ' ' || *cp == '\t')
302 cp++;
303 if (*cp == '\0' || *cp == '\n' || *cp == ';')
304 break;
305 net = cp;
306 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
307 isascii(*cp) && !isspace(*cp))
308 cp++;
309 n = *cp;
310 *cp = 0;
311 if (inet_aton(net, &a)) {
312 statp->sort_list[nsort].addr = a;
313 if (ISSORTMASK(n)) {
314 *cp++ = n;
315 net = cp;
316 while (*cp && *cp != ';' &&
317 isascii(*cp) && !isspace(*cp))
318 cp++;
319 n = *cp;
320 *cp = 0;
321 if (inet_aton(net, &a)) {
322 statp->sort_list[nsort].mask = a.s_addr;
323 } else {
324 statp->sort_list[nsort].mask =
325 net_mask(statp->sort_list[nsort].addr);
327 } else {
328 statp->sort_list[nsort].mask =
329 net_mask(statp->sort_list[nsort].addr);
331 nsort++;
333 *cp = n;
335 continue;
337 #endif
338 if (MATCH(buf, "options")) {
339 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
340 continue;
343 if (nserv > 1)
344 statp->nscount = nserv;
345 #ifdef RESOLVSORT
346 statp->nsort = nsort;
347 #endif
348 (void) fclose(fp);
350 if (statp->defdname[0] == 0 &&
351 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
352 (cp = strchr(buf, '.')) != NULL)
353 strcpy(statp->defdname, cp + 1);
355 /* find components of local domain that might be searched */
356 if (havesearch == 0) {
357 pp = statp->dnsrch;
358 *pp++ = statp->defdname;
359 *pp = NULL;
361 #ifndef RFC1535
362 dots = 0;
363 for (cp = statp->defdname; *cp; cp++)
364 dots += (*cp == '.');
366 cp = statp->defdname;
367 while (pp < statp->dnsrch + MAXDFLSRCH) {
368 if (dots < LOCALDOMAINPARTS)
369 break;
370 cp = __rawmemchr(cp, '.') + 1; /* we know there is one */
371 *pp++ = cp;
372 dots--;
374 *pp = NULL;
375 #ifdef DEBUG
376 if (statp->options & RES_DEBUG) {
377 printf(";; res_init()... default dnsrch list:\n");
378 for (pp = statp->dnsrch; *pp; pp++)
379 printf(";;\t%s\n", *pp);
380 printf(";;\t..END..\n");
382 #endif
383 #endif /* !RFC1535 */
386 if ((cp = __secure_getenv("RES_OPTIONS")) != NULL)
387 res_setoptions(statp, cp, "env");
388 statp->options |= RES_INIT;
389 return (0);
392 static void
393 internal_function
394 res_setoptions(res_state statp, const char *options, const char *source)
396 const char *cp = options;
397 int i;
399 #ifdef DEBUG
400 if (statp->options & RES_DEBUG)
401 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
402 options, source);
403 #endif
404 while (*cp) {
405 /* skip leading and inner runs of spaces */
406 while (*cp == ' ' || *cp == '\t')
407 cp++;
408 /* search for and process individual options */
409 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
410 i = atoi(cp + sizeof("ndots:") - 1);
411 if (i <= RES_MAXNDOTS)
412 statp->ndots = i;
413 else
414 statp->ndots = RES_MAXNDOTS;
415 #ifdef DEBUG
416 if (statp->options & RES_DEBUG)
417 printf(";;\tndots=%d\n", statp->ndots);
418 #endif
419 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
420 i = atoi(cp + sizeof("timeout:") - 1);
421 if (i <= RES_MAXRETRANS)
422 statp->retrans = i;
423 else
424 statp->retrans = RES_MAXRETRANS;
425 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
426 i = atoi(cp + sizeof("attempts:") - 1);
427 if (i <= RES_MAXRETRY)
428 statp->retry = i;
429 else
430 statp->retry = RES_MAXRETRY;
431 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
432 #ifdef DEBUG
433 if (!(statp->options & RES_DEBUG)) {
434 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
435 options, source);
436 statp->options |= RES_DEBUG;
438 printf(";;\tdebug\n");
439 #endif
440 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
441 statp->options |= RES_USE_INET6;
442 } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
443 statp->options |= RES_ROTATE;
444 } else if (!strncmp(cp, "no-check-names",
445 sizeof("no-check-names") - 1)) {
446 statp->options |= RES_NOCHECKNAME;
447 } else {
448 /* XXX - print a warning here? */
450 /* skip to next run of spaces */
451 while (*cp && *cp != ' ' && *cp != '\t')
452 cp++;
456 #ifdef RESOLVSORT
457 /* XXX - should really support CIDR which means explicit masks always. */
458 static u_int32_t
459 net_mask(in) /* XXX - should really use system's version of this */
460 struct in_addr in;
462 register u_int32_t i = ntohl(in.s_addr);
464 if (IN_CLASSA(i))
465 return (htonl(IN_CLASSA_NET));
466 else if (IN_CLASSB(i))
467 return (htonl(IN_CLASSB_NET));
468 return (htonl(IN_CLASSC_NET));
470 #endif
472 u_int
473 res_randomid(void) {
474 struct timeval now;
476 __gettimeofday(&now, NULL);
477 return (0xffff & (now.tv_sec ^ now.tv_usec ^ __getpid()));