]> git.saurik.com Git - apple/configd.git/blob - dnsinfo/dnsinfo_flatfile.c
configd-888.1.2.tar.gz
[apple/configd.git] / dnsinfo / dnsinfo_flatfile.c
1 /*
2 * Copyright (c) 2009, 2011, 2012, 2014, 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <libgen.h>
27 #include <netdb.h>
28 #include <resolv.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <net/if.h>
33 #include <sys/dir.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <unistd.h>
37
38 #include "dnsinfo.h"
39 #include "dnsinfo_private.h"
40 #include "dnsinfo_create.h"
41
42 static uint32_t _dnsinfo_flatfile_flags;
43
44 enum {
45 TOKEN_DOMAIN,
46 TOKEN_FLAGS,
47 TOKEN_INTERFACE,
48 TOKEN_NAMESERVER,
49 TOKEN_OPTIONS,
50 TOKEN_PORT,
51 TOKEN_SEARCH,
52 TOKEN_SEARCH_ORDER,
53 TOKEN_SORTLIST,
54 TOKEN_TIMEOUT,
55 TOKEN_MAX
56 };
57
58 /*
59 * tokens
60 * The supported configuration token strings and enumerated values.
61 */
62 static const struct {
63 const char *name;
64 int token;
65 int max_count;
66 } tokens [] = {
67 { "domain", TOKEN_DOMAIN, 1 },
68 { "flags", TOKEN_FLAGS, 1 },
69 { "interface", TOKEN_INTERFACE, 1 },
70 { "nameserver", TOKEN_NAMESERVER, MAXNS },
71 { "options", TOKEN_OPTIONS, 1 },
72 { "port", TOKEN_PORT, 1 },
73 { "search", TOKEN_SEARCH, 1 },
74 { "search_order", TOKEN_SEARCH_ORDER, 1 },
75 { "sortlist", TOKEN_SORTLIST, 1 },
76 { "timeout", TOKEN_TIMEOUT, 1 },
77 };
78
79
80 /*
81 * _dnsinfo_parse_address
82 *
83 * Parse IP address
84 */
85 static struct sockaddr *
86 _dnsinfo_parse_address(char *nameserver)
87 {
88 struct addrinfo *ai;
89 struct addrinfo hints;
90 int res;
91 struct sockaddr *sa = NULL;
92
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_flags = AI_NUMERICHOST;
95
96 res = getaddrinfo(nameserver, NULL, &hints, &ai);
97 if (res == 0) {
98 if ((ai->ai_family == AF_INET) || (ai->ai_family == AF_INET6)) {
99 sa = malloc(ai->ai_addrlen);
100 memcpy(sa, ai->ai_addr, ai->ai_addrlen);
101 }
102 freeaddrinfo(ai);
103 }
104
105 return sa;
106 }
107
108
109 /*
110 * _dnsinfo_parse_nameserver
111 *
112 * Parse arguments to the nameserver token. This is essentially a getaddrinfo(3)
113 * with AI_NUMERICHOST. However, if the conversion fails, check if the address
114 * contains an optional trailing '.' followed by a numeric port number. If found,
115 * remove the port number and retry the conversion (e.g. 127.0.0.1.55 or ::1.55).
116 */
117 static struct sockaddr *
118 _dnsinfo_parse_nameserver(char *token)
119 {
120 char *dot;
121 long number;
122 struct sockaddr *sa;
123
124 sa = _dnsinfo_parse_address(token);
125 if (sa != NULL) {
126 return sa;
127 }
128
129 // if we could not parse address, attempt to remove
130 // an optional trailing port number
131 dot = strrchr(token, '.');
132 if (dot == NULL) {
133 return NULL;
134 }
135
136 number = strtol(dot + 1, NULL, 10);
137 if ((number < 0) || (number > UINT16_MAX)) {
138 return NULL;
139 }
140
141 *dot = '\0';
142 sa = _dnsinfo_parse_address(token);
143 if (sa != NULL) {
144 in_port_t port = htons(number);
145
146 switch (sa->sa_family) {
147 case AF_INET :
148 /* ALIGN: cast ok, sockaddr was malloc'd */
149 ((struct sockaddr_in *)(void *)sa)->sin_port = port;
150 break;
151 case AF_INET6 :
152 /* ALIGN: cast ok, sockaddr was malloc'd */
153 ((struct sockaddr_in6 *)(void *)sa)->sin6_port = port;
154 break;
155 }
156 }
157
158 return sa;
159 }
160
161
162 /*
163 * _dnsinfo_parse_sortaddr
164 *
165 * Parse arguments to the sortlist token.
166 */
167 static dns_sortaddr_t *
168 _dnsinfo_parse_sortaddr(char *token)
169 {
170 struct in_addr addr;
171 struct in_addr mask;
172 struct sockaddr *sa;
173 char *slash;
174 dns_sortaddr_t *sortaddr = NULL;
175
176 slash = strchr(token, '/');
177 if (slash != NULL) {
178 *slash = '\0';
179 }
180
181 sa = _dnsinfo_parse_address(token);
182 if (sa == NULL) {
183 // if we could not parse the address
184 goto done;
185 } else if (sa->sa_family != AF_INET) {
186 // if not AF_INET
187 goto done;
188 } else {
189 /* ALIGN: cast ok, sockaddr was malloc'd */
190 addr = ((struct sockaddr_in *)(void *)sa)->sin_addr;
191 free(sa);
192 sa = NULL;
193 }
194
195 if (slash != NULL) {
196 sa = _dnsinfo_parse_address(slash + 1);
197 if (sa == NULL) {
198 // if we could not parse the provided mask
199 goto done;
200 } else if (sa->sa_family != AF_INET) {
201 // if mask not AF_INET
202 goto done;
203 } else {
204 /* ALIGN: cast ok, sockaddr was malloc'd */
205 mask = ((struct sockaddr_in *)(void *)sa)->sin_addr;
206 free(sa);
207 sa = NULL;
208 }
209 } else {
210 in_addr_t a;
211 in_addr_t m;
212
213 a = ntohl(addr.s_addr);
214 if (IN_CLASSA(a)) {
215 m = IN_CLASSA_NET;
216 } else if (IN_CLASSB(a)) {
217 m = IN_CLASSB_NET;
218 } else if (IN_CLASSC(a)) {
219 m = IN_CLASSC_NET;
220 } else {
221 goto done;
222 }
223
224 mask.s_addr = htonl(m);
225 }
226
227 sortaddr = malloc(sizeof(*sortaddr));
228 sortaddr->address = addr;
229 sortaddr->mask = mask;
230
231 done :
232
233 if (sa != NULL) free(sa);
234 return sortaddr;
235 }
236
237
238 /*
239 * _dnsinfo_flatfile_set_flags
240 *
241 * Set the default resolver flags.
242 */
243 __private_extern__
244 void
245 _dnsinfo_flatfile_set_flags(uint32_t flags)
246 {
247 _dnsinfo_flatfile_flags = flags;
248 return;
249 }
250
251
252 static void
253 _dnsinfo_flatfile_update_flags(dns_create_resolver_t *_resolver)
254 {
255 uint32_t new_flags;
256 uint32_t old_flags;
257 _dns_resolver_buf_t *resolver = (_dns_resolver_buf_t *)*_resolver;
258
259 old_flags = ntohl(resolver->resolver.flags);
260 new_flags = old_flags | _dnsinfo_flatfile_flags;
261 _dns_resolver_set_flags(_resolver, new_flags);
262 return;
263 }
264
265
266 /*
267 * _dnsinfo_flatfile_create_resolver
268 *
269 * Create a new dns resolver configuration from the configuration file at the
270 * specified path. (e.g. /etc/resolv.conf or /etc/resolver/apple.com)
271 */
272 static dns_create_resolver_t
273 _dnsinfo_flatfile_create_resolver(const char *dir, const char *path)
274 {
275 char *buf;
276 uint32_t config_flags = 0;
277 FILE *f;
278 char filename[FILENAME_MAX];
279 size_t len = 0;
280 char *line = NULL;
281 dns_create_resolver_t res = NULL;
282 const char *sep = " \t";
283 int token_count[TOKEN_MAX] = { 0 };
284
285 filename[0] = 0;
286 if (dir != NULL) {
287 strlcpy(filename, dir, sizeof(filename));
288 strlcat(filename, "/", sizeof(filename));
289 }
290 strlcat(filename, path, sizeof(filename));
291
292 f = fopen(filename, "r");
293 if (f == NULL) return NULL;
294
295 while ((buf = fgetln(f, &len)) != NULL) {
296 int i;
297 char *lineptr;
298 int max_count;
299 int token;
300 char *word;
301
302 if (len == 0) continue;
303 if (buf[len-1] == '\n') buf[len-1] = '\0';
304
305 line = reallocf(line, len+1);
306 if (line == NULL) continue;
307
308 strlcpy(line, buf, len+1);
309
310 // parse the first word of the line (the config token)
311 lineptr = line;
312 word = strsep(&lineptr, sep);
313 if (word == NULL) {
314 // if empty line
315 continue;
316 }
317 if (word[0] == ';' || word[0] == '#') {
318 // if comment
319 continue;
320 }
321
322 // translate config token to enumerated value
323 token = -1;
324 for (i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
325 if (strcasecmp(word, tokens[i].name) == 0) {
326 token = tokens[i].token;
327 max_count = tokens[i].max_count;
328 break;
329 }
330 }
331 if (token == -1) {
332 // if not a recognized token
333 continue;
334 }
335
336 // parse the next word of the line (the config option)
337 word = strsep(&lineptr, sep);
338 if (word == NULL) {
339 // if no option
340 continue;
341 }
342 if (++token_count[token] > max_count) {
343 // if too many options
344 continue;
345 }
346
347 // create resolver
348 if (res == NULL) {
349 res = _dns_resolver_create();
350 if (res == NULL) {
351 // if we could not create a resolver
352 goto done;
353 }
354 }
355
356 switch (token) {
357 case TOKEN_DOMAIN: {
358 size_t len;
359
360 len = strlen(word);
361 while ((len > 0) && (word[len - 1] == '.')) {
362 // trim trailing '.'
363 word[--len] = '\0';
364 }
365 if (len > 0) {
366 _dns_resolver_set_domain(&res, word);
367 }
368 break;
369 }
370
371 case TOKEN_FLAGS: {
372 while (word != NULL) {
373 if (word[0] != '\0') {
374 if (strcasecmp(word, "scoped") == 0) {
375 config_flags |= DNS_RESOLVER_FLAGS_SCOPED;
376 } else if (strcasecmp(word, "a") == 0) {
377 config_flags |= DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS;
378 } else if (strcasecmp(word, "aaaa") == 0) {
379 config_flags |= DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS;
380 }
381 }
382 word = strsep(&lineptr, sep);
383 }
384 break;
385 }
386
387 case TOKEN_INTERFACE: {
388 unsigned int if_index;
389
390 if_index = if_nametoindex(word);
391 if (if_index > 0) {
392 _dns_resolver_set_if_index(&res, if_index);
393 }
394 break;
395 }
396
397 case TOKEN_NAMESERVER: {
398 struct sockaddr *sa;
399
400 sa = _dnsinfo_parse_nameserver(word);
401 if (sa != NULL) {
402 _dns_resolver_add_nameserver(&res, sa);
403 free(sa);
404 }
405 break;
406 }
407
408 case TOKEN_OPTIONS: {
409 char *options = NULL;
410
411 while (word != NULL) {
412 if (word[0] != '\0') {
413 if (options == NULL) {
414 options = malloc(len+1);
415 if (options == NULL) break;
416
417 strlcpy(options, word, len+1);
418 } else {
419 strlcat(options, " ", len+1);
420 strlcat(options, word, len+1);
421 }
422 }
423 word = strsep(&lineptr, sep);
424 }
425
426 if (options != NULL) {
427 _dns_resolver_set_options(&res, options);
428 free(options);
429 }
430 break;
431 }
432
433 case TOKEN_PORT: {
434 long number = -1;
435
436 number = strtol(word, NULL, 0);
437 if (number < 0 || number > UINT16_MAX) break;
438 _dns_resolver_set_port(&res, number);
439 break;
440 }
441
442 case TOKEN_SEARCH: {
443 int n = 0;
444
445 // multiple search domains are supported
446 while ((word != NULL) && (n++ < MAXDNSRCH)) {
447 size_t len;
448
449 len = strlen(word);
450 while ((len > 0) && (word[len - 1] == '.')) {
451 // trim trailing '.'
452 word[--len] = '\0';
453 }
454 if (len > 0) {
455 _dns_resolver_add_search(&res, word);
456 }
457 word = strsep(&lineptr, sep);
458 }
459 break;
460 }
461
462 case TOKEN_SEARCH_ORDER: {
463 long number = -1;
464
465 number = strtol(word, NULL, 0);
466 if (number < 0 || number > UINT32_MAX) break;
467 _dns_resolver_set_order(&res, (uint32_t)number);
468 break;
469 }
470
471 case TOKEN_SORTLIST: {
472 int n = 0;
473
474 while ((word != NULL) && (n++ < MAXRESOLVSORT)) {
475 dns_sortaddr_t *sortaddr;
476
477 sortaddr = _dnsinfo_parse_sortaddr(word);
478 if (sortaddr == NULL) break;
479 _dns_resolver_add_sortaddr(&res, sortaddr);
480 free(sortaddr);
481 word = strsep(&lineptr, sep);
482 }
483 break;
484 }
485
486 case TOKEN_TIMEOUT: {
487 long number = -1;
488
489 number = strtol(word, NULL, 0);
490 if (number < 0 || number > UINT32_MAX) break;
491 _dns_resolver_set_timeout(&res, (uint32_t)number);
492 break;
493 }
494 }
495 }
496 if (line != NULL) free(line);
497
498 // set the domain to the basename of the path if not specified
499 if ((res != NULL) && (token_count[TOKEN_DOMAIN] == 0)) {
500 const char *domain;
501
502 domain = strrchr(path, '/');
503 if (domain == NULL) {
504 domain = path;
505 } else {
506 domain = domain + 1;
507 }
508 _dns_resolver_set_domain(&res, domain);
509 }
510
511 if (res != NULL) {
512 // config flags should overwrite any default flags
513 if (config_flags != 0) {
514 _dns_resolver_set_flags(&res, config_flags);
515 } else {
516 _dnsinfo_flatfile_update_flags(&res);
517 }
518 }
519
520 done :
521
522 fclose(f);
523 return res;
524 }
525
526
527 /*
528 * _dnsinfo_flatfile_add_resolvers
529 *
530 * Parse the files in the resolver config directory (/etc/resolver) and add each
531 * resolver to the dns config.
532 */
533 __private_extern__
534 void
535 _dnsinfo_flatfile_add_resolvers(dns_create_config_t *config)
536 {
537 struct dirent *de;
538 DIR *dp;
539 dns_create_resolver_t res;
540
541 dp = opendir(_PATH_RESOLVER_DIR);
542 if (dp == NULL) {
543 return;
544 }
545
546 while ((de = readdir(dp)) != NULL) {
547 if (strcmp(de->d_name, ".") == 0 ||
548 strcmp(de->d_name, "..") == 0) continue;
549
550 res = _dnsinfo_flatfile_create_resolver(_PATH_RESOLVER_DIR, de->d_name);
551 if (res != NULL) {
552 _dns_configuration_add_resolver(config, res);
553 _dns_resolver_free(&res);
554 }
555 }
556
557 closedir(dp);
558 return;
559 }
560
561
562 #ifdef MAIN
563 #undef MAIN
564
565 #include "dnsinfo_copy.c"
566
567 int
568 main(int argc, char **argv)
569 {
570 uint8_t *buf;
571 dns_config_t *config;
572 dns_create_config_t create_config;
573 _dns_config_buf_t *config_buf;
574 uint32_t n_config;
575 uint32_t n_padding;
576 dns_create_resolver_t resolver;
577
578 resolver = _dnsinfo_flatfile_create_resolver(NULL, _PATH_RESCONF);
579
580 create_config = _dns_configuration_create();
581 _dnsinfo_flatfile_add_resolvers(&create_config);
582
583 config_buf = (_dns_config_buf_t *)create_config;
584 n_config = sizeof(_dns_config_buf_t) + ntohl(config_buf->n_attribute);
585 n_padding = ntohl(config_buf->n_padding);
586 buf = malloc(n_config + n_padding);
587 bcopy((void *)config_buf, buf, n_config);
588 bzero(&buf[n_config], n_padding);
589 config = _dns_configuration_expand_config((_dns_config_buf_t *)buf);
590
591 return 0;
592 }
593
594 #endif