a17d3a1d |
1 | /**/ |
2 | |
964344fc |
3 | #include <stdarg.h> |
4 | #include <stdio.h> |
5 | #include <errno.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
e8c505a5 |
8 | #include <assert.h> |
80917fcc |
9 | #include <unistd.h> |
964344fc |
10 | |
80917fcc |
11 | #include <netdb.h> |
a17d3a1d |
12 | #include <arpa/nameser.h> |
964344fc |
13 | #include <sys/socket.h> |
14 | #include <netinet/in.h> |
15 | #include <arpa/inet.h> |
a17d3a1d |
16 | |
17 | #include "adns-internal.h" |
18 | |
19 | #define LIST_UNLINK(list,node) \ |
20 | do { \ |
21 | if ((node)->back) (node)->back->next= (node)->next; \ |
22 | else (list).head= (node)->next; \ |
23 | if ((node)->next) (node)->next->back= (node)->back; \ |
24 | else (list).tail= (node)->back; \ |
25 | } while(0) |
26 | |
27 | #define LIST_LINK_TAIL(list,node) \ |
28 | do { \ |
29 | (node)->back= 0; \ |
30 | (node)->next= (list).tail; \ |
964344fc |
31 | if ((list).tail) (list).tail->back= (node); else (list).head= (node); \ |
a17d3a1d |
32 | (list).tail= (node); \ |
33 | } while(0) |
34 | |
964344fc |
35 | static void vdebug(adns_state ads, const char *fmt, va_list al) { |
36 | if (!(ads->iflags & adns_if_debug)) return; |
37 | fputs("adns debug: ",stderr); |
38 | vfprintf(stderr,fmt,al); |
39 | fputc('\n',stderr); |
40 | } |
41 | |
42 | static void debug(adns_state ads, const char *fmt, ...) { |
43 | va_list al; |
44 | |
45 | va_start(al,fmt); |
46 | vdebug(ads,fmt,al); |
47 | va_end(al); |
48 | } |
49 | |
50 | static void vdiag(adns_state ads, const char *fmt, va_list al) { |
51 | if (ads->iflags & adns_if_noerrprint) return; |
52 | fputs("adns: ",stderr); |
53 | vfprintf(stderr,fmt,al); |
54 | fputc('\n',stderr); |
55 | } |
56 | |
57 | static void diag(adns_state ads, const char *fmt, ...) { |
58 | va_list al; |
59 | |
60 | va_start(al,fmt); |
61 | vdiag(ads,fmt,al); |
62 | va_end(al); |
63 | } |
64 | |
65 | static void addserver(adns_state ads, struct in_addr addr) { |
e8c505a5 |
66 | int i; |
80917fcc |
67 | struct server *ss; |
e8c505a5 |
68 | |
69 | for (i=0; i<ads->nservers; i++) { |
70 | if (ads->servers[i].addr.s_addr == addr.s_addr) { |
71 | debug(ads,"duplicate nameserver %s ignored",inet_ntoa(addr)); |
72 | return; |
73 | } |
74 | } |
75 | |
a17d3a1d |
76 | if (ads->nservers>=MAXSERVERS) { |
964344fc |
77 | diag(ads,"too many nameservers, ignoring %s",inet_ntoa(addr)); |
e8c505a5 |
78 | return; |
a17d3a1d |
79 | } |
e8c505a5 |
80 | |
80917fcc |
81 | ss= ads->servers+ads->nservers; |
82 | ss->addr= addr; |
83 | ss->state= server_disc; |
84 | ss->connw.head= ss->connw.tail= 0; |
e8c505a5 |
85 | ads->nservers++; |
a17d3a1d |
86 | } |
87 | |
964344fc |
88 | static void configparseerr(adns_state ads, const char *fn, int lno, |
89 | const char *fmt, ...) { |
90 | va_list al; |
91 | |
92 | if (ads->iflags & adns_if_noerrprint) return; |
93 | if (lno==-1) fprintf(stderr,"adns: %s: ",fn); |
94 | else fprintf(stderr,"adns: %s:%d: ",fn,lno); |
95 | va_start(al,fmt); |
96 | vfprintf(stderr,fmt,al); |
97 | va_end(al); |
98 | fputc('\n',stderr); |
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 | debug(ads,"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 | if (!buf) return; |
114 | diag(ads,"warning - `search' ignored FIXME"); |
115 | } |
116 | |
117 | static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) { |
118 | diag(ads,"warning - `sortlist' ignored FIXME"); |
119 | } |
120 | |
121 | static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) { |
122 | if (!buf) return; |
123 | diag(ads,"warning - `options' ignored FIXME"); |
124 | } |
125 | |
126 | static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) { |
127 | ads->nservers= 0; |
128 | } |
129 | |
130 | static const struct configcommandinfo { |
131 | const char *name; |
132 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); |
133 | } configcommandinfos[]= { |
134 | { "nameserver", ccf_nameserver }, |
135 | { "domain", ccf_search }, |
136 | { "search", ccf_search }, |
137 | { "sortlist", ccf_sortlist }, |
138 | { "options", ccf_options }, |
139 | { "clearnameservers", ccf_clearnss }, |
140 | { 0 } |
141 | }; |
142 | |
350d37d9 |
143 | static int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; } |
144 | static int ctype_digit(int c) { return c>='0' && c<='9'; } |
964344fc |
145 | |
146 | static void readconfig(adns_state ads, const char *filename) { |
147 | char linebuf[2000], *p, *q; |
148 | FILE *file; |
149 | int lno, l, c; |
150 | const struct configcommandinfo *ccip; |
151 | |
152 | file= fopen(filename,"r"); |
153 | if (!file) { |
154 | if (errno == ENOENT) { |
155 | debug(ads,"configuration file `%s' does not exist",filename); |
156 | return; |
157 | } |
158 | diag(ads,"cannot open configuration file `%s': %s",filename,strerror(errno)); |
159 | return; |
160 | } |
161 | |
162 | for (lno=1; fgets(linebuf,sizeof(linebuf),file); lno++) { |
163 | l= strlen(linebuf); |
164 | if (!l) continue; |
165 | if (linebuf[l-1] != '\n' && !feof(file)) { |
166 | diag(ads,"%s:%d: line too long",filename,lno); |
167 | while ((c= getc(file)) != EOF && c != '\n') { } |
168 | if (c == EOF) break; |
169 | continue; |
170 | } |
350d37d9 |
171 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
964344fc |
172 | linebuf[l]= 0; |
173 | p= linebuf; |
350d37d9 |
174 | while (ctype_whitespace(*p)) p++; |
964344fc |
175 | if (*p == '#' || *p == '\n') continue; |
176 | q= p; |
350d37d9 |
177 | while (*q && !ctype_whitespace(*q)) q++; |
964344fc |
178 | for (ccip=configcommandinfos; |
179 | ccip->name && strncmp(ccip->name,p,q-p); |
180 | ccip++); |
181 | if (!ccip->name) { |
182 | diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p); |
183 | continue; |
184 | } |
350d37d9 |
185 | while (ctype_whitespace(*q)) q++; |
964344fc |
186 | ccip->fn(ads,filename,lno,q); |
187 | } |
188 | if (ferror(file)) { |
189 | diag(ads,"%s:%d: read error: %s",filename,lno,strerror(errno)); |
190 | } |
191 | fclose(file); |
192 | } |
193 | |
194 | static const char *instrum_getenv(adns_state ads, const char *envvar) { |
195 | const char *value; |
196 | |
197 | value= getenv(envvar); |
e8c505a5 |
198 | if (!value) debug(ads,"environment variable %s not set",envvar); |
199 | else debug(ads,"environment variable %s set to `%s'",envvar,value); |
964344fc |
200 | return value; |
a17d3a1d |
201 | } |
202 | |
964344fc |
203 | static void readconfigenv(adns_state ads, const char *envvar) { |
a17d3a1d |
204 | const char *filename; |
205 | |
964344fc |
206 | if (ads->iflags & adns_if_noenv) { |
207 | debug(ads,"not checking environment variable `%s'",envvar); |
208 | return; |
209 | } |
210 | filename= instrum_getenv(ads,envvar); |
211 | if (filename) readconfig(ads,filename); |
a17d3a1d |
212 | } |
213 | |
964344fc |
214 | int adns_init(adns_state *ads_r, adns_initflags flags) { |
a17d3a1d |
215 | adns_state ads; |
964344fc |
216 | const char *res_options, *adns_res_options; |
80917fcc |
217 | struct protoent *proto; |
350d37d9 |
218 | int r; |
a17d3a1d |
219 | |
220 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
964344fc |
221 | ads->input.head= ads->input.tail= 0; |
a17d3a1d |
222 | ads->timew.head= ads->timew.tail= 0; |
964344fc |
223 | ads->childw.head= ads->childw.tail= 0; |
224 | ads->output.head= ads->output.tail= 0; |
80917fcc |
225 | ads->nextid= 0x311f; |
a17d3a1d |
226 | ads->udpsocket= -1; |
227 | ads->qbufavail= 0; |
228 | ads->qbuf= 0; |
229 | ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0; |
230 | ads->tcpbuf= 0; |
964344fc |
231 | ads->iflags= flags; |
a17d3a1d |
232 | ads->nservers= 0; |
964344fc |
233 | ads->iflags= flags; |
234 | |
235 | res_options= instrum_getenv(ads,"RES_OPTIONS"); |
236 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); |
237 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
238 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
a17d3a1d |
239 | |
240 | readconfig(ads,"/etc/resolv.conf"); |
241 | readconfigenv(ads,"RES_CONF"); |
242 | readconfigenv(ads,"ADNS_RES_CONF"); |
964344fc |
243 | |
244 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
245 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
246 | |
247 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); |
248 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); |
249 | |
a17d3a1d |
250 | if (!ads->nservers) { |
964344fc |
251 | struct in_addr ia; |
252 | if (ads->iflags & adns_if_debug) |
a17d3a1d |
253 | fprintf(stderr,"adns: no nameservers, using localhost\n"); |
964344fc |
254 | ia.s_addr= INADDR_LOOPBACK; |
255 | addserver(ads,ia); |
a17d3a1d |
256 | } |
80917fcc |
257 | |
258 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } |
259 | ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto); |
260 | if (!ads->udpsocket) { r= errno; goto x_closeudp; } |
a17d3a1d |
261 | |
262 | *ads_r= ads; |
263 | return 0; |
80917fcc |
264 | |
265 | x_closeudp: |
266 | close(ads->udpsocket); |
267 | x_free: |
268 | free(ads); |
269 | return r; |
a17d3a1d |
270 | } |
271 | |
964344fc |
272 | static void query_fail(adns_state ads, adns_query qu, adns_status stat) { |
e8c505a5 |
273 | adns_answer *ans; |
a17d3a1d |
274 | |
275 | ans= qu->answer; |
276 | if (!ans) ans= malloc(sizeof(*qu->answer)); |
277 | if (ans) { |
278 | ans->status= stat; |
279 | ans->cname= 0; |
280 | ans->type= qu->type; |
281 | ans->nrrs= 0; |
282 | } |
283 | qu->answer= ans; |
964344fc |
284 | LIST_LINK_TAIL(ads->input,qu); |
a17d3a1d |
285 | } |
286 | |
80917fcc |
287 | int adns_finish(adns_state ads) { |
288 | abort(); /* FIXME */ |
289 | } |
290 | |
e8c505a5 |
291 | void adns_interest(adns_state ads, int *maxfd, |
964344fc |
292 | fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
e8c505a5 |
293 | struct timeval **tv_io, struct timeval *tvbuf) { |
964344fc |
294 | abort(); /* FIXME */ |
a17d3a1d |
295 | } |
296 | |
e8c505a5 |
297 | static void autosys(adns_state ads) { |
298 | if (ads->iflags & adns_if_noautosys) return; |
299 | adns_callback(ads,-1,0,0,0); |
300 | } |
301 | |
302 | void adns_cancel(adns_state ads, adns_query query) { |
303 | abort(); /* FIXME */ |
304 | } |
305 | |
306 | int adns_callback(adns_state ads, int maxfd, |
307 | const fd_set *readfds, const fd_set *writefds, |
308 | const fd_set *exceptfds) { |
309 | abort(); /* FIXME */ |
310 | } |
311 | |
312 | static int internal_check(adns_state ads, |
313 | adns_query *query_io, |
314 | adns_answer *answer, |
315 | void *context_r) { |
316 | abort(); /* FIXME */ |
317 | } |
318 | |
319 | int adns_wait(adns_state ads, |
320 | adns_query *query_io, |
321 | adns_answer *answer, |
322 | void *context_r) { |
323 | int r, maxfd, rsel, rcb; |
324 | fd_set readfds, writefds, exceptfds; |
325 | struct timeval tvbuf, *tvp; |
326 | |
327 | for (;;) { |
328 | r= internal_check(ads,query_io,answer,context_r); |
329 | if (r && r != EWOULDBLOCK) return r; |
330 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
331 | maxfd= 0; tvp= 0; |
332 | adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); |
333 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
334 | if (rsel==-1) return r; |
335 | rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); |
336 | assert(rcb==rsel); |
337 | } |
338 | } |
339 | |
340 | int adns_check(adns_state ads, |
341 | adns_query *query_io, |
342 | adns_answer *answer, |
343 | void *context_r) { |
344 | autosys(ads); |
345 | return internal_check(ads,query_io,answer,context_r); |
346 | } |
347 | |
348 | int adns_synchronous(adns_state ads, |
349 | const char *owner, |
350 | adns_rrtype type, |
72934832 |
351 | adns_queryflags flags, |
e8c505a5 |
352 | adns_answer *answer) { |
353 | adns_query qu; |
354 | int r; |
355 | |
356 | r= adns_submit(ads,owner,type,flags,0,&qu); |
357 | if (r) return r; |
358 | |
72934832 |
359 | do { |
360 | r= adns_wait(ads,&qu,answer,0); |
361 | } while (r==EINTR); |
e8c505a5 |
362 | if (r) adns_cancel(ads,qu); |
363 | return r; |
364 | } |
365 | |
350d37d9 |
366 | static adns_status mkquery(adns_state ads, const char *owner, int ol, int id, |
367 | adns_rrtype type, adns_queryflags flags, int *qml_r) { |
368 | int ll, c, nlabs, qbufreq; |
369 | unsigned char label[255], *nqbuf; |
370 | const char *p, *pe; |
371 | |
372 | #define MKQUERY_ADDB(b) *nqbuf++= (b) |
373 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
374 | |
375 | qbufreq= 12+strlen(owner)+3; |
376 | if (ads->qbufavail < qbufreq) { |
377 | nqbuf= realloc(ads->qbuf,qbufreq); |
378 | if (!nqbuf) return adns_s_nolocalmem; |
379 | ads->qbuf= nqbuf; ads->qbufavail= qbufreq; |
380 | } |
381 | nqbuf= ads->qbuf; |
382 | |
383 | MKQUERY_ADDW(id); |
384 | MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ |
385 | MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ |
386 | MKQUERY_ADDW(1); /* QDCOUNT=1 */ |
387 | MKQUERY_ADDW(0); /* ANCOUNT=0 */ |
388 | MKQUERY_ADDW(0); /* NSCOUNT=0 */ |
389 | MKQUERY_ADDW(0); /* ARCOUNT=0 */ |
390 | p= owner; pe= owner+ol; |
391 | nlabs= 0; |
392 | if (!*p) return adns_s_invaliddomain; |
393 | do { |
394 | ll= 0; |
395 | while (p!=pe && (c= *p++)!='.') { |
396 | if (c=='\\') { |
397 | if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain; |
398 | if (ctype_digit(p[0])) { |
399 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
400 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
401 | if (c >= 256) return adns_s_invaliddomain; |
402 | } else { |
403 | return adns_s_invaliddomain; |
404 | } |
405 | } else if (!(c= *p++)) { |
406 | return adns_s_invaliddomain; |
407 | } |
408 | } |
409 | if (!(flags & adns_f_anyquote)) { |
410 | if ((c >= '0' && c <= '9') || c == '-') { |
411 | if (!ll) return adns_s_invaliddomain; |
412 | } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) { |
413 | return adns_s_invaliddomain; |
414 | } |
415 | } |
416 | if (ll == sizeof(label)) return adns_s_invaliddomain; |
417 | label[ll++]= c; |
418 | } |
419 | if (!ll) return adns_s_invaliddomain; |
420 | if (nlabs++ > 63) return adns_s_invaliddomain; |
421 | MKQUERY_ADDB(ll); |
422 | memcpy(nqbuf,label,ll); nqbuf+= ll; |
423 | } while (p!=pe); |
424 | |
425 | MKQUERY_ADDB(0); |
426 | MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */ |
427 | MKQUERY_ADDW(1); /* QCLASS=IN */ |
428 | |
429 | *qml_r= nqbuf - ads->qbuf; |
430 | |
431 | return adns_s_ok; |
80917fcc |
432 | } |
433 | |
350d37d9 |
434 | static adns_query allocquery(adns_state ads, const char *owner, int ol, |
435 | int qml, int id, adns_rrtype type, |
436 | adns_queryflags flags, void *context) { |
a17d3a1d |
437 | adns_query qu; |
350d37d9 |
438 | unsigned char *qm; |
439 | |
440 | qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0; |
a17d3a1d |
441 | qu->next= qu->back= qu->parent= qu->child= 0; |
350d37d9 |
442 | qu->id= id; |
a17d3a1d |
443 | qu->type= type; |
444 | qu->answer= 0; |
445 | qu->flags= flags; |
446 | qu->context= context; |
964344fc |
447 | qu->udpretries= 0; |
80917fcc |
448 | qu->sentudp= qu->senttcp= 0; |
449 | qu->nextserver= 0; |
a17d3a1d |
450 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
350d37d9 |
451 | qu->querymsg= qm= qu->owner+ol+1; |
452 | memcpy(qm,ads->qbuf,qml); |
453 | qu->querylen= qml; |
454 | return qu; |
455 | } |
80917fcc |
456 | |
350d37d9 |
457 | static int failsubmit(adns_state ads, void *context, adns_query *query_r, |
458 | adns_rrtype type, adns_queryflags flags, |
459 | int id, adns_status stat) { |
460 | adns_query qu; |
80917fcc |
461 | |
350d37d9 |
462 | qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno; |
463 | query_fail(ads,qu,stat); |
464 | *query_r= qu; |
465 | return 0; |
466 | } |
467 | |
468 | int adns_submit(adns_state ads, |
469 | const char *owner, |
470 | adns_rrtype type, |
471 | adns_queryflags flags, |
472 | void *context, |
473 | adns_query *query_r) { |
474 | adns_query qu; |
475 | adns_status stat; |
476 | int ol, id, qml; |
477 | |
478 | id= ads->nextid++; |
479 | |
480 | ol= strlen(owner); |
481 | if (ol<=1 || ol>MAXDNAME+1) |
482 | return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain); |
483 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; } |
484 | |
485 | stat= mkquery(ads,owner,ol,id,type,flags,&qml); |
486 | if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat); |
487 | |
488 | qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno; |
489 | |
490 | LIST_LINK_TAIL(ads->input,qu); |
491 | autosys(ads); |
80917fcc |
492 | |
a17d3a1d |
493 | *query_r= qu; |
e8c505a5 |
494 | return 0; |
a17d3a1d |
495 | } |