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