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