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