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