]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/res_init.c
resolv: Move _res deallocation functions to their own file
[thirdparty/glibc.git] / resolv / 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 * 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.
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
30 /*
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32 *
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.
39 *
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.
48 */
49
50 /*
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
52 *
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.
56 *
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.
65 */
66
67 #include <ctype.h>
68 #include <netdb.h>
69 #include <resolv/resolv-internal.h>
70 #include <stdio.h>
71 #include <stdio_ext.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <stdint.h>
76 #include <arpa/inet.h>
77 #include <arpa/nameser.h>
78 #include <net/if.h>
79 #include <netinet/in.h>
80 #include <sys/param.h>
81 #include <sys/socket.h>
82 #include <sys/time.h>
83 #include <sys/types.h>
84 #include <inet/net-internal.h>
85
86 /* Options. Should all be left alone. */
87 /* #undef DEBUG */
88
89 static void res_setoptions (res_state, const char *, const char *)
90 internal_function;
91
92 static const char sort_mask_chars[] = "/&";
93 #define ISSORTMASK(ch) (strchr(sort_mask_chars, ch) != NULL)
94 static u_int32_t net_mask (struct in_addr) __THROW;
95
96 unsigned long long int __res_initstamp attribute_hidden;
97
98 /*
99 * Resolver state default settings.
100 */
101
102 /*
103 * Set up default settings. If the configuration file exist, the values
104 * there will have precedence. Otherwise, the server address is set to
105 * INADDR_LOOPBACK and the default domain name comes from gethostname.
106 *
107 * Return 0 if completes successfully, -1 on error
108 */
109 int
110 res_ninit(res_state statp) {
111 return (__res_vinit(statp, 0));
112 }
113 libc_hidden_def (__res_ninit)
114
115 /* This function has to be reachable by res_data.c but not publically. */
116 int
117 __res_vinit(res_state statp, int preinit) {
118 FILE *fp;
119 char *cp, **pp;
120 int n;
121 char buf[BUFSIZ];
122 int nserv = 0; /* number of nameservers read from file */
123 int have_serv6 = 0;
124 int haveenv = 0;
125 int havesearch = 0;
126 int nsort = 0;
127 char *net;
128 statp->_u._ext.initstamp = __res_initstamp;
129
130 if (!preinit) {
131 statp->retrans = RES_TIMEOUT;
132 statp->retry = RES_DFLRETRY;
133 statp->options = RES_DEFAULT;
134 statp->id = res_randomid();
135 }
136
137 statp->nscount = 0;
138 statp->defdname[0] = '\0';
139 statp->ndots = 1;
140 statp->pfcode = 0;
141 statp->_vcsock = -1;
142 statp->_flags = 0;
143 statp->__glibc_unused_qhook = NULL;
144 statp->__glibc_unused_rhook = NULL;
145 statp->_u._ext.nscount = 0;
146 for (n = 0; n < MAXNS; n++)
147 statp->_u._ext.nsaddrs[n] = NULL;
148
149 /* Allow user to override the local domain definition */
150 if ((cp = getenv("LOCALDOMAIN")) != NULL) {
151 (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
152 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
153 haveenv++;
154
155 /*
156 * Set search list to be blank-separated strings
157 * from rest of env value. Permits users of LOCALDOMAIN
158 * to still have a search list, and anyone to set the
159 * one that they want to use as an individual (even more
160 * important now that the rfc1535 stuff restricts searches)
161 */
162 cp = statp->defdname;
163 pp = statp->dnsrch;
164 *pp++ = cp;
165 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
166 if (*cp == '\n') /* silly backwards compat */
167 break;
168 else if (*cp == ' ' || *cp == '\t') {
169 *cp = 0;
170 n = 1;
171 } else if (n) {
172 *pp++ = cp;
173 n = 0;
174 havesearch = 1;
175 }
176 }
177 /* null terminate last domain if there are excess */
178 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
179 cp++;
180 *cp = '\0';
181 *pp++ = 0;
182 }
183
184 #define MATCH(line, name) \
185 (!strncmp(line, name, sizeof(name) - 1) && \
186 (line[sizeof(name) - 1] == ' ' || \
187 line[sizeof(name) - 1] == '\t'))
188
189 if ((fp = fopen(_PATH_RESCONF, "rce")) != NULL) {
190 /* No threads use this stream. */
191 __fsetlocking (fp, FSETLOCKING_BYCALLER);
192 /* read the config file */
193 while (__fgets_unlocked(buf, sizeof(buf), fp) != NULL) {
194 /* skip comments */
195 if (*buf == ';' || *buf == '#')
196 continue;
197 /* read default domain name */
198 if (MATCH(buf, "domain")) {
199 if (haveenv) /* skip if have from environ */
200 continue;
201 cp = buf + sizeof("domain") - 1;
202 while (*cp == ' ' || *cp == '\t')
203 cp++;
204 if ((*cp == '\0') || (*cp == '\n'))
205 continue;
206 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
207 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
208 if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
209 *cp = '\0';
210 havesearch = 0;
211 continue;
212 }
213 /* set search list */
214 if (MATCH(buf, "search")) {
215 if (haveenv) /* skip if have from environ */
216 continue;
217 cp = buf + sizeof("search") - 1;
218 while (*cp == ' ' || *cp == '\t')
219 cp++;
220 if ((*cp == '\0') || (*cp == '\n'))
221 continue;
222 strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
223 statp->defdname[sizeof(statp->defdname) - 1] = '\0';
224 if ((cp = strchr(statp->defdname, '\n')) != NULL)
225 *cp = '\0';
226 /*
227 * Set search list to be blank-separated strings
228 * on rest of line.
229 */
230 cp = statp->defdname;
231 pp = statp->dnsrch;
232 *pp++ = cp;
233 for (n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++) {
234 if (*cp == ' ' || *cp == '\t') {
235 *cp = 0;
236 n = 1;
237 } else if (n) {
238 *pp++ = cp;
239 n = 0;
240 }
241 }
242 /* null terminate last domain if there are excess */
243 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
244 cp++;
245 *cp = '\0';
246 *pp++ = 0;
247 havesearch = 1;
248 continue;
249 }
250 /* read nameservers to query */
251 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
252 struct in_addr a;
253
254 cp = buf + sizeof("nameserver") - 1;
255 while (*cp == ' ' || *cp == '\t')
256 cp++;
257 if ((*cp != '\0') && (*cp != '\n')
258 && __inet_aton(cp, &a)) {
259 statp->nsaddr_list[nserv].sin_addr = a;
260 statp->nsaddr_list[nserv].sin_family = AF_INET;
261 statp->nsaddr_list[nserv].sin_port =
262 htons(NAMESERVER_PORT);
263 nserv++;
264 } else {
265 struct in6_addr a6;
266 char *el;
267
268 if ((el = strpbrk(cp, " \t\n")) != NULL)
269 *el = '\0';
270 if ((el = strchr(cp, SCOPE_DELIMITER)) != NULL)
271 *el = '\0';
272 if ((*cp != '\0') &&
273 (__inet_pton(AF_INET6, cp, &a6) > 0)) {
274 struct sockaddr_in6 *sa6;
275
276 sa6 = malloc(sizeof(*sa6));
277 if (sa6 != NULL) {
278 sa6->sin6_family = AF_INET6;
279 sa6->sin6_port = htons(NAMESERVER_PORT);
280 sa6->sin6_flowinfo = 0;
281 sa6->sin6_addr = a6;
282
283 sa6->sin6_scope_id = 0;
284 if (__glibc_likely (el != NULL)) {
285 /* Ignore errors, for backwards
286 compatibility. */
287 (void) __inet6_scopeid_pton
288 (&a6, el + 1, &sa6->sin6_scope_id);
289 }
290
291 statp->nsaddr_list[nserv].sin_family = 0;
292 statp->_u._ext.nsaddrs[nserv] = sa6;
293 statp->_u._ext.nssocks[nserv] = -1;
294 have_serv6 = 1;
295 nserv++;
296 }
297 }
298 }
299 continue;
300 }
301 if (MATCH(buf, "sortlist")) {
302 struct in_addr a;
303
304 cp = buf + sizeof("sortlist") - 1;
305 while (nsort < MAXRESOLVSORT) {
306 while (*cp == ' ' || *cp == '\t')
307 cp++;
308 if (*cp == '\0' || *cp == '\n' || *cp == ';')
309 break;
310 net = cp;
311 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
312 isascii(*cp) && !isspace(*cp))
313 cp++;
314 n = *cp;
315 *cp = 0;
316 if (__inet_aton(net, &a)) {
317 statp->sort_list[nsort].addr = a;
318 if (ISSORTMASK(n)) {
319 *cp++ = n;
320 net = cp;
321 while (*cp && *cp != ';' &&
322 isascii(*cp) && !isspace(*cp))
323 cp++;
324 n = *cp;
325 *cp = 0;
326 if (__inet_aton(net, &a)) {
327 statp->sort_list[nsort].mask = a.s_addr;
328 } else {
329 statp->sort_list[nsort].mask =
330 net_mask(statp->sort_list[nsort].addr);
331 }
332 } else {
333 statp->sort_list[nsort].mask =
334 net_mask(statp->sort_list[nsort].addr);
335 }
336 nsort++;
337 }
338 *cp = n;
339 }
340 continue;
341 }
342 if (MATCH(buf, "options")) {
343 res_setoptions(statp, buf + sizeof("options") - 1, "conf");
344 continue;
345 }
346 }
347 statp->nscount = nserv;
348 if (have_serv6) {
349 /* We try IPv6 servers again. */
350 statp->ipv6_unavail = false;
351 }
352 statp->nsort = nsort;
353 (void) fclose(fp);
354 }
355 if (__glibc_unlikely (statp->nscount == 0)) {
356 statp->nsaddr.sin_addr = __inet_makeaddr(IN_LOOPBACKNET, 1);
357 statp->nsaddr.sin_family = AF_INET;
358 statp->nsaddr.sin_port = htons(NAMESERVER_PORT);
359 statp->nscount = 1;
360 }
361 if (statp->defdname[0] == 0 &&
362 __gethostname(buf, sizeof(statp->defdname) - 1) == 0 &&
363 (cp = strchr(buf, '.')) != NULL)
364 strcpy(statp->defdname, cp + 1);
365
366 /* find components of local domain that might be searched */
367 if (havesearch == 0) {
368 pp = statp->dnsrch;
369 *pp++ = statp->defdname;
370 *pp = NULL;
371
372 }
373
374 if ((cp = getenv("RES_OPTIONS")) != NULL)
375 res_setoptions(statp, cp, "env");
376 statp->options |= RES_INIT;
377 return (0);
378 }
379
380 static void
381 internal_function
382 res_setoptions(res_state statp, const char *options, const char *source) {
383 const char *cp = options;
384 int i;
385
386 #ifdef DEBUG
387 if (statp->options & RES_DEBUG)
388 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
389 options, source);
390 #endif
391 while (*cp) {
392 /* skip leading and inner runs of spaces */
393 while (*cp == ' ' || *cp == '\t')
394 cp++;
395 /* search for and process individual options */
396 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
397 i = atoi(cp + sizeof("ndots:") - 1);
398 if (i <= RES_MAXNDOTS)
399 statp->ndots = i;
400 else
401 statp->ndots = RES_MAXNDOTS;
402 #ifdef DEBUG
403 if (statp->options & RES_DEBUG)
404 printf(";;\tndots=%d\n", statp->ndots);
405 #endif
406 } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
407 i = atoi(cp + sizeof("timeout:") - 1);
408 if (i <= RES_MAXRETRANS)
409 statp->retrans = i;
410 else
411 statp->retrans = RES_MAXRETRANS;
412 } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
413 i = atoi(cp + sizeof("attempts:") - 1);
414 if (i <= RES_MAXRETRY)
415 statp->retry = i;
416 else
417 statp->retry = RES_MAXRETRY;
418 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
419 #ifdef DEBUG
420 if (!(statp->options & RES_DEBUG)) {
421 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
422 options, source);
423 statp->options |= RES_DEBUG;
424 }
425 printf(";;\tdebug\n");
426 #endif
427 } else {
428 static const struct
429 {
430 char str[22];
431 uint8_t len;
432 uint8_t clear;
433 unsigned long int flag;
434 } options[] = {
435 #define STRnLEN(str) str, sizeof (str) - 1
436 { STRnLEN ("inet6"), 0, DEPRECATED_RES_USE_INET6 },
437 { STRnLEN ("rotate"), 0, RES_ROTATE },
438 { STRnLEN ("edns0"), 0, RES_USE_EDNS0 },
439 { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP },
440 { STRnLEN ("single-request"), 0, RES_SNGLKUP },
441 { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY },
442 { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY },
443 { STRnLEN ("use-vc"), 0, RES_USEVC }
444 };
445 #define noptions (sizeof (options) / sizeof (options[0]))
446 int i;
447 for (i = 0; i < noptions; ++i)
448 if (strncmp (cp, options[i].str, options[i].len) == 0)
449 {
450 if (options[i].clear)
451 statp->options &= options[i].flag;
452 else
453 statp->options |= options[i].flag;
454 break;
455 }
456 if (i == noptions) {
457 /* XXX - print a warning here? */
458 }
459 }
460 /* skip to next run of spaces */
461 while (*cp && *cp != ' ' && *cp != '\t')
462 cp++;
463 }
464 }
465
466 /* XXX - should really support CIDR which means explicit masks always. */
467 /* XXX - should really use system's version of this */
468 static u_int32_t
469 net_mask (struct in_addr in)
470 {
471 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 }