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