2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: lwconfig.c,v 1.38.18.5 2006/10/03 23:50:51 marka Exp $ */
23 * Module for parsing resolv.conf files.
25 * lwres_conf_init() creates an empty lwres_conf_t structure for
26 * lightweight resolver context ctx.
28 * lwres_conf_clear() frees up all the internal memory used by that
29 * lwres_conf_t structure in resolver context ctx.
31 * lwres_conf_parse() opens the file filename and parses it to initialise
32 * the resolver context ctx's lwres_conf_t structure.
34 * lwres_conf_print() prints the lwres_conf_t structure for resolver
35 * context ctx to the FILE fp.
37 * \section lwconfig_return Return Values
39 * lwres_conf_parse() returns #LWRES_R_SUCCESS if it successfully read and
40 * parsed filename. It returns #LWRES_R_FAILURE if filename could not be
41 * opened or contained incorrect resolver statements.
43 * lwres_conf_print() returns #LWRES_R_SUCCESS unless an error occurred
44 * when converting the network addresses to a numeric host address
45 * string. If this happens, the function returns #LWRES_R_FAILURE.
47 * \section lwconfig_see See Also
49 * stdio(3), \link resolver resolver \endlink
51 * \section files Files
66 #include <lwres/lwbuffer.h>
67 #include <lwres/lwres.h>
68 #include <lwres/net.h>
69 #include <lwres/result.h>
72 #include "context_p.h"
75 #if ! defined(NS_INADDRSZ)
79 #if ! defined(NS_IN6ADDRSZ)
80 #define NS_IN6ADDRSZ 16
84 lwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
87 lwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp);
90 lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
93 lwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
96 lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
99 lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
102 lwres_resetaddr(lwres_addr_t *addr);
104 static lwres_result_t
105 lwres_create_addr(const char *buff, lwres_addr_t *addr, int convert_zero);
107 static int lwresaddr2af(int lwresaddrtype);
111 lwresaddr2af(int lwresaddrtype)
115 switch (lwresaddrtype) {
116 case LWRES_ADDRTYPE_V4:
120 case LWRES_ADDRTYPE_V6:
130 * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
137 while (ch != '\n' && ch != EOF)
145 * Eats white space up to next newline or non-whitespace character (of
146 * EOF). Returns the last character read. Comments are considered white
154 while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
157 if (ch == ';' || ch == '#')
165 * Skip over any leading whitespace and then read in the next sequence of
166 * non-whitespace characters. In this context newline is not considered
167 * whitespace. Returns EOF on end-of-file, or the character
168 * that caused the reading to stop.
171 getword(FILE *fp, char *buffer, size_t size) {
175 REQUIRE(buffer != NULL);
188 if (ch == EOF || isspace((unsigned char)ch))
190 else if ((size_t) (p - buffer) == size - 1)
191 return (EOF); /* Not enough space. */
201 lwres_resetaddr(lwres_addr_t *addr) {
202 REQUIRE(addr != NULL);
204 memset(addr->address, 0, LWRES_ADDR_MAXLEN);
210 lwres_strdup(lwres_context_t *ctx, const char *str) {
213 REQUIRE(str != NULL);
214 REQUIRE(strlen(str) > 0U);
216 p = CTXMALLOC(strlen(str) + 1);
223 /*% intializes data structure for subsequent config parsing. */
225 lwres_conf_init(lwres_context_t *ctx) {
227 lwres_conf_t *confdata;
229 REQUIRE(ctx != NULL);
230 confdata = &ctx->confdata;
232 confdata->nsnext = 0;
233 confdata->lwnext = 0;
234 confdata->domainname = NULL;
235 confdata->searchnxt = 0;
236 confdata->sortlistnxt = 0;
237 confdata->resdebug = 0;
239 confdata->no_tld_query = 0;
241 for (i = 0; i < LWRES_CONFMAXNAMESERVERS; i++)
242 lwres_resetaddr(&confdata->nameservers[i]);
244 for (i = 0; i < LWRES_CONFMAXSEARCH; i++)
245 confdata->search[i] = NULL;
247 for (i = 0; i < LWRES_CONFMAXSORTLIST; i++) {
248 lwres_resetaddr(&confdata->sortlist[i].addr);
249 lwres_resetaddr(&confdata->sortlist[i].mask);
253 /*% Frees up all the internal memory used by the config data structure, returning it to the lwres_context_t. */
255 lwres_conf_clear(lwres_context_t *ctx) {
257 lwres_conf_t *confdata;
259 REQUIRE(ctx != NULL);
260 confdata = &ctx->confdata;
262 for (i = 0; i < confdata->nsnext; i++)
263 lwres_resetaddr(&confdata->nameservers[i]);
265 if (confdata->domainname != NULL) {
266 CTXFREE(confdata->domainname,
267 strlen(confdata->domainname) + 1);
268 confdata->domainname = NULL;
271 for (i = 0; i < confdata->searchnxt; i++) {
272 if (confdata->search[i] != NULL) {
273 CTXFREE(confdata->search[i],
274 strlen(confdata->search[i]) + 1);
275 confdata->search[i] = NULL;
279 for (i = 0; i < LWRES_CONFMAXSORTLIST; i++) {
280 lwres_resetaddr(&confdata->sortlist[i].addr);
281 lwres_resetaddr(&confdata->sortlist[i].mask);
284 confdata->nsnext = 0;
285 confdata->lwnext = 0;
286 confdata->domainname = NULL;
287 confdata->searchnxt = 0;
288 confdata->sortlistnxt = 0;
289 confdata->resdebug = 0;
291 confdata->no_tld_query = 0;
294 static lwres_result_t
295 lwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
296 char word[LWRES_CONFMAXLINELEN];
298 lwres_conf_t *confdata;
299 lwres_addr_t address;
301 confdata = &ctx->confdata;
303 if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
304 return (LWRES_R_SUCCESS);
306 res = getword(fp, word, sizeof(word));
307 if (strlen(word) == 0U)
308 return (LWRES_R_FAILURE); /* Nothing on line. */
309 else if (res == ' ' || res == '\t')
312 if (res != EOF && res != '\n')
313 return (LWRES_R_FAILURE); /* Extra junk on line. */
315 res = lwres_create_addr(word, &address, 1);
316 if (res == LWRES_R_SUCCESS)
317 confdata->nameservers[confdata->nsnext++] = address;
319 return (LWRES_R_SUCCESS);
322 static lwres_result_t
323 lwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp) {
324 char word[LWRES_CONFMAXLINELEN];
326 lwres_conf_t *confdata;
328 confdata = &ctx->confdata;
330 if (confdata->lwnext == LWRES_CONFMAXLWSERVERS)
331 return (LWRES_R_SUCCESS);
333 res = getword(fp, word, sizeof(word));
334 if (strlen(word) == 0U)
335 return (LWRES_R_FAILURE); /* Nothing on line. */
336 else if (res == ' ' || res == '\t')
339 if (res != EOF && res != '\n')
340 return (LWRES_R_FAILURE); /* Extra junk on line. */
342 res = lwres_create_addr(word,
343 &confdata->lwservers[confdata->lwnext++], 1);
344 if (res != LWRES_R_SUCCESS)
347 return (LWRES_R_SUCCESS);
350 static lwres_result_t
351 lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
352 char word[LWRES_CONFMAXLINELEN];
354 lwres_conf_t *confdata;
356 confdata = &ctx->confdata;
358 res = getword(fp, word, sizeof(word));
359 if (strlen(word) == 0U)
360 return (LWRES_R_FAILURE); /* Nothing else on line. */
361 else if (res == ' ' || res == '\t')
364 if (res != EOF && res != '\n')
365 return (LWRES_R_FAILURE); /* Extra junk on line. */
367 if (confdata->domainname != NULL)
368 CTXFREE(confdata->domainname,
369 strlen(confdata->domainname) + 1); /* */
372 * Search and domain are mutually exclusive.
374 for (i = 0; i < LWRES_CONFMAXSEARCH; i++) {
375 if (confdata->search[i] != NULL) {
376 CTXFREE(confdata->search[i],
377 strlen(confdata->search[i])+1);
378 confdata->search[i] = NULL;
381 confdata->searchnxt = 0;
383 confdata->domainname = lwres_strdup(ctx, word);
385 if (confdata->domainname == NULL)
386 return (LWRES_R_FAILURE);
388 return (LWRES_R_SUCCESS);
391 static lwres_result_t
392 lwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
394 char word[LWRES_CONFMAXLINELEN];
395 lwres_conf_t *confdata;
397 confdata = &ctx->confdata;
399 if (confdata->domainname != NULL) {
401 * Search and domain are mutually exclusive.
403 CTXFREE(confdata->domainname,
404 strlen(confdata->domainname) + 1);
405 confdata->domainname = NULL;
409 * Remove any previous search definitions.
411 for (idx = 0; idx < LWRES_CONFMAXSEARCH; idx++) {
412 if (confdata->search[idx] != NULL) {
413 CTXFREE(confdata->search[idx],
414 strlen(confdata->search[idx])+1);
415 confdata->search[idx] = NULL;
418 confdata->searchnxt = 0;
420 delim = getword(fp, word, sizeof(word));
421 if (strlen(word) == 0U)
422 return (LWRES_R_FAILURE); /* Nothing else on line. */
425 while (strlen(word) > 0U) {
426 if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
427 goto ignore; /* Too many domains. */
429 confdata->search[idx] = lwres_strdup(ctx, word);
430 if (confdata->search[idx] == NULL)
431 return (LWRES_R_FAILURE);
433 confdata->searchnxt++;
436 if (delim == EOF || delim == '\n')
439 delim = getword(fp, word, sizeof(word));
442 return (LWRES_R_SUCCESS);
445 static lwres_result_t
446 lwres_create_addr(const char *buffer, lwres_addr_t *addr, int convert_zero) {
450 if (lwres_net_aton(buffer, &v4) == 1) {
452 unsigned char zeroaddress[] = {0, 0, 0, 0};
453 unsigned char loopaddress[] = {127, 0, 0, 1};
454 if (memcmp(&v4, zeroaddress, 4) == 0)
455 memcpy(&v4, loopaddress, 4);
457 addr->family = LWRES_ADDRTYPE_V4;
458 addr->length = NS_INADDRSZ;
459 memcpy((void *)addr->address, &v4, NS_INADDRSZ);
461 } else if (lwres_net_pton(AF_INET6, buffer, &v6) == 1) {
462 addr->family = LWRES_ADDRTYPE_V6;
463 addr->length = NS_IN6ADDRSZ;
464 memcpy((void *)addr->address, &v6, NS_IN6ADDRSZ);
466 return (LWRES_R_FAILURE); /* Unrecognised format. */
469 return (LWRES_R_SUCCESS);
472 static lwres_result_t
473 lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
475 char word[LWRES_CONFMAXLINELEN];
477 lwres_conf_t *confdata;
479 confdata = &ctx->confdata;
481 delim = getword(fp, word, sizeof(word));
482 if (strlen(word) == 0U)
483 return (LWRES_R_FAILURE); /* Empty line after keyword. */
485 while (strlen(word) > 0U) {
486 if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
487 return (LWRES_R_FAILURE); /* Too many values. */
489 p = strchr(word, '/');
493 idx = confdata->sortlistnxt;
494 res = lwres_create_addr(word, &confdata->sortlist[idx].addr, 1);
495 if (res != LWRES_R_SUCCESS)
499 res = lwres_create_addr(p,
500 &confdata->sortlist[idx].mask,
502 if (res != LWRES_R_SUCCESS)
508 confdata->sortlist[idx].mask =
509 confdata->sortlist[idx].addr;
511 memset(&confdata->sortlist[idx].mask.address, 0xff,
512 confdata->sortlist[idx].addr.length);
515 confdata->sortlistnxt++;
517 if (delim == EOF || delim == '\n')
520 delim = getword(fp, word, sizeof(word));
523 return (LWRES_R_SUCCESS);
526 static lwres_result_t
527 lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
531 char word[LWRES_CONFMAXLINELEN];
532 lwres_conf_t *confdata;
534 REQUIRE(ctx != NULL);
535 confdata = &ctx->confdata;
537 delim = getword(fp, word, sizeof(word));
538 if (strlen(word) == 0U)
539 return (LWRES_R_FAILURE); /* Empty line after keyword. */
541 while (strlen(word) > 0U) {
542 if (strcmp("debug", word) == 0) {
543 confdata->resdebug = 1;
544 } else if (strcmp("no_tld_query", word) == 0) {
545 confdata->no_tld_query = 1;
546 } else if (strncmp("ndots:", word, 6) == 0) {
547 ndots = strtol(word + 6, &p, 10);
548 if (*p != '\0') /* Bad string. */
549 return (LWRES_R_FAILURE);
550 if (ndots < 0 || ndots > 0xff) /* Out of range. */
551 return (LWRES_R_FAILURE);
552 confdata->ndots = (lwres_uint8_t)ndots;
555 if (delim == EOF || delim == '\n')
558 delim = getword(fp, word, sizeof(word));
561 return (LWRES_R_SUCCESS);
564 /*% parses a file and fills in the data structure. */
566 lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
569 lwres_result_t rval, ret;
570 lwres_conf_t *confdata;
573 REQUIRE(ctx != NULL);
574 confdata = &ctx->confdata;
576 REQUIRE(filename != NULL);
577 REQUIRE(strlen(filename) > 0U);
578 REQUIRE(confdata != NULL);
581 if ((fp = fopen(filename, "r")) == NULL)
582 return (LWRES_R_NOTFOUND);
584 ret = LWRES_R_SUCCESS;
586 stopchar = getword(fp, word, sizeof(word));
587 if (stopchar == EOF) {
588 rval = LWRES_R_SUCCESS;
592 if (strlen(word) == 0U)
593 rval = LWRES_R_SUCCESS;
594 else if (strcmp(word, "nameserver") == 0)
595 rval = lwres_conf_parsenameserver(ctx, fp);
596 else if (strcmp(word, "lwserver") == 0)
597 rval = lwres_conf_parselwserver(ctx, fp);
598 else if (strcmp(word, "domain") == 0)
599 rval = lwres_conf_parsedomain(ctx, fp);
600 else if (strcmp(word, "search") == 0)
601 rval = lwres_conf_parsesearch(ctx, fp);
602 else if (strcmp(word, "sortlist") == 0)
603 rval = lwres_conf_parsesortlist(ctx, fp);
604 else if (strcmp(word, "options") == 0)
605 rval = lwres_conf_parseoption(ctx, fp);
607 /* unrecognised word. Ignore entire line */
608 rval = LWRES_R_SUCCESS;
609 stopchar = eatline(fp);
610 if (stopchar == EOF) {
614 if (ret == LWRES_R_SUCCESS && rval != LWRES_R_SUCCESS)
623 /*% Prints the config data structure to the FILE. */
625 lwres_conf_print(lwres_context_t *ctx, FILE *fp) {
628 char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
630 lwres_conf_t *confdata;
631 lwres_addr_t tmpaddr;
633 REQUIRE(ctx != NULL);
634 confdata = &ctx->confdata;
636 REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
638 for (i = 0; i < confdata->nsnext; i++) {
639 af = lwresaddr2af(confdata->nameservers[i].family);
641 p = lwres_net_ntop(af, confdata->nameservers[i].address,
644 return (LWRES_R_FAILURE);
646 fprintf(fp, "nameserver %s\n", tmp);
649 for (i = 0; i < confdata->lwnext; i++) {
650 af = lwresaddr2af(confdata->lwservers[i].family);
652 p = lwres_net_ntop(af, confdata->lwservers[i].address,
655 return (LWRES_R_FAILURE);
657 fprintf(fp, "lwserver %s\n", tmp);
660 if (confdata->domainname != NULL) {
661 fprintf(fp, "domain %s\n", confdata->domainname);
662 } else if (confdata->searchnxt > 0) {
663 REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
665 fprintf(fp, "search");
666 for (i = 0; i < confdata->searchnxt; i++)
667 fprintf(fp, " %s", confdata->search[i]);
671 REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
673 if (confdata->sortlistnxt > 0) {
674 fputs("sortlist", fp);
675 for (i = 0; i < confdata->sortlistnxt; i++) {
676 af = lwresaddr2af(confdata->sortlist[i].addr.family);
678 p = lwres_net_ntop(af,
679 confdata->sortlist[i].addr.address,
682 return (LWRES_R_FAILURE);
684 fprintf(fp, " %s", tmp);
686 tmpaddr = confdata->sortlist[i].mask;
687 memset(&tmpaddr.address, 0xff, tmpaddr.length);
689 if (memcmp(&tmpaddr.address,
690 confdata->sortlist[i].mask.address,
691 confdata->sortlist[i].mask.length) != 0) {
693 confdata->sortlist[i].mask.family);
696 confdata->sortlist[i].mask.address,
699 return (LWRES_R_FAILURE);
701 fprintf(fp, "/%s", tmp);
707 if (confdata->resdebug)
708 fprintf(fp, "options debug\n");
710 if (confdata->ndots > 0)
711 fprintf(fp, "options ndots:%d\n", confdata->ndots);
713 if (confdata->no_tld_query)
714 fprintf(fp, "options no_tld_query\n");
716 return (LWRES_R_SUCCESS);
719 /*% Returns a pointer to the current config structure. */
721 lwres_conf_get(lwres_context_t *ctx) {
722 REQUIRE(ctx != NULL);
724 return (&ctx->confdata);