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
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
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
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
87 #include <resolv/resolv-internal.h>
88 #include <res_hconf.h>
90 #include <stdio_ext.h>
95 #include <arpa/inet.h>
96 #include <arpa/nameser.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>
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"
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
130 res_vinit_1 (res_state statp
, bool preinit
, FILE *fp
, char **buffer
)
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;
140 statp
->_u
._ext
.initstamp
= __res_initstamp
;
144 statp
->retrans
= RES_TIMEOUT
;
145 statp
->retry
= RES_DFLRETRY
;
146 statp
->options
= RES_DEFAULT
;
147 statp
->id
= res_randomid ();
151 statp
->defdname
[0] = '\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';
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
;
177 for (int n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++)
181 else if (*cp
== ' ' || *cp
== '\t')
193 /* Null terminate last domain if there are excess. */
194 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
200 #define MATCH(line, name) \
201 (!strncmp ((line), name, sizeof (name) - 1) \
202 && ((line)[sizeof (name) - 1] == ' ' \
203 || (line)[sizeof (name) - 1] == '\t'))
207 /* No threads use this stream. */
208 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
209 /* Read the config file. */
213 ssize_t ret
= __getline (buffer
, &buffer_size
, fp
);
216 if (_IO_ferror_unlocked (fp
))
224 if (**buffer
== ';' || **buffer
== '#')
226 /* Read default domain name. */
227 if (MATCH (*buffer
, "domain"))
230 /* LOCALDOMAIN overrides the configuration file. */
232 cp
= *buffer
+ sizeof ("domain") - 1;
233 while (*cp
== ' ' || *cp
== '\t')
235 if ((*cp
== '\0') || (*cp
== '\n'))
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
)
244 /* Set search list. */
245 if (MATCH (*buffer
, "search"))
248 /* LOCALDOMAIN overrides the configuration file. */
250 cp
= *buffer
+ sizeof ("search") - 1;
251 while (*cp
== ' ' || *cp
== '\t')
253 if ((*cp
== '\0') || (*cp
== '\n'))
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
)
259 /* Set search list to be blank-separated strings on rest
261 cp
= statp
->defdname
;
264 for (int n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++)
266 if (*cp
== ' ' || *cp
== '\t')
277 /* Null terminate last domain if there are excess. */
278 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
285 /* Read nameservers to query. */
286 if (MATCH (*buffer
, "nameserver") && nserv
< MAXNS
)
290 cp
= *buffer
+ sizeof ("nameserver") - 1;
291 while (*cp
== ' ' || *cp
== '\t')
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
);
305 if ((el
= strpbrk (cp
, " \t\n")) != NULL
)
307 if ((el
= strchr (cp
, SCOPE_DELIMITER
)) != NULL
)
309 if ((*cp
!= '\0') && (__inet_pton (AF_INET6
, cp
, &a6
) > 0))
311 struct sockaddr_in6
*sa6
;
313 sa6
= malloc (sizeof (*sa6
));
317 sa6
->sin6_family
= AF_INET6
;
318 sa6
->sin6_port
= htons (NAMESERVER_PORT
);
319 sa6
->sin6_flowinfo
= 0;
322 sa6
->sin6_scope_id
= 0;
323 if (__glibc_likely (el
!= NULL
))
324 /* Ignore errors, for backwards
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;
338 if (MATCH (*buffer
, "sortlist"))
342 cp
= *buffer
+ sizeof ("sortlist") - 1;
343 while (nsort
< MAXRESOLVSORT
)
345 while (*cp
== ' ' || *cp
== '\t')
347 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
350 while (*cp
&& !is_sort_mask (*cp
) && *cp
!= ';'
351 && isascii (*cp
) && !isspace (*cp
))
353 char separator
= *cp
;
355 if (__inet_aton (net
, &a
))
357 statp
->sort_list
[nsort
].addr
= a
;
358 if (is_sort_mask (separator
))
362 while (*cp
&& *cp
!= ';'
363 && isascii (*cp
) && !isspace (*cp
))
367 if (__inet_aton (net
, &a
))
368 statp
->sort_list
[nsort
].mask
= a
.s_addr
;
370 statp
->sort_list
[nsort
].mask
371 = net_mask (statp
->sort_list
[nsort
].addr
);
374 statp
->sort_list
[nsort
].mask
375 = net_mask (statp
->sort_list
[nsort
].addr
);
382 if (MATCH (*buffer
, "options"))
384 res_setoptions (statp
, *buffer
+ sizeof ("options") - 1);
388 statp
->nscount
= nserv
;
390 /* We try IPv6 servers again. */
391 statp
->ipv6_unavail
= false;
392 statp
->nsort
= nsort
;
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
);
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. */
414 *pp
++ = statp
->defdname
;
419 if ((cp
= getenv ("RES_OPTIONS")) != NULL
)
420 res_setoptions (statp
, cp
);
421 statp
->options
|= RES_INIT
;
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). */
437 FILE *fp
= fopen (_PATH_RESCONF
, "rce");
447 /* Ignore these errors. They are persistent errors caused
448 by file system contents. */
451 /* Other errors refer to resource allocation problems and
452 need to be handled by the application. */
457 bool ok
= res_vinit_1 (statp
, preinit
, fp
, &buffer
);
462 /* Deallocate the name server addresses which have been
464 for (int n
= 0; n
< MAXNS
; n
++)
465 free (statp
->_u
._ext
.nsaddrs
[n
]);
472 res_setoptions (res_state statp
, const char *options
)
474 const char *cp
= options
;
478 /* Skip leading and inner runs of spaces. */
479 while (*cp
== ' ' || *cp
== '\t')
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
)
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
)
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
)
504 statp
->retry
= RES_MAXRETRY
;
513 unsigned long int flag
;
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
;
532 statp
->options
|= options
[i
].flag
;
536 /* Skip to next run of spaces. */
537 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
543 net_mask (struct in_addr in
)
545 uint32_t i
= ntohl (in
.s_addr
);
548 return htonl (IN_CLASSA_NET
);
549 else if (IN_CLASSB (i
))
550 return htonl (IN_CLASSB_NET
);
551 return htonl (IN_CLASSC_NET
);