]>
Commit | Line | Data |
---|---|---|
4c4480ee | 1 | /* Resolver state initialization and resolv.conf parsing. |
2b778ceb | 2 | Copyright (C) 1995-2021 Free Software Foundation, Inc. |
4c4480ee FW |
3 | This file is part of the GNU C Library. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
4c4480ee | 18 | |
28f540f4 | 19 | /* |
28f540f4 RM |
20 | * Copyright (c) 1985, 1989, 1993 |
21 | * The Regents of the University of California. All rights reserved. | |
4aebaa6b | 22 | * |
28f540f4 RM |
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. | |
28f540f4 RM |
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. | |
4aebaa6b | 34 | * |
28f540f4 RM |
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. | |
b43b13ac UD |
46 | */ |
47 | ||
48 | /* | |
28f540f4 | 49 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
4aebaa6b | 50 | * |
28f540f4 RM |
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. | |
4aebaa6b | 57 | * |
28f540f4 RM |
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. | |
b43b13ac UD |
66 | */ |
67 | ||
68 | /* | |
69 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. | |
70 | * | |
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. | |
74 | * | |
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. | |
28f540f4 RM |
83 | */ |
84 | ||
28f540f4 | 85 | #include <ctype.h> |
a7eba295 | 86 | #include <netdb.h> |
b76e0659 | 87 | #include <resolv/resolv-internal.h> |
e6b4e2de | 88 | #include <res_hconf.h> |
b43b13ac | 89 | #include <stdio.h> |
2706ee38 | 90 | #include <stdio_ext.h> |
b43b13ac UD |
91 | #include <stdlib.h> |
92 | #include <string.h> | |
93 | #include <unistd.h> | |
e054f494 | 94 | #include <stdint.h> |
a7eba295 UD |
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> | |
80d8cb91 | 103 | #include <inet/net-internal.h> |
ea4924ce | 104 | #include <errno.h> |
f30a54b2 | 105 | #include <resolv_conf.h> |
dd0b4df3 | 106 | #include <file_change_detection.h> |
28f540f4 | 107 | |
4c4480ee | 108 | static uint32_t net_mask (struct in_addr); |
28f540f4 | 109 | |
3d61b63c | 110 | int |
4c4480ee FW |
111 | res_ninit (res_state statp) |
112 | { | |
113 | return __res_vinit (statp, 0); | |
b43b13ac | 114 | } |
37ba7d66 | 115 | libc_hidden_def (__res_ninit) |
b43b13ac | 116 | |
94f094f2 FW |
117 | /* Return true if CH separates the netmask in the "sortlist" |
118 | directive. */ | |
119 | static inline bool | |
120 | is_sort_mask (char ch) | |
121 | { | |
122 | return ch == '/' || ch == '&'; | |
123 | } | |
124 | ||
a1c4eb87 FW |
125 | /* Array of name server addresses. */ |
126 | #define DYNARRAY_STRUCT nameserver_list | |
127 | #define DYNARRAY_ELEMENT const struct sockaddr * | |
128 | #define DYNARRAY_ELEMENT_FREE(e) free ((struct sockaddr *) *(e)) | |
129 | #define DYNARRAY_INITIAL_SIZE 3 | |
130 | #define DYNARRAY_PREFIX nameserver_list_ | |
131 | #include <malloc/dynarray-skeleton.c> | |
132 | ||
3f853f22 FW |
133 | /* Array of strings for the search array. The backing store is |
134 | managed separately. */ | |
135 | #define DYNARRAY_STRUCT search_list | |
136 | #define DYNARRAY_ELEMENT const char * | |
a1c4eb87 | 137 | #define DYNARRAY_INITIAL_SIZE 6 |
3f853f22 FW |
138 | #define DYNARRAY_PREFIX search_list_ |
139 | #include <malloc/dynarray-skeleton.c> | |
140 | ||
a1c4eb87 FW |
141 | /* Array of name server addresses. */ |
142 | #define DYNARRAY_STRUCT sort_list | |
143 | #define DYNARRAY_ELEMENT struct resolv_sortlist_entry | |
144 | #define DYNARRAY_INITIAL_SIZE 0 | |
145 | #define DYNARRAY_PREFIX sort_list_ | |
146 | #include <malloc/dynarray-skeleton.c> | |
147 | ||
3f853f22 FW |
148 | /* resolv.conf parser state and results. */ |
149 | struct resolv_conf_parser | |
150 | { | |
151 | char *buffer; /* Temporary buffer for reading lines. */ | |
a1c4eb87 FW |
152 | |
153 | struct nameserver_list nameserver_list; /* Nameserver addresses. */ | |
154 | ||
3f853f22 FW |
155 | char *search_list_store; /* Backing storage for search list entries. */ |
156 | struct search_list search_list; /* Points into search_list_store. */ | |
a1c4eb87 FW |
157 | |
158 | struct sort_list sort_list; /* Address preference sorting list. */ | |
159 | ||
160 | /* Configuration template. The non-array elements are filled in | |
161 | directly. The array elements are updated prior to the call to | |
162 | __resolv_conf_attach. */ | |
163 | struct resolv_conf template; | |
3f853f22 FW |
164 | }; |
165 | ||
aef16cc8 FW |
166 | /* Return true if *PREINIT contains actual preinitialization. */ |
167 | static bool | |
168 | has_preinit_values (const struct __res_state *preinit) | |
169 | { | |
170 | return (preinit->retrans != 0 && preinit->retrans != RES_TIMEOUT) | |
171 | || (preinit->retry != 0 && preinit->retry != RES_DFLRETRY) | |
172 | || (preinit->options != 0 | |
173 | && (preinit->options & ~RES_INIT) != RES_DEFAULT); | |
174 | } | |
175 | ||
3f853f22 | 176 | static void |
a1c4eb87 FW |
177 | resolv_conf_parser_init (struct resolv_conf_parser *parser, |
178 | const struct __res_state *preinit) | |
3f853f22 FW |
179 | { |
180 | parser->buffer = NULL; | |
181 | parser->search_list_store = NULL; | |
a1c4eb87 | 182 | nameserver_list_init (&parser->nameserver_list); |
3f853f22 | 183 | search_list_init (&parser->search_list); |
a1c4eb87 FW |
184 | sort_list_init (&parser->sort_list); |
185 | ||
186 | if (preinit != NULL) | |
187 | { | |
188 | parser->template.retrans = preinit->retrans; | |
189 | parser->template.retry = preinit->retry; | |
190 | parser->template.options = preinit->options | RES_INIT; | |
191 | } | |
192 | else | |
193 | { | |
194 | parser->template.retrans = RES_TIMEOUT; | |
195 | parser->template.retry = RES_DFLRETRY; | |
196 | parser->template.options = RES_DEFAULT | RES_INIT; | |
197 | } | |
198 | parser->template.ndots = 1; | |
3f853f22 FW |
199 | } |
200 | ||
201 | static void | |
202 | resolv_conf_parser_free (struct resolv_conf_parser *parser) | |
203 | { | |
204 | free (parser->buffer); | |
205 | free (parser->search_list_store); | |
a1c4eb87 | 206 | nameserver_list_free (&parser->nameserver_list); |
3f853f22 | 207 | search_list_free (&parser->search_list); |
a1c4eb87 FW |
208 | sort_list_free (&parser->sort_list); |
209 | } | |
210 | ||
211 | /* Allocate a struct sockaddr_in object on the heap, with the | |
212 | specified address and port. */ | |
213 | static struct sockaddr * | |
214 | allocate_address_v4 (struct in_addr a, uint16_t port) | |
215 | { | |
216 | struct sockaddr_in *sa4 = malloc (sizeof (*sa4)); | |
217 | if (sa4 == NULL) | |
218 | return NULL; | |
219 | sa4->sin_family = AF_INET; | |
220 | sa4->sin_addr = a; | |
221 | sa4->sin_port = htons (port); | |
222 | return (struct sockaddr *) sa4; | |
3f853f22 FW |
223 | } |
224 | ||
225 | /* Try to obtain the domain name from the host name and store it in | |
226 | *RESULT. Return false on memory allocation failure. If the domain | |
227 | name cannot be determined for any other reason, write NULL to | |
228 | *RESULT and return true. */ | |
229 | static bool | |
230 | domain_from_hostname (char **result) | |
231 | { | |
232 | char buf[256]; | |
233 | /* gethostbyname may not terminate the buffer. */ | |
234 | buf[sizeof (buf) - 1] = '\0'; | |
235 | if (__gethostname (buf, sizeof (buf) - 1) == 0) | |
236 | { | |
237 | char *dot = strchr (buf, '.'); | |
238 | if (dot != NULL) | |
239 | { | |
240 | *result = __strdup (dot + 1); | |
241 | if (*result == NULL) | |
242 | return false; | |
243 | return true; | |
244 | } | |
245 | } | |
246 | *result = NULL; | |
247 | return true; | |
248 | } | |
249 | ||
a1c4eb87 FW |
250 | static void res_setoptions (struct resolv_conf_parser *, const char *options); |
251 | ||
ea4924ce FW |
252 | /* Internal helper function for __res_vinit, to aid with resource |
253 | deallocation and error handling. Return true on success, false on | |
254 | failure. */ | |
255 | static bool | |
a1c4eb87 | 256 | res_vinit_1 (FILE *fp, struct resolv_conf_parser *parser) |
4c4480ee | 257 | { |
3f853f22 | 258 | char *cp; |
89f187a4 | 259 | size_t buffer_size = 0; |
4c4480ee | 260 | bool haveenv = false; |
28f540f4 | 261 | |
4c4480ee FW |
262 | /* Allow user to override the local domain definition. */ |
263 | if ((cp = getenv ("LOCALDOMAIN")) != NULL) | |
264 | { | |
3f853f22 FW |
265 | /* The code below splits the string in place. */ |
266 | cp = __strdup (cp); | |
267 | if (cp == NULL) | |
268 | return false; | |
269 | free (parser->search_list_store); | |
270 | parser->search_list_store = cp; | |
4c4480ee | 271 | haveenv = true; |
28f540f4 | 272 | |
3f853f22 FW |
273 | /* The string will be truncated as needed below. */ |
274 | search_list_add (&parser->search_list, cp); | |
275 | ||
4c4480ee FW |
276 | /* Set search list to be blank-separated strings from rest of |
277 | env value. Permits users of LOCALDOMAIN to still have a | |
278 | search list, and anyone to set the one that they want to use | |
279 | as an individual (even more important now that the rfc1535 | |
280 | stuff restricts searches). */ | |
3f853f22 | 281 | for (bool in_name = true; *cp != '\0'; cp++) |
4c4480ee FW |
282 | { |
283 | if (*cp == '\n') | |
3f853f22 FW |
284 | { |
285 | *cp = '\0'; | |
286 | break; | |
287 | } | |
4c4480ee FW |
288 | else if (*cp == ' ' || *cp == '\t') |
289 | { | |
3f853f22 FW |
290 | *cp = '\0'; |
291 | in_name = false; | |
4c4480ee | 292 | } |
3f853f22 | 293 | else if (!in_name) |
4c4480ee | 294 | { |
3f853f22 FW |
295 | search_list_add (&parser->search_list, cp); |
296 | in_name = true; | |
4c4480ee FW |
297 | } |
298 | } | |
4c4480ee | 299 | } |
28f540f4 | 300 | |
4c4480ee | 301 | #define MATCH(line, name) \ |
89f187a4 FW |
302 | (!strncmp ((line), name, sizeof (name) - 1) \ |
303 | && ((line)[sizeof (name) - 1] == ' ' \ | |
304 | || (line)[sizeof (name) - 1] == '\t')) | |
28f540f4 | 305 | |
4c4480ee FW |
306 | if (fp != NULL) |
307 | { | |
308 | /* No threads use this stream. */ | |
309 | __fsetlocking (fp, FSETLOCKING_BYCALLER); | |
310 | /* Read the config file. */ | |
89f187a4 | 311 | while (true) |
4c4480ee | 312 | { |
89f187a4 | 313 | { |
3f853f22 | 314 | ssize_t ret = __getline (&parser->buffer, &buffer_size, fp); |
89f187a4 FW |
315 | if (ret <= 0) |
316 | { | |
317 | if (_IO_ferror_unlocked (fp)) | |
318 | return false; | |
319 | else | |
320 | break; | |
321 | } | |
322 | } | |
323 | ||
4c4480ee | 324 | /* Skip comments. */ |
3f853f22 | 325 | if (*parser->buffer == ';' || *parser->buffer == '#') |
4c4480ee FW |
326 | continue; |
327 | /* Read default domain name. */ | |
3f853f22 | 328 | if (MATCH (parser->buffer, "domain")) |
4c4480ee FW |
329 | { |
330 | if (haveenv) | |
331 | /* LOCALDOMAIN overrides the configuration file. */ | |
332 | continue; | |
3f853f22 | 333 | cp = parser->buffer + sizeof ("domain") - 1; |
4c4480ee FW |
334 | while (*cp == ' ' || *cp == '\t') |
335 | cp++; | |
336 | if ((*cp == '\0') || (*cp == '\n')) | |
337 | continue; | |
3f853f22 FW |
338 | |
339 | cp = __strdup (cp); | |
340 | if (cp == NULL) | |
341 | return false; | |
342 | free (parser->search_list_store); | |
343 | parser->search_list_store = cp; | |
344 | search_list_clear (&parser->search_list); | |
345 | search_list_add (&parser->search_list, cp); | |
346 | /* Replace trailing whitespace. */ | |
347 | if ((cp = strpbrk (cp, " \t\n")) != NULL) | |
4c4480ee | 348 | *cp = '\0'; |
4c4480ee FW |
349 | continue; |
350 | } | |
351 | /* Set search list. */ | |
3f853f22 | 352 | if (MATCH (parser->buffer, "search")) |
4c4480ee FW |
353 | { |
354 | if (haveenv) | |
355 | /* LOCALDOMAIN overrides the configuration file. */ | |
356 | continue; | |
3f853f22 | 357 | cp = parser->buffer + sizeof ("search") - 1; |
4c4480ee FW |
358 | while (*cp == ' ' || *cp == '\t') |
359 | cp++; | |
360 | if ((*cp == '\0') || (*cp == '\n')) | |
361 | continue; | |
3f853f22 FW |
362 | |
363 | { | |
364 | char *p = strchr (cp, '\n'); | |
365 | if (p != NULL) | |
366 | *p = '\0'; | |
367 | } | |
368 | cp = __strdup (cp); | |
369 | if (cp == NULL) | |
370 | return false; | |
371 | free (parser->search_list_store); | |
372 | parser->search_list_store = cp; | |
373 | ||
374 | /* The string is truncated below. */ | |
375 | search_list_clear (&parser->search_list); | |
376 | search_list_add (&parser->search_list, cp); | |
377 | ||
4c4480ee FW |
378 | /* Set search list to be blank-separated strings on rest |
379 | of line. */ | |
3f853f22 | 380 | for (bool in_name = true; *cp != '\0'; cp++) |
4c4480ee FW |
381 | { |
382 | if (*cp == ' ' || *cp == '\t') | |
383 | { | |
3f853f22 FW |
384 | *cp = '\0'; |
385 | in_name = false; | |
4c4480ee | 386 | } |
3f853f22 | 387 | else if (!in_name) |
4c4480ee | 388 | { |
3f853f22 FW |
389 | search_list_add (&parser->search_list, cp); |
390 | in_name = true; | |
4c4480ee FW |
391 | } |
392 | } | |
4c4480ee FW |
393 | continue; |
394 | } | |
395 | /* Read nameservers to query. */ | |
a1c4eb87 | 396 | if (MATCH (parser->buffer, "nameserver")) |
4c4480ee FW |
397 | { |
398 | struct in_addr a; | |
28f540f4 | 399 | |
3f853f22 | 400 | cp = parser->buffer + sizeof ("nameserver") - 1; |
4c4480ee FW |
401 | while (*cp == ' ' || *cp == '\t') |
402 | cp++; | |
108bc404 FW |
403 | |
404 | /* Ignore trailing contents on the name server line. */ | |
405 | { | |
406 | char *el; | |
407 | if ((el = strpbrk (cp, " \t\n")) != NULL) | |
408 | *el = '\0'; | |
409 | } | |
410 | ||
a1c4eb87 | 411 | struct sockaddr *sa; |
108bc404 | 412 | if ((*cp != '\0') && (*cp != '\n') && __inet_aton_exact (cp, &a)) |
4c4480ee | 413 | { |
a1c4eb87 FW |
414 | sa = allocate_address_v4 (a, NAMESERVER_PORT); |
415 | if (sa == NULL) | |
416 | return false; | |
4c4480ee FW |
417 | } |
418 | else | |
419 | { | |
420 | struct in6_addr a6; | |
421 | char *el; | |
4c4480ee FW |
422 | if ((el = strchr (cp, SCOPE_DELIMITER)) != NULL) |
423 | *el = '\0'; | |
424 | if ((*cp != '\0') && (__inet_pton (AF_INET6, cp, &a6) > 0)) | |
425 | { | |
426 | struct sockaddr_in6 *sa6; | |
df24a73e | 427 | |
4c4480ee | 428 | sa6 = malloc (sizeof (*sa6)); |
ea4924ce | 429 | if (sa6 == NULL) |
89f187a4 | 430 | return false; |
a7eba295 | 431 | |
ea4924ce FW |
432 | sa6->sin6_family = AF_INET6; |
433 | sa6->sin6_port = htons (NAMESERVER_PORT); | |
434 | sa6->sin6_flowinfo = 0; | |
435 | sa6->sin6_addr = a6; | |
a7eba295 | 436 | |
ea4924ce FW |
437 | sa6->sin6_scope_id = 0; |
438 | if (__glibc_likely (el != NULL)) | |
439 | /* Ignore errors, for backwards | |
440 | compatibility. */ | |
441 | __inet6_scopeid_pton | |
442 | (&a6, el + 1, &sa6->sin6_scope_id); | |
a1c4eb87 | 443 | sa = (struct sockaddr *) sa6; |
4c4480ee | 444 | } |
a1c4eb87 FW |
445 | else |
446 | /* IPv6 address parse failure. */ | |
447 | sa = NULL; | |
448 | } | |
449 | if (sa != NULL) | |
450 | { | |
451 | const struct sockaddr **p = nameserver_list_emplace | |
452 | (&parser->nameserver_list); | |
453 | if (p != NULL) | |
454 | *p = sa; | |
5670c4ab FW |
455 | else |
456 | { | |
457 | free (sa); | |
458 | return false; | |
459 | } | |
4c4480ee FW |
460 | } |
461 | continue; | |
462 | } | |
3f853f22 | 463 | if (MATCH (parser->buffer, "sortlist")) |
4c4480ee FW |
464 | { |
465 | struct in_addr a; | |
28f540f4 | 466 | |
3f853f22 | 467 | cp = parser->buffer + sizeof ("sortlist") - 1; |
a1c4eb87 | 468 | while (true) |
4c4480ee FW |
469 | { |
470 | while (*cp == ' ' || *cp == '\t') | |
471 | cp++; | |
472 | if (*cp == '\0' || *cp == '\n' || *cp == ';') | |
473 | break; | |
a1c4eb87 | 474 | char *net = cp; |
4c4480ee FW |
475 | while (*cp && !is_sort_mask (*cp) && *cp != ';' |
476 | && isascii (*cp) && !isspace (*cp)) | |
477 | cp++; | |
478 | char separator = *cp; | |
479 | *cp = 0; | |
a1c4eb87 | 480 | struct resolv_sortlist_entry e; |
108bc404 | 481 | if (__inet_aton_exact (net, &a)) |
4c4480ee | 482 | { |
a1c4eb87 | 483 | e.addr = a; |
4c4480ee FW |
484 | if (is_sort_mask (separator)) |
485 | { | |
486 | *cp++ = separator; | |
487 | net = cp; | |
488 | while (*cp && *cp != ';' | |
489 | && isascii (*cp) && !isspace (*cp)) | |
490 | cp++; | |
491 | separator = *cp; | |
492 | *cp = 0; | |
108bc404 | 493 | if (__inet_aton_exact (net, &a)) |
a1c4eb87 | 494 | e.mask = a.s_addr; |
4c4480ee | 495 | else |
a1c4eb87 | 496 | e.mask = net_mask (e.addr); |
4c4480ee FW |
497 | } |
498 | else | |
a1c4eb87 FW |
499 | e.mask = net_mask (e.addr); |
500 | sort_list_add (&parser->sort_list, e); | |
4c4480ee FW |
501 | } |
502 | *cp = separator; | |
503 | } | |
504 | continue; | |
505 | } | |
3f853f22 | 506 | if (MATCH (parser->buffer, "options")) |
4c4480ee | 507 | { |
a1c4eb87 | 508 | res_setoptions (parser, parser->buffer + sizeof ("options") - 1); |
4c4480ee FW |
509 | continue; |
510 | } | |
511 | } | |
4c4480ee | 512 | } |
a1c4eb87 | 513 | if (__glibc_unlikely (nameserver_list_size (&parser->nameserver_list) == 0)) |
4c4480ee | 514 | { |
a1c4eb87 FW |
515 | const struct sockaddr **p |
516 | = nameserver_list_emplace (&parser->nameserver_list); | |
517 | if (p == NULL) | |
518 | return false; | |
519 | *p = allocate_address_v4 (__inet_makeaddr (IN_LOOPBACKNET, 1), | |
520 | NAMESERVER_PORT); | |
521 | if (*p == NULL) | |
522 | return false; | |
4c4480ee | 523 | } |
28f540f4 | 524 | |
3f853f22 | 525 | if (search_list_size (&parser->search_list) == 0) |
4c4480ee | 526 | { |
3f853f22 FW |
527 | char *domain; |
528 | if (!domain_from_hostname (&domain)) | |
529 | return false; | |
530 | if (domain != NULL) | |
531 | { | |
532 | free (parser->search_list_store); | |
533 | parser->search_list_store = domain; | |
534 | search_list_add (&parser->search_list, domain); | |
535 | } | |
4c4480ee | 536 | } |
28f540f4 | 537 | |
4c4480ee | 538 | if ((cp = getenv ("RES_OPTIONS")) != NULL) |
a1c4eb87 | 539 | res_setoptions (parser, cp); |
3f853f22 | 540 | |
a1c4eb87 FW |
541 | if (nameserver_list_has_failed (&parser->nameserver_list) |
542 | || search_list_has_failed (&parser->search_list) | |
543 | || sort_list_has_failed (&parser->sort_list)) | |
3f853f22 FW |
544 | { |
545 | __set_errno (ENOMEM); | |
546 | return false; | |
547 | } | |
548 | ||
89f187a4 | 549 | return true; |
28f540f4 RM |
550 | } |
551 | ||
aef16cc8 | 552 | struct resolv_conf * |
dd0b4df3 FW |
553 | __resolv_conf_load (struct __res_state *preinit, |
554 | struct file_change_detection *change) | |
ea4924ce | 555 | { |
e6b4e2de FW |
556 | /* Ensure that /etc/hosts.conf has been loaded (once). */ |
557 | _res_hconf_init (); | |
558 | ||
ea4924ce FW |
559 | FILE *fp = fopen (_PATH_RESCONF, "rce"); |
560 | if (fp == NULL) | |
561 | switch (errno) | |
562 | { | |
563 | case EACCES: | |
564 | case EISDIR: | |
565 | case ELOOP: | |
566 | case ENOENT: | |
567 | case ENOTDIR: | |
568 | case EPERM: | |
569 | /* Ignore these errors. They are persistent errors caused | |
570 | by file system contents. */ | |
571 | break; | |
572 | default: | |
573 | /* Other errors refer to resource allocation problems and | |
574 | need to be handled by the application. */ | |
aef16cc8 | 575 | return NULL; |
ea4924ce | 576 | } |
89f187a4 | 577 | |
3f853f22 | 578 | struct resolv_conf_parser parser; |
aef16cc8 | 579 | resolv_conf_parser_init (&parser, preinit); |
89f187a4 | 580 | |
aef16cc8 | 581 | struct resolv_conf *conf = NULL; |
dd0b4df3 FW |
582 | bool ok = res_vinit_1 (fp, &parser); |
583 | if (ok && change != NULL) | |
584 | /* Update the file change information if the configuration was | |
585 | loaded successfully. */ | |
631cf64b | 586 | ok = __file_change_detection_for_fp (change, fp); |
dd0b4df3 FW |
587 | |
588 | if (ok) | |
f30a54b2 | 589 | { |
a1c4eb87 FW |
590 | parser.template.nameserver_list |
591 | = nameserver_list_begin (&parser.nameserver_list); | |
592 | parser.template.nameserver_list_size | |
593 | = nameserver_list_size (&parser.nameserver_list); | |
594 | parser.template.search_list = search_list_begin (&parser.search_list); | |
595 | parser.template.search_list_size | |
596 | = search_list_size (&parser.search_list); | |
597 | parser.template.sort_list = sort_list_begin (&parser.sort_list); | |
598 | parser.template.sort_list_size = sort_list_size (&parser.sort_list); | |
aef16cc8 | 599 | conf = __resolv_conf_allocate (&parser.template); |
f30a54b2 | 600 | } |
3f853f22 | 601 | resolv_conf_parser_free (&parser); |
f30a54b2 | 602 | |
a1a20f02 FW |
603 | if (fp != NULL) |
604 | { | |
605 | int saved_errno = errno; | |
606 | fclose (fp); | |
607 | __set_errno (saved_errno); | |
608 | } | |
609 | ||
aef16cc8 FW |
610 | return conf; |
611 | } | |
612 | ||
613 | /* Set up default settings. If the /etc/resolv.conf configuration | |
614 | file exist, the values there will have precedence. Otherwise, the | |
615 | server address is set to INADDR_LOOPBACK and the default domain | |
616 | name comes from gethostname. The RES_OPTIONS and LOCALDOMAIN | |
617 | environment variables can be used to override some settings. | |
618 | Return 0 if completes successfully, -1 on error. */ | |
619 | int | |
620 | __res_vinit (res_state statp, int preinit) | |
621 | { | |
622 | struct resolv_conf *conf; | |
623 | if (preinit && has_preinit_values (statp)) | |
624 | /* For the preinit case, we cannot use the cached configuration | |
625 | because some settings could be different. */ | |
dd0b4df3 | 626 | conf = __resolv_conf_load (statp, NULL); |
aef16cc8 FW |
627 | else |
628 | conf = __resolv_conf_get_current (); | |
629 | if (conf == NULL) | |
a1c4eb87 | 630 | return -1; |
aef16cc8 FW |
631 | |
632 | bool ok = __resolv_conf_attach (statp, conf); | |
633 | __resolv_conf_put (conf); | |
634 | if (ok) | |
635 | { | |
636 | if (preinit) | |
637 | statp->id = res_randomid (); | |
638 | return 0; | |
639 | } | |
a1c4eb87 | 640 | else |
aef16cc8 | 641 | return -1; |
ea4924ce FW |
642 | } |
643 | ||
28f540f4 | 644 | static void |
a1c4eb87 | 645 | res_setoptions (struct resolv_conf_parser *parser, const char *options) |
4c4480ee FW |
646 | { |
647 | const char *cp = options; | |
28f540f4 | 648 | |
4c4480ee FW |
649 | while (*cp) |
650 | { | |
651 | /* Skip leading and inner runs of spaces. */ | |
652 | while (*cp == ' ' || *cp == '\t') | |
653 | cp++; | |
654 | /* Search for and process individual options. */ | |
655 | if (!strncmp (cp, "ndots:", sizeof ("ndots:") - 1)) | |
656 | { | |
657 | int i = atoi (cp + sizeof ("ndots:") - 1); | |
658 | if (i <= RES_MAXNDOTS) | |
a1c4eb87 | 659 | parser->template.ndots = i; |
4c4480ee | 660 | else |
a1c4eb87 | 661 | parser->template.ndots = RES_MAXNDOTS; |
4c4480ee FW |
662 | } |
663 | else if (!strncmp (cp, "timeout:", sizeof ("timeout:") - 1)) | |
664 | { | |
665 | int i = atoi (cp + sizeof ("timeout:") - 1); | |
666 | if (i <= RES_MAXRETRANS) | |
a1c4eb87 | 667 | parser->template.retrans = i; |
4c4480ee | 668 | else |
a1c4eb87 | 669 | parser->template.retrans = RES_MAXRETRANS; |
4c4480ee FW |
670 | } |
671 | else if (!strncmp (cp, "attempts:", sizeof ("attempts:") - 1)) | |
672 | { | |
673 | int i = atoi (cp + sizeof ("attempts:") - 1); | |
674 | if (i <= RES_MAXRETRY) | |
a1c4eb87 | 675 | parser->template.retry = i; |
4c4480ee | 676 | else |
a1c4eb87 | 677 | parser->template.retry = RES_MAXRETRY; |
4c4480ee FW |
678 | } |
679 | else | |
680 | { | |
681 | static const struct | |
682 | { | |
683 | char str[22]; | |
684 | uint8_t len; | |
685 | uint8_t clear; | |
686 | unsigned long int flag; | |
687 | } options[] = { | |
652ffab1 | 688 | #define STRnLEN(str) str, sizeof (str) - 1 |
4c4480ee FW |
689 | { STRnLEN ("rotate"), 0, RES_ROTATE }, |
690 | { STRnLEN ("edns0"), 0, RES_USE_EDNS0 }, | |
691 | { STRnLEN ("single-request-reopen"), 0, RES_SNGLKUPREOP }, | |
692 | { STRnLEN ("single-request"), 0, RES_SNGLKUP }, | |
693 | { STRnLEN ("no_tld_query"), 0, RES_NOTLDQUERY }, | |
694 | { STRnLEN ("no-tld-query"), 0, RES_NOTLDQUERY }, | |
aef16cc8 | 695 | { STRnLEN ("no-reload"), 0, RES_NORELOAD }, |
446997ff FW |
696 | { STRnLEN ("use-vc"), 0, RES_USEVC }, |
697 | { STRnLEN ("trust-ad"), 0, RES_TRUSTAD }, | |
4c4480ee | 698 | }; |
652ffab1 | 699 | #define noptions (sizeof (options) / sizeof (options[0])) |
4c4480ee FW |
700 | for (int i = 0; i < noptions; ++i) |
701 | if (strncmp (cp, options[i].str, options[i].len) == 0) | |
702 | { | |
703 | if (options[i].clear) | |
a1c4eb87 | 704 | parser->template.options &= options[i].flag; |
4c4480ee | 705 | else |
a1c4eb87 | 706 | parser->template.options |= options[i].flag; |
4c4480ee FW |
707 | break; |
708 | } | |
709 | } | |
710 | /* Skip to next run of spaces. */ | |
711 | while (*cp && *cp != ' ' && *cp != '\t') | |
712 | cp++; | |
713 | } | |
28f540f4 RM |
714 | } |
715 | ||
4c4480ee | 716 | static uint32_t |
85231522 | 717 | net_mask (struct in_addr in) |
28f540f4 | 718 | { |
4c4480ee | 719 | uint32_t i = ntohl (in.s_addr); |
28f540f4 | 720 | |
4c4480ee FW |
721 | if (IN_CLASSA (i)) |
722 | return htonl (IN_CLASSA_NET); | |
723 | else if (IN_CLASSB (i)) | |
724 | return htonl (IN_CLASSB_NET); | |
725 | return htonl (IN_CLASSC_NET); | |
28f540f4 | 726 | } |