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 |
8 | * Copyright (C) 1997-2000,2003,2006 Ian Jackson | |
9 | * Copyright (C) 1999-2000,2003,2006 Tony Finch | |
10 | * Copyright (C) 1991 Massachusetts Institute of Technology | |
11 | * (See the file INSTALL for full details.) | |
e576be50 | 12 | * |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2, or (at your option) | |
16 | * any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software Foundation, | |
25 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
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 | |
f930c455 | 44 | static void addserver(adns_state ads, const struct sockaddr *sa, int n) { |
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++) { | |
07554ccd | 50 | if (adns__sockaddr_equal_p(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; | |
f930c455 MW |
64 | assert(n <= sizeof(ss->addr)); |
65 | ss->len = n; | |
66 | memcpy(&ss->addr, sa, n); | |
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 | ||
b0e8338a MW |
168 | static int gen_pton(const char *text, int *af_io, union gen_addr *a) { |
169 | adns_rr_addr addr; | |
170 | int err; | |
171 | ||
172 | addr.len= sizeof(addr.addr); | |
173 | err= adns_text2addr(text,0, adns_qf_addrlit_scope_forbid, | |
174 | &addr.addr.sa, &addr.len); | |
175 | if (err) { assert(err == EINVAL); return 0; } | |
176 | if (*af_io == AF_UNSPEC) *af_io= addr.addr.sa.sa_family; | |
177 | else if (*af_io != addr.addr.sa.sa_family) return 0; | |
178 | adns__sockaddr_extract(&addr.addr.sa, a, 0); | |
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; |
b0e8338a | 189 | int af; |
07554ccd | 190 | int initial= -1; |
09957b1c | 191 | |
32af6b2a | 192 | if (!buf) return; |
193 | ||
09957b1c | 194 | ads->nsortlist= 0; |
32af6b2a | 195 | while (nextword(&buf,&word,&l)) { |
09957b1c | 196 | if (ads->nsortlist >= MAXSORTLIST) { |
609133ee | 197 | adns__diag(ads,-1,0,"too many sortlist entries," |
198 | " ignoring %.*s onwards",l,word); | |
09957b1c | 199 | return; |
200 | } | |
201 | ||
202 | if (l >= sizeof(tbuf)) { | |
32af6b2a | 203 | configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word); |
09957b1c | 204 | continue; |
205 | } | |
206 | ||
78bcc172 | 207 | memcpy(tbuf,word,l); tbuf[l]= 0; |
09957b1c | 208 | slash= strchr(tbuf,'/'); |
209 | if (slash) *slash++= 0; | |
07554ccd MW |
210 | |
211 | sl= &ads->sortlist[ads->nsortlist]; | |
b0e8338a MW |
212 | af= AF_UNSPEC; |
213 | if (!gen_pton(tbuf, &af, &sl->base)) { | |
09957b1c | 214 | configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf); |
215 | continue; | |
216 | } | |
217 | ||
218 | if (slash) { | |
07554ccd | 219 | if (slash[strspn(slash, "0123456789")]) { |
95bee3e1 | 220 | maskwhat = "mask"; |
b0e8338a | 221 | if (!gen_pton(slash,&af,&sl->mask)) { |
09957b1c | 222 | configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash); |
223 | continue; | |
224 | } | |
09957b1c | 225 | } else { |
95bee3e1 | 226 | maskwhat = "prefix length"; |
09957b1c | 227 | initial= strtoul(slash,&ep,10); |
07554ccd | 228 | if (*ep || initial>adns__addr_width(af)) { |
09957b1c | 229 | configparseerr(ads,fn,lno,"mask length `%s' invalid",slash); |
230 | continue; | |
231 | } | |
07554ccd | 232 | adns__prefix_mask(af, initial, &sl->mask); |
09957b1c | 233 | } |
234 | } else { | |
07554ccd MW |
235 | maskwhat = "implied prefix length"; |
236 | initial= adns__guess_prefix_length(af, &sl->base); | |
237 | if (initial < 0) { | |
609133ee | 238 | configparseerr(ads,fn,lno, "network address `%s'" |
239 | " in sortlist is not in classed ranges," | |
09957b1c | 240 | " must specify mask explicitly", tbuf); |
241 | continue; | |
242 | } | |
07554ccd | 243 | adns__prefix_mask(af, initial, &sl->mask); |
09957b1c | 244 | } |
245 | ||
07554ccd MW |
246 | if (!adns__addr_match_p(af,&sl->base, af,&sl->base,&sl->mask)) { |
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 | ||
07554ccd | 257 | sl->af= af; |
09957b1c | 258 | ads->nsortlist++; |
259 | } | |
656b2da9 | 260 | } |
261 | ||
609133ee | 262 | static void ccf_options(adns_state ads, const char *fn, |
263 | int lno, const char *buf) { | |
78bcc172 | 264 | const char *word; |
265 | char *ep; | |
266 | unsigned long v; | |
267 | int l; | |
268 | ||
656b2da9 | 269 | if (!buf) return; |
78bcc172 | 270 | |
271 | while (nextword(&buf,&word,&l)) { | |
272 | if (l==5 && !memcmp(word,"debug",5)) { | |
273 | ads->iflags |= adns_if_debug; | |
274 | continue; | |
275 | } | |
276 | if (l>=6 && !memcmp(word,"ndots:",6)) { | |
277 | v= strtoul(word+6,&ep,10); | |
278 | if (l==6 || ep != word+l || v > INT_MAX) { | |
609133ee | 279 | configparseerr(ads,fn,lno,"option `%.*s' malformed" |
280 | " or has bad value",l,word); | |
78bcc172 | 281 | continue; |
282 | } | |
283 | ads->searchndots= v; | |
284 | continue; | |
285 | } | |
3e2e5fab | 286 | if (l>=12 && !memcmp(word,"adns_checkc:",12)) { |
287 | if (!strcmp(word+12,"none")) { | |
288 | ads->iflags &= ~adns_if_checkc_freq; | |
289 | ads->iflags |= adns_if_checkc_entex; | |
290 | } else if (!strcmp(word+12,"entex")) { | |
291 | ads->iflags &= ~adns_if_checkc_freq; | |
292 | ads->iflags |= adns_if_checkc_entex; | |
293 | } else if (!strcmp(word+12,"freq")) { | |
294 | ads->iflags |= adns_if_checkc_freq; | |
295 | } else { | |
296 | configparseerr(ads,fn,lno, "option adns_checkc has bad value `%s' " | |
297 | "(must be none, entex or freq", word+12); | |
298 | } | |
299 | continue; | |
300 | } | |
78bcc172 | 301 | adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word); |
302 | } | |
656b2da9 | 303 | } |
304 | ||
609133ee | 305 | static void ccf_clearnss(adns_state ads, const char *fn, |
306 | int lno, const char *buf) { | |
656b2da9 | 307 | ads->nservers= 0; |
308 | } | |
309 | ||
609133ee | 310 | static void ccf_include(adns_state ads, const char *fn, |
311 | int lno, const char *buf) { | |
36369543 | 312 | if (!*buf) { |
313 | configparseerr(ads,fn,lno,"`include' directive with no filename"); | |
314 | return; | |
315 | } | |
fb7fbb66 | 316 | readconfig(ads,buf,1); |
36369543 | 317 | } |
318 | ||
0f15dd7b | 319 | static void ccf_lookup(adns_state ads, const char *fn, int lno, |
320 | const char *buf) { | |
321 | int found_bind=0; | |
322 | const char *word; | |
323 | int l; | |
324 | ||
325 | if (!*buf) { | |
326 | configparseerr(ads,fn,lno,"`lookup' directive with no databases"); | |
327 | return; | |
328 | } | |
329 | ||
330 | while (nextword(&buf,&word,&l)) { | |
331 | if (l==4 && !memcmp(word,"bind",4)) { | |
332 | found_bind=1; | |
333 | } else if (l==4 && !memcmp(word,"file",4)) { | |
334 | /* ignore this and hope /etc/hosts is not essential */ | |
335 | } else if (l==2 && !memcmp(word,"yp",2)) { | |
336 | adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno); | |
337 | found_bind=-1; | |
338 | } else { | |
339 | adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'", | |
340 | fn,lno, l,word); | |
341 | found_bind=-1; | |
342 | } | |
343 | } | |
344 | if (!found_bind) | |
345 | adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno); | |
346 | } | |
347 | ||
656b2da9 | 348 | static const struct configcommandinfo { |
349 | const char *name; | |
350 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); | |
351 | } configcommandinfos[]= { | |
352 | { "nameserver", ccf_nameserver }, | |
353 | { "domain", ccf_search }, | |
354 | { "search", ccf_search }, | |
355 | { "sortlist", ccf_sortlist }, | |
356 | { "options", ccf_options }, | |
357 | { "clearnameservers", ccf_clearnss }, | |
36369543 | 358 | { "include", ccf_include }, |
0f15dd7b | 359 | { "lookup", ccf_lookup }, /* OpenBSD */ |
656b2da9 | 360 | { 0 } |
361 | }; | |
362 | ||
36369543 | 363 | typedef union { |
656b2da9 | 364 | FILE *file; |
36369543 | 365 | const char *text; |
366 | } getline_ctx; | |
656b2da9 | 367 | |
36369543 | 368 | static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename, |
369 | int lno, char *buf, int buflen) { | |
370 | FILE *file= src_io->file; | |
371 | int c, i; | |
372 | char *p; | |
373 | ||
374 | p= buf; | |
375 | buflen--; | |
376 | i= 0; | |
377 | ||
378 | for (;;) { /* loop over chars */ | |
379 | if (i == buflen) { | |
380 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); | |
381 | goto x_badline; | |
382 | } | |
383 | c= getc(file); | |
384 | if (!c) { | |
385 | adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno); | |
386 | goto x_badline; | |
387 | } else if (c == '\n') { | |
388 | break; | |
389 | } else if (c == EOF) { | |
390 | if (ferror(file)) { | |
391 | saveerr(ads,errno); | |
609133ee | 392 | adns__diag(ads,-1,0,"%s:%d: read error: %s", |
393 | filename,lno,strerror(errno)); | |
36369543 | 394 | return -1; |
395 | } | |
396 | if (!i) return -1; | |
397 | break; | |
398 | } else { | |
399 | *p++= c; | |
400 | i++; | |
656b2da9 | 401 | } |
656b2da9 | 402 | } |
403 | ||
36369543 | 404 | *p++= 0; |
405 | return i; | |
406 | ||
407 | x_badline: | |
408 | saveerr(ads,EINVAL); | |
409 | while ((c= getc(file)) != EOF && c != '\n'); | |
410 | return -2; | |
411 | } | |
412 | ||
413 | static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename, | |
414 | int lno, char *buf, int buflen) { | |
09957b1c | 415 | const char *cp= src_io->text; |
36369543 | 416 | int l; |
417 | ||
09957b1c | 418 | if (!cp || !*cp) return -1; |
36369543 | 419 | |
09957b1c | 420 | if (*cp == ';' || *cp == '\n') cp++; |
421 | l= strcspn(cp,";\n"); | |
422 | src_io->text = cp+l; | |
36369543 | 423 | |
424 | if (l >= buflen) { | |
425 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); | |
426 | saveerr(ads,EINVAL); | |
427 | return -2; | |
428 | } | |
429 | ||
430 | memcpy(buf,cp,l); | |
431 | buf[l]= 0; | |
432 | return l; | |
433 | } | |
434 | ||
435 | static void readconfiggeneric(adns_state ads, const char *filename, | |
436 | int (*getline)(adns_state ads, getline_ctx*, | |
437 | const char *filename, int lno, | |
438 | char *buf, int buflen), | |
439 | /* Returns >=0 for success, -1 for EOF or error | |
440 | * (error will have been reported), or -2 for | |
441 | * bad line was encountered, try again. | |
442 | */ | |
443 | getline_ctx gl_ctx) { | |
444 | char linebuf[2000], *p, *q; | |
445 | int lno, l, dirl; | |
446 | const struct configcommandinfo *ccip; | |
447 | ||
448 | for (lno=1; | |
449 | (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1; | |
450 | lno++) { | |
451 | if (l == -2) continue; | |
656b2da9 | 452 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
453 | linebuf[l]= 0; | |
454 | p= linebuf; | |
455 | while (ctype_whitespace(*p)) p++; | |
2d230487 | 456 | if (*p == '#' || *p == ';' || !*p) continue; |
656b2da9 | 457 | q= p; |
458 | while (*q && !ctype_whitespace(*q)) q++; | |
36369543 | 459 | dirl= q-p; |
656b2da9 | 460 | for (ccip=configcommandinfos; |
609133ee | 461 | ccip->name && |
462 | !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p)); | |
656b2da9 | 463 | ccip++); |
464 | if (!ccip->name) { | |
3955725c | 465 | adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'", |
0ca555c6 | 466 | filename,lno,(int)(q-p),p); |
656b2da9 | 467 | continue; |
468 | } | |
469 | while (ctype_whitespace(*q)) q++; | |
470 | ccip->fn(ads,filename,lno,q); | |
471 | } | |
656b2da9 | 472 | } |
473 | ||
474 | static const char *instrum_getenv(adns_state ads, const char *envvar) { | |
475 | const char *value; | |
476 | ||
477 | value= getenv(envvar); | |
3955725c | 478 | if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar); |
609133ee | 479 | else adns__debug(ads,-1,0,"environment variable %s" |
480 | " set to `%s'",envvar,value); | |
656b2da9 | 481 | return value; |
482 | } | |
483 | ||
fb7fbb66 | 484 | static void readconfig(adns_state ads, const char *filename, int warnmissing) { |
36369543 | 485 | getline_ctx gl_ctx; |
486 | ||
487 | gl_ctx.file= fopen(filename,"r"); | |
488 | if (!gl_ctx.file) { | |
489 | if (errno == ENOENT) { | |
fb7fbb66 | 490 | if (warnmissing) |
609133ee | 491 | adns__debug(ads,-1,0, "configuration file" |
492 | " `%s' does not exist",filename); | |
36369543 | 493 | return; |
494 | } | |
495 | saveerr(ads,errno); | |
496 | adns__diag(ads,-1,0,"cannot open configuration file `%s': %s", | |
497 | filename,strerror(errno)); | |
498 | return; | |
499 | } | |
500 | ||
501 | readconfiggeneric(ads,filename,gl_file,gl_ctx); | |
502 | ||
503 | fclose(gl_ctx.file); | |
504 | } | |
505 | ||
609133ee | 506 | static void readconfigtext(adns_state ads, const char *text, |
507 | const char *showname) { | |
36369543 | 508 | getline_ctx gl_ctx; |
509 | ||
510 | gl_ctx.text= text; | |
511 | readconfiggeneric(ads,showname,gl_text,gl_ctx); | |
512 | } | |
513 | ||
656b2da9 | 514 | static void readconfigenv(adns_state ads, const char *envvar) { |
515 | const char *filename; | |
516 | ||
517 | if (ads->iflags & adns_if_noenv) { | |
3955725c | 518 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
656b2da9 | 519 | return; |
520 | } | |
521 | filename= instrum_getenv(ads,envvar); | |
fb7fbb66 | 522 | if (filename) readconfig(ads,filename,1); |
656b2da9 | 523 | } |
4353a5c4 | 524 | |
09957b1c | 525 | static void readconfigenvtext(adns_state ads, const char *envvar) { |
526 | const char *textdata; | |
527 | ||
528 | if (ads->iflags & adns_if_noenv) { | |
529 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); | |
530 | return; | |
531 | } | |
532 | textdata= instrum_getenv(ads,envvar); | |
533 | if (textdata) readconfigtext(ads,textdata,envvar); | |
534 | } | |
535 | ||
4353a5c4 | 536 | |
537 | int adns__setnonblock(adns_state ads, int fd) { | |
538 | int r; | |
656b2da9 | 539 | |
4353a5c4 | 540 | r= fcntl(fd,F_GETFL,0); if (r<0) return errno; |
541 | r |= O_NONBLOCK; | |
542 | r= fcntl(fd,F_SETFL,r); if (r<0) return errno; | |
543 | return 0; | |
544 | } | |
545 | ||
609133ee | 546 | static int init_begin(adns_state *ads_r, adns_initflags flags, |
d3a102c4 | 547 | adns_logcallbackfn *logfn, void *logfndata) { |
656b2da9 | 548 | adns_state ads; |
0e45654b | 549 | pid_t pid; |
656b2da9 | 550 | |
551 | ads= malloc(sizeof(*ads)); if (!ads) return errno; | |
36369543 | 552 | |
656b2da9 | 553 | ads->iflags= flags; |
d3a102c4 | 554 | ads->logfn= logfn; |
555 | ads->logfndata= logfndata; | |
d855b532 | 556 | ads->configerrno= 0; |
f7f83b4a | 557 | LIST_INIT(ads->udpw); |
558 | LIST_INIT(ads->tcpw); | |
4353a5c4 | 559 | LIST_INIT(ads->childw); |
560 | LIST_INIT(ads->output); | |
8ce38e76 | 561 | ads->forallnext= 0; |
4353a5c4 | 562 | ads->nextid= 0x311f; |
3eb20edd MW |
563 | ads->nudp= 0; |
564 | ads->tcpsocket= -1; | |
4353a5c4 | 565 | adns__vbuf_init(&ads->tcpsend); |
566 | adns__vbuf_init(&ads->tcprecv); | |
70ad7a2a | 567 | ads->tcprecv_skip= 0; |
32af6b2a | 568 | ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0; |
660d7d3b | 569 | ads->searchndots= 1; |
d855b532 | 570 | ads->tcpstate= server_disconnected; |
4353a5c4 | 571 | timerclear(&ads->tcptimeout); |
d855b532 | 572 | ads->searchlist= 0; |
656b2da9 | 573 | |
0e45654b | 574 | pid= getpid(); |
575 | ads->rand48xsubi[0]= pid; | |
576 | ads->rand48xsubi[1]= (unsigned long)pid >> 16; | |
577 | ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16); | |
578 | ||
36369543 | 579 | *ads_r= ads; |
580 | return 0; | |
581 | } | |
656b2da9 | 582 | |
36369543 | 583 | static int init_finish(adns_state ads) { |
f930c455 | 584 | struct sockaddr_in sin; |
36369543 | 585 | struct protoent *proto; |
3eb20edd MW |
586 | struct udpsocket *udp; |
587 | int i, j; | |
588 | int r; | |
36369543 | 589 | |
656b2da9 | 590 | if (!ads->nservers) { |
d3a102c4 | 591 | if (ads->logfn && ads->iflags & adns_if_debug) |
f930c455 MW |
592 | adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n"); |
593 | memset(&sin, 0, sizeof(sin)); | |
594 | sin.sin_family = AF_INET; | |
595 | sin.sin_port = htons(DNS_PORT); | |
596 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
597 | addserver(ads,(struct sockaddr *)&sin, sizeof(sin)); | |
656b2da9 | 598 | } |
599 | ||
600 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } | |
3eb20edd MW |
601 | ads->nudp= 0; |
602 | for (i=0; i<ads->nservers; i++) { | |
603 | if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family)) | |
604 | continue; | |
605 | assert(ads->nudp < MAXUDP); | |
606 | udp= &ads->udpsocket[ads->nudp]; | |
607 | udp->af= ads->servers[i].addr.sa.sa_family; | |
608 | udp->fd= socket(udp->af,SOCK_DGRAM,proto->p_proto); | |
609 | if (udp->fd < 0) { r= errno; goto x_free; } | |
610 | ads->nudp++; | |
611 | r= adns__setnonblock(ads,udp->fd); | |
612 | if (r) { r= errno; goto x_closeudp; } | |
613 | } | |
656b2da9 | 614 | |
656b2da9 | 615 | return 0; |
616 | ||
94436798 | 617 | x_closeudp: |
3eb20edd | 618 | for (j=0; j<ads->nudp; j++) close(ads->udpsocket[j].fd); |
656b2da9 | 619 | x_free: |
620 | free(ads); | |
621 | return r; | |
622 | } | |
623 | ||
32af6b2a | 624 | static void init_abort(adns_state ads) { |
625 | if (ads->nsearchlist) { | |
626 | free(ads->searchlist[0]); | |
627 | free(ads->searchlist); | |
628 | } | |
629 | free(ads); | |
630 | } | |
631 | ||
d3a102c4 | 632 | static void logfn_file(adns_state ads, void *logfndata, |
633 | const char *fmt, va_list al) { | |
634 | vfprintf(logfndata,fmt,al); | |
635 | } | |
636 | ||
637 | static int init_files(adns_state *ads_r, adns_initflags flags, | |
638 | adns_logcallbackfn *logfn, void *logfndata) { | |
36369543 | 639 | adns_state ads; |
640 | const char *res_options, *adns_res_options; | |
641 | int r; | |
642 | ||
d3a102c4 | 643 | r= init_begin(&ads, flags, logfn, logfndata); |
36369543 | 644 | if (r) return r; |
645 | ||
646 | res_options= instrum_getenv(ads,"RES_OPTIONS"); | |
647 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); | |
648 | ccf_options(ads,"RES_OPTIONS",-1,res_options); | |
649 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); | |
650 | ||
fb7fbb66 | 651 | readconfig(ads,"/etc/resolv.conf",1); |
652 | readconfig(ads,"/etc/resolv-adns.conf",0); | |
36369543 | 653 | readconfigenv(ads,"RES_CONF"); |
654 | readconfigenv(ads,"ADNS_RES_CONF"); | |
655 | ||
09957b1c | 656 | readconfigenvtext(ads,"RES_CONF_TEXT"); |
657 | readconfigenvtext(ads,"ADNS_RES_CONF_TEXT"); | |
658 | ||
36369543 | 659 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
660 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); | |
661 | ||
662 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); | |
663 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); | |
664 | ||
32af6b2a | 665 | if (ads->configerrno && ads->configerrno != EINVAL) { |
666 | r= ads->configerrno; | |
667 | init_abort(ads); | |
668 | return r; | |
669 | } | |
670 | ||
36369543 | 671 | r= init_finish(ads); |
672 | if (r) return r; | |
673 | ||
28de6442 | 674 | adns__consistency(ads,0,cc_entex); |
36369543 | 675 | *ads_r= ads; |
676 | return 0; | |
677 | } | |
678 | ||
d3a102c4 | 679 | int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
680 | return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr); | |
681 | } | |
682 | ||
683 | static int init_strcfg(adns_state *ads_r, adns_initflags flags, | |
684 | adns_logcallbackfn *logfn, void *logfndata, | |
685 | const char *configtext) { | |
36369543 | 686 | adns_state ads; |
687 | int r; | |
688 | ||
d3a102c4 | 689 | r= init_begin(&ads, flags, logfn, logfndata); |
690 | if (r) return r; | |
36369543 | 691 | |
692 | readconfigtext(ads,configtext,"<supplied configuration text>"); | |
693 | if (ads->configerrno) { | |
694 | r= ads->configerrno; | |
32af6b2a | 695 | init_abort(ads); |
36369543 | 696 | return r; |
697 | } | |
698 | ||
699 | r= init_finish(ads); if (r) return r; | |
28de6442 | 700 | adns__consistency(ads,0,cc_entex); |
36369543 | 701 | *ads_r= ads; |
702 | return 0; | |
703 | } | |
704 | ||
d3a102c4 | 705 | int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, |
706 | FILE *diagfile, const char *configtext) { | |
707 | return init_strcfg(ads_r, flags, | |
708 | diagfile ? logfn_file : 0, diagfile, | |
709 | configtext); | |
710 | } | |
711 | ||
712 | int adns_init_logfn(adns_state *newstate_r, adns_initflags flags, | |
713 | const char *configtext /*0=>use default config files*/, | |
714 | adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */, | |
715 | void *logfndata /*0 with logfn==0 => discard*/) { | |
716 | if (!logfn && logfndata) | |
717 | logfn= logfn_file; | |
718 | if (configtext) | |
719 | return init_strcfg(newstate_r, flags, logfn, logfndata, configtext); | |
720 | else | |
721 | return init_files(newstate_r, flags, logfn, logfndata); | |
722 | } | |
3e2e5fab | 723 | |
9ec44266 | 724 | void adns_finish(adns_state ads) { |
3eb20edd | 725 | int i; |
28de6442 | 726 | adns__consistency(ads,0,cc_entex); |
9ec44266 | 727 | for (;;) { |
f7f83b4a | 728 | if (ads->udpw.head) adns_cancel(ads->udpw.head); |
729 | else if (ads->tcpw.head) adns_cancel(ads->tcpw.head); | |
9ec44266 | 730 | else if (ads->childw.head) adns_cancel(ads->childw.head); |
731 | else if (ads->output.head) adns_cancel(ads->output.head); | |
732 | else break; | |
733 | } | |
3eb20edd | 734 | for (i=0; i<ads->nudp; i++) close(ads->udpsocket[i].fd); |
9ec44266 | 735 | if (ads->tcpsocket >= 0) close(ads->tcpsocket); |
736 | adns__vbuf_free(&ads->tcpsend); | |
737 | adns__vbuf_free(&ads->tcprecv); | |
914a5ff5 | 738 | freesearchlist(ads); |
9ec44266 | 739 | free(ads); |
656b2da9 | 740 | } |
8ce38e76 | 741 | |
742 | void adns_forallqueries_begin(adns_state ads) { | |
28de6442 | 743 | adns__consistency(ads,0,cc_entex); |
8ce38e76 | 744 | ads->forallnext= |
f7f83b4a | 745 | ads->udpw.head ? ads->udpw.head : |
746 | ads->tcpw.head ? ads->tcpw.head : | |
8ce38e76 | 747 | ads->childw.head ? ads->childw.head : |
748 | ads->output.head; | |
749 | } | |
750 | ||
751 | adns_query adns_forallqueries_next(adns_state ads, void **context_r) { | |
752 | adns_query qu, nqu; | |
753 | ||
28de6442 | 754 | adns__consistency(ads,0,cc_entex); |
8ce38e76 | 755 | nqu= ads->forallnext; |
756 | for (;;) { | |
757 | qu= nqu; | |
758 | if (!qu) return 0; | |
4218fb9a | 759 | if (qu->next) { |
760 | nqu= qu->next; | |
f7f83b4a | 761 | } else if (qu == ads->udpw.tail) { |
762 | nqu= | |
763 | ads->tcpw.head ? ads->tcpw.head : | |
764 | ads->childw.head ? ads->childw.head : | |
765 | ads->output.head; | |
766 | } else if (qu == ads->tcpw.tail) { | |
767 | nqu= | |
768 | ads->childw.head ? ads->childw.head : | |
769 | ads->output.head; | |
4218fb9a | 770 | } else if (qu == ads->childw.tail) { |
771 | nqu= ads->output.head; | |
772 | } else { | |
773 | nqu= 0; | |
774 | } | |
8ce38e76 | 775 | if (!qu->parent) break; |
776 | } | |
777 | ads->forallnext= nqu; | |
778 | if (context_r) *context_r= qu->ctx.ext; | |
779 | return qu; | |
780 | } |