Commit | Line | Data |
---|---|---|
e576be50 | 1 | /* |
2 | * setup.c | |
3 | * - configuration file parsing | |
4 | * - management of global state | |
5 | */ | |
6 | /* | |
ae8cc977 | 7 | * This file is part of adns, which is |
26e1c3d6 | 8 | * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson |
17cd4f48 | 9 | * Copyright (C) 2014 Mark Wooding |
ae8cc977 | 10 | * Copyright (C) 1999-2000,2003,2006 Tony Finch |
11 | * Copyright (C) 1991 Massachusetts Institute of Technology | |
12 | * (See the file INSTALL for full details.) | |
e576be50 | 13 | * |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
7f8bbe29 | 16 | * the Free Software Foundation; either version 3, or (at your option) |
e576be50 | 17 | * any later version. |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
8c09a4c6 | 25 | * along with this program; if not, write to the Free Software Foundation. |
e576be50 | 26 | */ |
656b2da9 | 27 | |
4353a5c4 | 28 | #include <stdlib.h> |
29 | #include <errno.h> | |
78bcc172 | 30 | #include <limits.h> |
4353a5c4 | 31 | #include <unistd.h> |
32 | #include <fcntl.h> | |
656b2da9 | 33 | |
5aabad0d | 34 | #include <sys/types.h> |
4353a5c4 | 35 | #include <netdb.h> |
71a6ff46 | 36 | #include <sys/socket.h> |
37 | #include <netinet/in.h> | |
4353a5c4 | 38 | #include <arpa/inet.h> |
39 | ||
40 | #include "internal.h" | |
41 | ||
fb7fbb66 | 42 | static void readconfig(adns_state ads, const char *filename, int warnmissing); |
36369543 | 43 | |
c32f3e30 | 44 | static void addserver(adns_state ads, const struct sockaddr *sa, int salen) { |
656b2da9 | 45 | int i; |
f930c455 | 46 | adns_rr_addr *ss; |
b0e8338a | 47 | char buf[ADNS_ADDR2TEXT_BUFLEN]; |
656b2da9 | 48 | |
49 | for (i=0; i<ads->nservers; i++) { | |
82ae109e | 50 | if (adns__sockaddrs_equal(sa, &ads->servers[i].addr.sa)) { |
f930c455 | 51 | adns__debug(ads,-1,0,"duplicate nameserver %s ignored", |
b0e8338a | 52 | adns__sockaddr_ntoa(sa, buf)); |
656b2da9 | 53 | return; |
54 | } | |
55 | } | |
56 | ||
57 | if (ads->nservers>=MAXSERVERS) { | |
f930c455 | 58 | adns__diag(ads,-1,0,"too many nameservers, ignoring %s", |
b0e8338a | 59 | adns__sockaddr_ntoa(sa, buf)); |
656b2da9 | 60 | return; |
61 | } | |
62 | ||
63 | ss= ads->servers+ads->nservers; | |
c32f3e30 IJ |
64 | assert(salen <= sizeof(ss->addr)); |
65 | ss->len = salen; | |
66 | memcpy(&ss->addr, sa, salen); | |
656b2da9 | 67 | ads->nservers++; |
68 | } | |
69 | ||
914a5ff5 | 70 | static void freesearchlist(adns_state ads) { |
71 | if (ads->nsearchlist) free(*ads->searchlist); | |
72 | free(ads->searchlist); | |
73 | } | |
74 | ||
36369543 | 75 | static void saveerr(adns_state ads, int en) { |
76 | if (!ads->configerrno) ads->configerrno= en; | |
77 | } | |
78 | ||
656b2da9 | 79 | static void configparseerr(adns_state ads, const char *fn, int lno, |
80 | const char *fmt, ...) { | |
81 | va_list al; | |
36369543 | 82 | |
83 | saveerr(ads,EINVAL); | |
d3a102c4 | 84 | if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return; |
36369543 | 85 | |
d3a102c4 | 86 | if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn); |
87 | else adns__lprintf(ads,"adns: %s:%d: ",fn,lno); | |
656b2da9 | 88 | va_start(al,fmt); |
d3a102c4 | 89 | adns__vlprintf(ads,fmt,al); |
656b2da9 | 90 | va_end(al); |
d3a102c4 | 91 | adns__lprintf(ads,"\n"); |
656b2da9 | 92 | } |
93 | ||
32af6b2a | 94 | static int nextword(const char **bufp_io, const char **word_r, int *l_r) { |
95 | const char *p, *q; | |
96 | ||
97 | p= *bufp_io; | |
98 | while (ctype_whitespace(*p)) p++; | |
99 | if (!*p) return 0; | |
100 | ||
101 | q= p; | |
102 | while (*q && !ctype_whitespace(*q)) q++; | |
103 | ||
104 | *l_r= q-p; | |
105 | *word_r= p; | |
106 | *bufp_io= q; | |
107 | ||
108 | return 1; | |
109 | } | |
110 | ||
609133ee | 111 | static void ccf_nameserver(adns_state ads, const char *fn, |
112 | int lno, const char *buf) { | |
b0e8338a MW |
113 | adns_rr_addr a; |
114 | char addrbuf[ADNS_ADDR2TEXT_BUFLEN]; | |
115 | int err; | |
116 | ||
117 | a.len= sizeof(a.addr); | |
118 | err= adns_text2addr(buf,DNS_PORT, 0, &a.addr.sa,&a.len); | |
119 | switch (err) { | |
120 | case 0: | |
121 | break; | |
122 | case EINVAL: | |
656b2da9 | 123 | configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf); |
124 | return; | |
b0e8338a MW |
125 | default: |
126 | configparseerr(ads,fn,lno,"failed to parse nameserver address `%s': %s", | |
127 | buf,strerror(err)); | |
128 | return; | |
656b2da9 | 129 | } |
b0e8338a MW |
130 | adns__debug(ads,-1,0,"using nameserver %s", |
131 | adns__sockaddr_ntoa(&a.addr.sa, addrbuf)); | |
132 | addserver(ads,&a.addr.sa,a.len); | |
656b2da9 | 133 | } |
134 | ||
609133ee | 135 | static void ccf_search(adns_state ads, const char *fn, |
136 | int lno, const char *buf) { | |
32af6b2a | 137 | const char *bufp, *word; |
138 | char *newchars, **newptrs, **pp; | |
139 | int count, tl, l; | |
140 | ||
656b2da9 | 141 | if (!buf) return; |
32af6b2a | 142 | |
143 | bufp= buf; | |
144 | count= 0; | |
145 | tl= 0; | |
146 | while (nextword(&bufp,&word,&l)) { count++; tl += l+1; } | |
147 | ||
609133ee | 148 | newptrs= malloc(sizeof(char*)*count); |
149 | if (!newptrs) { saveerr(ads,errno); return; } | |
150 | ||
151 | newchars= malloc(tl); | |
152 | if (!newchars) { saveerr(ads,errno); free(newptrs); return; } | |
32af6b2a | 153 | |
154 | bufp= buf; | |
155 | pp= newptrs; | |
156 | while (nextword(&bufp,&word,&l)) { | |
157 | *pp++= newchars; | |
158 | memcpy(newchars,word,l); | |
159 | newchars += l; | |
160 | *newchars++ = 0; | |
161 | } | |
162 | ||
914a5ff5 | 163 | freesearchlist(ads); |
32af6b2a | 164 | ads->nsearchlist= count; |
165 | ads->searchlist= newptrs; | |
656b2da9 | 166 | } |
167 | ||
82ae109e | 168 | static int gen_pton(const char *text, int want_af, adns_sockaddr *a) { |
b0e8338a | 169 | int err; |
82ae109e | 170 | int len; |
b0e8338a | 171 | |
82ae109e | 172 | len= sizeof(*a); |
b0e8338a | 173 | err= adns_text2addr(text,0, adns_qf_addrlit_scope_forbid, |
82ae109e | 174 | &a->sa, &len); |
b0e8338a | 175 | if (err) { assert(err == EINVAL); return 0; } |
82ae109e | 176 | if (want_af != AF_UNSPEC && a->sa.sa_family != want_af) return 0; |
b0e8338a MW |
177 | return 1; |
178 | } | |
179 | ||
609133ee | 180 | static void ccf_sortlist(adns_state ads, const char *fn, |
181 | int lno, const char *buf) { | |
32af6b2a | 182 | const char *word; |
09957b1c | 183 | char tbuf[200], *slash, *ep; |
95bee3e1 | 184 | const char *maskwhat; |
07554ccd | 185 | struct sortlist *sl; |
09957b1c | 186 | int l; |
07554ccd | 187 | int initial= -1; |
09957b1c | 188 | |
32af6b2a | 189 | if (!buf) return; |
190 | ||
09957b1c | 191 | ads->nsortlist= 0; |
32af6b2a | 192 | while (nextword(&buf,&word,&l)) { |
09957b1c | 193 | if (ads->nsortlist >= MAXSORTLIST) { |
609133ee | 194 | adns__diag(ads,-1,0,"too many sortlist entries," |
195 | " ignoring %.*s onwards",l,word); | |
09957b1c | 196 | return; |
197 | } | |
198 | ||
199 | if (l >= sizeof(tbuf)) { | |
32af6b2a | 200 | configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word); |
09957b1c | 201 | continue; |
202 | } | |
203 | ||
78bcc172 | 204 | memcpy(tbuf,word,l); tbuf[l]= 0; |
09957b1c | 205 | slash= strchr(tbuf,'/'); |
206 | if (slash) *slash++= 0; | |
07554ccd MW |
207 | |
208 | sl= &ads->sortlist[ads->nsortlist]; | |
82ae109e | 209 | if (!gen_pton(tbuf, AF_UNSPEC, &sl->base)) { |
09957b1c | 210 | configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf); |
211 | continue; | |
212 | } | |
213 | ||
214 | if (slash) { | |
07554ccd | 215 | if (slash[strspn(slash, "0123456789")]) { |
95bee3e1 | 216 | maskwhat = "mask"; |
82ae109e | 217 | if (!gen_pton(slash, sl->base.sa.sa_family, &sl->mask)) { |
09957b1c | 218 | configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash); |
219 | continue; | |
220 | } | |
09957b1c | 221 | } else { |
95bee3e1 | 222 | maskwhat = "prefix length"; |
09957b1c | 223 | initial= strtoul(slash,&ep,10); |
82ae109e | 224 | if (*ep || initial>adns__addr_width(sl->base.sa.sa_family)) { |
09957b1c | 225 | configparseerr(ads,fn,lno,"mask length `%s' invalid",slash); |
226 | continue; | |
227 | } | |
82ae109e IJ |
228 | sl->mask.sa.sa_family= sl->base.sa.sa_family; |
229 | adns__prefix_mask(&sl->mask, initial); | |
09957b1c | 230 | } |
231 | } else { | |
07554ccd | 232 | maskwhat = "implied prefix length"; |
82ae109e | 233 | initial= adns__guess_prefix_length(&sl->base); |
07554ccd | 234 | if (initial < 0) { |
609133ee | 235 | configparseerr(ads,fn,lno, "network address `%s'" |
236 | " in sortlist is not in classed ranges," | |
09957b1c | 237 | " must specify mask explicitly", tbuf); |
238 | continue; | |
239 | } | |
82ae109e IJ |
240 | sl->mask.sa.sa_family= sl->base.sa.sa_family; |
241 | adns__prefix_mask(&sl->mask, initial); | |
09957b1c | 242 | } |
243 | ||
82ae109e IJ |
244 | if (!adns__addr_matches(sl->base.sa.sa_family, |
245 | adns__sockaddr_addr(&sl->base.sa), | |
246 | &sl->base,&sl->mask)) { | |
07554ccd MW |
247 | if (initial >= 0) { |
248 | configparseerr(ads,fn,lno, "%s %d in sortlist" | |
249 | " overlaps address `%s'",maskwhat,initial,tbuf); | |
250 | } else { | |
251 | configparseerr(ads,fn,lno, "%s `%s' in sortlist" | |
252 | " overlaps address `%s'",maskwhat,slash,tbuf); | |
253 | } | |
95bee3e1 MW |
254 | continue; |
255 | } | |
256 | ||
09957b1c | 257 | ads->nsortlist++; |
258 | } | |
656b2da9 | 259 | } |
260 | ||
609133ee | 261 | static void ccf_options(adns_state ads, const char *fn, |
262 | int lno, const char *buf) { | |
229f72fa | 263 | const char *word, *rhs; |
78bcc172 | 264 | char *ep; |
265 | unsigned long v; | |
138722f0 | 266 | int i,l; |
78bcc172 | 267 | |
656b2da9 | 268 | if (!buf) return; |
78bcc172 | 269 | |
229f72fa IJ |
270 | #define OPTION__IS(s,op) (l op (sizeof(s)-1) && !memcmp(word,s,(sizeof(s)-1))) |
271 | #define OPTION_IS(s) (OPTION__IS(s,==)) | |
272 | #define OPTION_STARTS(s) (OPTION__IS(s,>=) ? ((rhs=word+sizeof(s)-1)) : 0) | |
273 | ||
78bcc172 | 274 | while (nextword(&buf,&word,&l)) { |
229f72fa | 275 | if (OPTION_IS("debug")) { |
78bcc172 | 276 | ads->iflags |= adns_if_debug; |
277 | continue; | |
278 | } | |
229f72fa IJ |
279 | if (OPTION_STARTS("ndots:")) { |
280 | v= strtoul(rhs,&ep,10); | |
281 | if (ep==rhs || ep != word+l || v > INT_MAX) { | |
609133ee | 282 | configparseerr(ads,fn,lno,"option `%.*s' malformed" |
283 | " or has bad value",l,word); | |
78bcc172 | 284 | continue; |
285 | } | |
286 | ads->searchndots= v; | |
287 | continue; | |
288 | } | |
229f72fa IJ |
289 | if (OPTION_STARTS("adns_checkc:")) { |
290 | if (!strcmp(rhs,"none")) { | |
3e2e5fab | 291 | ads->iflags &= ~adns_if_checkc_freq; |
292 | ads->iflags |= adns_if_checkc_entex; | |
229f72fa | 293 | } else if (!strcmp(rhs,"entex")) { |
3e2e5fab | 294 | ads->iflags &= ~adns_if_checkc_freq; |
295 | ads->iflags |= adns_if_checkc_entex; | |
229f72fa | 296 | } else if (!strcmp(rhs,"freq")) { |
3e2e5fab | 297 | ads->iflags |= adns_if_checkc_freq; |
298 | } else { | |
299 | configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' " | |
229f72fa | 300 | "(must be none, entex or freq", rhs); |
3e2e5fab | 301 | } |
302 | continue; | |
303 | } | |
229f72fa IJ |
304 | if (OPTION_STARTS("adns_af:")) { |
305 | word= rhs; | |
138722f0 MW |
306 | ads->iflags &= ~adns_if_afmask; |
307 | if (strcmp(word,"any")) for (;;) { | |
308 | i= strcspn(word,","); | |
309 | if (i>=4 && !memcmp(word,"ipv4",4)) | |
310 | ads->iflags |= adns_if_permit_ipv4; | |
311 | else if (i>=4 && !memcmp(word,"ipv6",4)) | |
312 | ads->iflags |= adns_if_permit_ipv6; | |
313 | else { | |
314 | configparseerr(ads,fn,lno, "option adns_af has bad value `%.*s' " | |
315 | "(must be `any' or list {`ipv4',`ipv6'},...)", | |
316 | i, word); | |
317 | break; | |
318 | } | |
319 | if (!word[i]) break; | |
320 | word= word + i + 1; | |
321 | } | |
322 | continue; | |
323 | } | |
78bcc172 | 324 | adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word); |
325 | } | |
229f72fa IJ |
326 | |
327 | #undef OPTION__IS | |
328 | #undef OPTION_IS | |
329 | #undef OPTION_STARTS | |
656b2da9 | 330 | } |
331 | ||
609133ee | 332 | static void ccf_clearnss(adns_state ads, const char *fn, |
333 | int lno, const char *buf) { | |
656b2da9 | 334 | ads->nservers= 0; |
335 | } | |
336 | ||
609133ee | 337 | static void ccf_include(adns_state ads, const char *fn, |
338 | int lno, const char *buf) { | |
36369543 | 339 | if (!*buf) { |
340 | configparseerr(ads,fn,lno,"`include' directive with no filename"); | |
341 | return; | |
342 | } | |
fb7fbb66 | 343 | readconfig(ads,buf,1); |
36369543 | 344 | } |
345 | ||
0f15dd7b | 346 | static void ccf_lookup(adns_state ads, const char *fn, int lno, |
347 | const char *buf) { | |
348 | int found_bind=0; | |
349 | const char *word; | |
350 | int l; | |
351 | ||
352 | if (!*buf) { | |
353 | configparseerr(ads,fn,lno,"`lookup' directive with no databases"); | |
354 | return; | |
355 | } | |
356 | ||
357 | while (nextword(&buf,&word,&l)) { | |
358 | if (l==4 && !memcmp(word,"bind",4)) { | |
359 | found_bind=1; | |
360 | } else if (l==4 && !memcmp(word,"file",4)) { | |
361 | /* ignore this and hope /etc/hosts is not essential */ | |
362 | } else if (l==2 && !memcmp(word,"yp",2)) { | |
363 | adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno); | |
364 | found_bind=-1; | |
365 | } else { | |
366 | adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'", | |
367 | fn,lno, l,word); | |
368 | found_bind=-1; | |
369 | } | |
370 | } | |
371 | if (!found_bind) | |
372 | adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno); | |
373 | } | |
374 | ||
656b2da9 | 375 | static const struct configcommandinfo { |
376 | const char *name; | |
377 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); | |
378 | } configcommandinfos[]= { | |
379 | { "nameserver", ccf_nameserver }, | |
380 | { "domain", ccf_search }, | |
381 | { "search", ccf_search }, | |
382 | { "sortlist", ccf_sortlist }, | |
383 | { "options", ccf_options }, | |
384 | { "clearnameservers", ccf_clearnss }, | |
36369543 | 385 | { "include", ccf_include }, |
0f15dd7b | 386 | { "lookup", ccf_lookup }, /* OpenBSD */ |
656b2da9 | 387 | { 0 } |
388 | }; | |
389 | ||
36369543 | 390 | typedef union { |
656b2da9 | 391 | FILE *file; |
36369543 | 392 | const char *text; |
393 | } getline_ctx; | |
656b2da9 | 394 | |
36369543 | 395 | static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename, |
396 | int lno, char *buf, int buflen) { | |
397 | FILE *file= src_io->file; | |
398 | int c, i; | |
399 | char *p; | |
400 | ||
401 | p= buf; | |
402 | buflen--; | |
403 | i= 0; | |
404 | ||
405 | for (;;) { /* loop over chars */ | |
406 | if (i == buflen) { | |
407 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); | |
408 | goto x_badline; | |
409 | } | |
410 | c= getc(file); | |
411 | if (!c) { | |
412 | adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno); | |
413 | goto x_badline; | |
414 | } else if (c == '\n') { | |
415 | break; | |
416 | } else if (c == EOF) { | |
417 | if (ferror(file)) { | |
418 | saveerr(ads,errno); | |
609133ee | 419 | adns__diag(ads,-1,0,"%s:%d: read error: %s", |
420 | filename,lno,strerror(errno)); | |
36369543 | 421 | return -1; |
422 | } | |
423 | if (!i) return -1; | |
424 | break; | |
425 | } else { | |
426 | *p++= c; | |
427 | i++; | |
656b2da9 | 428 | } |
656b2da9 | 429 | } |
430 | ||
36369543 | 431 | *p++= 0; |
432 | return i; | |
433 | ||
434 | x_badline: | |
435 | saveerr(ads,EINVAL); | |
436 | while ((c= getc(file)) != EOF && c != '\n'); | |
437 | return -2; | |
438 | } | |
439 | ||
440 | static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename, | |
441 | int lno, char *buf, int buflen) { | |
09957b1c | 442 | const char *cp= src_io->text; |
36369543 | 443 | int l; |
444 | ||
09957b1c | 445 | if (!cp || !*cp) return -1; |
36369543 | 446 | |
09957b1c | 447 | if (*cp == ';' || *cp == '\n') cp++; |
448 | l= strcspn(cp,";\n"); | |
449 | src_io->text = cp+l; | |
36369543 | 450 | |
451 | if (l >= buflen) { | |
452 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); | |
453 | saveerr(ads,EINVAL); | |
454 | return -2; | |
455 | } | |
456 | ||
457 | memcpy(buf,cp,l); | |
458 | buf[l]= 0; | |
459 | return l; | |
460 | } | |
461 | ||
462 | static void readconfiggeneric(adns_state ads, const char *filename, | |
463 | int (*getline)(adns_state ads, getline_ctx*, | |
464 | const char *filename, int lno, | |
465 | char *buf, int buflen), | |
466 | /* Returns >=0 for success, -1 for EOF or error | |
467 | * (error will have been reported), or -2 for | |
468 | * bad line was encountered, try again. | |
469 | */ | |
470 | getline_ctx gl_ctx) { | |
471 | char linebuf[2000], *p, *q; | |
472 | int lno, l, dirl; | |
473 | const struct configcommandinfo *ccip; | |
474 | ||
475 | for (lno=1; | |
476 | (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1; | |
477 | lno++) { | |
478 | if (l == -2) continue; | |
656b2da9 | 479 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
480 | linebuf[l]= 0; | |
481 | p= linebuf; | |
482 | while (ctype_whitespace(*p)) p++; | |
2d230487 | 483 | if (*p == '#' || *p == ';' || !*p) continue; |
656b2da9 | 484 | q= p; |
485 | while (*q && !ctype_whitespace(*q)) q++; | |
36369543 | 486 | dirl= q-p; |
656b2da9 | 487 | for (ccip=configcommandinfos; |
609133ee | 488 | ccip->name && |
489 | !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p)); | |
656b2da9 | 490 | ccip++); |
491 | if (!ccip->name) { | |
3955725c | 492 | adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'", |
0ca555c6 | 493 | filename,lno,(int)(q-p),p); |
656b2da9 | 494 | continue; |
495 | } | |
496 | while (ctype_whitespace(*q)) q++; | |
497 | ccip->fn(ads,filename,lno,q); | |
498 | } | |
656b2da9 | 499 | } |
500 | ||
501 | static const char *instrum_getenv(adns_state ads, const char *envvar) { | |
502 | const char *value; | |
503 | ||
504 | value= getenv(envvar); | |
3955725c | 505 | if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar); |
609133ee | 506 | else adns__debug(ads,-1,0,"environment variable %s" |
507 | " set to `%s'",envvar,value); | |
656b2da9 | 508 | return value; |
509 | } | |
510 | ||
fb7fbb66 | 511 | static void readconfig(adns_state ads, const char *filename, int warnmissing) { |
36369543 | 512 | getline_ctx gl_ctx; |
513 | ||
514 | gl_ctx.file= fopen(filename,"r"); | |
515 | if (!gl_ctx.file) { | |
516 | if (errno == ENOENT) { | |
fb7fbb66 | 517 | if (warnmissing) |
609133ee | 518 | adns__debug(ads,-1,0, "configuration file" |
519 | " `%s' does not exist",filename); | |
36369543 | 520 | return; |
521 | } | |
522 | saveerr(ads,errno); | |
523 | adns__diag(ads,-1,0,"cannot open configuration file `%s': %s", | |
524 | filename,strerror(errno)); | |
525 | return; | |
526 | } | |
527 | ||
528 | readconfiggeneric(ads,filename,gl_file,gl_ctx); | |
529 | ||
530 | fclose(gl_ctx.file); | |
531 | } | |
532 | ||
609133ee | 533 | static void readconfigtext(adns_state ads, const char *text, |
534 | const char *showname) { | |
36369543 | 535 | getline_ctx gl_ctx; |
536 | ||
537 | gl_ctx.text= text; | |
538 | readconfiggeneric(ads,showname,gl_text,gl_ctx); | |
539 | } | |
540 | ||
656b2da9 | 541 | static void readconfigenv(adns_state ads, const char *envvar) { |
542 | const char *filename; | |
543 | ||
544 | if (ads->iflags & adns_if_noenv) { | |
3955725c | 545 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
656b2da9 | 546 | return; |
547 | } | |
548 | filename= instrum_getenv(ads,envvar); | |
fb7fbb66 | 549 | if (filename) readconfig(ads,filename,1); |
656b2da9 | 550 | } |
4353a5c4 | 551 | |
09957b1c | 552 | static void readconfigenvtext(adns_state ads, const char *envvar) { |
553 | const char *textdata; | |
554 | ||
555 | if (ads->iflags & adns_if_noenv) { | |
556 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); | |
557 | return; | |
558 | } | |
559 | textdata= instrum_getenv(ads,envvar); | |
560 | if (textdata) readconfigtext(ads,textdata,envvar); | |
561 | } | |
562 | ||
4353a5c4 | 563 | |
564 | int adns__setnonblock(adns_state ads, int fd) { | |
565 | int r; | |
656b2da9 | 566 | |
4353a5c4 | 567 | r= fcntl(fd,F_GETFL,0); if (r<0) return errno; |
568 | r |= O_NONBLOCK; | |
569 | r= fcntl(fd,F_SETFL,r); if (r<0) return errno; | |
570 | return 0; | |
571 | } | |
572 | ||
609133ee | 573 | static int init_begin(adns_state *ads_r, adns_initflags flags, |
d3a102c4 | 574 | adns_logcallbackfn *logfn, void *logfndata) { |
656b2da9 | 575 | adns_state ads; |
0e45654b | 576 | pid_t pid; |
656b2da9 | 577 | |
039a0566 IJ |
578 | if (flags & ~(adns_initflags)(0x4fff)) |
579 | /* 0x4000 is reserved for `harmless' future expansion */ | |
580 | return ENOSYS; | |
581 | ||
656b2da9 | 582 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
36369543 | 583 | |
656b2da9 | 584 | ads->iflags= flags; |
d3a102c4 | 585 | ads->logfn= logfn; |
586 | ads->logfndata= logfndata; | |
d855b532 | 587 | ads->configerrno= 0; |
f7f83b4a | 588 | LIST_INIT(ads->udpw); |
589 | LIST_INIT(ads->tcpw); | |
4353a5c4 | 590 | LIST_INIT(ads->childw); |
591 | LIST_INIT(ads->output); | |
b582ca0f | 592 | LIST_INIT(ads->intdone); |
8ce38e76 | 593 | ads->forallnext= 0; |
4353a5c4 | 594 | ads->nextid= 0x311f; |
97a937c8 | 595 | ads->nudpsockets= 0; |
3eb20edd | 596 | ads->tcpsocket= -1; |
4353a5c4 | 597 | adns__vbuf_init(&ads->tcpsend); |
598 | adns__vbuf_init(&ads->tcprecv); | |
70ad7a2a | 599 | ads->tcprecv_skip= 0; |
32af6b2a | 600 | ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0; |
660d7d3b | 601 | ads->searchndots= 1; |
d855b532 | 602 | ads->tcpstate= server_disconnected; |
4353a5c4 | 603 | timerclear(&ads->tcptimeout); |
d855b532 | 604 | ads->searchlist= 0; |
656b2da9 | 605 | |
0e45654b | 606 | pid= getpid(); |
607 | ads->rand48xsubi[0]= pid; | |
608 | ads->rand48xsubi[1]= (unsigned long)pid >> 16; | |
609 | ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16); | |
610 | ||
36369543 | 611 | *ads_r= ads; |
612 | return 0; | |
613 | } | |
656b2da9 | 614 | |
36369543 | 615 | static int init_finish(adns_state ads) { |
f930c455 | 616 | struct sockaddr_in sin; |
36369543 | 617 | struct protoent *proto; |
3eb20edd | 618 | struct udpsocket *udp; |
27c763d5 | 619 | int i; |
3eb20edd | 620 | int r; |
36369543 | 621 | |
656b2da9 | 622 | if (!ads->nservers) { |
d3a102c4 | 623 | if (ads->logfn && ads->iflags & adns_if_debug) |
f930c455 MW |
624 | adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n"); |
625 | memset(&sin, 0, sizeof(sin)); | |
626 | sin.sin_family = AF_INET; | |
627 | sin.sin_port = htons(DNS_PORT); | |
628 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
629 | addserver(ads,(struct sockaddr *)&sin, sizeof(sin)); | |
656b2da9 | 630 | } |
631 | ||
632 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } | |
97a937c8 | 633 | ads->nudpsockets= 0; |
3eb20edd MW |
634 | for (i=0; i<ads->nservers; i++) { |
635 | if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family)) | |
636 | continue; | |
97a937c8 IJ |
637 | assert(ads->nudpsockets < MAXUDP); |
638 | udp= &ads->udpsockets[ads->nudpsockets]; | |
3eb20edd MW |
639 | udp->af= ads->servers[i].addr.sa.sa_family; |
640 | udp->fd= socket(udp->af,SOCK_DGRAM,proto->p_proto); | |
641 | if (udp->fd < 0) { r= errno; goto x_free; } | |
97a937c8 | 642 | ads->nudpsockets++; |
3eb20edd MW |
643 | r= adns__setnonblock(ads,udp->fd); |
644 | if (r) { r= errno; goto x_closeudp; } | |
645 | } | |
656b2da9 | 646 | |
656b2da9 | 647 | return 0; |
648 | ||
94436798 | 649 | x_closeudp: |
27c763d5 | 650 | for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd); |
656b2da9 | 651 | x_free: |
652 | free(ads); | |
653 | return r; | |
654 | } | |
655 | ||
32af6b2a | 656 | static void init_abort(adns_state ads) { |
657 | if (ads->nsearchlist) { | |
658 | free(ads->searchlist[0]); | |
659 | free(ads->searchlist); | |
660 | } | |
661 | free(ads); | |
662 | } | |
663 | ||
d3a102c4 | 664 | static void logfn_file(adns_state ads, void *logfndata, |
665 | const char *fmt, va_list al) { | |
666 | vfprintf(logfndata,fmt,al); | |
667 | } | |
668 | ||
669 | static int init_files(adns_state *ads_r, adns_initflags flags, | |
670 | adns_logcallbackfn *logfn, void *logfndata) { | |
36369543 | 671 | adns_state ads; |
672 | const char *res_options, *adns_res_options; | |
673 | int r; | |
674 | ||
d3a102c4 | 675 | r= init_begin(&ads, flags, logfn, logfndata); |
36369543 | 676 | if (r) return r; |
677 | ||
678 | res_options= instrum_getenv(ads,"RES_OPTIONS"); | |
679 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); | |
680 | ccf_options(ads,"RES_OPTIONS",-1,res_options); | |
681 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); | |
682 | ||
fb7fbb66 | 683 | readconfig(ads,"/etc/resolv.conf",1); |
684 | readconfig(ads,"/etc/resolv-adns.conf",0); | |
36369543 | 685 | readconfigenv(ads,"RES_CONF"); |
686 | readconfigenv(ads,"ADNS_RES_CONF"); | |
687 | ||
09957b1c | 688 | readconfigenvtext(ads,"RES_CONF_TEXT"); |
689 | readconfigenvtext(ads,"ADNS_RES_CONF_TEXT"); | |
690 | ||
36369543 | 691 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
692 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); | |
693 | ||
694 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); | |
695 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); | |
696 | ||
32af6b2a | 697 | if (ads->configerrno && ads->configerrno != EINVAL) { |
698 | r= ads->configerrno; | |
699 | init_abort(ads); | |
700 | return r; | |
701 | } | |
702 | ||
36369543 | 703 | r= init_finish(ads); |
704 | if (r) return r; | |
705 | ||
28de6442 | 706 | adns__consistency(ads,0,cc_entex); |
36369543 | 707 | *ads_r= ads; |
708 | return 0; | |
709 | } | |
710 | ||
d3a102c4 | 711 | int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
712 | return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr); | |
713 | } | |
714 | ||
715 | static int init_strcfg(adns_state *ads_r, adns_initflags flags, | |
716 | adns_logcallbackfn *logfn, void *logfndata, | |
717 | const char *configtext) { | |
36369543 | 718 | adns_state ads; |
719 | int r; | |
720 | ||
d3a102c4 | 721 | r= init_begin(&ads, flags, logfn, logfndata); |
722 | if (r) return r; | |
36369543 | 723 | |
724 | readconfigtext(ads,configtext,"<supplied configuration text>"); | |
725 | if (ads->configerrno) { | |
726 | r= ads->configerrno; | |
32af6b2a | 727 | init_abort(ads); |
36369543 | 728 | return r; |
729 | } | |
730 | ||
731 | r= init_finish(ads); if (r) return r; | |
28de6442 | 732 | adns__consistency(ads,0,cc_entex); |
36369543 | 733 | *ads_r= ads; |
734 | return 0; | |
735 | } | |
736 | ||
d3a102c4 | 737 | int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, |
738 | FILE *diagfile, const char *configtext) { | |
739 | return init_strcfg(ads_r, flags, | |
740 | diagfile ? logfn_file : 0, diagfile, | |
741 | configtext); | |
742 | } | |
743 | ||
744 | int adns_init_logfn(adns_state *newstate_r, adns_initflags flags, | |
745 | const char *configtext /*0=>use default config files*/, | |
746 | adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */, | |
747 | void *logfndata /*0 with logfn==0 => discard*/) { | |
748 | if (!logfn && logfndata) | |
749 | logfn= logfn_file; | |
750 | if (configtext) | |
751 | return init_strcfg(newstate_r, flags, logfn, logfndata, configtext); | |
752 | else | |
753 | return init_files(newstate_r, flags, logfn, logfndata); | |
754 | } | |
3e2e5fab | 755 | |
9ec44266 | 756 | void adns_finish(adns_state ads) { |
3eb20edd | 757 | int i; |
28de6442 | 758 | adns__consistency(ads,0,cc_entex); |
9ec44266 | 759 | for (;;) { |
64aae7c8 IJ |
760 | if (ads->udpw.head) adns__cancel(ads->udpw.head); |
761 | else if (ads->tcpw.head) adns__cancel(ads->tcpw.head); | |
762 | else if (ads->childw.head) adns__cancel(ads->childw.head); | |
763 | else if (ads->output.head) adns__cancel(ads->output.head); | |
b582ca0f | 764 | else if (ads->intdone.head) adns__cancel(ads->output.head); |
9ec44266 | 765 | else break; |
766 | } | |
97a937c8 | 767 | for (i=0; i<ads->nudpsockets; i++) close(ads->udpsockets[i].fd); |
9ec44266 | 768 | if (ads->tcpsocket >= 0) close(ads->tcpsocket); |
769 | adns__vbuf_free(&ads->tcpsend); | |
770 | adns__vbuf_free(&ads->tcprecv); | |
914a5ff5 | 771 | freesearchlist(ads); |
9ec44266 | 772 | free(ads); |
656b2da9 | 773 | } |
8ce38e76 | 774 | |
775 | void adns_forallqueries_begin(adns_state ads) { | |
28de6442 | 776 | adns__consistency(ads,0,cc_entex); |
8ce38e76 | 777 | ads->forallnext= |
f7f83b4a | 778 | ads->udpw.head ? ads->udpw.head : |
779 | ads->tcpw.head ? ads->tcpw.head : | |
8ce38e76 | 780 | ads->childw.head ? ads->childw.head : |
781 | ads->output.head; | |
782 | } | |
783 | ||
784 | adns_query adns_forallqueries_next(adns_state ads, void **context_r) { | |
785 | adns_query qu, nqu; | |
786 | ||
28de6442 | 787 | adns__consistency(ads,0,cc_entex); |
8ce38e76 | 788 | nqu= ads->forallnext; |
789 | for (;;) { | |
790 | qu= nqu; | |
791 | if (!qu) return 0; | |
4218fb9a | 792 | if (qu->next) { |
793 | nqu= qu->next; | |
f7f83b4a | 794 | } else if (qu == ads->udpw.tail) { |
795 | nqu= | |
796 | ads->tcpw.head ? ads->tcpw.head : | |
797 | ads->childw.head ? ads->childw.head : | |
798 | ads->output.head; | |
799 | } else if (qu == ads->tcpw.tail) { | |
800 | nqu= | |
801 | ads->childw.head ? ads->childw.head : | |
802 | ads->output.head; | |
4218fb9a | 803 | } else if (qu == ads->childw.tail) { |
804 | nqu= ads->output.head; | |
805 | } else { | |
806 | nqu= 0; | |
807 | } | |
8ce38e76 | 808 | if (!qu->parent) break; |
809 | } | |
810 | ads->forallnext= nqu; | |
811 | if (context_r) *context_r= qu->ctx.ext; | |
812 | return qu; | |
813 | } |