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