Prevent an implicit int promotion in malloc/tst-alloc_buffer.c
[glibc.git] / resolv / res_init.c
blob9aa907ee199f01a6e0d918299ba9334fa8299799
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 <stdio.h>
89 #include <stdio_ext.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93 #include <stdint.h>
94 #include <arpa/inet.h>
95 #include <arpa/nameser.h>
96 #include <net/if.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>
103 #include <errno.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"
118 directive. */
119 static inline bool
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
127 failure. */
128 static bool
129 res_vinit_1 (res_state statp, bool preinit, FILE *fp, char **buffer)
131 char *cp, **pp;
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;
137 int nsort = 0;
138 char *net;
139 statp->_u._ext.initstamp = __res_initstamp;
141 if (!preinit)
143 statp->retrans = RES_TIMEOUT;
144 statp->retry = RES_DFLRETRY;
145 statp->options = RES_DEFAULT;
146 statp->id = res_randomid ();
149 statp->nscount = 0;
150 statp->defdname[0] = '\0';
151 statp->ndots = 1;
152 statp->pfcode = 0;
153 statp->_vcsock = -1;
154 statp->_flags = 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';
166 haveenv = true;
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;
174 pp = statp->dnsrch;
175 *pp++ = cp;
176 for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
178 if (*cp == '\n')
179 break;
180 else if (*cp == ' ' || *cp == '\t')
182 *cp = 0;
183 n = 1;
185 else if (n > 0)
187 *pp++ = cp;
188 n = 0;
189 havesearch = true;
192 /* Null terminate last domain if there are excess. */
193 while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
194 cp++;
195 *cp = '\0';
196 *pp++ = 0;
199 #define MATCH(line, name) \
200 (!strncmp ((line), name, sizeof (name) - 1) \
201 && ((line)[sizeof (name) - 1] == ' ' \
202 || (line)[sizeof (name) - 1] == '\t'))
204 if (fp != NULL)
206 /* No threads use this stream. */
207 __fsetlocking (fp, FSETLOCKING_BYCALLER);
208 /* Read the config file. */
209 while (true)
212 ssize_t ret = __getline (buffer, &buffer_size, fp);
213 if (ret <= 0)
215 if (_IO_ferror_unlocked (fp))
216 return false;
217 else
218 break;
222 /* Skip comments. */
223 if (**buffer == ';' || **buffer == '#')
224 continue;
225 /* Read default domain name. */
226 if (MATCH (*buffer, "domain"))
228 if (haveenv)
229 /* LOCALDOMAIN overrides the configuration file. */
230 continue;
231 cp = *buffer + sizeof ("domain") - 1;
232 while (*cp == ' ' || *cp == '\t')
233 cp++;
234 if ((*cp == '\0') || (*cp == '\n'))
235 continue;
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)
239 *cp = '\0';
240 havesearch = false;
241 continue;
243 /* Set search list. */
244 if (MATCH (*buffer, "search"))
246 if (haveenv)
247 /* LOCALDOMAIN overrides the configuration file. */
248 continue;
249 cp = *buffer + sizeof ("search") - 1;
250 while (*cp == ' ' || *cp == '\t')
251 cp++;
252 if ((*cp == '\0') || (*cp == '\n'))
253 continue;
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)
257 *cp = '\0';
258 /* Set search list to be blank-separated strings on rest
259 of line. */
260 cp = statp->defdname;
261 pp = statp->dnsrch;
262 *pp++ = cp;
263 for (int n = 0; *cp && pp < statp->dnsrch + MAXDNSRCH; cp++)
265 if (*cp == ' ' || *cp == '\t')
267 *cp = 0;
268 n = 1;
270 else if (n)
272 *pp++ = cp;
273 n = 0;
276 /* Null terminate last domain if there are excess. */
277 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
278 cp++;
279 *cp = '\0';
280 *pp++ = 0;
281 havesearch = true;
282 continue;
284 /* Read nameservers to query. */
285 if (MATCH (*buffer, "nameserver") && nserv < MAXNS)
287 struct in_addr a;
289 cp = *buffer + sizeof ("nameserver") - 1;
290 while (*cp == ' ' || *cp == '\t')
291 cp++;
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);
297 nserv++;
299 else
301 struct in6_addr a6;
302 char *el;
304 if ((el = strpbrk (cp, " \t\n")) != NULL)
305 *el = '\0';
306 if ((el = strchr (cp, SCOPE_DELIMITER)) != NULL)
307 *el = '\0';
308 if ((*cp != '\0') && (__inet_pton (AF_INET6, cp, &a6) > 0))
310 struct sockaddr_in6 *sa6;
312 sa6 = malloc (sizeof (*sa6));
313 if (sa6 == NULL)
314 return false;
316 sa6->sin6_family = AF_INET6;
317 sa6->sin6_port = htons (NAMESERVER_PORT);
318 sa6->sin6_flowinfo = 0;
319 sa6->sin6_addr = a6;
321 sa6->sin6_scope_id = 0;
322 if (__glibc_likely (el != NULL))
323 /* Ignore errors, for backwards
324 compatibility. */
325 __inet6_scopeid_pton
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;
331 have_serv6 = true;
332 nserv++;
335 continue;
337 if (MATCH (*buffer, "sortlist"))
339 struct in_addr a;
341 cp = *buffer + sizeof ("sortlist") - 1;
342 while (nsort < MAXRESOLVSORT)
344 while (*cp == ' ' || *cp == '\t')
345 cp++;
346 if (*cp == '\0' || *cp == '\n' || *cp == ';')
347 break;
348 net = cp;
349 while (*cp && !is_sort_mask (*cp) && *cp != ';'
350 && isascii (*cp) && !isspace (*cp))
351 cp++;
352 char separator = *cp;
353 *cp = 0;
354 if (__inet_aton (net, &a))
356 statp->sort_list[nsort].addr = a;
357 if (is_sort_mask (separator))
359 *cp++ = separator;
360 net = cp;
361 while (*cp && *cp != ';'
362 && isascii (*cp) && !isspace (*cp))
363 cp++;
364 separator = *cp;
365 *cp = 0;
366 if (__inet_aton (net, &a))
367 statp->sort_list[nsort].mask = a.s_addr;
368 else
369 statp->sort_list[nsort].mask
370 = net_mask (statp->sort_list[nsort].addr);
372 else
373 statp->sort_list[nsort].mask
374 = net_mask (statp->sort_list[nsort].addr);
375 nsort++;
377 *cp = separator;
379 continue;
381 if (MATCH (*buffer, "options"))
383 res_setoptions (statp, *buffer + sizeof ("options") - 1, "conf");
384 continue;
387 statp->nscount = nserv;
388 if (have_serv6)
389 /* We try IPv6 servers again. */
390 statp->ipv6_unavail = false;
391 statp->nsort = nsort;
392 fclose (fp);
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);
399 statp->nscount = 1;
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. */
410 if (!havesearch)
412 pp = statp->dnsrch;
413 *pp++ = statp->defdname;
414 *pp = NULL;
418 if ((cp = getenv ("RES_OPTIONS")) != NULL)
419 res_setoptions (statp, cp, "env");
420 statp->options |= RES_INIT;
421 return true;
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");
434 if (fp == NULL)
435 switch (errno)
437 case EACCES:
438 case EISDIR:
439 case ELOOP:
440 case ENOENT:
441 case ENOTDIR:
442 case EPERM:
443 /* Ignore these errors. They are persistent errors caused
444 by file system contents. */
445 break;
446 default:
447 /* Other errors refer to resource allocation problems and
448 need to be handled by the application. */
449 return -1;
452 char *buffer = NULL;
453 bool ok = res_vinit_1 (statp, preinit, fp, &buffer);
454 free (buffer);
456 if (!ok)
458 /* Deallocate the name server addresses which have been
459 allocated. */
460 for (int n = 0; n < MAXNS; n++)
461 free (statp->_u._ext.nsaddrs[n]);
462 return -1;
464 return 0;
467 static void
468 res_setoptions (res_state statp, const char *options, const char *source)
470 const char *cp = options;
472 while (*cp)
474 /* Skip leading and inner runs of spaces. */
475 while (*cp == ' ' || *cp == '\t')
476 cp++;
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)
482 statp->ndots = i;
483 else
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)
490 statp->retrans = i;
491 else
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)
498 statp->retry = i;
499 else
500 statp->retry = RES_MAXRETRY;
502 else
504 static const struct
506 char str[22];
507 uint8_t len;
508 uint8_t clear;
509 unsigned long int flag;
510 } options[] = {
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;
527 else
528 statp->options |= options[i].flag;
529 break;
532 /* Skip to next run of spaces. */
533 while (*cp && *cp != ' ' && *cp != '\t')
534 cp++;
538 static uint32_t
539 net_mask (struct in_addr in)
541 uint32_t i = ntohl (in.s_addr);
543 if (IN_CLASSA (i))
544 return htonl (IN_CLASSA_NET);
545 else if (IN_CLASSB (i))
546 return htonl (IN_CLASSB_NET);
547 return htonl (IN_CLASSC_NET);