Uniformly refer to RFCs as 'RFC xxxx' and not 'RFCxxxx' or 'RFC-xxxx'.
[dragonfly.git] / lib / libc / net / res_init.c
1 /*
2  * Copyright (c) 1985, 1989, 1993
3  *    The Regents of the University of California.  All rights reserved.
4  * 
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  * 3. 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.
16  * 
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.
28  *
29  * @(#)res_init.c       8.1 (Berkeley) 6/7/93
30  * $From: Id: res_init.c,v 8.7 1996/11/18 09:10:04 vixie Exp $
31  * $FreeBSD: src/lib/libc/net/res_init.c,v 1.19.2.7 2002/02/04 18:30:55 ume Exp $
32  * $DragonFly: src/lib/libc/net/res_init.c,v 1.6 2005/11/13 02:04:47 swildner Exp $
33  */
34
35 /*
36  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37  * 
38  * Permission to use, copy, modify, and distribute this software for any
39  * purpose with or without fee is hereby granted, provided that the above
40  * copyright notice and this permission notice appear in all copies, and that
41  * the name of Digital Equipment Corporation not be used in advertising or
42  * publicity pertaining to distribution of the document or software without
43  * specific, written prior permission.
44  * 
45  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
46  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
48  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
49  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
50  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
51  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52  * SOFTWARE.
53  */
54
55 /*
56  * Portions Copyright (c) 1996 by Internet Software Consortium.
57  *
58  * Permission to use, copy, modify, and distribute this software for any
59  * purpose with or without fee is hereby granted, provided that the above
60  * copyright notice and this permission notice appear in all copies.
61  *
62  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
63  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
64  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
65  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
69  * SOFTWARE.
70  */
71
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/time.h>
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
78 #include <arpa/nameser.h>
79 #include <ctype.h>
80 #include <resolv.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85 #include <netdb.h>
86
87 #include "res_config.h"
88
89 static void res_setoptions (char *, char *);
90
91 #ifdef RESOLVSORT
92 static const char sort_mask[] = "/&";
93 #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
94 static u_int32_t net_mask (struct in_addr);
95 #endif
96
97 #if !defined(isascii) /* XXX - could be a function */
98 # define isascii(c) (!(c & 0200))
99 #endif
100
101 /*
102  * Resolver state default settings.
103  */
104
105 struct __res_state _res
106 # if defined(__BIND_RES_TEXT)
107         = { RES_TIMEOUT, }      /* Motorola, et al. */
108 # endif
109         ;
110
111 struct __res_state_ext _res_ext;
112
113 /*
114  * Set up default settings.  If the configuration file exist, the values
115  * there will have precedence.  Otherwise, the server address is set to
116  * INADDR_ANY and the default domain name comes from the gethostname().
117  *
118  * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
119  * rather than INADDR_ANY ("0.0.0.0") as the default name server address
120  * since it was noted that INADDR_ANY actually meant ``the first interface
121  * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
122  * it had to be "up" in order for you to reach your own name server.  It
123  * was later decided that since the recommended practice is to always 
124  * install local static routes through 127.0.0.1 for all your network
125  * interfaces, that we could solve this problem without a code change.
126  *
127  * The configuration file should always be used, since it is the only way
128  * to specify a default domain.  If you are running a server on your local
129  * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
130  * in the configuration file.
131  *
132  * Return 0 if completes successfully, -1 on error
133  */
134 int
135 res_init(void)
136 {
137         FILE *fp;
138         char *cp, **pp;
139         int n;
140         char buf[MAXDNAME];
141         int nserv = 0;    /* number of nameserver records read from file */
142         int haveenv = 0;
143         int havesearch = 0;
144 #ifdef RESOLVSORT
145         int nsort = 0;
146         char *net;
147 #endif
148 #ifndef RFC1535
149         int dots;
150 #endif
151
152         /*
153          * These three fields used to be statically initialized.  This made
154          * it hard to use this code in a shared library.  It is necessary,
155          * now that we're doing dynamic initialization here, that we preserve
156          * the old semantics: if an application modifies one of these three
157          * fields of _res before res_init() is called, res_init() will not
158          * alter them.  Of course, if an application is setting them to
159          * _zero_ before calling res_init(), hoping to override what used
160          * to be the static default, we can't detect it and unexpected results
161          * will follow.  Zero for any of these fields would make no sense,
162          * so one can safely assume that the applications were already getting
163          * unexpected results.
164          *
165          * _res.options is tricky since some apps were known to diddle the bits
166          * before res_init() was first called. We can't replicate that semantic
167          * with dynamic initialization (they may have turned bits off that are
168          * set in RES_DEFAULT).  Our solution is to declare such applications
169          * "broken".  They could fool us by setting RES_INIT but none do (yet).
170          */
171         if (!_res.retrans)
172                 _res.retrans = RES_TIMEOUT;
173         if (!_res.retry)
174                 _res.retry = 4;
175         if (!(_res.options & RES_INIT))
176                 _res.options = RES_DEFAULT;
177
178         /*
179          * This one used to initialize implicitly to zero, so unless the app
180          * has set it to something in particular, we can randomize it now.
181          */
182         if (!_res.id)
183                 _res.id = res_randomid();
184
185 #ifdef USELOOPBACK
186         _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
187 #else
188         _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
189 #endif
190         _res.nsaddr.sin_family = AF_INET;
191         _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
192         _res.nsaddr.sin_len = sizeof(struct sockaddr_in);
193         if (sizeof(_res_ext.nsaddr) >= _res.nsaddr.sin_len)
194                 memcpy(&_res_ext.nsaddr, &_res.nsaddr, _res.nsaddr.sin_len);
195         _res.nscount = 1;
196         _res.ndots = 1;
197         _res.pfcode = 0;
198
199         /* Allow user to override the local domain definition */
200         if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
201                 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
202                 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
203                 haveenv++;
204
205                 /*
206                  * Set search list to be blank-separated strings
207                  * from rest of env value.  Permits users of LOCALDOMAIN
208                  * to still have a search list, and anyone to set the
209                  * one that they want to use as an individual (even more
210                  * important now that the rfc1535 stuff restricts searches)
211                  */
212                 cp = _res.defdname;
213                 pp = _res.dnsrch;
214                 *pp++ = cp;
215                 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
216                         if (*cp == '\n')        /* silly backwards compat */
217                                 break;
218                         else if (*cp == ' ' || *cp == '\t') {
219                                 *cp = 0;
220                                 n = 1;
221                         } else if (n) {
222                                 *pp++ = cp;
223                                 n = 0;
224                                 havesearch = 1;
225                         }
226                 }
227                 /* null terminate last domain if there are excess */
228                 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
229                         cp++;
230                 *cp = '\0';
231                 *pp++ = 0;
232         }
233
234 #define MATCH(line, name) \
235         (!strncmp(line, name, sizeof(name) - 1) && \
236         (line[sizeof(name) - 1] == ' ' || \
237          line[sizeof(name) - 1] == '\t'))
238
239         if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
240             /* read the config file */
241             while (fgets(buf, sizeof(buf), fp) != NULL) {
242                 /* skip comments */
243                 if (*buf == ';' || *buf == '#')
244                         continue;
245                 /* read default domain name */
246                 if (MATCH(buf, "domain")) {
247                     if (haveenv)        /* skip if have from environ */
248                             continue;
249                     cp = buf + sizeof("domain") - 1;
250                     while (*cp == ' ' || *cp == '\t')
251                             cp++;
252                     if ((*cp == '\0') || (*cp == '\n'))
253                             continue;
254                     strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
255                     _res.defdname[sizeof(_res.defdname) - 1] = '\0';
256                     if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
257                             *cp = '\0';
258                     havesearch = 0;
259                     continue;
260                 }
261                 /* set search list */
262                 if (MATCH(buf, "search")) {
263                     if (haveenv)        /* skip if have from environ */
264                             continue;
265                     cp = buf + sizeof("search") - 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                     _res.defdname[sizeof(_res.defdname) - 1] = '\0';
272                     if ((cp = strchr(_res.defdname, '\n')) != NULL)
273                             *cp = '\0';
274                     /*
275                      * Set search list to be blank-separated strings
276                      * on rest of line.
277                      */
278                     cp = _res.defdname;
279                     pp = _res.dnsrch;
280                     *pp++ = cp;
281                     for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
282                             if (*cp == ' ' || *cp == '\t') {
283                                     *cp = 0;
284                                     n = 1;
285                             } else if (n) {
286                                     *pp++ = cp;
287                                     n = 0;
288                             }
289                     }
290                     /* null terminate last domain if there are excess */
291                     while (*cp != '\0' && *cp != ' ' && *cp != '\t')
292                             cp++;
293                     *cp = '\0';
294                     *pp++ = 0;
295                     havesearch = 1;
296                     continue;
297                 }
298                 /* read nameservers to query */
299                 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
300                     char *q;
301                     struct addrinfo hints, *res;
302                     char pbuf[NI_MAXSERV];
303
304                     cp = buf + sizeof("nameserver") - 1;
305                     while (*cp == ' ' || *cp == '\t')
306                         cp++;
307                     if ((*cp == '\0') || (*cp == '\n'))
308                         continue;
309                     for (q = cp; *q; q++) {
310                         if (isspace(*q)) {
311                             *q = '\0';
312                             break;
313                         }
314                     }
315                     memset(&hints, 0, sizeof(hints));
316                     hints.ai_flags = AI_NUMERICHOST;
317                     hints.ai_socktype = SOCK_DGRAM;
318                     snprintf(pbuf, sizeof(pbuf), "%d", NAMESERVER_PORT);
319                     if (getaddrinfo(cp, pbuf, &hints, &res) == 0 &&
320                             res->ai_next == NULL) {
321                         if (res->ai_addrlen <= sizeof(_res_ext.nsaddr_list[nserv])) {
322                             memcpy(&_res_ext.nsaddr_list[nserv], res->ai_addr,
323                                 res->ai_addrlen);
324                         } else {
325                             memset(&_res_ext.nsaddr_list[nserv], 0,
326                                 sizeof(_res_ext.nsaddr_list[nserv]));
327                         }
328                         if (res->ai_addrlen <= sizeof(_res.nsaddr_list[nserv])) {
329                             memcpy(&_res.nsaddr_list[nserv], res->ai_addr,
330                                 res->ai_addrlen);
331                         } else {
332                             memset(&_res.nsaddr_list[nserv], 0,
333                                 sizeof(_res.nsaddr_list[nserv]));
334                         }
335                         nserv++;
336                     }
337                     if (res)
338                             freeaddrinfo(res);
339                     continue;
340                 }
341 #ifdef RESOLVSORT
342                 if (MATCH(buf, "sortlist")) {
343                     struct in_addr a;
344                     struct in6_addr a6;
345                     int m, i;
346                     u_char *u;
347
348                     cp = buf + sizeof("sortlist") - 1;
349                     while (nsort < MAXRESOLVSORT) {
350                         while (*cp == ' ' || *cp == '\t')
351                             cp++;
352                         if (*cp == '\0' || *cp == '\n' || *cp == ';')
353                             break;
354                         net = cp;
355                         while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
356                                isascii(*cp) && !isspace(*cp))
357                                 cp++;
358                         n = *cp;
359                         *cp = 0;
360                         if (inet_aton(net, &a)) {
361                             _res.sort_list[nsort].addr = a;
362                             if (ISSORTMASK(n)) {
363                                 *cp++ = n;
364                                 net = cp;
365                                 while (*cp && *cp != ';' &&
366                                         isascii(*cp) && !isspace(*cp))
367                                     cp++;
368                                 n = *cp;
369                                 *cp = 0;
370                                 if (inet_aton(net, &a)) {
371                                     _res.sort_list[nsort].mask = a.s_addr;
372                                 } else {
373                                     _res.sort_list[nsort].mask = 
374                                         net_mask(_res.sort_list[nsort].addr);
375                                 }
376                             } else {
377                                 _res.sort_list[nsort].mask = 
378                                     net_mask(_res.sort_list[nsort].addr);
379                             }
380                             _res_ext.sort_list[nsort].af = AF_INET;
381                             _res_ext.sort_list[nsort].addr.ina =
382                                 _res.sort_list[nsort].addr;
383                             _res_ext.sort_list[nsort].mask.ina.s_addr =
384                                 _res.sort_list[nsort].mask;
385                             nsort++;
386                         }
387                         else if (inet_pton(AF_INET6, net, &a6) == 1) {
388
389                             _res_ext.sort_list[nsort].af = AF_INET6;
390                             _res_ext.sort_list[nsort].addr.in6a = a6;
391                             u = (u_char *)&_res_ext.sort_list[nsort].mask.in6a;
392                             *cp++ = n;
393                             net = cp;
394                             while (*cp && *cp != ';' &&
395                                     isascii(*cp) && !isspace(*cp))
396                                 cp++;
397                             m = n;
398                             n = *cp;
399                             *cp = 0;
400                             switch (m) {
401                             case '/':
402                                 m = atoi(net);
403                                 break;
404                             case '&':
405                                 if (inet_pton(AF_INET6, net, u) == 1) {
406                                     m = -1;
407                                     break;
408                                 }
409                                 /*FALLTHROUGH*/
410                             default:
411                                 m = sizeof(struct in6_addr) * NBBY;
412                                 break;
413                             }
414                             if (m >= 0) {
415                                 for (i = 0; i < sizeof(struct in6_addr); i++) {
416                                     if (m <= 0) {
417                                         *u = 0;
418                                     } else {
419                                         m -= NBBY;
420                                         *u = (u_char)~0;
421                                         if (m < 0)
422                                             *u <<= -m;
423                                     }
424                                     u++;
425                                 }
426                             }
427                             _res.sort_list[nsort].addr.s_addr =
428                                 (u_int32_t)0xffffffff;
429                             _res.sort_list[nsort].mask = (u_int32_t)0xffffffff;
430                             nsort++;
431                         }
432                         *cp = n;
433                     }
434                     continue;
435                 }
436 #endif
437                 if (MATCH(buf, "options")) {
438                     res_setoptions(buf + sizeof("options") - 1, "conf");
439                     continue;
440                 }
441             }
442             if (nserv > 1) 
443                 _res.nscount = nserv;
444 #ifdef RESOLVSORT
445             _res.nsort = nsort;
446 #endif
447             fclose(fp);
448         }
449         if (_res.defdname[0] == 0 &&
450             gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
451             (cp = strchr(buf, '.')) != NULL)
452                 strcpy(_res.defdname, cp + 1);
453
454         /* find components of local domain that might be searched */
455         if (havesearch == 0) {
456                 pp = _res.dnsrch;
457                 *pp++ = _res.defdname;
458                 *pp = NULL;
459
460 #ifndef RFC1535
461                 dots = 0;
462                 for (cp = _res.defdname; *cp; cp++)
463                         dots += (*cp == '.');
464
465                 cp = _res.defdname;
466                 while (pp < _res.dnsrch + MAXDFLSRCH) {
467                         if (dots < LOCALDOMAINPARTS)
468                                 break;
469                         cp = strchr(cp, '.') + 1;    /* we know there is one */
470                         *pp++ = cp;
471                         dots--;
472                 }
473                 *pp = NULL;
474 #ifdef DEBUG
475                 if (_res.options & RES_DEBUG) {
476                         printf(";; res_init()... default dnsrch list:\n");
477                         for (pp = _res.dnsrch; *pp; pp++)
478                                 printf(";;\t%s\n", *pp);
479                         printf(";;\t..END..\n");
480                 }
481 #endif
482 #endif /* !RFC1535 */
483         }
484
485         if (issetugid())
486                 _res.options |= RES_NOALIASES;
487         else if ((cp = getenv("RES_OPTIONS")) != NULL)
488                 res_setoptions(cp, "env");
489         _res.options |= RES_INIT;
490         return (0);
491 }
492
493 static void
494 res_setoptions(char *options, char *source)
495 {
496         char *cp = options;
497         int i;
498
499 #ifdef DEBUG
500         if (_res.options & RES_DEBUG)
501                 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
502                        options, source);
503 #endif
504         while (*cp) {
505                 /* skip leading and inner runs of spaces */
506                 while (*cp == ' ' || *cp == '\t')
507                         cp++;
508                 /* search for and process individual options */
509                 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
510                         i = atoi(cp + sizeof("ndots:") - 1);
511                         if (i <= RES_MAXNDOTS)
512                                 _res.ndots = i;
513                         else
514                                 _res.ndots = RES_MAXNDOTS;
515 #ifdef DEBUG
516                         if (_res.options & RES_DEBUG)
517                                 printf(";;\tndots=%d\n", _res.ndots);
518 #endif
519                 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
520 #ifdef DEBUG
521                         if (!(_res.options & RES_DEBUG)) {
522                                 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
523                                        options, source);
524                                 _res.options |= RES_DEBUG;
525                         }
526                         printf(";;\tdebug\n");
527 #endif
528                 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
529                         _res.options |= RES_USE_INET6;
530                 } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) {
531                        _res.options |= RES_INSECURE1;
532                 } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) {
533                        _res.options |= RES_INSECURE2;
534                 } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1)) {
535                         _res.options |= RES_NOTLDQUERY;
536                 } else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
537                        _res.options |= RES_USE_EDNS0;
538                 } else {
539                         /* XXX - print a warning here? */
540                 }
541                 /* skip to next run of spaces */
542                 while (*cp && *cp != ' ' && *cp != '\t')
543                         cp++;
544         }
545 }
546
547 #ifdef RESOLVSORT
548 /* XXX - should really support CIDR which means explicit masks always. */
549 static u_int32_t
550 net_mask(struct in_addr in)     /* XXX - should really use system's version of this */
551 {
552         u_int32_t i = ntohl(in.s_addr);
553
554         if (IN_CLASSA(i))
555                 return (htonl(IN_CLASSA_NET));
556         else if (IN_CLASSB(i))
557                 return (htonl(IN_CLASSB_NET));
558         return (htonl(IN_CLASSC_NET));
559 }
560 #endif
561
562 u_int
563 res_randomid(void)
564 {
565         struct timeval now;
566
567         gettimeofday(&now, NULL);
568         return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
569 }
570
571 /*
572  * Weak aliases for applications that use certain private entry points,
573  * and fail to include <resolv.h>.
574  */
575 #undef res_init
576 __weak_reference(__res_init, res_init);