| 1 | /**/ |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <assert.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include <netdb.h> |
| 12 | #include <arpa/nameser.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <arpa/inet.h> |
| 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; \ |
| 31 | if ((list).tail) (list).tail->back= (node); else (list).head= (node); \ |
| 32 | (list).tail= (node); \ |
| 33 | } while(0) |
| 34 | |
| 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) { |
| 66 | int i; |
| 67 | struct server *ss; |
| 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 | |
| 76 | if (ads->nservers>=MAXSERVERS) { |
| 77 | diag(ads,"too many nameservers, ignoring %s",inet_ntoa(addr)); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | ss= ads->servers+ads->nservers; |
| 82 | ss->addr= addr; |
| 83 | ss->state= server_disc; |
| 84 | ss->connw.head= ss->connw.tail= 0; |
| 85 | ads->nservers++; |
| 86 | } |
| 87 | |
| 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 | |
| 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'; } |
| 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 | } |
| 171 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
| 172 | linebuf[l]= 0; |
| 173 | p= linebuf; |
| 174 | while (ctype_whitespace(*p)) p++; |
| 175 | if (*p == '#' || *p == '\n') continue; |
| 176 | q= p; |
| 177 | while (*q && !ctype_whitespace(*q)) q++; |
| 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 | } |
| 185 | while (ctype_whitespace(*q)) q++; |
| 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); |
| 198 | if (!value) debug(ads,"environment variable %s not set",envvar); |
| 199 | else debug(ads,"environment variable %s set to `%s'",envvar,value); |
| 200 | return value; |
| 201 | } |
| 202 | |
| 203 | static void readconfigenv(adns_state ads, const char *envvar) { |
| 204 | const char *filename; |
| 205 | |
| 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); |
| 212 | } |
| 213 | |
| 214 | int adns_init(adns_state *ads_r, adns_initflags flags) { |
| 215 | adns_state ads; |
| 216 | const char *res_options, *adns_res_options; |
| 217 | struct protoent *proto; |
| 218 | int r; |
| 219 | |
| 220 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
| 221 | ads->tosend.head= ads->tosend.tail= 0; |
| 222 | ads->timew.head= ads->timew.tail= 0; |
| 223 | ads->childw.head= ads->childw.tail= 0; |
| 224 | ads->output.head= ads->output.tail= 0; |
| 225 | ads->nextid= 0x311f; |
| 226 | ads->udpsocket= -1; |
| 227 | ads->qbufavail= 0; |
| 228 | ads->qbuf= 0; |
| 229 | ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0; |
| 230 | ads->tcpbuf= 0; |
| 231 | ads->iflags= flags; |
| 232 | ads->nservers= 0; |
| 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); |
| 239 | |
| 240 | readconfig(ads,"/etc/resolv.conf"); |
| 241 | readconfigenv(ads,"RES_CONF"); |
| 242 | readconfigenv(ads,"ADNS_RES_CONF"); |
| 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 | |
| 250 | if (!ads->nservers) { |
| 251 | struct in_addr ia; |
| 252 | if (ads->iflags & adns_if_debug) |
| 253 | fprintf(stderr,"adns: no nameservers, using localhost\n"); |
| 254 | ia.s_addr= INADDR_LOOPBACK; |
| 255 | addserver(ads,ia); |
| 256 | } |
| 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; } |
| 261 | |
| 262 | *ads_r= ads; |
| 263 | return 0; |
| 264 | |
| 265 | x_closeudp: |
| 266 | close(ads->udpsocket); |
| 267 | x_free: |
| 268 | free(ads); |
| 269 | return r; |
| 270 | } |
| 271 | |
| 272 | static void query_fail(adns_state ads, adns_query qu, adns_status stat) { |
| 273 | adns_answer *ans; |
| 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; |
| 284 | qu->id= -1; |
| 285 | LIST_LINK_TAIL(ads->output,qu); |
| 286 | } |
| 287 | |
| 288 | int adns_finish(adns_state ads) { |
| 289 | abort(); /* FIXME */ |
| 290 | } |
| 291 | |
| 292 | void adns_interest(adns_state ads, int *maxfd, |
| 293 | fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 294 | struct timeval **tv_io, struct timeval *tvbuf) { |
| 295 | abort(); /* FIXME */ |
| 296 | } |
| 297 | |
| 298 | static void autosys(adns_state ads) { |
| 299 | if (ads->iflags & adns_if_noautosys) return; |
| 300 | adns_callback(ads,-1,0,0,0); |
| 301 | } |
| 302 | |
| 303 | void adns_cancel(adns_state ads, adns_query query) { |
| 304 | abort(); /* FIXME */ |
| 305 | } |
| 306 | |
| 307 | int adns_callback(adns_state ads, int maxfd, |
| 308 | const fd_set *readfds, const fd_set *writefds, |
| 309 | const fd_set *exceptfds) { |
| 310 | abort(); /* FIXME */ |
| 311 | } |
| 312 | |
| 313 | static int internal_check(adns_state ads, |
| 314 | adns_query *query_io, |
| 315 | adns_answer **answer, |
| 316 | void **context_r) { |
| 317 | adns_query qu; |
| 318 | |
| 319 | qu= *query_io; |
| 320 | if (!qu) { |
| 321 | if (!ads->output.head) return EWOULDBLOCK; |
| 322 | qu= ads->output.head; |
| 323 | } else { |
| 324 | if (qu->id>=0) return EWOULDBLOCK; |
| 325 | } |
| 326 | LIST_UNLINK(ads->output,qu); |
| 327 | *answer= qu->answer; |
| 328 | if (context_r) *context_r= qu->context; |
| 329 | free(qu); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | int adns_wait(adns_state ads, |
| 334 | adns_query *query_io, |
| 335 | adns_answer **answer_r, |
| 336 | void **context_r) { |
| 337 | int r, maxfd, rsel, rcb; |
| 338 | fd_set readfds, writefds, exceptfds; |
| 339 | struct timeval tvbuf, *tvp; |
| 340 | |
| 341 | for (;;) { |
| 342 | r= internal_check(ads,query_io,answer_r,context_r); |
| 343 | if (r && r != EWOULDBLOCK) return r; |
| 344 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
| 345 | maxfd= 0; tvp= 0; |
| 346 | adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); |
| 347 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
| 348 | if (rsel==-1) return r; |
| 349 | rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); |
| 350 | assert(rcb==rsel); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | int adns_check(adns_state ads, |
| 355 | adns_query *query_io, |
| 356 | adns_answer **answer_r, |
| 357 | void **context_r) { |
| 358 | autosys(ads); |
| 359 | return internal_check(ads,query_io,answer_r,context_r); |
| 360 | } |
| 361 | |
| 362 | int adns_synchronous(adns_state ads, |
| 363 | const char *owner, |
| 364 | adns_rrtype type, |
| 365 | adns_queryflags flags, |
| 366 | adns_answer **answer_r) { |
| 367 | adns_query qu; |
| 368 | int r; |
| 369 | |
| 370 | r= adns_submit(ads,owner,type,flags,0,&qu); |
| 371 | if (r) return r; |
| 372 | |
| 373 | do { |
| 374 | r= adns_wait(ads,&qu,answer_r,0); |
| 375 | } while (r==EINTR); |
| 376 | if (r) adns_cancel(ads,qu); |
| 377 | return r; |
| 378 | } |
| 379 | |
| 380 | static adns_status mkquery(adns_state ads, const char *owner, int ol, int id, |
| 381 | adns_rrtype type, adns_queryflags flags, int *qml_r) { |
| 382 | int ll, c, nlabs, qbufreq; |
| 383 | unsigned char label[255], *nqbuf; |
| 384 | const char *p, *pe; |
| 385 | |
| 386 | #define MKQUERY_ADDB(b) *nqbuf++= (b) |
| 387 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
| 388 | |
| 389 | qbufreq= 12+strlen(owner)+3; |
| 390 | if (ads->qbufavail < qbufreq) { |
| 391 | nqbuf= realloc(ads->qbuf,qbufreq); |
| 392 | if (!nqbuf) return adns_s_nolocalmem; |
| 393 | ads->qbuf= nqbuf; ads->qbufavail= qbufreq; |
| 394 | } |
| 395 | nqbuf= ads->qbuf; |
| 396 | |
| 397 | MKQUERY_ADDW(id); |
| 398 | MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ |
| 399 | MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ |
| 400 | MKQUERY_ADDW(1); /* QDCOUNT=1 */ |
| 401 | MKQUERY_ADDW(0); /* ANCOUNT=0 */ |
| 402 | MKQUERY_ADDW(0); /* NSCOUNT=0 */ |
| 403 | MKQUERY_ADDW(0); /* ARCOUNT=0 */ |
| 404 | p= owner; pe= owner+ol; |
| 405 | nlabs= 0; |
| 406 | if (!*p) return adns_s_invaliddomain; |
| 407 | do { |
| 408 | ll= 0; |
| 409 | while (p!=pe && (c= *p++)!='.') { |
| 410 | if (c=='\\') { |
| 411 | if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain; |
| 412 | if (ctype_digit(p[0])) { |
| 413 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
| 414 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
| 415 | if (c >= 256) return adns_s_invaliddomain; |
| 416 | } else { |
| 417 | return adns_s_invaliddomain; |
| 418 | } |
| 419 | } else if (!(c= *p++)) { |
| 420 | return adns_s_invaliddomain; |
| 421 | } |
| 422 | } |
| 423 | if (!(flags & adns_f_anyquote)) { |
| 424 | if ((c >= '0' && c <= '9') || c == '-') { |
| 425 | if (!ll) return adns_s_invaliddomain; |
| 426 | } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) { |
| 427 | return adns_s_invaliddomain; |
| 428 | } |
| 429 | } |
| 430 | if (ll == sizeof(label)) return adns_s_invaliddomain; |
| 431 | label[ll++]= c; |
| 432 | } |
| 433 | if (!ll) return adns_s_invaliddomain; |
| 434 | if (nlabs++ > 63) return adns_s_invaliddomain; |
| 435 | MKQUERY_ADDB(ll); |
| 436 | memcpy(nqbuf,label,ll); nqbuf+= ll; |
| 437 | } while (p!=pe); |
| 438 | |
| 439 | MKQUERY_ADDB(0); |
| 440 | MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */ |
| 441 | MKQUERY_ADDW(1); /* QCLASS=IN */ |
| 442 | |
| 443 | *qml_r= nqbuf - ads->qbuf; |
| 444 | |
| 445 | return adns_s_ok; |
| 446 | } |
| 447 | |
| 448 | static adns_query allocquery(adns_state ads, const char *owner, int ol, |
| 449 | int qml, int id, adns_rrtype type, |
| 450 | adns_queryflags flags, void *context) { |
| 451 | adns_query qu; |
| 452 | unsigned char *qm; |
| 453 | |
| 454 | qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0; |
| 455 | qu->next= qu->back= qu->parent= qu->child= 0; |
| 456 | qu->id= id; |
| 457 | qu->type= type; |
| 458 | qu->answer= 0; |
| 459 | qu->flags= flags; |
| 460 | qu->context= context; |
| 461 | qu->udpretries= 0; |
| 462 | qu->sentudp= qu->senttcp= 0; |
| 463 | qu->nextserver= 0; |
| 464 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
| 465 | qu->querymsg= qm= qu->owner+ol+1; |
| 466 | memcpy(qm,ads->qbuf,qml); |
| 467 | qu->querylen= qml; |
| 468 | return qu; |
| 469 | } |
| 470 | |
| 471 | static int failsubmit(adns_state ads, void *context, adns_query *query_r, |
| 472 | adns_rrtype type, adns_queryflags flags, |
| 473 | int id, adns_status stat) { |
| 474 | adns_query qu; |
| 475 | |
| 476 | qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno; |
| 477 | query_fail(ads,qu,stat); |
| 478 | *query_r= qu; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static void trysendudp(adns_state ads, adns_query qu) { |
| 483 | struct sockaddr_in servaddr; |
| 484 | /* FIXME: _f_usevc not implemented */ |
| 485 | memset(&servaddr,0,sizeof(servaddr)); |
| 486 | servaddr.sin_family= AF_INET; |
| 487 | servaddr.sin_addr= ads->servers[qu->nextserver].addr; |
| 488 | servaddr.sin_port= htons(53); |
| 489 | sendto(ads->udpsocket,qu->querymsg,qu->querylen,0,&servaddr,sizeof(servaddr)); |
| 490 | } |
| 491 | |
| 492 | int adns_submit(adns_state ads, |
| 493 | const char *owner, |
| 494 | adns_rrtype type, |
| 495 | adns_queryflags flags, |
| 496 | void *context, |
| 497 | adns_query *query_r) { |
| 498 | adns_query qu; |
| 499 | adns_status stat; |
| 500 | int ol, id, qml; |
| 501 | |
| 502 | id= ads->nextid++; |
| 503 | |
| 504 | ol= strlen(owner); |
| 505 | if (ol<=1 || ol>MAXDNAME+1) |
| 506 | return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain); |
| 507 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; } |
| 508 | |
| 509 | stat= mkquery(ads,owner,ol,id,type,flags,&qml); |
| 510 | if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat); |
| 511 | |
| 512 | qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno; |
| 513 | |
| 514 | LIST_LINK_TAIL(ads->tosend,qu); |
| 515 | trysendudp(ads,qu); |
| 516 | autosys(ads); |
| 517 | |
| 518 | *query_r= qu; |
| 519 | return 0; |
| 520 | } |