1 /* Copyright (C) 1993-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by David Mosberger (davidm@azstarnet.com).
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/>. */
19 /* This file provides a Linux /etc/host.conf compatible front end to
20 the various name resolvers (/etc/hosts, named, NIS server, etc.).
21 Though mostly compatibly, the following differences exist compared
22 to the original implementation:
24 - new command "spoof" takes an arguments like RESOLV_SPOOF_CHECK
25 environment variable (i.e., `off', `nowarn', or `warn').
27 - line comments can appear anywhere (not just at the beginning of
37 #include <stdio_ext.h>
41 #include <sys/ioctl.h>
43 #include <netinet/in.h>
44 #include <libc-lock.h>
46 #include "res_hconf.h"
51 # define fgets_unlocked __fgets_unlocked
54 #define _PATH_HOSTCONF "/etc/host.conf"
56 /* Environment vars that all user to override default behavior: */
57 #define ENV_HOSTCONF "RESOLV_HOST_CONF"
58 #define ENV_SPOOF "RESOLV_SPOOF_CHECK"
59 #define ENV_TRIM_OVERR "RESOLV_OVERRIDE_TRIM_DOMAINS"
60 #define ENV_TRIM_ADD "RESOLV_ADD_TRIM_DOMAINS"
61 #define ENV_MULTI "RESOLV_MULTI"
62 #define ENV_REORDER "RESOLV_REORDER"
67 CB_arg_trimdomain_list
,
72 static const struct cmd
79 {"order", CB_none
, 0},
80 {"trim", CB_arg_trimdomain_list
, 0},
81 {"spoof", CB_arg_spoof
, 0},
82 {"multi", CB_arg_bool
, HCONF_FLAG_MULTI
},
83 {"nospoof", CB_arg_bool
, HCONF_FLAG_SPOOF
},
84 {"spoofalert", CB_arg_bool
, HCONF_FLAG_SPOOFALERT
},
85 {"reorder", CB_arg_bool
, HCONF_FLAG_REORDER
}
88 /* Structure containing the state. */
89 struct hconf _res_hconf
;
91 /* Skip white space. */
93 skip_ws (const char *str
)
95 while (isspace (*str
)) ++str
;
100 /* Skip until whitespace, comma, end of line, or comment character. */
102 skip_string (const char *str
)
104 while (*str
&& !isspace (*str
) && *str
!= '#' && *str
!= ',')
111 arg_trimdomain_list (const char *fname
, int line_num
, const char *args
)
119 args
= skip_string (args
);
122 if (_res_hconf
.num_trimdomains
>= TRIMDOMAINS_MAX
)
126 if (__asprintf (&buf
, _("\
127 %s: line %d: cannot specify more than %d trim domains"),
128 fname
, line_num
, TRIMDOMAINS_MAX
) < 0)
131 __fxprintf (NULL
, "%s", buf
);
136 _res_hconf
.trimdomain
[_res_hconf
.num_trimdomains
++] =
137 __strndup (start
, len
);
138 args
= skip_ws (args
);
141 case ',': case ';': case ':':
142 args
= skip_ws (++args
);
143 if (!*args
|| *args
== '#')
147 if (__asprintf (&buf
, _("\
148 %s: line %d: list delimiter not followed by domain"),
149 fname
, line_num
) < 0)
152 __fxprintf (NULL
, "%s", buf
);
161 while (*args
&& *args
!= '#');
167 arg_spoof (const char *fname
, int line_num
, const char *args
)
169 const char *start
= args
;
172 args
= skip_string (args
);
175 if (len
== 3 && __strncasecmp (start
, "off", len
) == 0)
176 _res_hconf
.flags
&= ~(HCONF_FLAG_SPOOF
| HCONF_FLAG_SPOOFALERT
);
179 _res_hconf
.flags
|= (HCONF_FLAG_SPOOF
| HCONF_FLAG_SPOOFALERT
);
180 if ((len
== 6 && __strncasecmp (start
, "nowarn", len
) == 0)
181 || !(len
== 4 && __strncasecmp (start
, "warn", len
) == 0))
182 _res_hconf
.flags
&= ~HCONF_FLAG_SPOOFALERT
;
189 arg_bool (const char *fname
, int line_num
, const char *args
, unsigned flag
)
191 if (__strncasecmp (args
, "on", 2) == 0)
194 _res_hconf
.flags
|= flag
;
196 else if (__strncasecmp (args
, "off", 3) == 0)
199 _res_hconf
.flags
&= ~flag
;
205 if (__asprintf (&buf
,
206 _("%s: line %d: expected `on' or `off', found `%s'\n"),
207 fname
, line_num
, args
) < 0)
210 __fxprintf (NULL
, "%s", buf
);
220 parse_line (const char *fname
, int line_num
, const char *str
)
223 const struct cmd
*c
= 0;
229 /* skip line comment and empty lines: */
230 if (*str
== '\0' || *str
== '#') return;
233 str
= skip_string (str
);
236 for (i
= 0; i
< sizeof (cmd
) / sizeof (cmd
[0]); ++i
)
238 if (__strncasecmp (start
, cmd
[i
].name
, len
) == 0
239 && strlen (cmd
[i
].name
) == len
)
249 if (__asprintf (&buf
, _("%s: line %d: bad command `%s'\n"),
250 fname
, line_num
, start
) < 0)
253 __fxprintf (NULL
, "%s", buf
);
262 if (c
->cb
== CB_arg_trimdomain_list
)
263 str
= arg_trimdomain_list (fname
, line_num
, str
);
264 else if (c
->cb
== CB_arg_spoof
)
265 str
= arg_spoof (fname
, line_num
, str
);
266 else if (c
->cb
== CB_arg_bool
)
267 str
= arg_bool (fname
, line_num
, str
, c
->arg
);
269 /* Ignore the line. */
275 /* rest of line must contain white space or comment only: */
278 if (!isspace (*str
)) {
283 if (__asprintf (&buf
,
284 _("%s: line %d: ignoring trailing garbage `%s'\n"),
285 fname
, line_num
, str
) < 0)
288 __fxprintf (NULL
, "%s", buf
);
302 const char *hconf_name
;
304 char buf
[256], *envval
;
307 memset (&_res_hconf
, '\0', sizeof (_res_hconf
));
309 hconf_name
= getenv (ENV_HOSTCONF
);
310 if (hconf_name
== NULL
)
311 hconf_name
= _PATH_HOSTCONF
;
313 fp
= fopen (hconf_name
, "rce");
316 /* No threads using this stream. */
317 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
319 while (fgets_unlocked (buf
, sizeof (buf
), fp
))
322 *__strchrnul (buf
, '\n') = '\0';
323 parse_line (hconf_name
, line_num
, buf
);
328 envval
= getenv (ENV_SPOOF
);
330 arg_spoof (ENV_SPOOF
, 1, envval
);
332 envval
= getenv (ENV_MULTI
);
334 arg_bool (ENV_MULTI
, 1, envval
, HCONF_FLAG_MULTI
);
336 envval
= getenv (ENV_REORDER
);
338 arg_bool (ENV_REORDER
, 1, envval
, HCONF_FLAG_REORDER
);
340 envval
= getenv (ENV_TRIM_ADD
);
342 arg_trimdomain_list (ENV_TRIM_ADD
, 1, envval
);
344 envval
= getenv (ENV_TRIM_OVERR
);
347 _res_hconf
.num_trimdomains
= 0;
348 arg_trimdomain_list (ENV_TRIM_OVERR
, 1, envval
);
351 _res_hconf
.initialized
= 1;
355 /* Initialize hconf datastructure by reading host.conf file and
356 environment variables. */
358 _res_hconf_init (void)
360 __libc_once_define (static, once
);
362 __libc_once (once
, do_init
);
367 # if defined SIOCGIFCONF && defined SIOCGIFNETMASK
368 /* List of known interfaces. */
370 static struct netaddr
384 /* Reorder addresses returned in a hostent such that the first address
385 is an address on the local subnet, if there is such an address.
386 Otherwise, nothing is changed.
388 Note that this function currently only handles IPv4 addresses. */
391 _res_hconf_reorder_addrs (struct hostent
*hp
)
393 #if defined SIOCGIFCONF && defined SIOCGIFNETMASK
395 /* Number of interfaces. Also serves as a flag for the
396 double-checked locking idiom. */
397 static int num_ifs
= -1;
398 /* Local copy of num_ifs, for non-atomic access. */
400 /* We need to protect the dynamic buffer handling. The lock is only
401 acquired during initialization. Afterwards, a positive num_ifs
402 value indicates completed initialization. */
403 __libc_lock_define_initialized (static, lock
);
405 /* Only reorder if we're supposed to. */
406 if ((_res_hconf
.flags
& HCONF_FLAG_REORDER
) == 0)
409 /* Can't deal with anything but IPv4 for now... */
410 if (hp
->h_addrtype
!= AF_INET
)
413 /* This load synchronizes with the release MO store in the
414 initialization block below. */
415 num_ifs_local
= atomic_load_acquire (&num_ifs
);
416 if (num_ifs_local
<= 0)
418 struct ifreq
*ifr
, *cur_ifr
;
423 /* Initialize interface table. */
425 /* The SIOCGIFNETMASK ioctl will only work on an AF_INET socket. */
426 sd
= __socket (AF_INET
, SOCK_DGRAM
, 0);
431 __libc_lock_lock (lock
);
433 /* Recheck, somebody else might have done the work by now. No
434 ordering is required for the load because we have the lock,
435 and num_ifs is only updated under the lock. Also see (3) in
436 the analysis below. */
437 num_ifs_local
= atomic_load_relaxed (&num_ifs
);
438 if (num_ifs_local
<= 0)
440 /* This is the only block which writes to num_ifs. It can
441 be executed several times (sequentially) if
442 initialization does not yield any interfaces, and num_ifs
443 remains zero. However, once we stored a positive value
444 in num_ifs below, this block cannot be entered again due
445 to the condition above. */
448 /* Get a list of interfaces. */
449 __ifreq (&ifr
, &num
, sd
);
453 ifaddrs
= malloc (num
* sizeof (ifaddrs
[0]));
457 /* Copy usable interfaces in ifaddrs structure. */
458 for (cur_ifr
= ifr
, i
= 0; i
< num
;
459 cur_ifr
= __if_nextreq (cur_ifr
), ++i
)
464 struct sockaddr_in sin
;
467 if (cur_ifr
->ifr_addr
.sa_family
!= AF_INET
)
470 ifaddrs
[new_num_ifs
].addrtype
= AF_INET
;
471 ss
.sa
= cur_ifr
->ifr_addr
;
472 ifaddrs
[new_num_ifs
].u
.ipv4
.addr
= ss
.sin
.sin_addr
.s_addr
;
474 if (__ioctl (sd
, SIOCGIFNETMASK
, cur_ifr
) < 0)
477 ss
.sa
= cur_ifr
->ifr_netmask
;
478 ifaddrs
[new_num_ifs
].u
.ipv4
.mask
= ss
.sin
.sin_addr
.s_addr
;
480 /* Now we're committed to this entry. */
483 /* Just keep enough memory to hold all the interfaces we want. */
484 ifaddrs
= realloc (ifaddrs
, new_num_ifs
* sizeof (ifaddrs
[0]));
485 assert (ifaddrs
!= NULL
);
488 __if_freereq (ifr
, num
);
491 /* Release lock, preserve error value, and close socket. */
494 /* Advertise successful initialization if new_num_ifs is
495 positive (and no updates to ifaddrs are permitted after
496 that). Otherwise, num_ifs remains unchanged, at zero.
497 This store synchronizes with the initial acquire MO
499 atomic_store_release (&num_ifs
, new_num_ifs
);
500 /* Keep the local copy current, to save another load. */
501 num_ifs_local
= new_num_ifs
;
504 __libc_lock_unlock (lock
);
509 /* num_ifs_local cannot be negative because the if statement above
510 covered this case. It can still be zero if we just performed
511 initialization, but could not find any interfaces. */
512 if (num_ifs_local
== 0)
515 /* The code below accesses ifaddrs, so we need to ensure that the
516 initialization happens-before this point.
518 The actual initialization is sequenced-before the release store
519 to num_ifs, and sequenced-before the end of the critical section.
521 This means there are three possible executions:
523 (1) The thread that initialized the data also uses it, so
524 sequenced-before is sufficient to ensure happens-before.
526 (2) The release MO store of num_ifs synchronizes-with the acquire
527 MO load, and the acquire MO load is sequenced before the use
528 of the initialized data below.
530 (3) We enter the critical section, and the relaxed MO load of
531 num_ifs yields a positive value. The write to ifaddrs is
532 sequenced-before leaving the critical section. Leaving the
533 critical section happens-before we entered the critical
534 section ourselves, which means that the write to ifaddrs
535 happens-before this point.
537 Consequently, all potential writes to ifaddrs (and the data it
538 points to) happens-before this point. */
540 /* Find an address for which we have a direct connection. */
541 for (i
= 0; hp
->h_addr_list
[i
]; ++i
)
543 struct in_addr
*haddr
= (struct in_addr
*) hp
->h_addr_list
[i
];
545 for (j
= 0; j
< num_ifs_local
; ++j
)
547 u_int32_t if_addr
= ifaddrs
[j
].u
.ipv4
.addr
;
548 u_int32_t if_netmask
= ifaddrs
[j
].u
.ipv4
.mask
;
550 if (((haddr
->s_addr
^ if_addr
) & if_netmask
) == 0)
554 tmp
= hp
->h_addr_list
[i
];
555 hp
->h_addr_list
[i
] = hp
->h_addr_list
[0];
556 hp
->h_addr_list
[0] = tmp
;
561 #endif /* defined(SIOCGIFCONF) && ... */
565 /* If HOSTNAME has a postfix matching any of the trimdomains, trim away
566 that postfix. Notice that HOSTNAME is modified inplace. Also, the
567 original code applied all trimdomains in order, meaning that the
568 same domainname could be trimmed multiple times. I believe this
569 was unintentional. */
571 _res_hconf_trim_domain (char *hostname
)
573 size_t hostname_len
, trim_len
;
576 hostname_len
= strlen (hostname
);
578 for (i
= 0; i
< _res_hconf
.num_trimdomains
; ++i
)
580 const char *trim
= _res_hconf
.trimdomain
[i
];
582 trim_len
= strlen (trim
);
583 if (hostname_len
> trim_len
584 && __strcasecmp (&hostname
[hostname_len
- trim_len
], trim
) == 0)
586 hostname
[hostname_len
- trim_len
] = '\0';
593 /* Trim all hostnames/aliases in HP according to the trimdomain list.
594 Notice that HP is modified inplace! */
596 _res_hconf_trim_domains (struct hostent
*hp
)
600 if (_res_hconf
.num_trimdomains
== 0)
603 _res_hconf_trim_domain (hp
->h_name
);
604 for (i
= 0; hp
->h_aliases
[i
]; ++i
)
605 _res_hconf_trim_domain (hp
->h_aliases
[i
]);