resolv: Remove source argument fron res_options
[glibc.git] / resolv / res_init.c
blob5d8b2c994d8e6f0446468857c980f1cf65822342
1 /* Resolver state initialization and resolv.conf parsing.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 * Copyright (c) 1985, 1989, 1993
21 * The Regents of the University of California. All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
51 * Permission to use, copy, modify, and distribute this software for any
52 * purpose with or without fee is hereby granted, provided that the above
53 * copyright notice and this permission notice appear in all copies, and that
54 * the name of Digital Equipment Corporation not be used in advertising or
55 * publicity pertaining to distribution of the document or software without
56 * specific, written prior permission.
58 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
59 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
61 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
62 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
63 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
64 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
65 * SOFTWARE.
69 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
71 * Permission to use, copy, modify, and distribute this software for any
72 * purpose with or without fee is hereby granted, provided that the above
73 * copyright notice and this permission notice appear in all copies.
75 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
76 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
77 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
78 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
79 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
80 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
81 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
82 * SOFTWARE.
85 #include <ctype.h>
86 #include <netdb.h>
87 #include <resolv/resolv-internal.h>
88 #include <res_hconf.h>
89 #include <stdio.h>
90 #include <stdio_ext.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94 #include <stdint.h>
95 #include <arpa/inet.h>
96 #include <arpa/nameser.h>
97 #include <net/if.h>
98 #include <netinet/in.h>
99 #include <sys/param.h>
100 #include <sys/socket.h>
101 #include <sys/time.h>
102 #include <sys/types.h>
103 #include <inet/net-internal.h>
104 #include <errno.h>
106 static void res_setoptions (res_state, const char *);
107 static uint32_t net_mask (struct in_addr);
109 unsigned long long int __res_initstamp;
112 res_ninit (res_state statp)
114 return __res_vinit (statp, 0);
116 libc_hidden_def (__res_ninit)
118 /* Return true if CH separates the netmask in the "sortlist"
119 directive. */
120 static inline bool
121 is_sort_mask (char ch)
123 return ch == '/' || ch == '&';
126 /* Internal helper function for __res_vinit, to aid with resource
127 deallocation and error handling. Return true on success, false on
128 failure. */
129 static bool
130 res_vinit_1 (res_state statp, bool preinit, FILE *fp, char **buffer)
132 char *cp, **pp;
133 size_t buffer_size = 0;
134 int nserv = 0; /* Number of nameservers read from file. */
135 bool have_serv6 = false;
136 bool haveenv = false;
137 bool havesearch = false;
138 int nsort = 0;
139 char *net;
140 statp->_u._ext.initstamp = __res_initstamp;
142 if (!preinit)
144 statp->retrans = RES_TIMEOUT;
145 statp->retry = RES_DFLRETRY;
146 statp->options = RES_DEFAULT;
147 statp->id = res_randomid ();
150 statp->nscount = 0;
151 statp->defdname[0] = '\0';
152 statp->ndots = 1;
153 statp->pfcode = 0;
154 statp->_vcsock = -1;
155 statp->_flags = 0;
156 statp->__glibc_unused_qhook = NULL;
157 statp->__glibc_unused_rhook = NULL;
158 statp->_u._ext.nscount = 0;
159 for (int n = 0; n < MAXNS; n++)
160 statp->_u._ext.nsaddrs[n] = NULL;
162 /* Allow user to override the local domain definition. */
163 if ((cp = getenv ("LOCALDOMAIN")) != NULL)
165 strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
166 statp->defdname[sizeof (statp->defdname) - 1] = '\0';
167 haveenv = true;
169 /* Set search list to be blank-separated strings from rest of
170 env value. Permits users of LOCALDOMAIN to still have a
171 search list, and anyone to set the one that they want to use
172 as an individual (even more important now that the rfc1535
173 stuff restricts searches). */
174 cp = statp->defdname;
175 pp = statp->dnsrch;
176 *pp++ = cp;
177 for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
179 if (*cp == '\n')
180 break;
181 else if (*cp == ' ' || *cp == '\t')
183 *cp = 0;
184 n = 1;
186 else if (n > 0)
188 *pp++ = cp;
189 n = 0;
190 havesearch = true;
193 /* Null terminate last domain if there are excess. */
194 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
195 cp++;
196 *cp = '\0';
197 *pp++ = 0;
200 #define MATCH(line, name) \
201 (!strncmp ((line), name, sizeof (name) - 1) \
202 && ((line)[sizeof (name) - 1] == ' ' \
203 || (line)[sizeof (name) - 1] == '\t'))
205 if (fp != NULL)
207 /* No threads use this stream. */
208 __fsetlocking (fp, FSETLOCKING_BYCALLER);
209 /* Read the config file. */
210 while (true)
213 ssize_t ret = __getline (buffer, &buffer_size, fp);
214 if (ret <= 0)
216 if (_IO_ferror_unlocked (fp))
217 return false;
218 else
219 break;
223 /* Skip comments. */
224 if (**buffer == ';' || **buffer == '#')
225 continue;
226 /* Read default domain name. */
227 if (MATCH (*buffer, "domain"))
229 if (haveenv)
230 /* LOCALDOMAIN overrides the configuration file. */
231 continue;
232 cp = *buffer + sizeof ("domain") - 1;
233 while (*cp == ' ' || *cp == '\t')
234 cp++;
235 if ((*cp == '\0') || (*cp == '\n'))
236 continue;
237 strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
238 statp->defdname[sizeof (statp->defdname) - 1] = '\0';
239 if ((cp = strpbrk (statp->defdname, " \t\n")) != NULL)
240 *cp = '\0';
241 havesearch = false;
242 continue;
244 /* Set search list. */
245 if (MATCH (*buffer, "search"))
247 if (haveenv)
248 /* LOCALDOMAIN overrides the configuration file. */
249 continue;
250 cp = *buffer + sizeof ("search") - 1;
251 while (*cp == ' ' || *cp == '\t')
252 cp++;
253 if ((*cp == '\0') || (*cp == '\n'))
254 continue;
255 strncpy (statp->defdname, cp, sizeof (statp->defdname) - 1);
256 statp->defdname[sizeof (statp->defdname) - 1] = '\0';
257 if ((cp = strchr (statp->defdname, '\n')) != NULL)
258 *cp = '\0';
259 /* Set search list to be blank-separated strings on rest
260 of line. */
261 cp = statp->defdname;
262 pp = statp->dnsrch;
263 *pp++ = cp;
264 for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
266 if (*cp == ' ' || *cp == '\t')
268 *cp = 0;
269 n = 1;
271 else if (n)
273 *pp++ = cp;
274 n = 0;
277 /* Null terminate last domain if there are excess. */
278 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
279 cp++;
280 *cp = '\0';
281 *pp++ = 0;
282 havesearch = true;
283 continue;
285 /* Read nameservers to query. */
286 if (MATCH (*buffer, "nameserver") && nserv < MAXNS)
288 struct in_addr a;
290 cp = *buffer + sizeof ("nameserver") - 1;
291 while (*cp == ' ' || *cp == '\t')
292 cp++;
293 if ((*cp != '\0') && (*cp != '\n') && __inet_aton (cp, &a))
295 statp->nsaddr_list[nserv].sin_addr = a;
296 statp->nsaddr_list[nserv].sin_family = AF_INET;
297 statp->nsaddr_list[nserv].sin_port = htons (NAMESERVER_PORT);
298 nserv++;
300 else
302 struct in6_addr a6;
303 char *el;
305 if ((el = strpbrk (cp, " \t\n")) != NULL)
306 *el = '\0';
307 if ((el = strchr (cp, SCOPE_DELIMITER)) != NULL)
308 *el = '\0';
309 if ((*cp != '\0') && (__inet_pton (AF_INET6, cp, &a6) > 0))
311 struct sockaddr_in6 *sa6;
313 sa6 = malloc (sizeof (*sa6));
314 if (sa6 == NULL)
315 return false;
317 sa6->sin6_family = AF_INET6;
318 sa6->sin6_port = htons (NAMESERVER_PORT);
319 sa6->sin6_flowinfo = 0;
320 sa6->sin6_addr = a6;
322 sa6->sin6_scope_id = 0;
323 if (__glibc_likely (el != NULL))
324 /* Ignore errors, for backwards
325 compatibility. */
326 __inet6_scopeid_pton
327 (&a6, el + 1, &sa6->sin6_scope_id);
329 statp->nsaddr_list[nserv].sin_family = 0;
330 statp->_u._ext.nsaddrs[nserv] = sa6;
331 statp->_u._ext.nssocks[nserv] = -1;
332 have_serv6 = true;
333 nserv++;
336 continue;
338 if (MATCH (*buffer, "sortlist"))
340 struct in_addr a;
342 cp = *buffer + sizeof ("sortlist") - 1;
343 while (nsort < MAXRESOLVSORT)
345 while (*cp == ' ' || *cp == '\t')
346 cp++;
347 if (*cp == '\0' || *cp == '\n' || *cp == ';')
348 break;
349 net = cp;
350 while (*cp && !is_sort_mask (*cp) && *cp != ';'
351 && isascii (*cp) && !isspace (*cp))
352 cp++;
353 char separator = *cp;
354 *cp = 0;
355 if (__inet_aton (net, &a))
357 statp->sort_list[nsort].addr = a;
358 if (is_sort_mask (separator))
360 *cp++ = separator;
361 net = cp;
362 while (*cp && *cp != ';'
363 && isascii (*cp) && !isspace (*cp))
364 cp++;
365 separator = *cp;
366 *cp = 0;
367 if (__inet_aton (net, &a))
368 statp->sort_list[nsort].mask = a.s_addr;
369 else
370 statp->sort_list[nsort].mask
371 = net_mask (statp->sort_list[nsort].addr);
373 else
374 statp->sort_list[nsort].mask
375 = net_mask (statp->sort_list[nsort].addr);
376 nsort++;
378 *cp = separator;
380 continue;
382 if (MATCH (*buffer, "options"))
384 res_setoptions (statp, *buffer + sizeof ("options") - 1);
385 continue;
388 statp->nscount = nserv;
389 if (have_serv6)
390 /* We try IPv6 servers again. */
391 statp->ipv6_unavail = false;
392 statp->nsort = nsort;
393 fclose (fp);
395 if (__glibc_unlikely (statp->nscount == 0))
397 statp->nsaddr.sin_addr = __inet_makeaddr (IN_LOOPBACKNET, 1);
398 statp->nsaddr.sin_family = AF_INET;
399 statp->nsaddr.sin_port = htons (NAMESERVER_PORT);
400 statp->nscount = 1;
402 if (statp->defdname[0] == 0)
404 char buf[sizeof (statp->defdname)];
405 if (__gethostname (buf, sizeof (statp->defdname) - 1) == 0
406 && (cp = strchr (buf, '.')) != NULL)
407 strcpy (statp->defdname, cp + 1);
410 /* Find components of local domain that might be searched. */
411 if (!havesearch)
413 pp = statp->dnsrch;
414 *pp++ = statp->defdname;
415 *pp = NULL;
419 if ((cp = getenv ("RES_OPTIONS")) != NULL)
420 res_setoptions (statp, cp);
421 statp->options |= RES_INIT;
422 return true;
425 /* Set up default settings. If the /etc/resolv.conf configuration
426 file exist, the values there will have precedence. Otherwise, the
427 server address is set to INADDR_LOOPBACK and the default domain
428 name comes from gethostname. The RES_OPTIONS and LOCALDOMAIN
429 environment variables can be used to override some settings.
430 Return 0 if completes successfully, -1 on error. */
432 __res_vinit (res_state statp, int preinit)
434 /* Ensure that /etc/hosts.conf has been loaded (once). */
435 _res_hconf_init ();
437 FILE *fp = fopen (_PATH_RESCONF, "rce");
438 if (fp == NULL)
439 switch (errno)
441 case EACCES:
442 case EISDIR:
443 case ELOOP:
444 case ENOENT:
445 case ENOTDIR:
446 case EPERM:
447 /* Ignore these errors. They are persistent errors caused
448 by file system contents. */
449 break;
450 default:
451 /* Other errors refer to resource allocation problems and
452 need to be handled by the application. */
453 return -1;
456 char *buffer = NULL;
457 bool ok = res_vinit_1 (statp, preinit, fp, &buffer);
458 free (buffer);
460 if (!ok)
462 /* Deallocate the name server addresses which have been
463 allocated. */
464 for (int n = 0; n < MAXNS; n++)
465 free (statp->_u._ext.nsaddrs[n]);
466 return -1;
468 return 0;
471 static void
472 res_setoptions (res_state statp, const char *options)
474 const char *cp = options;
476 while (*cp)
478 /* Skip leading and inner runs of spaces. */
479 while (*cp == ' ' || *cp == '\t')
480 cp++;
481 /* Search for and process individual options. */
482 if (!strncmp (cp, "ndots:", sizeof ("ndots:") - 1))
484 int i = atoi (cp + sizeof ("ndots:") - 1);
485 if (i <= RES_MAXNDOTS)
486 statp->ndots = i;
487 else
488 statp->ndots = RES_MAXNDOTS;
490 else if (!strncmp (cp, "timeout:", sizeof ("timeout:") - 1))
492 int i = atoi (cp + sizeof ("timeout:") - 1);
493 if (i <= RES_MAXRETRANS)
494 statp->retrans = i;
495 else
496 statp->retrans = RES_MAXRETRANS;
498 else if (!strncmp (cp, "attempts:", sizeof ("attempts:") - 1))
500 int i = atoi (cp + sizeof ("attempts:") - 1);
501 if (i <= RES_MAXRETRY)
502 statp->retry = i;
503 else
504 statp->retry = RES_MAXRETRY;
506 else
508 static const struct
510 char str[22];
511 uint8_t len;
512 uint8_t clear;
513 unsigned long int flag;
514 } options[] = {
515 #define STRnLEN(str) str, sizeof (str) - 1
516 { STRnLEN ("inet6"), 0, DEPRECATED_RES_USE_INET6 },
517 { STRnLEN ("rotate"), 0, RES_ROTATE },
518 { STRnLEN ("edns0"), 0, RES_USE_EDNS0 },
519 { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP },
520 { STRnLEN ("single-request"), 0, RES_SNGLKUP },
521 { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY },
522 { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY },
523 { STRnLEN ("use-vc"), 0, RES_USEVC }
525 #define noptions (sizeof (options) / sizeof (options[0]))
526 for (int i = 0; i < noptions; ++i)
527 if (strncmp (cp, options[i].str, options[i].len) == 0)
529 if (options[i].clear)
530 statp->options &= options[i].flag;
531 else
532 statp->options |= options[i].flag;
533 break;
536 /* Skip to next run of spaces. */
537 while (*cp && *cp != ' ' && *cp != '\t')
538 cp++;
542 static uint32_t
543 net_mask (struct in_addr in)
545 uint32_t i = ntohl (in.s_addr);
547 if (IN_CLASSA (i))
548 return htonl (IN_CLASSA_NET);
549 else if (IN_CLASSB (i))
550 return htonl (IN_CLASSB_NET);
551 return htonl (IN_CLASSC_NET);