2 * Copyright (c) 2009, 2011, 2012, 2014, 2015, 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 #define my_log(__level, __format, ...) SCPrint(TRUE, stdout, CFSTR(__format "\n"), ## __VA_ARGS__)
43 #include "dnsinfo_private.h"
44 #include "dnsinfo_create.h"
46 static uint32_t _dnsinfo_flatfile_flags
;
64 * The supported configuration token strings and enumerated values.
71 { "domain", TOKEN_DOMAIN
, 1 },
72 { "flags", TOKEN_FLAGS
, 1 },
73 { "interface", TOKEN_INTERFACE
, 1 },
74 { "nameserver", TOKEN_NAMESERVER
, MAXNS
},
75 { "options", TOKEN_OPTIONS
, 1 },
76 { "port", TOKEN_PORT
, 1 },
77 { "search", TOKEN_SEARCH
, 1 },
78 { "search_order", TOKEN_SEARCH_ORDER
, 1 },
79 { "sortlist", TOKEN_SORTLIST
, 1 },
80 { "timeout", TOKEN_TIMEOUT
, 1 },
85 * _dnsinfo_parse_address
89 static struct sockaddr
*
90 _dnsinfo_parse_address(char *nameserver
)
93 struct addrinfo hints
;
95 struct sockaddr
*sa
= NULL
;
97 memset(&hints
, 0, sizeof(hints
));
98 hints
.ai_flags
= AI_NUMERICHOST
;
100 res
= getaddrinfo(nameserver
, NULL
, &hints
, &ai
);
102 if ((ai
->ai_family
== AF_INET
) || (ai
->ai_family
== AF_INET6
)) {
103 sa
= malloc(ai
->ai_addrlen
);
104 memcpy(sa
, ai
->ai_addr
, ai
->ai_addrlen
);
114 * _dnsinfo_parse_nameserver
116 * Parse arguments to the nameserver token. This is essentially a getaddrinfo(3)
117 * with AI_NUMERICHOST. However, if the conversion fails, check if the address
118 * contains an optional trailing '.' followed by a numeric port number. If found,
119 * remove the port number and retry the conversion (e.g. 127.0.0.1.55 or ::1.55).
121 static struct sockaddr
*
122 _dnsinfo_parse_nameserver(char *token
)
128 sa
= _dnsinfo_parse_address(token
);
133 // if we could not parse address, attempt to remove
134 // an optional trailing port number
135 dot
= strrchr(token
, '.');
140 number
= strtol(dot
+ 1, NULL
, 10);
141 if ((number
< 0) || (number
> UINT16_MAX
)) {
146 sa
= _dnsinfo_parse_address(token
);
148 in_port_t port
= htons(number
);
150 switch (sa
->sa_family
) {
152 /* ALIGN: cast ok, sockaddr was malloc'd */
153 ((struct sockaddr_in
*)(void *)sa
)->sin_port
= port
;
156 /* ALIGN: cast ok, sockaddr was malloc'd */
157 ((struct sockaddr_in6
*)(void *)sa
)->sin6_port
= port
;
167 * _dnsinfo_parse_sortaddr
169 * Parse arguments to the sortlist token.
171 static dns_sortaddr_t
*
172 _dnsinfo_parse_sortaddr(char *token
)
178 dns_sortaddr_t
*sortaddr
= NULL
;
180 slash
= strchr(token
, '/');
185 sa
= _dnsinfo_parse_address(token
);
187 // if we could not parse the address
189 } else if (sa
->sa_family
!= AF_INET
) {
193 /* ALIGN: cast ok, sockaddr was malloc'd */
194 addr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
200 sa
= _dnsinfo_parse_address(slash
+ 1);
202 // if we could not parse the provided mask
204 } else if (sa
->sa_family
!= AF_INET
) {
205 // if mask not AF_INET
208 /* ALIGN: cast ok, sockaddr was malloc'd */
209 mask
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
217 a
= ntohl(addr
.s_addr
);
220 } else if (IN_CLASSB(a
)) {
222 } else if (IN_CLASSC(a
)) {
228 mask
.s_addr
= htonl(m
);
231 sortaddr
= malloc(sizeof(*sortaddr
));
232 sortaddr
->address
= addr
;
233 sortaddr
->mask
= mask
;
237 if (sa
!= NULL
) free(sa
);
243 * _dnsinfo_flatfile_set_flags
245 * Set the default resolver flags.
249 _dnsinfo_flatfile_set_flags(uint32_t flags
)
251 _dnsinfo_flatfile_flags
= flags
;
257 _dnsinfo_flatfile_update_flags(dns_create_resolver_t
*_resolver
)
261 _dns_resolver_buf_t
*resolver
= (_dns_resolver_buf_t
*)*_resolver
;
263 old_flags
= ntohl(resolver
->resolver
.flags
);
264 new_flags
= old_flags
| _dnsinfo_flatfile_flags
;
265 _dns_resolver_set_flags(_resolver
, new_flags
);
271 * _dnsinfo_flatfile_create_resolver
273 * Create a new dns resolver configuration from the configuration file at the
274 * specified path. (e.g. /etc/resolv.conf or /etc/resolver/apple.com)
276 static dns_create_resolver_t
277 _dnsinfo_flatfile_create_resolver(const char *dir
, const char *path
)
280 uint32_t config_flags
= 0;
282 char filename
[FILENAME_MAX
];
285 dns_create_resolver_t res
= NULL
;
286 const char *sep
= " \t";
287 int token_count
[TOKEN_MAX
] = { 0 };
291 strlcpy(filename
, dir
, sizeof(filename
));
292 strlcat(filename
, "/", sizeof(filename
));
294 strlcat(filename
, path
, sizeof(filename
));
296 f
= fopen(filename
, "r");
297 if (f
== NULL
) return NULL
;
299 while ((buf
= fgetln(f
, &len
)) != NULL
) {
305 if (len
== 0) continue;
306 if (buf
[len
-1] == '\n') buf
[len
-1] = '\0';
308 line
= reallocf(line
, len
+1);
309 if (line
== NULL
) continue;
311 strlcpy(line
, buf
, len
+1);
313 // parse the first word of the line (the config token)
315 word
= strsep(&lineptr
, sep
);
320 if (word
[0] == ';' || word
[0] == '#') {
325 // translate config token to enumerated value
327 for (size_t i
= 0; i
< sizeof(tokens
) / sizeof(tokens
[0]); i
++) {
328 if (strcasecmp(word
, tokens
[i
].name
) == 0) {
329 token
= tokens
[i
].token
;
330 max_count
= tokens
[i
].max_count
;
335 // if not a recognized token
339 // parse the next word of the line (the config option)
340 word
= strsep(&lineptr
, sep
);
345 if (++token_count
[token
] > max_count
) {
346 // if too many options
352 res
= _dns_resolver_create();
354 // if we could not create a resolver
364 while ((len
> 0) && (word
[len
- 1] == '.')) {
369 _dns_resolver_set_domain(&res
, word
);
375 while (word
!= NULL
) {
376 if (word
[0] != '\0') {
377 if (strcasecmp(word
, "scoped") == 0) {
378 config_flags
|= DNS_RESOLVER_FLAGS_SCOPED
;
379 } else if (strcasecmp(word
, "a") == 0) {
380 config_flags
|= DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS
;
381 } else if (strcasecmp(word
, "aaaa") == 0) {
382 config_flags
|= DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS
;
385 word
= strsep(&lineptr
, sep
);
390 case TOKEN_INTERFACE
: {
391 unsigned int if_index
;
393 if_index
= if_nametoindex(word
);
395 _dns_resolver_set_if_index(&res
, if_index
, word
);
400 case TOKEN_NAMESERVER
: {
403 sa
= _dnsinfo_parse_nameserver(word
);
405 _dns_resolver_add_nameserver(&res
, sa
);
411 case TOKEN_OPTIONS
: {
412 char *options
= NULL
;
414 while (word
!= NULL
) {
415 if (word
[0] != '\0') {
416 if (options
== NULL
) {
417 options
= malloc(len
+1);
418 if (options
== NULL
) break;
420 strlcpy(options
, word
, len
+1);
422 strlcat(options
, " ", len
+1);
423 strlcat(options
, word
, len
+1);
426 word
= strsep(&lineptr
, sep
);
429 if (options
!= NULL
) {
430 _dns_resolver_set_options(&res
, options
);
439 number
= strtol(word
, NULL
, 0);
440 if (number
< 0 || number
> UINT16_MAX
) break;
441 _dns_resolver_set_port(&res
, number
);
448 // multiple search domains are supported
449 while ((word
!= NULL
) && (n
++ < MAXDNSRCH
)) {
453 while ((len
> 0) && (word
[len
- 1] == '.')) {
458 _dns_resolver_add_search(&res
, word
);
460 word
= strsep(&lineptr
, sep
);
465 case TOKEN_SEARCH_ORDER
: {
468 number
= strtol(word
, NULL
, 0);
469 if (number
< 0 || number
> UINT32_MAX
) break;
470 _dns_resolver_set_order(&res
, (uint32_t)number
);
474 case TOKEN_SORTLIST
: {
477 while ((word
!= NULL
) && (n
++ < MAXRESOLVSORT
)) {
478 dns_sortaddr_t
*sortaddr
;
480 sortaddr
= _dnsinfo_parse_sortaddr(word
);
481 if (sortaddr
== NULL
) break;
482 _dns_resolver_add_sortaddr(&res
, sortaddr
);
484 word
= strsep(&lineptr
, sep
);
489 case TOKEN_TIMEOUT
: {
492 number
= strtol(word
, NULL
, 0);
493 if (number
< 0 || number
> UINT32_MAX
) break;
494 _dns_resolver_set_timeout(&res
, (uint32_t)number
);
499 if (line
!= NULL
) free(line
);
501 // set the domain to the basename of the path if not specified
502 if ((res
!= NULL
) && (token_count
[TOKEN_DOMAIN
] == 0)) {
505 domain
= strrchr(path
, '/');
506 if (domain
== NULL
) {
511 _dns_resolver_set_domain(&res
, domain
);
515 // config flags should overwrite any default flags
516 if (config_flags
!= 0) {
517 _dns_resolver_set_flags(&res
, config_flags
);
519 _dnsinfo_flatfile_update_flags(&res
);
531 * _dnsinfo_flatfile_add_resolvers
533 * Parse the files in the resolver config directory (/etc/resolver) and add each
534 * resolver to the dns config.
538 _dnsinfo_flatfile_add_resolvers(dns_create_config_t
*config
)
542 dns_create_resolver_t res
;
544 dp
= opendir(_PATH_RESOLVER_DIR
);
549 while ((de
= readdir(dp
)) != NULL
) {
550 if (strcmp(de
->d_name
, ".") == 0 ||
551 strcmp(de
->d_name
, "..") == 0) continue;
553 res
= _dnsinfo_flatfile_create_resolver(_PATH_RESOLVER_DIR
, de
->d_name
);
555 _dns_configuration_add_resolver(config
, res
);
556 _dns_resolver_free(&res
);
568 #include "dnsinfo_logging.h"
569 #include "dnsinfo_copy.c"
572 main(int argc
, char **argv
)
574 dns_config_t
*dns_config
= NULL
;
575 _dns_config_buf_t
*dns_config_buf
= NULL
;
576 dns_create_config_t dns_create_config
;
577 dns_create_resolver_t dns_create_resolver
;
579 dns_create_resolver
= _dnsinfo_flatfile_create_resolver(NULL
, _PATH_RESCONF
);
580 _dns_resolver_free(&dns_create_resolver
);
582 dns_create_config
= _dns_configuration_create();
583 if (dns_create_config
!= NULL
) {
586 _dnsinfo_flatfile_add_resolvers(&dns_create_config
);
588 n
= sizeof(_dns_config_buf_t
);
589 n
+= ntohl(((_dns_config_buf_t
*)dns_create_config
)->n_attribute
);
590 dns_config_buf
= _dns_configuration_buffer_create((void *)dns_create_config
, n
);
591 _dns_configuration_free(&dns_create_config
);
594 if (dns_config_buf
!= NULL
) {
595 dns_config
= _dns_configuration_buffer_expand(dns_config_buf
);
596 if (dns_config
== NULL
) {
597 // if we were unable to expand the configuration
598 _dns_configuration_buffer_free(&dns_config_buf
);
602 if (dns_config
!= NULL
) {
603 _dns_configuration_log(dns_config
, TRUE
, NULL
);