]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/lib/lwres/lwconfig.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / lib / lwres / lwconfig.c
1 /*
2  * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: lwconfig.c,v 1.38.18.5 2006/10/03 23:50:51 marka Exp $ */
19
20 /*! \file */
21
22 /**
23  * Module for parsing resolv.conf files.
24  *
25  *    lwres_conf_init() creates an empty lwres_conf_t structure for
26  *    lightweight resolver context ctx.
27  * 
28  *    lwres_conf_clear() frees up all the internal memory used by that
29  *    lwres_conf_t structure in resolver context ctx.
30  * 
31  *    lwres_conf_parse() opens the file filename and parses it to initialise
32  *    the resolver context ctx's lwres_conf_t structure.
33  * 
34  *    lwres_conf_print() prints the lwres_conf_t structure for resolver
35  *    context ctx to the FILE fp.
36  * 
37  * \section lwconfig_return Return Values
38  * 
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.
42  * 
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.
46  * 
47  * \section lwconfig_see See Also
48  * 
49  *    stdio(3), \link resolver resolver \endlink
50  * 
51  * \section files Files
52  * 
53  *    /etc/resolv.conf
54  */
55
56 #include <config.h>
57
58 #include <assert.h>
59 #include <ctype.h>
60 #include <errno.h>
61 #include <stdlib.h>
62 #include <stdio.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include <lwres/lwbuffer.h>
67 #include <lwres/lwres.h>
68 #include <lwres/net.h>
69 #include <lwres/result.h>
70
71 #include "assert_p.h"
72 #include "context_p.h"
73
74
75 #if ! defined(NS_INADDRSZ)
76 #define NS_INADDRSZ      4
77 #endif
78
79 #if ! defined(NS_IN6ADDRSZ)
80 #define NS_IN6ADDRSZ    16
81 #endif
82
83 static lwres_result_t
84 lwres_conf_parsenameserver(lwres_context_t *ctx,  FILE *fp);
85
86 static lwres_result_t
87 lwres_conf_parselwserver(lwres_context_t *ctx,  FILE *fp);
88
89 static lwres_result_t
90 lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
91
92 static lwres_result_t
93 lwres_conf_parsesearch(lwres_context_t *ctx,  FILE *fp);
94
95 static lwres_result_t
96 lwres_conf_parsesortlist(lwres_context_t *ctx,  FILE *fp);
97
98 static lwres_result_t
99 lwres_conf_parseoption(lwres_context_t *ctx,  FILE *fp);
100
101 static void
102 lwres_resetaddr(lwres_addr_t *addr);
103
104 static lwres_result_t
105 lwres_create_addr(const char *buff, lwres_addr_t *addr, int convert_zero);
106
107 static int lwresaddr2af(int lwresaddrtype);
108
109
110 static int
111 lwresaddr2af(int lwresaddrtype)
112 {
113         int af = 0;
114
115         switch (lwresaddrtype) {
116         case LWRES_ADDRTYPE_V4:
117                 af = AF_INET;
118                 break;
119
120         case LWRES_ADDRTYPE_V6:
121                 af = AF_INET6;
122                 break;
123         }
124
125         return (af);
126 }
127
128
129 /*!
130  * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
131  */
132 static int
133 eatline(FILE *fp) {
134         int ch;
135
136         ch = fgetc(fp);
137         while (ch != '\n' && ch != EOF)
138                 ch = fgetc(fp);
139
140         return (ch);
141 }
142
143
144 /*!
145  * Eats white space up to next newline or non-whitespace character (of
146  * EOF). Returns the last character read. Comments are considered white
147  * space.
148  */
149 static int
150 eatwhite(FILE *fp) {
151         int ch;
152
153         ch = fgetc(fp);
154         while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
155                 ch = fgetc(fp);
156
157         if (ch == ';' || ch == '#')
158                 ch = eatline(fp);
159
160         return (ch);
161 }
162
163
164 /*!
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.
169  */
170 static int
171 getword(FILE *fp, char *buffer, size_t size) {
172         int ch;
173         char *p = buffer;
174
175         REQUIRE(buffer != NULL);
176         REQUIRE(size > 0U);
177
178         *p = '\0';
179
180         ch = eatwhite(fp);
181
182         if (ch == EOF)
183                 return (EOF);
184
185         do {
186                 *p = '\0';
187
188                 if (ch == EOF || isspace((unsigned char)ch))
189                         break;
190                 else if ((size_t) (p - buffer) == size - 1)
191                         return (EOF);   /* Not enough space. */
192
193                 *p++ = (char)ch;
194                 ch = fgetc(fp);
195         } while (1);
196
197         return (ch);
198 }
199
200 static void
201 lwres_resetaddr(lwres_addr_t *addr) {
202         REQUIRE(addr != NULL);
203
204         memset(addr->address, 0, LWRES_ADDR_MAXLEN);
205         addr->family = 0;
206         addr->length = 0;
207 }
208
209 static char *
210 lwres_strdup(lwres_context_t *ctx, const char *str) {
211         char *p;
212
213         REQUIRE(str != NULL);
214         REQUIRE(strlen(str) > 0U);
215
216         p = CTXMALLOC(strlen(str) + 1);
217         if (p != NULL)
218                 strcpy(p, str);
219
220         return (p);
221 }
222
223 /*% intializes data structure for subsequent config parsing. */
224 void
225 lwres_conf_init(lwres_context_t *ctx) {
226         int i;
227         lwres_conf_t *confdata;
228
229         REQUIRE(ctx != NULL);
230         confdata = &ctx->confdata;
231
232         confdata->nsnext = 0;
233         confdata->lwnext = 0;
234         confdata->domainname = NULL;
235         confdata->searchnxt = 0;
236         confdata->sortlistnxt = 0;
237         confdata->resdebug = 0;
238         confdata->ndots = 1;
239         confdata->no_tld_query = 0;
240
241         for (i = 0; i < LWRES_CONFMAXNAMESERVERS; i++)
242                 lwres_resetaddr(&confdata->nameservers[i]);
243
244         for (i = 0; i < LWRES_CONFMAXSEARCH; i++)
245                 confdata->search[i] = NULL;
246
247         for (i = 0; i < LWRES_CONFMAXSORTLIST; i++) {
248                 lwres_resetaddr(&confdata->sortlist[i].addr);
249                 lwres_resetaddr(&confdata->sortlist[i].mask);
250         }
251 }
252
253 /*% Frees up all the internal memory used by the config data structure, returning it to the lwres_context_t. */
254 void
255 lwres_conf_clear(lwres_context_t *ctx) {
256         int i;
257         lwres_conf_t *confdata;
258
259         REQUIRE(ctx != NULL);
260         confdata = &ctx->confdata;
261
262         for (i = 0; i < confdata->nsnext; i++)
263                 lwres_resetaddr(&confdata->nameservers[i]);
264
265         if (confdata->domainname != NULL) {
266                 CTXFREE(confdata->domainname,
267                         strlen(confdata->domainname) + 1);
268                 confdata->domainname = NULL;
269         }
270
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;
276                 }
277         }
278
279         for (i = 0; i < LWRES_CONFMAXSORTLIST; i++) {
280                 lwres_resetaddr(&confdata->sortlist[i].addr);
281                 lwres_resetaddr(&confdata->sortlist[i].mask);
282         }
283
284         confdata->nsnext = 0;
285         confdata->lwnext = 0;
286         confdata->domainname = NULL;
287         confdata->searchnxt = 0;
288         confdata->sortlistnxt = 0;
289         confdata->resdebug = 0;
290         confdata->ndots = 1;
291         confdata->no_tld_query = 0;
292 }
293
294 static lwres_result_t
295 lwres_conf_parsenameserver(lwres_context_t *ctx,  FILE *fp) {
296         char word[LWRES_CONFMAXLINELEN];
297         int res;
298         lwres_conf_t *confdata;
299         lwres_addr_t address;
300
301         confdata = &ctx->confdata;
302
303         if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
304                 return (LWRES_R_SUCCESS);
305
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')
310                 res = eatwhite(fp);
311
312         if (res != EOF && res != '\n')
313                 return (LWRES_R_FAILURE); /* Extra junk on line. */
314
315         res = lwres_create_addr(word, &address, 1);
316         if (res == LWRES_R_SUCCESS)
317                 confdata->nameservers[confdata->nsnext++] = address;
318
319         return (LWRES_R_SUCCESS);
320 }
321
322 static lwres_result_t
323 lwres_conf_parselwserver(lwres_context_t *ctx,  FILE *fp) {
324         char word[LWRES_CONFMAXLINELEN];
325         int res;
326         lwres_conf_t *confdata;
327
328         confdata = &ctx->confdata;
329
330         if (confdata->lwnext == LWRES_CONFMAXLWSERVERS)
331                 return (LWRES_R_SUCCESS);
332
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')
337                 res = eatwhite(fp);
338
339         if (res != EOF && res != '\n')
340                 return (LWRES_R_FAILURE); /* Extra junk on line. */
341
342         res = lwres_create_addr(word,
343                                 &confdata->lwservers[confdata->lwnext++], 1);
344         if (res != LWRES_R_SUCCESS)
345                 return (res);
346
347         return (LWRES_R_SUCCESS);
348 }
349
350 static lwres_result_t
351 lwres_conf_parsedomain(lwres_context_t *ctx,  FILE *fp) {
352         char word[LWRES_CONFMAXLINELEN];
353         int res, i;
354         lwres_conf_t *confdata;
355
356         confdata = &ctx->confdata;
357
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')
362                 res = eatwhite(fp);
363
364         if (res != EOF && res != '\n')
365                 return (LWRES_R_FAILURE); /* Extra junk on line. */
366
367         if (confdata->domainname != NULL)
368                 CTXFREE(confdata->domainname,
369                         strlen(confdata->domainname) + 1); /*  */
370
371         /*
372          * Search and domain are mutually exclusive.
373          */
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;
379                 }
380         }
381         confdata->searchnxt = 0;
382
383         confdata->domainname = lwres_strdup(ctx, word);
384
385         if (confdata->domainname == NULL)
386                 return (LWRES_R_FAILURE);
387
388         return (LWRES_R_SUCCESS);
389 }
390
391 static lwres_result_t
392 lwres_conf_parsesearch(lwres_context_t *ctx,  FILE *fp) {
393         int idx, delim;
394         char word[LWRES_CONFMAXLINELEN];
395         lwres_conf_t *confdata;
396
397         confdata = &ctx->confdata;
398
399         if (confdata->domainname != NULL) {
400                 /*
401                  * Search and domain are mutually exclusive.
402                  */
403                 CTXFREE(confdata->domainname,
404                         strlen(confdata->domainname) + 1);
405                 confdata->domainname = NULL;
406         }
407
408         /*
409          * Remove any previous search definitions.
410          */
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;
416                 }
417         }
418         confdata->searchnxt = 0;
419
420         delim = getword(fp, word, sizeof(word));
421         if (strlen(word) == 0U)
422                 return (LWRES_R_FAILURE); /* Nothing else on line. */
423
424         idx = 0;
425         while (strlen(word) > 0U) {
426                 if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
427                         goto ignore; /* Too many domains. */
428
429                 confdata->search[idx] = lwres_strdup(ctx, word);
430                 if (confdata->search[idx] == NULL)
431                         return (LWRES_R_FAILURE);
432                 idx++;
433                 confdata->searchnxt++;
434
435         ignore:
436                 if (delim == EOF || delim == '\n')
437                         break;
438                 else
439                         delim = getword(fp, word, sizeof(word));
440         }
441
442         return (LWRES_R_SUCCESS);
443 }
444
445 static lwres_result_t
446 lwres_create_addr(const char *buffer, lwres_addr_t *addr, int convert_zero) {
447         struct in_addr v4;
448         struct in6_addr v6;
449
450         if (lwres_net_aton(buffer, &v4) == 1) {
451                 if (convert_zero) {
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);
456                 }
457                 addr->family = LWRES_ADDRTYPE_V4;
458                 addr->length = NS_INADDRSZ;
459                 memcpy((void *)addr->address, &v4, NS_INADDRSZ);
460
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);
465         } else {
466                 return (LWRES_R_FAILURE); /* Unrecognised format. */
467         }
468
469         return (LWRES_R_SUCCESS);
470 }
471
472 static lwres_result_t
473 lwres_conf_parsesortlist(lwres_context_t *ctx,  FILE *fp) {
474         int delim, res, idx;
475         char word[LWRES_CONFMAXLINELEN];
476         char *p;
477         lwres_conf_t *confdata;
478
479         confdata = &ctx->confdata;
480
481         delim = getword(fp, word, sizeof(word));
482         if (strlen(word) == 0U)
483                 return (LWRES_R_FAILURE); /* Empty line after keyword. */
484
485         while (strlen(word) > 0U) {
486                 if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
487                         return (LWRES_R_FAILURE); /* Too many values. */
488
489                 p = strchr(word, '/');
490                 if (p != NULL)
491                         *p++ = '\0';
492
493                 idx = confdata->sortlistnxt;
494                 res = lwres_create_addr(word, &confdata->sortlist[idx].addr, 1);
495                 if (res != LWRES_R_SUCCESS)
496                         return (res);
497
498                 if (p != NULL) {
499                         res = lwres_create_addr(p,
500                                                 &confdata->sortlist[idx].mask,
501                                                 0);
502                         if (res != LWRES_R_SUCCESS)
503                                 return (res);
504                 } else {
505                         /*
506                          * Make up a mask.
507                          */
508                         confdata->sortlist[idx].mask =
509                                 confdata->sortlist[idx].addr;
510
511                         memset(&confdata->sortlist[idx].mask.address, 0xff,
512                                confdata->sortlist[idx].addr.length);
513                 }
514
515                 confdata->sortlistnxt++;
516
517                 if (delim == EOF || delim == '\n')
518                         break;
519                 else
520                         delim = getword(fp, word, sizeof(word));
521         }
522
523         return (LWRES_R_SUCCESS);
524 }
525
526 static lwres_result_t
527 lwres_conf_parseoption(lwres_context_t *ctx,  FILE *fp) {
528         int delim;
529         long ndots;
530         char *p;
531         char word[LWRES_CONFMAXLINELEN];
532         lwres_conf_t *confdata;
533
534         REQUIRE(ctx != NULL);
535         confdata = &ctx->confdata;
536
537         delim = getword(fp, word, sizeof(word));
538         if (strlen(word) == 0U)
539                 return (LWRES_R_FAILURE); /* Empty line after keyword. */
540
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;
553                 }
554
555                 if (delim == EOF || delim == '\n')
556                         break;
557                 else
558                         delim = getword(fp, word, sizeof(word));
559         }
560
561         return (LWRES_R_SUCCESS);
562 }
563
564 /*% parses a file and fills in the data structure. */
565 lwres_result_t
566 lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
567         FILE *fp = NULL;
568         char word[256];
569         lwres_result_t rval, ret;
570         lwres_conf_t *confdata;
571         int stopchar;
572
573         REQUIRE(ctx != NULL);
574         confdata = &ctx->confdata;
575
576         REQUIRE(filename != NULL);
577         REQUIRE(strlen(filename) > 0U);
578         REQUIRE(confdata != NULL);
579
580         errno = 0;
581         if ((fp = fopen(filename, "r")) == NULL)
582                 return (LWRES_R_NOTFOUND);
583
584         ret = LWRES_R_SUCCESS;
585         do {
586                 stopchar = getword(fp, word, sizeof(word));
587                 if (stopchar == EOF) {
588                         rval = LWRES_R_SUCCESS;
589                         break;
590                 }
591
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);
606                 else {
607                         /* unrecognised word. Ignore entire line */
608                         rval = LWRES_R_SUCCESS;
609                         stopchar = eatline(fp);
610                         if (stopchar == EOF) {
611                                 break;
612                         }
613                 }
614                 if (ret == LWRES_R_SUCCESS && rval != LWRES_R_SUCCESS)
615                         ret = rval;
616         } while (1);
617
618         fclose(fp);
619
620         return (ret);
621 }
622
623 /*% Prints the config data structure to the FILE. */
624 lwres_result_t
625 lwres_conf_print(lwres_context_t *ctx, FILE *fp) {
626         int i;
627         int af;
628         char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
629         const char *p;
630         lwres_conf_t *confdata;
631         lwres_addr_t tmpaddr;
632
633         REQUIRE(ctx != NULL);
634         confdata = &ctx->confdata;
635
636         REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
637
638         for (i = 0; i < confdata->nsnext; i++) {
639                 af = lwresaddr2af(confdata->nameservers[i].family);
640
641                 p = lwres_net_ntop(af, confdata->nameservers[i].address,
642                                    tmp, sizeof(tmp));
643                 if (p != tmp)
644                         return (LWRES_R_FAILURE);
645
646                 fprintf(fp, "nameserver %s\n", tmp);
647         }
648
649         for (i = 0; i < confdata->lwnext; i++) {
650                 af = lwresaddr2af(confdata->lwservers[i].family);
651
652                 p = lwres_net_ntop(af, confdata->lwservers[i].address,
653                                    tmp, sizeof(tmp));
654                 if (p != tmp)
655                         return (LWRES_R_FAILURE);
656
657                 fprintf(fp, "lwserver %s\n", tmp);
658         }
659
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);
664
665                 fprintf(fp, "search");
666                 for (i = 0; i < confdata->searchnxt; i++)
667                         fprintf(fp, " %s", confdata->search[i]);
668                 fputc('\n', fp);
669         }
670
671         REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
672
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);
677
678                         p = lwres_net_ntop(af,
679                                            confdata->sortlist[i].addr.address,
680                                            tmp, sizeof(tmp));
681                         if (p != tmp)
682                                 return (LWRES_R_FAILURE);
683
684                         fprintf(fp, " %s", tmp);
685
686                         tmpaddr = confdata->sortlist[i].mask;
687                         memset(&tmpaddr.address, 0xff, tmpaddr.length);
688
689                         if (memcmp(&tmpaddr.address,
690                                    confdata->sortlist[i].mask.address,
691                                    confdata->sortlist[i].mask.length) != 0) {
692                                 af = lwresaddr2af(
693                                             confdata->sortlist[i].mask.family);
694                                 p = lwres_net_ntop
695                                         (af,
696                                          confdata->sortlist[i].mask.address,
697                                          tmp, sizeof(tmp));
698                                 if (p != tmp)
699                                         return (LWRES_R_FAILURE);
700
701                                 fprintf(fp, "/%s", tmp);
702                         }
703                 }
704                 fputc('\n', fp);
705         }
706
707         if (confdata->resdebug)
708                 fprintf(fp, "options debug\n");
709
710         if (confdata->ndots > 0)
711                 fprintf(fp, "options ndots:%d\n", confdata->ndots);
712
713         if (confdata->no_tld_query)
714                 fprintf(fp, "options no_tld_query\n");
715
716         return (LWRES_R_SUCCESS);
717 }
718
719 /*% Returns a pointer to the current config structure. */
720 lwres_conf_t *
721 lwres_conf_get(lwres_context_t *ctx) {
722         REQUIRE(ctx != NULL);
723
724         return (&ctx->confdata);
725 }