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