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