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>
89 #include <stdio_ext.h>
94 #include <arpa/inet.h>
95 #include <arpa/nameser.h>
97 #include <netinet/in.h>
98 #include <sys/param.h>
99 #include <sys/socket.h>
100 #include <sys/time.h>
101 #include <sys/types.h>
102 #include <inet/net-internal.h>
105 static void res_setoptions (res_state
, const char *, const char *);
106 static uint32_t net_mask (struct in_addr
);
108 unsigned long long int __res_initstamp
;
111 res_ninit (res_state statp
)
113 return __res_vinit (statp
, 0);
115 libc_hidden_def (__res_ninit
)
117 /* Return true if CH separates the netmask in the "sortlist"
120 is_sort_mask (char ch
)
122 return ch
== '/' || ch
== '&';
125 /* Internal helper function for __res_vinit, to aid with resource
126 deallocation and error handling. Return true on success, false on
129 res_vinit_1 (res_state statp
, bool preinit
, FILE *fp
, char **buffer
)
132 size_t buffer_size
= 0;
133 int nserv
= 0; /* Number of nameservers read from file. */
134 bool have_serv6
= false;
135 bool haveenv
= false;
136 bool havesearch
= false;
139 statp
->_u
._ext
.initstamp
= __res_initstamp
;
143 statp
->retrans
= RES_TIMEOUT
;
144 statp
->retry
= RES_DFLRETRY
;
145 statp
->options
= RES_DEFAULT
;
146 statp
->id
= res_randomid ();
150 statp
->defdname
[0] = '\0';
155 statp
->__glibc_unused_qhook
= NULL
;
156 statp
->__glibc_unused_rhook
= NULL
;
157 statp
->_u
._ext
.nscount
= 0;
158 for (int n
= 0; n
< MAXNS
; n
++)
159 statp
->_u
._ext
.nsaddrs
[n
] = NULL
;
161 /* Allow user to override the local domain definition. */
162 if ((cp
= getenv ("LOCALDOMAIN")) != NULL
)
164 strncpy (statp
->defdname
, cp
, sizeof (statp
->defdname
) - 1);
165 statp
->defdname
[sizeof (statp
->defdname
) - 1] = '\0';
168 /* Set search list to be blank-separated strings from rest of
169 env value. Permits users of LOCALDOMAIN to still have a
170 search list, and anyone to set the one that they want to use
171 as an individual (even more important now that the rfc1535
172 stuff restricts searches). */
173 cp
= statp
->defdname
;
176 for (int n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++)
180 else if (*cp
== ' ' || *cp
== '\t')
192 /* Null terminate last domain if there are excess. */
193 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' && *cp
!= '\n')
199 #define MATCH(line, name) \
200 (!strncmp ((line), name, sizeof (name) - 1) \
201 && ((line)[sizeof (name) - 1] == ' ' \
202 || (line)[sizeof (name) - 1] == '\t'))
206 /* No threads use this stream. */
207 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
208 /* Read the config file. */
212 ssize_t ret
= __getline (buffer
, &buffer_size
, fp
);
215 if (_IO_ferror_unlocked (fp
))
223 if (**buffer
== ';' || **buffer
== '#')
225 /* Read default domain name. */
226 if (MATCH (*buffer
, "domain"))
229 /* LOCALDOMAIN overrides the configuration file. */
231 cp
= *buffer
+ sizeof ("domain") - 1;
232 while (*cp
== ' ' || *cp
== '\t')
234 if ((*cp
== '\0') || (*cp
== '\n'))
236 strncpy (statp
->defdname
, cp
, sizeof (statp
->defdname
) - 1);
237 statp
->defdname
[sizeof (statp
->defdname
) - 1] = '\0';
238 if ((cp
= strpbrk (statp
->defdname
, " \t\n")) != NULL
)
243 /* Set search list. */
244 if (MATCH (*buffer
, "search"))
247 /* LOCALDOMAIN overrides the configuration file. */
249 cp
= *buffer
+ sizeof ("search") - 1;
250 while (*cp
== ' ' || *cp
== '\t')
252 if ((*cp
== '\0') || (*cp
== '\n'))
254 strncpy (statp
->defdname
, cp
, sizeof (statp
->defdname
) - 1);
255 statp
->defdname
[sizeof (statp
->defdname
) - 1] = '\0';
256 if ((cp
= strchr (statp
->defdname
, '\n')) != NULL
)
258 /* Set search list to be blank-separated strings on rest
260 cp
= statp
->defdname
;
263 for (int n
= 0; *cp
&& pp
< statp
->dnsrch
+ MAXDNSRCH
; cp
++)
265 if (*cp
== ' ' || *cp
== '\t')
276 /* Null terminate last domain if there are excess. */
277 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
284 /* Read nameservers to query. */
285 if (MATCH (*buffer
, "nameserver") && nserv
< MAXNS
)
289 cp
= *buffer
+ sizeof ("nameserver") - 1;
290 while (*cp
== ' ' || *cp
== '\t')
292 if ((*cp
!= '\0') && (*cp
!= '\n') && __inet_aton (cp
, &a
))
294 statp
->nsaddr_list
[nserv
].sin_addr
= a
;
295 statp
->nsaddr_list
[nserv
].sin_family
= AF_INET
;
296 statp
->nsaddr_list
[nserv
].sin_port
= htons (NAMESERVER_PORT
);
304 if ((el
= strpbrk (cp
, " \t\n")) != NULL
)
306 if ((el
= strchr (cp
, SCOPE_DELIMITER
)) != NULL
)
308 if ((*cp
!= '\0') && (__inet_pton (AF_INET6
, cp
, &a6
) > 0))
310 struct sockaddr_in6
*sa6
;
312 sa6
= malloc (sizeof (*sa6
));
316 sa6
->sin6_family
= AF_INET6
;
317 sa6
->sin6_port
= htons (NAMESERVER_PORT
);
318 sa6
->sin6_flowinfo
= 0;
321 sa6
->sin6_scope_id
= 0;
322 if (__glibc_likely (el
!= NULL
))
323 /* Ignore errors, for backwards
326 (&a6
, el
+ 1, &sa6
->sin6_scope_id
);
328 statp
->nsaddr_list
[nserv
].sin_family
= 0;
329 statp
->_u
._ext
.nsaddrs
[nserv
] = sa6
;
330 statp
->_u
._ext
.nssocks
[nserv
] = -1;
337 if (MATCH (*buffer
, "sortlist"))
341 cp
= *buffer
+ sizeof ("sortlist") - 1;
342 while (nsort
< MAXRESOLVSORT
)
344 while (*cp
== ' ' || *cp
== '\t')
346 if (*cp
== '\0' || *cp
== '\n' || *cp
== ';')
349 while (*cp
&& !is_sort_mask (*cp
) && *cp
!= ';'
350 && isascii (*cp
) && !isspace (*cp
))
352 char separator
= *cp
;
354 if (__inet_aton (net
, &a
))
356 statp
->sort_list
[nsort
].addr
= a
;
357 if (is_sort_mask (separator
))
361 while (*cp
&& *cp
!= ';'
362 && isascii (*cp
) && !isspace (*cp
))
366 if (__inet_aton (net
, &a
))
367 statp
->sort_list
[nsort
].mask
= a
.s_addr
;
369 statp
->sort_list
[nsort
].mask
370 = net_mask (statp
->sort_list
[nsort
].addr
);
373 statp
->sort_list
[nsort
].mask
374 = net_mask (statp
->sort_list
[nsort
].addr
);
381 if (MATCH (*buffer
, "options"))
383 res_setoptions (statp
, *buffer
+ sizeof ("options") - 1, "conf");
387 statp
->nscount
= nserv
;
389 /* We try IPv6 servers again. */
390 statp
->ipv6_unavail
= false;
391 statp
->nsort
= nsort
;
394 if (__glibc_unlikely (statp
->nscount
== 0))
396 statp
->nsaddr
.sin_addr
= __inet_makeaddr (IN_LOOPBACKNET
, 1);
397 statp
->nsaddr
.sin_family
= AF_INET
;
398 statp
->nsaddr
.sin_port
= htons (NAMESERVER_PORT
);
401 if (statp
->defdname
[0] == 0)
403 char buf
[sizeof (statp
->defdname
)];
404 if (__gethostname (buf
, sizeof (statp
->defdname
) - 1) == 0
405 && (cp
= strchr (buf
, '.')) != NULL
)
406 strcpy (statp
->defdname
, cp
+ 1);
409 /* Find components of local domain that might be searched. */
413 *pp
++ = statp
->defdname
;
418 if ((cp
= getenv ("RES_OPTIONS")) != NULL
)
419 res_setoptions (statp
, cp
, "env");
420 statp
->options
|= RES_INIT
;
424 /* Set up default settings. If the /etc/resolv.conf configuration
425 file exist, the values there will have precedence. Otherwise, the
426 server address is set to INADDR_LOOPBACK and the default domain
427 name comes from gethostname. The RES_OPTIONS and LOCALDOMAIN
428 environment variables can be used to override some settings.
429 Return 0 if completes successfully, -1 on error. */
431 __res_vinit (res_state statp
, int preinit
)
433 FILE *fp
= fopen (_PATH_RESCONF
, "rce");
443 /* Ignore these errors. They are persistent errors caused
444 by file system contents. */
447 /* Other errors refer to resource allocation problems and
448 need to be handled by the application. */
453 bool ok
= res_vinit_1 (statp
, preinit
, fp
, &buffer
);
458 /* Deallocate the name server addresses which have been
460 for (int n
= 0; n
< MAXNS
; n
++)
461 free (statp
->_u
._ext
.nsaddrs
[n
]);
468 res_setoptions (res_state statp
, const char *options
, const char *source
)
470 const char *cp
= options
;
474 /* Skip leading and inner runs of spaces. */
475 while (*cp
== ' ' || *cp
== '\t')
477 /* Search for and process individual options. */
478 if (!strncmp (cp
, "ndots:", sizeof ("ndots:") - 1))
480 int i
= atoi (cp
+ sizeof ("ndots:") - 1);
481 if (i
<= RES_MAXNDOTS
)
484 statp
->ndots
= RES_MAXNDOTS
;
486 else if (!strncmp (cp
, "timeout:", sizeof ("timeout:") - 1))
488 int i
= atoi (cp
+ sizeof ("timeout:") - 1);
489 if (i
<= RES_MAXRETRANS
)
492 statp
->retrans
= RES_MAXRETRANS
;
494 else if (!strncmp (cp
, "attempts:", sizeof ("attempts:") - 1))
496 int i
= atoi (cp
+ sizeof ("attempts:") - 1);
497 if (i
<= RES_MAXRETRY
)
500 statp
->retry
= RES_MAXRETRY
;
509 unsigned long int flag
;
511 #define STRnLEN(str) str, sizeof (str) - 1
512 { STRnLEN ("inet6"), 0, DEPRECATED_RES_USE_INET6
},
513 { STRnLEN ("rotate"), 0, RES_ROTATE
},
514 { STRnLEN ("edns0"), 0, RES_USE_EDNS0
},
515 { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP
},
516 { STRnLEN ("single-request"), 0, RES_SNGLKUP
},
517 { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY
},
518 { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY
},
519 { STRnLEN ("use-vc"), 0, RES_USEVC
}
521 #define noptions (sizeof (options) / sizeof (options[0]))
522 for (int i
= 0; i
< noptions
; ++i
)
523 if (strncmp (cp
, options
[i
].str
, options
[i
].len
) == 0)
525 if (options
[i
].clear
)
526 statp
->options
&= options
[i
].flag
;
528 statp
->options
|= options
[i
].flag
;
532 /* Skip to next run of spaces. */
533 while (*cp
&& *cp
!= ' ' && *cp
!= '\t')
539 net_mask (struct in_addr in
)
541 uint32_t i
= ntohl (in
.s_addr
);
544 return htonl (IN_CLASSA_NET
);
545 else if (IN_CLASSB (i
))
546 return htonl (IN_CLASSB_NET
);
547 return htonl (IN_CLASSC_NET
);