summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_init.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_init.c')
-rw-r--r--src/lib/libc/net/res_init.c518
1 files changed, 518 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c
new file mode 100644
index 0000000000..df176b7fa1
--- /dev/null
+++ b/src/lib/libc/net/res_init.c
@@ -0,0 +1,518 @@
1/* $OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1989, 1993
5 * -
6 * Copyright (c) 1985, 1989, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 * -
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 * -
55 * --Copyright--
56 */
57
58#if defined(LIBC_SCCS) && !defined(lint)
59#if 0
60static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
61static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $";
62#else
63static char rcsid[] = "$OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $";
64#endif
65#endif /* LIBC_SCCS and not lint */
66
67#include <sys/types.h>
68#include <sys/param.h>
69#include <sys/socket.h>
70#include <sys/time.h>
71#include <netinet/in.h>
72#include <arpa/inet.h>
73#include <arpa/nameser.h>
74
75#include <stdio.h>
76#include <ctype.h>
77#include <resolv.h>
78#include <unistd.h>
79#include <stdlib.h>
80#include <string.h>
81
82/*-------------------------------------- info about "sortlist" --------------
83 * Marc Majka 1994/04/16
84 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
85 *
86 * NetInfo resolver configuration directory support.
87 *
88 * Allow a NetInfo directory to be created in the hierarchy which
89 * contains the same information as the resolver configuration file.
90 *
91 * - The local domain name is stored as the value of the "domain" property.
92 * - The Internet address(es) of the name server(s) are stored as values
93 * of the "nameserver" property.
94 * - The name server addresses are stored as values of the "nameserver"
95 * property.
96 * - The search list for host-name lookup is stored as values of the
97 * "search" property.
98 * - The sortlist comprised of IP address netmask pairs are stored as
99 * values of the "sortlist" property. The IP address and optional netmask
100 * should be seperated by a slash (/) or ampersand (&) character.
101 * - Internal resolver variables can be set from the value of the "options"
102 * property.
103 */
104
105static void res_setoptions __P((char *, char *));
106
107#ifdef RESOLVSORT
108static const char sort_mask[] = "/&";
109#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
110static u_int32_t net_mask __P((struct in_addr));
111#endif
112
113/*
114 * Resolver state default settings.
115 */
116
117struct __res_state _res
118# if defined(__BIND_RES_TEXT)
119 = { RES_TIMEOUT, } /* Motorola, et al. */
120# endif
121 ;
122
123/*
124 * Set up default settings. If the configuration file exist, the values
125 * there will have precedence. Otherwise, the server address is set to
126 * INADDR_ANY and the default domain name comes from the gethostname().
127 *
128 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
129 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
130 * since it was noted that INADDR_ANY actually meant ``the first interface
131 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
132 * it had to be "up" in order for you to reach your own name server. It
133 * was later decided that since the recommended practice is to always
134 * install local static routes through 127.0.0.1 for all your network
135 * interfaces, that we could solve this problem without a code change.
136 *
137 * The configuration file should always be used, since it is the only way
138 * to specify a default domain. If you are running a server on your local
139 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
140 * in the configuration file.
141 *
142 * Return 0 if completes successfully, -1 on error
143 */
144int
145res_init()
146{
147 register FILE *fp;
148 register char *cp, **pp;
149 register int n;
150 char buf[BUFSIZ];
151 int nserv = 0; /* number of nameserver records read from file */
152 int haveenv = 0;
153 int havesearch = 0;
154 size_t len;
155#ifdef RESOLVSORT
156 int nsort = 0;
157 char *net;
158#endif
159#ifndef RFC1535
160 int dots;
161#endif
162
163 /*
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.
175 *
176 * _res.options is tricky since some apps were known to diddle the bits
177 * before res_init() was first called. We can't replicate that semantic
178 * with dynamic initialization (they may have turned bits off that are
179 * set in RES_DEFAULT). Our solution is to declare such applications
180 * "broken". They could fool us by setting RES_INIT but none do (yet).
181 */
182 if (!_res.retrans)
183 _res.retrans = RES_TIMEOUT;
184 if (!_res.retry)
185 _res.retry = 4;
186 if (!(_res.options & RES_INIT))
187 _res.options = RES_DEFAULT;
188
189#ifdef USELOOPBACK
190 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
191#else
192 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
193#endif
194 _res.nsaddr.sin_family = AF_INET;
195 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
196 _res.nscount = 1;
197 _res.ndots = 1;
198 _res.pfcode = 0;
199 strncpy(_res.lookups, "f", sizeof _res.lookups);
200
201 /* Allow user to override the local domain definition */
202 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
203 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
204 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
205 haveenv++;
206
207 /*
208 * Set search list to be blank-separated strings
209 * from rest of env value. Permits users of LOCALDOMAIN
210 * to still have a search list, and anyone to set the
211 * one that they want to use as an individual (even more
212 * important now that the rfc1535 stuff restricts searches)
213 */
214 cp = _res.defdname;
215 pp = _res.dnsrch;
216 *pp++ = cp;
217 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
218 if (*cp == '\n') /* silly backwards compat */
219 break;
220 else if (*cp == ' ' || *cp == '\t') {
221 *cp = 0;
222 n = 1;
223 } else if (n) {
224 *pp++ = cp;
225 n = 0;
226 havesearch = 1;
227 }
228 }
229 /* null terminate last domain if there are excess */
230 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
231 cp++;
232 *cp = '\0';
233 *pp++ = 0;
234 }
235
236#define MATCH(line, name) \
237 (!strncmp(line, name, sizeof(name) - 1) && \
238 (line[sizeof(name) - 1] == ' ' || \
239 line[sizeof(name) - 1] == '\t'))
240
241 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
242 strncpy(_res.lookups, "bf", sizeof _res.lookups);
243
244 /* read the config file */
245 buf[0] = '\0';
246 while ((cp = fgetln(fp, &len)) != NULL) {
247 /* skip lines that are too long or zero length */
248 if (len >= sizeof(buf) || len == 0)
249 continue;
250 (void)memcpy(buf, cp, len);
251 buf[len] = '\0';
252 /* skip comments */
253 if ((cp = strpbrk(buf, ";#")) != NULL)
254 *cp = '\0';
255 if (buf[0] == '\0')
256 continue;
257 /* read default domain name */
258 if (MATCH(buf, "domain")) {
259 if (haveenv) /* skip if have from environ */
260 continue;
261 cp = buf + sizeof("domain") - 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 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
268 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
269 *cp = '\0';
270 havesearch = 0;
271 continue;
272 }
273 /* lookup types */
274 if (MATCH(buf, "lookup")) {
275 char *sp = NULL;
276
277 bzero(_res.lookups, sizeof _res.lookups);
278 cp = buf + sizeof("lookup") - 1;
279 for (n = 0;; cp++) {
280 if (n == MAXDNSLUS)
281 break;
282 if ((*cp == '\0') || (*cp == '\n')) {
283 if (sp) {
284 if (*sp=='y' || *sp=='b' || *sp=='f')
285 _res.lookups[n++] = *sp;
286 sp = NULL;
287 }
288 break;
289 } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) {
290 if (sp) {
291 if (*sp=='y' || *sp=='b' || *sp=='f')
292 _res.lookups[n++] = *sp;
293 sp = NULL;
294 }
295 } else if (sp == NULL)
296 sp = cp;
297 }
298 continue;
299 }
300 /* set search list */
301 if (MATCH(buf, "search")) {
302 if (haveenv) /* skip if have from environ */
303 continue;
304 cp = buf + sizeof("search") - 1;
305 while (*cp == ' ' || *cp == '\t')
306 cp++;
307 if ((*cp == '\0') || (*cp == '\n'))
308 continue;
309 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
310 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
311 if ((cp = strchr(_res.defdname, '\n')) != NULL)
312 *cp = '\0';
313 /*
314 * Set search list to be blank-separated strings
315 * on rest of line.
316 */
317 cp = _res.defdname;
318 pp = _res.dnsrch;
319 *pp++ = cp;
320 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
321 if (*cp == ' ' || *cp == '\t') {
322 *cp = 0;
323 n = 1;
324 } else if (n) {
325 *pp++ = cp;
326 n = 0;
327 }
328 }
329 /* null terminate last domain if there are excess */
330 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
331 cp++;
332 *cp = '\0';
333 *pp++ = 0;
334 havesearch = 1;
335 continue;
336 }
337 /* read nameservers to query */
338 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
339 struct in_addr a;
340
341 cp = buf + sizeof("nameserver") - 1;
342 while (*cp == ' ' || *cp == '\t')
343 cp++;
344 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
345 _res.nsaddr_list[nserv].sin_addr = a;
346 _res.nsaddr_list[nserv].sin_family = AF_INET;
347 _res.nsaddr_list[nserv].sin_port =
348 htons(NAMESERVER_PORT);
349 nserv++;
350 }
351 continue;
352 }
353#ifdef RESOLVSORT
354 if (MATCH(buf, "sortlist")) {
355 struct in_addr a;
356
357 cp = buf + sizeof("sortlist") - 1;
358 while (nsort < MAXRESOLVSORT) {
359 while (*cp == ' ' || *cp == '\t')
360 cp++;
361 if (*cp == '\0' || *cp == '\n' || *cp == ';')
362 break;
363 net = cp;
364 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
365 isascii(*cp) && !isspace(*cp))
366 cp++;
367 n = *cp;
368 *cp = 0;
369 if (inet_aton(net, &a)) {
370 _res.sort_list[nsort].addr = a;
371 if (ISSORTMASK(n)) {
372 *cp++ = n;
373 net = cp;
374 while (*cp && *cp != ';' &&
375 isascii(*cp) && !isspace(*cp))
376 cp++;
377 n = *cp;
378 *cp = 0;
379 if (inet_aton(net, &a)) {
380 _res.sort_list[nsort].mask = a.s_addr;
381 } else {
382 _res.sort_list[nsort].mask =
383 net_mask(_res.sort_list[nsort].addr);
384 }
385 } else {
386 _res.sort_list[nsort].mask =
387 net_mask(_res.sort_list[nsort].addr);
388 }
389 nsort++;
390 }
391 *cp = n;
392 }
393 continue;
394 }
395#endif
396 if (MATCH(buf, "options")) {
397 res_setoptions(buf + sizeof("options") - 1, "conf");
398 continue;
399 }
400 }
401 if (nserv > 1)
402 _res.nscount = nserv;
403#ifdef RESOLVSORT
404 _res.nsort = nsort;
405#endif
406 (void) fclose(fp);
407 }
408 if (_res.defdname[0] == 0 &&
409 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
410 (cp = strchr(buf, '.')) != NULL)
411 {
412 strncpy(_res.defdname, cp + 1,
413 sizeof(_res.defdname) - 1);
414 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
415 }
416
417 /* find components of local domain that might be searched */
418 if (havesearch == 0) {
419 pp = _res.dnsrch;
420 *pp++ = _res.defdname;
421 *pp = NULL;
422
423#ifndef RFC1535
424 dots = 0;
425 for (cp = _res.defdname; *cp; cp++)
426 dots += (*cp == '.');
427
428 cp = _res.defdname;
429 while (pp < _res.dnsrch + MAXDFLSRCH) {
430 if (dots < LOCALDOMAINPARTS)
431 break;
432 cp = strchr(cp, '.') + 1; /* we know there is one */
433 *pp++ = cp;
434 dots--;
435 }
436 *pp = NULL;
437#ifdef DEBUG
438 if (_res.options & RES_DEBUG) {
439 printf(";; res_init()... default dnsrch list:\n");
440 for (pp = _res.dnsrch; *pp; pp++)
441 printf(";;\t%s\n", *pp);
442 printf(";;\t..END..\n");
443 }
444#endif /* DEBUG */
445#endif /* !RFC1535 */
446 }
447
448 if (issetugid())
449 _res.options |= RES_NOALIASES;
450 else if ((cp = getenv("RES_OPTIONS")) != NULL)
451 res_setoptions(cp, "env");
452 _res.options |= RES_INIT;
453 return (0);
454}
455
456/* ARGSUSED */
457static void
458res_setoptions(options, source)
459 char *options, *source;
460{
461 char *cp = options;
462 int i;
463
464#ifdef DEBUG
465 if (_res.options & RES_DEBUG)
466 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
467 options, source);
468#endif
469 while (*cp) {
470 /* skip leading and inner runs of spaces */
471 while (*cp == ' ' || *cp == '\t')
472 cp++;
473 /* search for and process individual options */
474 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
475 i = atoi(cp + sizeof("ndots:") - 1);
476 if (i <= RES_MAXNDOTS)
477 _res.ndots = i;
478 else
479 _res.ndots = RES_MAXNDOTS;
480#ifdef DEBUG
481 if (_res.options & RES_DEBUG)
482 printf(";;\tndots=%d\n", _res.ndots);
483#endif
484 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
485#ifdef DEBUG
486 if (!(_res.options & RES_DEBUG)) {
487 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
488 options, source);
489 _res.options |= RES_DEBUG;
490 }
491 printf(";;\tdebug\n");
492#endif
493 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
494 _res.options |= RES_USE_INET6;
495 } else {
496 /* XXX - print a warning here? */
497 }
498 /* skip to next run of spaces */
499 while (*cp && *cp != ' ' && *cp != '\t')
500 cp++;
501 }
502}
503
504#ifdef RESOLVSORT
505/* XXX - should really support CIDR which means explicit masks always. */
506static u_int32_t
507net_mask(in) /* XXX - should really use system's version of this */
508 struct in_addr in;
509{
510 register u_int32_t i = ntohl(in.s_addr);
511
512 if (IN_CLASSA(i))
513 return (htonl(IN_CLASSA_NET));
514 else if (IN_CLASSB(i))
515 return (htonl(IN_CLASSB_NET));
516 return (htonl(IN_CLASSC_NET));
517}
518#endif