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 | } | |
20078efc MW |
313 | if (l>=8 && !memcmp(word,"adns_af:",8)) { |
314 | if (!strcmp(word+8,"v4only")) | |
315 | ads->iflags = (ads->iflags & ~adns_if_afmask) | adns_if_af_v4only; | |
316 | else if (!strcmp(word+8,"v6only")) | |
317 | ads->iflags = (ads->iflags & ~adns_if_afmask) | adns_if_af_v6only; | |
318 | else if (!strcmp(word+8,"any")) | |
319 | ads->iflags = (ads->iflags & ~adns_if_afmask); | |
320 | else { | |
321 | configparseerr(ads,fn,lno, "option adns_af has bad value `%s' " | |
322 | "(must be any, v4only or v6only", word+8); | |
323 | } | |
324 | continue; | |
325 | } | |
7ca1d685 | 326 | adns__diag(ads,-1,0,"%s:%d: unknown option `%.*s'", fn,lno, l,word); |
327 | } | |
6f17710a | 328 | } |
329 | ||
9c344a42 | 330 | static void ccf_clearnss(adns_state ads, const char *fn, |
331 | int lno, const char *buf) { | |
6f17710a | 332 | ads->nservers= 0; |
333 | } | |
334 | ||
9c344a42 | 335 | static void ccf_include(adns_state ads, const char *fn, |
336 | int lno, const char *buf) { | |
d6b271ae | 337 | if (!*buf) { |
338 | configparseerr(ads,fn,lno,"`include' directive with no filename"); | |
339 | return; | |
340 | } | |
45efc92a | 341 | readconfig(ads,buf,1); |
d6b271ae | 342 | } |
343 | ||
b2a4fdb5 | 344 | static void ccf_lookup(adns_state ads, const char *fn, int lno, |
345 | const char *buf) { | |
346 | int found_bind=0; | |
347 | const char *word; | |
348 | int l; | |
349 | ||
350 | if (!*buf) { | |
351 | configparseerr(ads,fn,lno,"`lookup' directive with no databases"); | |
352 | return; | |
353 | } | |
354 | ||
355 | while (nextword(&buf,&word,&l)) { | |
356 | if (l==4 && !memcmp(word,"bind",4)) { | |
357 | found_bind=1; | |
358 | } else if (l==4 && !memcmp(word,"file",4)) { | |
359 | /* ignore this and hope /etc/hosts is not essential */ | |
360 | } else if (l==2 && !memcmp(word,"yp",2)) { | |
361 | adns__diag(ads,-1,0,"%s:%d: yp lookups not supported by adns", fn,lno); | |
362 | found_bind=-1; | |
363 | } else { | |
364 | adns__diag(ads,-1,0,"%s:%d: unknown `lookup' database `%.*s'", | |
365 | fn,lno, l,word); | |
366 | found_bind=-1; | |
367 | } | |
368 | } | |
369 | if (!found_bind) | |
370 | adns__diag(ads,-1,0,"%s:%d: `lookup' specified, but not `bind'", fn,lno); | |
371 | } | |
372 | ||
6f17710a | 373 | static const struct configcommandinfo { |
374 | const char *name; | |
375 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); | |
376 | } configcommandinfos[]= { | |
377 | { "nameserver", ccf_nameserver }, | |
378 | { "domain", ccf_search }, | |
379 | { "search", ccf_search }, | |
380 | { "sortlist", ccf_sortlist }, | |
381 | { "options", ccf_options }, | |
382 | { "clearnameservers", ccf_clearnss }, | |
d6b271ae | 383 | { "include", ccf_include }, |
b2a4fdb5 | 384 | { "lookup", ccf_lookup }, /* OpenBSD */ |
6f17710a | 385 | { 0 } |
386 | }; | |
387 | ||
d6b271ae | 388 | typedef union { |
6f17710a | 389 | FILE *file; |
d6b271ae | 390 | const char *text; |
391 | } getline_ctx; | |
6f17710a | 392 | |
d6b271ae | 393 | static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename, |
394 | int lno, char *buf, int buflen) { | |
395 | FILE *file= src_io->file; | |
396 | int c, i; | |
397 | char *p; | |
398 | ||
399 | p= buf; | |
400 | buflen--; | |
401 | i= 0; | |
402 | ||
403 | for (;;) { /* loop over chars */ | |
404 | if (i == buflen) { | |
405 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); | |
406 | goto x_badline; | |
407 | } | |
408 | c= getc(file); | |
409 | if (!c) { | |
410 | adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno); | |
411 | goto x_badline; | |
412 | } else if (c == '\n') { | |
413 | break; | |
414 | } else if (c == EOF) { | |
415 | if (ferror(file)) { | |
416 | saveerr(ads,errno); | |
9c344a42 | 417 | adns__diag(ads,-1,0,"%s:%d: read error: %s", |
418 | filename,lno,strerror(errno)); | |
d6b271ae | 419 | return -1; |
420 | } | |
421 | if (!i) return -1; | |
422 | break; | |
423 | } else { | |
424 | *p++= c; | |
425 | i++; | |
6f17710a | 426 | } |
6f17710a | 427 | } |
428 | ||
d6b271ae | 429 | *p++= 0; |
430 | return i; | |
431 | ||
432 | x_badline: | |
433 | saveerr(ads,EINVAL); | |
434 | while ((c= getc(file)) != EOF && c != '\n'); | |
435 | return -2; | |
436 | } | |
437 | ||
438 | static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename, | |
439 | int lno, char *buf, int buflen) { | |
8c3aa944 | 440 | const char *cp= src_io->text; |
d6b271ae | 441 | int l; |
442 | ||
8c3aa944 | 443 | if (!cp || !*cp) return -1; |
d6b271ae | 444 | |
8c3aa944 | 445 | if (*cp == ';' || *cp == '\n') cp++; |
446 | l= strcspn(cp,";\n"); | |
447 | src_io->text = cp+l; | |
d6b271ae | 448 | |
449 | if (l >= buflen) { | |
450 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); | |
451 | saveerr(ads,EINVAL); | |
452 | return -2; | |
453 | } | |
454 | ||
455 | memcpy(buf,cp,l); | |
456 | buf[l]= 0; | |
457 | return l; | |
458 | } | |
459 | ||
460 | static void readconfiggeneric(adns_state ads, const char *filename, | |
461 | int (*getline)(adns_state ads, getline_ctx*, | |
462 | const char *filename, int lno, | |
463 | char *buf, int buflen), | |
464 | /* Returns >=0 for success, -1 for EOF or error | |
465 | * (error will have been reported), or -2 for | |
466 | * bad line was encountered, try again. | |
467 | */ | |
468 | getline_ctx gl_ctx) { | |
469 | char linebuf[2000], *p, *q; | |
470 | int lno, l, dirl; | |
471 | const struct configcommandinfo *ccip; | |
472 | ||
473 | for (lno=1; | |
474 | (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1; | |
475 | lno++) { | |
476 | if (l == -2) continue; | |
6f17710a | 477 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
478 | linebuf[l]= 0; | |
479 | p= linebuf; | |
480 | while (ctype_whitespace(*p)) p++; | |
9ae68b5e | 481 | if (*p == '#' || *p == ';' || !*p) continue; |
6f17710a | 482 | q= p; |
483 | while (*q && !ctype_whitespace(*q)) q++; | |
d6b271ae | 484 | dirl= q-p; |
6f17710a | 485 | for (ccip=configcommandinfos; |
9c344a42 | 486 | ccip->name && |
487 | !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p)); | |
6f17710a | 488 | ccip++); |
489 | if (!ccip->name) { | |
11c8bf9b | 490 | adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'", |
c2180d3e | 491 | filename,lno,(int)(q-p),p); |
6f17710a | 492 | continue; |
493 | } | |
494 | while (ctype_whitespace(*q)) q++; | |
495 | ccip->fn(ads,filename,lno,q); | |
496 | } | |
6f17710a | 497 | } |
498 | ||
499 | static const char *instrum_getenv(adns_state ads, const char *envvar) { | |
500 | const char *value; | |
501 | ||
502 | value= getenv(envvar); | |
11c8bf9b | 503 | if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar); |
9c344a42 | 504 | else adns__debug(ads,-1,0,"environment variable %s" |
505 | " set to `%s'",envvar,value); | |
6f17710a | 506 | return value; |
507 | } | |
508 | ||
45efc92a | 509 | static void readconfig(adns_state ads, const char *filename, int warnmissing) { |
d6b271ae | 510 | getline_ctx gl_ctx; |
511 | ||
512 | gl_ctx.file= fopen(filename,"r"); | |
513 | if (!gl_ctx.file) { | |
514 | if (errno == ENOENT) { | |
45efc92a | 515 | if (warnmissing) |
9c344a42 | 516 | adns__debug(ads,-1,0, "configuration file" |
517 | " `%s' does not exist",filename); | |
d6b271ae | 518 | return; |
519 | } | |
520 | saveerr(ads,errno); | |
521 | adns__diag(ads,-1,0,"cannot open configuration file `%s': %s", | |
522 | filename,strerror(errno)); | |
523 | return; | |
524 | } | |
525 | ||
526 | readconfiggeneric(ads,filename,gl_file,gl_ctx); | |
527 | ||
528 | fclose(gl_ctx.file); | |
529 | } | |
530 | ||
9c344a42 | 531 | static void readconfigtext(adns_state ads, const char *text, |
532 | const char *showname) { | |
d6b271ae | 533 | getline_ctx gl_ctx; |
534 | ||
535 | gl_ctx.text= text; | |
536 | readconfiggeneric(ads,showname,gl_text,gl_ctx); | |
537 | } | |
538 | ||
6f17710a | 539 | static void readconfigenv(adns_state ads, const char *envvar) { |
540 | const char *filename; | |
541 | ||
542 | if (ads->iflags & adns_if_noenv) { | |
11c8bf9b | 543 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
6f17710a | 544 | return; |
545 | } | |
546 | filename= instrum_getenv(ads,envvar); | |
45efc92a | 547 | if (filename) readconfig(ads,filename,1); |
6f17710a | 548 | } |
d05cc330 | 549 | |
8c3aa944 | 550 | static void readconfigenvtext(adns_state ads, const char *envvar) { |
551 | const char *textdata; | |
552 | ||
553 | if (ads->iflags & adns_if_noenv) { | |
554 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); | |
555 | return; | |
556 | } | |
557 | textdata= instrum_getenv(ads,envvar); | |
558 | if (textdata) readconfigtext(ads,textdata,envvar); | |
559 | } | |
560 | ||
d05cc330 | 561 | |
562 | int adns__setnonblock(adns_state ads, int fd) { | |
563 | int r; | |
6f17710a | 564 | |
d05cc330 | 565 | r= fcntl(fd,F_GETFL,0); if (r<0) return errno; |
566 | r |= O_NONBLOCK; | |
567 | r= fcntl(fd,F_SETFL,r); if (r<0) return errno; | |
568 | return 0; | |
569 | } | |
570 | ||
9c344a42 | 571 | static int init_begin(adns_state *ads_r, adns_initflags flags, |
86ea5e62 | 572 | adns_logcallbackfn *logfn, void *logfndata) { |
6f17710a | 573 | adns_state ads; |
375c6c48 | 574 | pid_t pid; |
6f17710a | 575 | |
576 | ads= malloc(sizeof(*ads)); if (!ads) return errno; | |
d6b271ae | 577 | |
6f17710a | 578 | ads->iflags= flags; |
86ea5e62 | 579 | ads->logfn= logfn; |
580 | ads->logfndata= logfndata; | |
7def4935 | 581 | ads->configerrno= 0; |
d0a057ac | 582 | LIST_INIT(ads->udpw); |
583 | LIST_INIT(ads->tcpw); | |
d05cc330 | 584 | LIST_INIT(ads->childw); |
585 | LIST_INIT(ads->output); | |
8f2aa812 | 586 | ads->forallnext= 0; |
d05cc330 | 587 | ads->nextid= 0x311f; |
705b9b15 MW |
588 | ads->nudp= 0; |
589 | ads->tcpsocket= -1; | |
d05cc330 | 590 | adns__vbuf_init(&ads->tcpsend); |
591 | adns__vbuf_init(&ads->tcprecv); | |
ab898cf4 | 592 | ads->tcprecv_skip= 0; |
11f553d9 | 593 | ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0; |
7e6a84a1 | 594 | ads->searchndots= 1; |
7def4935 | 595 | ads->tcpstate= server_disconnected; |
d05cc330 | 596 | timerclear(&ads->tcptimeout); |
7def4935 | 597 | ads->searchlist= 0; |
6f17710a | 598 | |
375c6c48 | 599 | pid= getpid(); |
600 | ads->rand48xsubi[0]= pid; | |
601 | ads->rand48xsubi[1]= (unsigned long)pid >> 16; | |
602 | ads->rand48xsubi[2]= pid ^ ((unsigned long)pid >> 16); | |
603 | ||
d6b271ae | 604 | *ads_r= ads; |
605 | return 0; | |
606 | } | |
6f17710a | 607 | |
d6b271ae | 608 | static int init_finish(adns_state ads) { |
705b9b15 | 609 | struct sockaddr_in sin; |
d6b271ae | 610 | struct protoent *proto; |
705b9b15 MW |
611 | struct udpsocket *udp; |
612 | int i, j; | |
d6b271ae | 613 | int r; |
614 | ||
6f17710a | 615 | if (!ads->nservers) { |
86ea5e62 | 616 | if (ads->logfn && ads->iflags & adns_if_debug) |
705b9b15 MW |
617 | adns__lprintf(ads,"adns: no nameservers, using IPv4 localhost\n"); |
618 | memset(&sin, 0, sizeof(sin)); | |
e1d31292 | 619 | sin.sin_family = AF_INET; |
705b9b15 MW |
620 | sin.sin_port = htons(DNS_PORT); |
621 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
622 | addserver(ads,(struct sockaddr *)&sin, sizeof(sin)); | |
6f17710a | 623 | } |
624 | ||
625 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } | |
705b9b15 MW |
626 | ads->nudp = 0; |
627 | for (i = 0; i < ads->nservers; i++) { | |
628 | for (j = 0; j < ads->nudp; j++) { | |
629 | if (ads->udpsocket[j].ai->af == ads->servers[i].addr.sa.sa_family) | |
630 | goto afmatch; | |
631 | } | |
48cb0b4b | 632 | |
705b9b15 MW |
633 | assert(ads->nudp < MAXUDP); |
634 | udp = &ads->udpsocket[ads->nudp]; | |
635 | udp->ai = find_afinfo(ads->servers[i].addr.sa.sa_family); | |
636 | assert(udp->ai); | |
637 | udp->fd = socket(udp->ai->af,SOCK_DGRAM,proto->p_proto); | |
638 | if (udp->fd < 0) { r= errno; goto x_free; } | |
639 | r= adns__setnonblock(ads,udp->fd); | |
640 | if (r) { r= errno; goto x_closeudp; } | |
641 | ads->nudp++; | |
642 | ||
643 | afmatch:; | |
644 | } | |
6f17710a | 645 | |
6f17710a | 646 | return 0; |
647 | ||
de8b18da | 648 | x_closeudp: |
705b9b15 | 649 | for (j = 0; j < ads->nudp; j++) close(ads->udpsocket[j].fd); |
6f17710a | 650 | x_free: |
651 | free(ads); | |
652 | return r; | |
653 | } | |
654 | ||
11f553d9 | 655 | static void init_abort(adns_state ads) { |
656 | if (ads->nsearchlist) { | |
657 | free(ads->searchlist[0]); | |
658 | free(ads->searchlist); | |
659 | } | |
660 | free(ads); | |
661 | } | |
662 | ||
86ea5e62 | 663 | static void logfn_file(adns_state ads, void *logfndata, |
664 | const char *fmt, va_list al) { | |
665 | vfprintf(logfndata,fmt,al); | |
666 | } | |
667 | ||
668 | static int init_files(adns_state *ads_r, adns_initflags flags, | |
669 | adns_logcallbackfn *logfn, void *logfndata) { | |
d6b271ae | 670 | adns_state ads; |
671 | const char *res_options, *adns_res_options; | |
672 | int r; | |
673 | ||
86ea5e62 | 674 | r= init_begin(&ads, flags, logfn, logfndata); |
d6b271ae | 675 | if (r) return r; |
676 | ||
677 | res_options= instrum_getenv(ads,"RES_OPTIONS"); | |
678 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); | |
679 | ccf_options(ads,"RES_OPTIONS",-1,res_options); | |
680 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); | |
681 | ||
45efc92a | 682 | readconfig(ads,"/etc/resolv.conf",1); |
683 | readconfig(ads,"/etc/resolv-adns.conf",0); | |
d6b271ae | 684 | readconfigenv(ads,"RES_CONF"); |
685 | readconfigenv(ads,"ADNS_RES_CONF"); | |
686 | ||
8c3aa944 | 687 | readconfigenvtext(ads,"RES_CONF_TEXT"); |
688 | readconfigenvtext(ads,"ADNS_RES_CONF_TEXT"); | |
689 | ||
d6b271ae | 690 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
691 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); | |
692 | ||
693 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); | |
694 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); | |
695 | ||
11f553d9 | 696 | if (ads->configerrno && ads->configerrno != EINVAL) { |
697 | r= ads->configerrno; | |
698 | init_abort(ads); | |
699 | return r; | |
700 | } | |
701 | ||
d6b271ae | 702 | r= init_finish(ads); |
703 | if (r) return r; | |
704 | ||
2ac463bf | 705 | adns__consistency(ads,0,cc_entex); |
d6b271ae | 706 | *ads_r= ads; |
707 | return 0; | |
708 | } | |
709 | ||
86ea5e62 | 710 | int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
711 | return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr); | |
712 | } | |
713 | ||
714 | static int init_strcfg(adns_state *ads_r, adns_initflags flags, | |
715 | adns_logcallbackfn *logfn, void *logfndata, | |
716 | const char *configtext) { | |
d6b271ae | 717 | adns_state ads; |
718 | int r; | |
719 | ||
86ea5e62 | 720 | r= init_begin(&ads, flags, logfn, logfndata); |
721 | if (r) return r; | |
d6b271ae | 722 | |
723 | readconfigtext(ads,configtext,"<supplied configuration text>"); | |
724 | if (ads->configerrno) { | |
725 | r= ads->configerrno; | |
11f553d9 | 726 | init_abort(ads); |
d6b271ae | 727 | return r; |
728 | } | |
729 | ||
730 | r= init_finish(ads); if (r) return r; | |
2ac463bf | 731 | adns__consistency(ads,0,cc_entex); |
d6b271ae | 732 | *ads_r= ads; |
733 | return 0; | |
734 | } | |
735 | ||
86ea5e62 | 736 | int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, |
737 | FILE *diagfile, const char *configtext) { | |
738 | return init_strcfg(ads_r, flags, | |
739 | diagfile ? logfn_file : 0, diagfile, | |
740 | configtext); | |
741 | } | |
742 | ||
743 | int adns_init_logfn(adns_state *newstate_r, adns_initflags flags, | |
744 | const char *configtext /*0=>use default config files*/, | |
745 | adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */, | |
746 | void *logfndata /*0 with logfn==0 => discard*/) { | |
747 | if (!logfn && logfndata) | |
748 | logfn= logfn_file; | |
749 | if (configtext) | |
750 | return init_strcfg(newstate_r, flags, logfn, logfndata, configtext); | |
751 | else | |
752 | return init_files(newstate_r, flags, logfn, logfndata); | |
753 | } | |
1389dc72 | 754 | |
61093792 | 755 | void adns_finish(adns_state ads) { |
705b9b15 | 756 | int i; |
2ac463bf | 757 | adns__consistency(ads,0,cc_entex); |
61093792 | 758 | for (;;) { |
d0a057ac | 759 | if (ads->udpw.head) adns_cancel(ads->udpw.head); |
760 | else if (ads->tcpw.head) adns_cancel(ads->tcpw.head); | |
61093792 | 761 | else if (ads->childw.head) adns_cancel(ads->childw.head); |
762 | else if (ads->output.head) adns_cancel(ads->output.head); | |
763 | else break; | |
764 | } | |
705b9b15 | 765 | for (i = 0; i < ads->nudp; i++) close(ads->udpsocket[i].fd); |
61093792 | 766 | if (ads->tcpsocket >= 0) close(ads->tcpsocket); |
767 | adns__vbuf_free(&ads->tcpsend); | |
768 | adns__vbuf_free(&ads->tcprecv); | |
fc86e61f | 769 | freesearchlist(ads); |
61093792 | 770 | free(ads); |
6f17710a | 771 | } |
8f2aa812 | 772 | |
773 | void adns_forallqueries_begin(adns_state ads) { | |
2ac463bf | 774 | adns__consistency(ads,0,cc_entex); |
8f2aa812 | 775 | ads->forallnext= |
d0a057ac | 776 | ads->udpw.head ? ads->udpw.head : |
777 | ads->tcpw.head ? ads->tcpw.head : | |
8f2aa812 | 778 | ads->childw.head ? ads->childw.head : |
779 | ads->output.head; | |
780 | } | |
781 | ||
782 | adns_query adns_forallqueries_next(adns_state ads, void **context_r) { | |
783 | adns_query qu, nqu; | |
784 | ||
2ac463bf | 785 | adns__consistency(ads,0,cc_entex); |
8f2aa812 | 786 | nqu= ads->forallnext; |
787 | for (;;) { | |
788 | qu= nqu; | |
789 | if (!qu) return 0; | |
cb32030b | 790 | if (qu->next) { |
791 | nqu= qu->next; | |
d0a057ac | 792 | } else if (qu == ads->udpw.tail) { |
793 | nqu= | |
794 | ads->tcpw.head ? ads->tcpw.head : | |
795 | ads->childw.head ? ads->childw.head : | |
796 | ads->output.head; | |
797 | } else if (qu == ads->tcpw.tail) { | |
798 | nqu= | |
799 | ads->childw.head ? ads->childw.head : | |
800 | ads->output.head; | |
cb32030b | 801 | } else if (qu == ads->childw.tail) { |
802 | nqu= ads->output.head; | |
803 | } else { | |
804 | nqu= 0; | |
805 | } | |
8f2aa812 | 806 | if (!qu->parent) break; |
807 | } | |
808 | ads->forallnext= nqu; | |
809 | if (context_r) *context_r= qu->ctx.ext; | |
810 | return qu; | |
811 | } |