98db6da3 |
1 | /* |
2 | * setup.c |
3 | * - configuration file parsing |
4 | * - management of global state |
5 | */ |
6 | /* |
3ff64957 |
7 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
98db6da3 |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2, or (at your option) |
12 | * any later version. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software Foundation, |
21 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22 | */ |
6f17710a |
23 | |
d05cc330 |
24 | #include <stdlib.h> |
25 | #include <errno.h> |
26 | #include <string.h> |
27 | #include <unistd.h> |
28 | #include <fcntl.h> |
6f17710a |
29 | |
d05cc330 |
30 | #include <netdb.h> |
31 | #include <arpa/inet.h> |
32 | |
33 | #include "internal.h" |
34 | |
d6b271ae |
35 | static void readconfig(adns_state ads, const char *filename); |
36 | |
6f17710a |
37 | static void addserver(adns_state ads, struct in_addr addr) { |
38 | int i; |
39 | struct server *ss; |
40 | |
41 | for (i=0; i<ads->nservers; i++) { |
42 | if (ads->servers[i].addr.s_addr == addr.s_addr) { |
11c8bf9b |
43 | adns__debug(ads,-1,0,"duplicate nameserver %s ignored",inet_ntoa(addr)); |
6f17710a |
44 | return; |
45 | } |
46 | } |
47 | |
48 | if (ads->nservers>=MAXSERVERS) { |
11c8bf9b |
49 | adns__diag(ads,-1,0,"too many nameservers, ignoring %s",inet_ntoa(addr)); |
6f17710a |
50 | return; |
51 | } |
52 | |
53 | ss= ads->servers+ads->nservers; |
54 | ss->addr= addr; |
6f17710a |
55 | ads->nservers++; |
56 | } |
57 | |
d6b271ae |
58 | static void saveerr(adns_state ads, int en) { |
59 | if (!ads->configerrno) ads->configerrno= en; |
60 | } |
61 | |
6f17710a |
62 | static void configparseerr(adns_state ads, const char *fn, int lno, |
63 | const char *fmt, ...) { |
64 | va_list al; |
d6b271ae |
65 | |
66 | saveerr(ads,EINVAL); |
67 | if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return; |
68 | |
69 | if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn); |
70 | else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno); |
6f17710a |
71 | va_start(al,fmt); |
d6b271ae |
72 | vfprintf(ads->diagfile,fmt,al); |
6f17710a |
73 | va_end(al); |
d6b271ae |
74 | fputc('\n',ads->diagfile); |
6f17710a |
75 | } |
76 | |
11f553d9 |
77 | static int nextword(const char **bufp_io, const char **word_r, int *l_r) { |
78 | const char *p, *q; |
79 | |
80 | p= *bufp_io; |
81 | while (ctype_whitespace(*p)) p++; |
82 | if (!*p) return 0; |
83 | |
84 | q= p; |
85 | while (*q && !ctype_whitespace(*q)) q++; |
86 | |
87 | *l_r= q-p; |
88 | *word_r= p; |
89 | *bufp_io= q; |
90 | |
91 | return 1; |
92 | } |
93 | |
6f17710a |
94 | static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) { |
95 | struct in_addr ia; |
96 | |
97 | if (!inet_aton(buf,&ia)) { |
98 | configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf); |
99 | return; |
100 | } |
11c8bf9b |
101 | adns__debug(ads,-1,0,"using nameserver %s",inet_ntoa(ia)); |
6f17710a |
102 | addserver(ads,ia); |
103 | } |
104 | |
105 | static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) { |
11f553d9 |
106 | const char *bufp, *word; |
107 | char *newchars, **newptrs, **pp; |
108 | int count, tl, l; |
109 | |
6f17710a |
110 | if (!buf) return; |
11f553d9 |
111 | |
112 | bufp= buf; |
113 | count= 0; |
114 | tl= 0; |
115 | while (nextword(&bufp,&word,&l)) { count++; tl += l+1; } |
116 | |
117 | newptrs= malloc(sizeof(char*)*count); if (!newptrs) { saveerr(ads,errno); return; } |
118 | newchars= malloc(tl); if (!newchars) { saveerr(ads,errno); free(newptrs); return; } |
119 | |
120 | bufp= buf; |
121 | pp= newptrs; |
122 | while (nextword(&bufp,&word,&l)) { |
123 | *pp++= newchars; |
124 | memcpy(newchars,word,l); |
125 | newchars += l; |
126 | *newchars++ = 0; |
127 | } |
128 | |
129 | free(ads->searchlist); |
130 | ads->nsearchlist= count; |
131 | ads->searchlist= newptrs; |
132 | /* fixme: actually pay attention */ |
6f17710a |
133 | } |
134 | |
11f553d9 |
135 | static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) { |
136 | const char *word; |
8c3aa944 |
137 | char tbuf[200], *slash, *ep; |
138 | struct in_addr base, mask; |
139 | int l; |
140 | unsigned long initial, baselocal; |
141 | |
11f553d9 |
142 | if (!buf) return; |
143 | |
8c3aa944 |
144 | ads->nsortlist= 0; |
11f553d9 |
145 | while (nextword(&buf,&word,&l)) { |
8c3aa944 |
146 | if (ads->nsortlist >= MAXSORTLIST) { |
11f553d9 |
147 | adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,word); |
8c3aa944 |
148 | return; |
149 | } |
150 | |
151 | if (l >= sizeof(tbuf)) { |
11f553d9 |
152 | configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,word); |
8c3aa944 |
153 | continue; |
154 | } |
155 | |
11f553d9 |
156 | memcpy(tbuf,word,l); |
8c3aa944 |
157 | slash= strchr(tbuf,'/'); |
158 | if (slash) *slash++= 0; |
159 | |
160 | if (!inet_aton(tbuf,&base)) { |
161 | configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf); |
162 | continue; |
163 | } |
164 | |
165 | if (slash) { |
166 | if (strchr(slash,'.')) { |
167 | if (!inet_aton(slash,&mask)) { |
168 | configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash); |
169 | continue; |
170 | } |
171 | if (base.s_addr & ~mask.s_addr) { |
172 | configparseerr(ads,fn,lno, |
173 | "mask `%s' in sortlist overlaps address `%s'",slash,tbuf); |
174 | continue; |
175 | } |
176 | } else { |
177 | initial= strtoul(slash,&ep,10); |
178 | if (*ep || initial>32) { |
179 | configparseerr(ads,fn,lno,"mask length `%s' invalid",slash); |
180 | continue; |
181 | } |
182 | mask.s_addr= htonl((0x0ffffffffUL) << (32-initial)); |
183 | } |
184 | } else { |
185 | baselocal= ntohl(base.s_addr); |
186 | if (!baselocal & 0x080000000UL) /* class A */ |
187 | mask.s_addr= htonl(0x0ff000000UL); |
188 | else if ((baselocal & 0x0c0000000UL) == 0x080000000UL) |
189 | mask.s_addr= htonl(0x0ffff0000UL); /* class B */ |
190 | else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL) |
191 | mask.s_addr= htonl(0x0ff000000UL); /* class C */ |
192 | else { |
193 | configparseerr(ads,fn,lno, |
194 | "network address `%s' in sortlist is not in classed ranges," |
195 | " must specify mask explicitly", tbuf); |
196 | continue; |
197 | } |
198 | } |
199 | |
200 | ads->sortlist[ads->nsortlist].base= base; |
201 | ads->sortlist[ads->nsortlist].mask= mask; |
202 | ads->nsortlist++; |
203 | } |
6f17710a |
204 | } |
205 | |
206 | static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) { |
207 | if (!buf) return; |
11c8bf9b |
208 | adns__diag(ads,-1,0,"warning - `options' ignored fixme"); |
6f17710a |
209 | } |
210 | |
211 | static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) { |
212 | ads->nservers= 0; |
213 | } |
214 | |
d6b271ae |
215 | static void ccf_include(adns_state ads, const char *fn, int lno, const char *buf) { |
216 | if (!*buf) { |
217 | configparseerr(ads,fn,lno,"`include' directive with no filename"); |
218 | return; |
219 | } |
220 | readconfig(ads,buf); |
221 | } |
222 | |
6f17710a |
223 | static const struct configcommandinfo { |
224 | const char *name; |
225 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); |
226 | } configcommandinfos[]= { |
227 | { "nameserver", ccf_nameserver }, |
228 | { "domain", ccf_search }, |
229 | { "search", ccf_search }, |
230 | { "sortlist", ccf_sortlist }, |
231 | { "options", ccf_options }, |
232 | { "clearnameservers", ccf_clearnss }, |
d6b271ae |
233 | { "include", ccf_include }, |
6f17710a |
234 | { 0 } |
235 | }; |
236 | |
d6b271ae |
237 | typedef union { |
6f17710a |
238 | FILE *file; |
d6b271ae |
239 | const char *text; |
240 | } getline_ctx; |
6f17710a |
241 | |
d6b271ae |
242 | static int gl_file(adns_state ads, getline_ctx *src_io, const char *filename, |
243 | int lno, char *buf, int buflen) { |
244 | FILE *file= src_io->file; |
245 | int c, i; |
246 | char *p; |
247 | |
248 | p= buf; |
249 | buflen--; |
250 | i= 0; |
251 | |
252 | for (;;) { /* loop over chars */ |
253 | if (i == buflen) { |
254 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); |
255 | goto x_badline; |
256 | } |
257 | c= getc(file); |
258 | if (!c) { |
259 | adns__diag(ads,-1,0,"%s:%d: line contains nul, ignored",filename,lno); |
260 | goto x_badline; |
261 | } else if (c == '\n') { |
262 | break; |
263 | } else if (c == EOF) { |
264 | if (ferror(file)) { |
265 | saveerr(ads,errno); |
266 | adns__diag(ads,-1,0,"%s:%d: read error: %s",filename,lno,strerror(errno)); |
267 | return -1; |
268 | } |
269 | if (!i) return -1; |
270 | break; |
271 | } else { |
272 | *p++= c; |
273 | i++; |
6f17710a |
274 | } |
6f17710a |
275 | } |
276 | |
d6b271ae |
277 | *p++= 0; |
278 | return i; |
279 | |
280 | x_badline: |
281 | saveerr(ads,EINVAL); |
282 | while ((c= getc(file)) != EOF && c != '\n'); |
283 | return -2; |
284 | } |
285 | |
286 | static int gl_text(adns_state ads, getline_ctx *src_io, const char *filename, |
287 | int lno, char *buf, int buflen) { |
8c3aa944 |
288 | const char *cp= src_io->text; |
d6b271ae |
289 | int l; |
290 | |
8c3aa944 |
291 | if (!cp || !*cp) return -1; |
d6b271ae |
292 | |
8c3aa944 |
293 | if (*cp == ';' || *cp == '\n') cp++; |
294 | l= strcspn(cp,";\n"); |
295 | src_io->text = cp+l; |
d6b271ae |
296 | |
297 | if (l >= buflen) { |
298 | adns__diag(ads,-1,0,"%s:%d: line too long, ignored",filename,lno); |
299 | saveerr(ads,EINVAL); |
300 | return -2; |
301 | } |
302 | |
303 | memcpy(buf,cp,l); |
304 | buf[l]= 0; |
305 | return l; |
306 | } |
307 | |
308 | static void readconfiggeneric(adns_state ads, const char *filename, |
309 | int (*getline)(adns_state ads, getline_ctx*, |
310 | const char *filename, int lno, |
311 | char *buf, int buflen), |
312 | /* Returns >=0 for success, -1 for EOF or error |
313 | * (error will have been reported), or -2 for |
314 | * bad line was encountered, try again. |
315 | */ |
316 | getline_ctx gl_ctx) { |
317 | char linebuf[2000], *p, *q; |
318 | int lno, l, dirl; |
319 | const struct configcommandinfo *ccip; |
320 | |
321 | for (lno=1; |
322 | (l= getline(ads,&gl_ctx, filename,lno, linebuf,sizeof(linebuf))) != -1; |
323 | lno++) { |
324 | if (l == -2) continue; |
6f17710a |
325 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
326 | linebuf[l]= 0; |
327 | p= linebuf; |
328 | while (ctype_whitespace(*p)) p++; |
d6b271ae |
329 | if (*p == '#' || !*p) continue; |
6f17710a |
330 | q= p; |
331 | while (*q && !ctype_whitespace(*q)) q++; |
d6b271ae |
332 | dirl= q-p; |
6f17710a |
333 | for (ccip=configcommandinfos; |
d6b271ae |
334 | ccip->name && !(strlen(ccip->name)==dirl && !memcmp(ccip->name,p,q-p)); |
6f17710a |
335 | ccip++); |
336 | if (!ccip->name) { |
11c8bf9b |
337 | adns__diag(ads,-1,0,"%s:%d: unknown configuration directive `%.*s'", |
d05cc330 |
338 | filename,lno,q-p,p); |
6f17710a |
339 | continue; |
340 | } |
341 | while (ctype_whitespace(*q)) q++; |
342 | ccip->fn(ads,filename,lno,q); |
343 | } |
6f17710a |
344 | } |
345 | |
346 | static const char *instrum_getenv(adns_state ads, const char *envvar) { |
347 | const char *value; |
348 | |
349 | value= getenv(envvar); |
11c8bf9b |
350 | if (!value) adns__debug(ads,-1,0,"environment variable %s not set",envvar); |
351 | else adns__debug(ads,-1,0,"environment variable %s set to `%s'",envvar,value); |
6f17710a |
352 | return value; |
353 | } |
354 | |
d6b271ae |
355 | static void readconfig(adns_state ads, const char *filename) { |
356 | getline_ctx gl_ctx; |
357 | |
358 | gl_ctx.file= fopen(filename,"r"); |
359 | if (!gl_ctx.file) { |
360 | if (errno == ENOENT) { |
361 | adns__debug(ads,-1,0,"configuration file `%s' does not exist",filename); |
362 | return; |
363 | } |
364 | saveerr(ads,errno); |
365 | adns__diag(ads,-1,0,"cannot open configuration file `%s': %s", |
366 | filename,strerror(errno)); |
367 | return; |
368 | } |
369 | |
370 | readconfiggeneric(ads,filename,gl_file,gl_ctx); |
371 | |
372 | fclose(gl_ctx.file); |
373 | } |
374 | |
375 | static void readconfigtext(adns_state ads, const char *text, const char *showname) { |
376 | getline_ctx gl_ctx; |
377 | |
378 | gl_ctx.text= text; |
379 | readconfiggeneric(ads,showname,gl_text,gl_ctx); |
380 | } |
381 | |
6f17710a |
382 | static void readconfigenv(adns_state ads, const char *envvar) { |
383 | const char *filename; |
384 | |
385 | if (ads->iflags & adns_if_noenv) { |
11c8bf9b |
386 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
6f17710a |
387 | return; |
388 | } |
389 | filename= instrum_getenv(ads,envvar); |
390 | if (filename) readconfig(ads,filename); |
391 | } |
d05cc330 |
392 | |
8c3aa944 |
393 | static void readconfigenvtext(adns_state ads, const char *envvar) { |
394 | const char *textdata; |
395 | |
396 | if (ads->iflags & adns_if_noenv) { |
397 | adns__debug(ads,-1,0,"not checking environment variable `%s'",envvar); |
398 | return; |
399 | } |
400 | textdata= instrum_getenv(ads,envvar); |
401 | if (textdata) readconfigtext(ads,textdata,envvar); |
402 | } |
403 | |
d05cc330 |
404 | |
405 | int adns__setnonblock(adns_state ads, int fd) { |
406 | int r; |
6f17710a |
407 | |
d05cc330 |
408 | r= fcntl(fd,F_GETFL,0); if (r<0) return errno; |
409 | r |= O_NONBLOCK; |
410 | r= fcntl(fd,F_SETFL,r); if (r<0) return errno; |
411 | return 0; |
412 | } |
413 | |
d6b271ae |
414 | static int init_begin(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
6f17710a |
415 | adns_state ads; |
6f17710a |
416 | |
417 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
d6b271ae |
418 | |
6f17710a |
419 | ads->iflags= flags; |
d6b271ae |
420 | ads->diagfile= diagfile; |
d05cc330 |
421 | LIST_INIT(ads->timew); |
422 | LIST_INIT(ads->childw); |
423 | LIST_INIT(ads->output); |
424 | ads->nextid= 0x311f; |
425 | ads->udpsocket= ads->tcpsocket= -1; |
d05cc330 |
426 | adns__vbuf_init(&ads->tcpsend); |
427 | adns__vbuf_init(&ads->tcprecv); |
11f553d9 |
428 | ads->nservers= ads->nsortlist= ads->nsearchlist= ads->tcpserver= 0; |
d05cc330 |
429 | ads->tcpstate= server_disconnected; |
11f553d9 |
430 | ads->searchlist= 0; |
7e6a84a1 |
431 | ads->searchndots= 1; |
d05cc330 |
432 | timerclear(&ads->tcptimeout); |
6f17710a |
433 | |
d6b271ae |
434 | *ads_r= ads; |
435 | return 0; |
436 | } |
6f17710a |
437 | |
d6b271ae |
438 | static int init_finish(adns_state ads) { |
439 | struct in_addr ia; |
440 | struct protoent *proto; |
441 | int r; |
442 | |
6f17710a |
443 | if (!ads->nservers) { |
d6b271ae |
444 | if (ads->diagfile && ads->iflags & adns_if_debug) |
445 | fprintf(ads->diagfile,"adns: no nameservers, using localhost\n"); |
8c3aa944 |
446 | ia.s_addr= htonl(INADDR_LOOPBACK); |
6f17710a |
447 | addserver(ads,ia); |
448 | } |
449 | |
450 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } |
451 | ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto); |
71324651 |
452 | if (ads->udpsocket<0) { r= errno; goto x_free; } |
48cb0b4b |
453 | |
de8b18da |
454 | r= adns__setnonblock(ads,ads->udpsocket); |
455 | if (r) { r= errno; goto x_closeudp; } |
6f17710a |
456 | |
6f17710a |
457 | return 0; |
458 | |
de8b18da |
459 | x_closeudp: |
460 | close(ads->udpsocket); |
6f17710a |
461 | x_free: |
462 | free(ads); |
463 | return r; |
464 | } |
465 | |
11f553d9 |
466 | static void init_abort(adns_state ads) { |
467 | if (ads->nsearchlist) { |
468 | free(ads->searchlist[0]); |
469 | free(ads->searchlist); |
470 | } |
471 | free(ads); |
472 | } |
473 | |
d6b271ae |
474 | int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { |
475 | adns_state ads; |
476 | const char *res_options, *adns_res_options; |
477 | int r; |
478 | |
479 | r= init_begin(&ads, flags, diagfile ? diagfile : stderr); |
480 | if (r) return r; |
481 | |
482 | res_options= instrum_getenv(ads,"RES_OPTIONS"); |
483 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); |
484 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
485 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
486 | |
487 | readconfig(ads,"/etc/resolv.conf"); |
488 | readconfigenv(ads,"RES_CONF"); |
489 | readconfigenv(ads,"ADNS_RES_CONF"); |
490 | |
8c3aa944 |
491 | readconfigenvtext(ads,"RES_CONF_TEXT"); |
492 | readconfigenvtext(ads,"ADNS_RES_CONF_TEXT"); |
493 | |
d6b271ae |
494 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
495 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
496 | |
497 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); |
498 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); |
499 | |
11f553d9 |
500 | if (ads->configerrno && ads->configerrno != EINVAL) { |
501 | r= ads->configerrno; |
502 | init_abort(ads); |
503 | return r; |
504 | } |
505 | |
d6b271ae |
506 | r= init_finish(ads); |
507 | if (r) return r; |
508 | |
509 | *ads_r= ads; |
510 | return 0; |
511 | } |
512 | |
513 | int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, |
514 | FILE *diagfile, const char *configtext) { |
515 | adns_state ads; |
516 | int r; |
517 | |
518 | r= init_begin(&ads, flags, diagfile); if (r) return r; |
519 | |
520 | readconfigtext(ads,configtext,"<supplied configuration text>"); |
521 | if (ads->configerrno) { |
522 | r= ads->configerrno; |
11f553d9 |
523 | init_abort(ads); |
d6b271ae |
524 | return r; |
525 | } |
526 | |
527 | r= init_finish(ads); if (r) return r; |
528 | *ads_r= ads; |
529 | return 0; |
530 | } |
531 | |
61093792 |
532 | void adns_finish(adns_state ads) { |
533 | for (;;) { |
534 | if (ads->timew.head) adns_cancel(ads->timew.head); |
535 | else if (ads->childw.head) adns_cancel(ads->childw.head); |
536 | else if (ads->output.head) adns_cancel(ads->output.head); |
537 | else break; |
538 | } |
539 | close(ads->udpsocket); |
540 | if (ads->tcpsocket >= 0) close(ads->tcpsocket); |
541 | adns__vbuf_free(&ads->tcpsend); |
542 | adns__vbuf_free(&ads->tcprecv); |
543 | free(ads); |
6f17710a |
544 | } |