]>
Commit | Line | Data |
---|---|---|
03fb6eb0 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ad21edcc A |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
03fb6eb0 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
ad21edcc | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
03fb6eb0 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ad21edcc A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
03fb6eb0 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * ++Copyright++ 1985, 1989, 1993 | |
26 | * - | |
27 | * Copyright (c) 1985, 1989, 1993 | |
28 | * The Regents of the University of California. All rights reserved. | |
29 | * | |
30 | * Redistribution and use in source and binary forms, with or without | |
31 | * modification, are permitted provided that the following conditions | |
32 | * are met: | |
33 | * 1. Redistributions of source code must retain the above copyright | |
34 | * notice, this list of conditions and the following disclaimer. | |
35 | * 2. Redistributions in binary form must reproduce the above copyright | |
36 | * notice, this list of conditions and the following disclaimer in the | |
37 | * documentation and/or other materials provided with the distribution. | |
38 | * 3. All advertising materials mentioning features or use of this software | |
39 | * must display the following acknowledgement: | |
40 | * This product includes software developed by the University of | |
41 | * California, Berkeley and its contributors. | |
42 | * 4. Neither the name of the University nor the names of its contributors | |
43 | * may be used to endorse or promote products derived from this software | |
44 | * without specific prior written permission. | |
45 | * | |
46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
56 | * SUCH DAMAGE. | |
57 | * - | |
58 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | |
59 | * | |
60 | * Permission to use, copy, modify, and distribute this software for any | |
61 | * purpose with or without fee is hereby granted, provided that the above | |
62 | * copyright notice and this permission notice appear in all copies, and that | |
63 | * the name of Digital Equipment Corporation not be used in advertising or | |
64 | * publicity pertaining to distribution of the document or software without | |
65 | * specific, written prior permission. | |
66 | * | |
67 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | |
68 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
69 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
70 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
71 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
72 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
73 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
74 | * SOFTWARE. | |
75 | * - | |
76 | * --Copyright-- | |
77 | */ | |
78 | ||
79 | #if defined(LIBC_SCCS) && !defined(lint) | |
80 | static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; | |
ccd4a120 | 81 | static char rcsid[] = "$Id: res_init.c,v 1.8 2003/02/18 17:29:24 majka Exp $"; |
03fb6eb0 A |
82 | #endif /* LIBC_SCCS and not lint */ |
83 | ||
84 | #include <sys/param.h> | |
85 | #include <sys/socket.h> | |
86 | #include <sys/time.h> | |
87 | #include <netinet/in.h> | |
88 | #include <arpa/inet.h> | |
ccd4a120 | 89 | #include <arpa/nameser8_compat.h> |
03fb6eb0 A |
90 | |
91 | #include <stdio.h> | |
92 | #include <ctype.h> | |
ccd4a120 | 93 | #include <resolv8_compat.h> |
03fb6eb0 A |
94 | #if defined(BSD) && (BSD >= 199103) |
95 | # include <unistd.h> | |
96 | # include <stdlib.h> | |
97 | # include <string.h> | |
98 | #else | |
99 | # include "portability.h" | |
100 | #endif | |
101 | ||
03fb6eb0 A |
102 | #if defined(USE_OPTIONS_H) |
103 | # include "options.h" | |
104 | #endif | |
105 | ||
106 | static void res_setoptions __P((char *, char *)); | |
107 | ||
108 | #ifdef RESOLVSORT | |
109 | static const char sort_mask[] = "/&"; | |
110 | #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) | |
111 | static u_int32_t net_mask __P((struct in_addr)); | |
112 | #endif | |
113 | ||
114 | #if !defined(isascii) /* XXX - could be a function */ | |
115 | # define isascii(c) (!(c & 0200)) | |
116 | #endif | |
117 | ||
118 | /* | |
119 | * Resolver state default settings. | |
120 | */ | |
121 | ||
122 | #if defined(__APPLE__) | |
123 | extern | |
124 | #endif /* NeXT */ | |
125 | struct __res_state _res; | |
126 | ||
127 | /* | |
128 | * Set up default settings. If the configuration file exist, the values | |
129 | * there will have precedence. Otherwise, the server address is set to | |
130 | * INADDR_ANY and the default domain name comes from the gethostname(). | |
131 | * | |
132 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 | |
133 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address | |
134 | * since it was noted that INADDR_ANY actually meant ``the first interface | |
135 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, | |
136 | * it had to be "up" in order for you to reach your own name server. It | |
137 | * was later decided that since the recommended practice is to always | |
138 | * install local static routes through 127.0.0.1 for all your network | |
139 | * interfaces, that we could solve this problem without a code change. | |
140 | * | |
141 | * The configuration file should always be used, since it is the only way | |
142 | * to specify a default domain. If you are running a server on your local | |
143 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" | |
144 | * in the configuration file. | |
145 | * | |
146 | * Return 0 if completes successfully, -1 on error | |
147 | */ | |
148 | int | |
149 | res_init() | |
150 | { | |
151 | register FILE *fp; | |
152 | register char *cp, **pp; | |
153 | register int n; | |
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 | |
162 | #ifndef RFC1535 | |
163 | int dots; | |
164 | #endif | |
165 | ||
166 | /* | |
167 | * These three fields used to be statically initialized. This made | |
168 | * it hard to use this code in a shared library. It is necessary, | |
169 | * now that we're doing dynamic initialization here, that we preserve | |
170 | * the old semantics: if an application modifies one of these three | |
171 | * fields of _res before res_init() is called, res_init() will not | |
172 | * alter them. Of course, if an application is setting them to | |
173 | * _zero_ before calling res_init(), hoping to override what used | |
174 | * to be the static default, we can't detect it and unexpected results | |
175 | * will follow. Zero for any of these fields would make no sense, | |
176 | * so one can safely assume that the applications were already getting | |
177 | * unexpected results. | |
178 | * | |
179 | * _res.options is tricky since some apps were known to diddle the bits | |
180 | * before res_init() was first called. We can't replicate that semantic | |
181 | * with dynamic initialization (they may have turned bits off that are | |
182 | * set in RES_DEFAULT). Our solution is to declare such applications | |
183 | * "broken". They could fool us by setting RES_INIT but none do (yet). | |
184 | */ | |
185 | if (!_res.retrans) | |
186 | _res.retrans = RES_TIMEOUT; | |
187 | if (!_res.retry) | |
188 | _res.retry = 4; | |
189 | if (!(_res.options & RES_INIT)) | |
190 | _res.options = RES_DEFAULT; | |
191 | ||
192 | /* | |
193 | * This one used to initialize implicitly to zero, so unless the app | |
194 | * has set it to something in particular, we can randomize it now. | |
195 | */ | |
196 | if (!_res.id) | |
197 | _res.id = res_randomid(); | |
198 | ||
199 | #ifdef USELOOPBACK | |
200 | _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); | |
201 | #else | |
202 | _res.nsaddr.sin_addr.s_addr = INADDR_ANY; | |
203 | #endif | |
204 | _res.nsaddr.sin_family = AF_INET; | |
205 | _res.nsaddr.sin_port = htons(NAMESERVER_PORT); | |
206 | _res.nscount = 1; | |
207 | _res.ndots = 1; | |
208 | _res.pfcode = 0; | |
209 | ||
210 | /* Allow user to override the local domain definition */ | |
211 | if ((cp = getenv("LOCALDOMAIN")) != NULL) { | |
212 | (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); | |
213 | haveenv++; | |
214 | ||
215 | /* | |
216 | * Set search list to be blank-separated strings | |
217 | * from rest of env value. Permits users of LOCALDOMAIN | |
218 | * to still have a search list, and anyone to set the | |
219 | * one that they want to use as an individual (even more | |
220 | * important now that the rfc1535 stuff restricts searches) | |
221 | */ | |
222 | cp = _res.defdname; | |
223 | pp = _res.dnsrch; | |
224 | *pp++ = cp; | |
225 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | |
226 | if (*cp == '\n') /* silly backwards compat */ | |
227 | break; | |
228 | else if (*cp == ' ' || *cp == '\t') { | |
229 | *cp = 0; | |
230 | n = 1; | |
231 | } else if (n) { | |
232 | *pp++ = cp; | |
233 | n = 0; | |
234 | havesearch = 1; | |
235 | } | |
236 | } | |
237 | /* null terminate last domain if there are excess */ | |
238 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') | |
239 | cp++; | |
240 | *cp = '\0'; | |
241 | *pp++ = 0; | |
242 | } | |
243 | ||
244 | #define MATCH(line, name) \ | |
245 | (!strncmp(line, name, sizeof(name) - 1) && \ | |
246 | (line[sizeof(name) - 1] == ' ' || \ | |
247 | line[sizeof(name) - 1] == '\t')) | |
248 | ||
249 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { | |
250 | /* read the config file */ | |
251 | while (fgets(buf, sizeof(buf), fp) != NULL) { | |
252 | /* skip comments */ | |
253 | if (*buf == ';' || *buf == '#') | |
254 | continue; | |
255 | /* read default domain name */ | |
256 | if (MATCH(buf, "domain")) { | |
257 | if (haveenv) /* skip if have from environ */ | |
258 | continue; | |
259 | cp = buf + sizeof("domain") - 1; | |
260 | while (*cp == ' ' || *cp == '\t') | |
261 | cp++; | |
262 | if ((*cp == '\0') || (*cp == '\n')) | |
263 | continue; | |
264 | strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); | |
265 | if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) | |
266 | *cp = '\0'; | |
267 | havesearch = 0; | |
268 | continue; | |
269 | } | |
270 | /* set search list */ | |
271 | if (MATCH(buf, "search")) { | |
272 | if (haveenv) /* skip if have from environ */ | |
273 | continue; | |
274 | cp = buf + sizeof("search") - 1; | |
275 | while (*cp == ' ' || *cp == '\t') | |
276 | cp++; | |
277 | if ((*cp == '\0') || (*cp == '\n')) | |
278 | continue; | |
279 | strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); | |
280 | if ((cp = strchr(_res.defdname, '\n')) != NULL) | |
281 | *cp = '\0'; | |
282 | /* | |
283 | * Set search list to be blank-separated strings | |
284 | * on rest of line. | |
285 | */ | |
286 | cp = _res.defdname; | |
287 | pp = _res.dnsrch; | |
288 | *pp++ = cp; | |
289 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | |
290 | if (*cp == ' ' || *cp == '\t') { | |
291 | *cp = 0; | |
292 | n = 1; | |
293 | } else if (n) { | |
294 | *pp++ = cp; | |
295 | n = 0; | |
296 | } | |
297 | } | |
298 | /* null terminate last domain if there are excess */ | |
299 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') | |
300 | cp++; | |
301 | *cp = '\0'; | |
302 | *pp++ = 0; | |
303 | havesearch = 1; | |
304 | continue; | |
305 | } | |
306 | /* read nameservers to query */ | |
307 | if (MATCH(buf, "nameserver") && nserv < MAXNS) { | |
308 | struct in_addr a; | |
309 | ||
310 | cp = buf + sizeof("nameserver") - 1; | |
311 | while (*cp == ' ' || *cp == '\t') | |
312 | cp++; | |
313 | if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { | |
314 | _res.nsaddr_list[nserv].sin_addr = a; | |
315 | _res.nsaddr_list[nserv].sin_family = AF_INET; | |
316 | _res.nsaddr_list[nserv].sin_port = | |
317 | htons(NAMESERVER_PORT); | |
318 | nserv++; | |
319 | } | |
320 | continue; | |
321 | } | |
322 | #ifdef RESOLVSORT | |
323 | if (MATCH(buf, "sortlist")) { | |
324 | struct in_addr a; | |
325 | ||
326 | cp = buf + sizeof("sortlist") - 1; | |
327 | while (nsort < MAXRESOLVSORT) { | |
328 | while (*cp == ' ' || *cp == '\t') | |
329 | cp++; | |
330 | if (*cp == '\0' || *cp == '\n' || *cp == ';') | |
331 | break; | |
332 | net = cp; | |
333 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && | |
334 | isascii(*cp) && !isspace(*cp)) | |
335 | cp++; | |
336 | n = *cp; | |
337 | *cp = 0; | |
338 | if (inet_aton(net, &a)) { | |
339 | _res.sort_list[nsort].addr = a; | |
340 | if (ISSORTMASK(n)) { | |
341 | *cp++ = n; | |
342 | net = cp; | |
343 | while (*cp && *cp != ';' && | |
344 | isascii(*cp) && !isspace(*cp)) | |
345 | cp++; | |
346 | n = *cp; | |
347 | *cp = 0; | |
348 | if (inet_aton(net, &a)) { | |
349 | _res.sort_list[nsort].mask = a.s_addr; | |
350 | } else { | |
351 | _res.sort_list[nsort].mask = | |
352 | net_mask(_res.sort_list[nsort].addr); | |
353 | } | |
354 | } else { | |
355 | _res.sort_list[nsort].mask = | |
356 | net_mask(_res.sort_list[nsort].addr); | |
357 | } | |
358 | nsort++; | |
359 | } | |
360 | *cp = n; | |
361 | } | |
362 | continue; | |
363 | } | |
364 | #endif | |
365 | if (MATCH(buf, "options")) { | |
366 | res_setoptions(buf + sizeof("options") - 1, "conf"); | |
367 | continue; | |
368 | } | |
369 | } | |
370 | if (nserv > 1) | |
371 | _res.nscount = nserv; | |
372 | #ifdef RESOLVSORT | |
373 | _res.nsort = nsort; | |
374 | #endif | |
375 | (void) fclose(fp); | |
376 | } | |
03fb6eb0 A |
377 | |
378 | if (_res.defdname[0] == 0 && | |
379 | gethostname(buf, sizeof(_res.defdname) - 1) == 0 && | |
380 | (cp = strchr(buf, '.')) != NULL) | |
381 | strcpy(_res.defdname, cp + 1); | |
382 | ||
383 | /* find components of local domain that might be searched */ | |
384 | if (havesearch == 0) { | |
385 | pp = _res.dnsrch; | |
386 | *pp++ = _res.defdname; | |
387 | *pp = NULL; | |
388 | ||
389 | #ifndef RFC1535 | |
390 | dots = 0; | |
391 | for (cp = _res.defdname; *cp; cp++) | |
392 | dots += (*cp == '.'); | |
393 | ||
394 | cp = _res.defdname; | |
395 | while (pp < _res.dnsrch + MAXDFLSRCH) { | |
396 | if (dots < LOCALDOMAINPARTS) | |
397 | break; | |
398 | cp = strchr(cp, '.') + 1; /* we know there is one */ | |
399 | *pp++ = cp; | |
400 | dots--; | |
401 | } | |
402 | *pp = NULL; | |
403 | #ifdef DEBUG | |
404 | if (_res.options & RES_DEBUG) { | |
405 | printf(";; res_init()... default dnsrch list:\n"); | |
406 | for (pp = _res.dnsrch; *pp; pp++) | |
407 | printf(";;\t%s\n", *pp); | |
408 | printf(";;\t..END..\n"); | |
409 | } | |
410 | #endif /* DEBUG */ | |
411 | #endif /* !RFC1535 */ | |
412 | } | |
413 | ||
414 | if ((cp = getenv("RES_OPTIONS")) != NULL) | |
415 | res_setoptions(cp, "env"); | |
416 | _res.options |= RES_INIT; | |
417 | return (0); | |
418 | } | |
419 | ||
420 | static void | |
421 | res_setoptions(options, source) | |
422 | char *options, *source; | |
423 | { | |
424 | char *cp = options; | |
425 | int i; | |
426 | ||
427 | #ifdef DEBUG | |
428 | if (_res.options & RES_DEBUG) | |
429 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", | |
430 | options, source); | |
431 | #endif | |
432 | while (*cp) { | |
433 | /* skip leading and inner runs of spaces */ | |
434 | while (*cp == ' ' || *cp == '\t') | |
435 | cp++; | |
436 | /* search for and process individual options */ | |
437 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { | |
438 | i = atoi(cp + sizeof("ndots:") - 1); | |
439 | if (i <= RES_MAXNDOTS) | |
440 | _res.ndots = i; | |
441 | else | |
442 | _res.ndots = RES_MAXNDOTS; | |
443 | #ifdef DEBUG | |
444 | if (_res.options & RES_DEBUG) | |
445 | printf(";;\tndots=%d\n", _res.ndots); | |
446 | #endif | |
447 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { | |
448 | #ifdef DEBUG | |
449 | if (!(_res.options & RES_DEBUG)) { | |
450 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", | |
451 | options, source); | |
452 | _res.options |= RES_DEBUG; | |
453 | } | |
454 | printf(";;\tdebug\n"); | |
455 | #endif | |
456 | } else { | |
457 | /* XXX - print a warning here? */ | |
458 | } | |
459 | /* skip to next run of spaces */ | |
460 | while (*cp && *cp != ' ' && *cp != '\t') | |
461 | cp++; | |
462 | } | |
463 | } | |
464 | ||
465 | #ifdef RESOLVSORT | |
466 | /* XXX - should really support CIDR which means explicit masks always. */ | |
467 | static u_int32_t | |
468 | net_mask(in) /* XXX - should really use system's version of this */ | |
469 | struct in_addr in; | |
470 | { | |
471 | register u_int32_t i = ntohl(in.s_addr); | |
472 | ||
473 | if (IN_CLASSA(i)) | |
474 | return (htonl(IN_CLASSA_NET)); | |
475 | else if (IN_CLASSB(i)) | |
476 | return (htonl(IN_CLASSB_NET)); | |
477 | return (htonl(IN_CLASSC_NET)); | |
478 | } | |
479 | #endif | |
480 | ||
03fb6eb0 A |
481 | u_int16_t |
482 | res_randomid() | |
483 | { | |
484 | struct timeval now; | |
485 | ||
486 | gettimeofday(&now, NULL); | |
487 | return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid())); | |
488 | } |