| 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 whitespace(int c) { |
| 144 | return c==' ' || c=='\n' || c=='\t'; |
| 145 | } |
| 146 | |
| 147 | static void readconfig(adns_state ads, const char *filename) { |
| 148 | char linebuf[2000], *p, *q; |
| 149 | FILE *file; |
| 150 | int lno, l, c; |
| 151 | const struct configcommandinfo *ccip; |
| 152 | |
| 153 | file= fopen(filename,"r"); |
| 154 | if (!file) { |
| 155 | if (errno == ENOENT) { |
| 156 | debug(ads,"configuration file `%s' does not exist",filename); |
| 157 | return; |
| 158 | } |
| 159 | diag(ads,"cannot open configuration file `%s': %s",filename,strerror(errno)); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | for (lno=1; fgets(linebuf,sizeof(linebuf),file); lno++) { |
| 164 | l= strlen(linebuf); |
| 165 | if (!l) continue; |
| 166 | if (linebuf[l-1] != '\n' && !feof(file)) { |
| 167 | diag(ads,"%s:%d: line too long",filename,lno); |
| 168 | while ((c= getc(file)) != EOF && c != '\n') { } |
| 169 | if (c == EOF) break; |
| 170 | continue; |
| 171 | } |
| 172 | while (l>0 && whitespace(linebuf[l-1])) l--; |
| 173 | linebuf[l]= 0; |
| 174 | p= linebuf; |
| 175 | while (whitespace(*p)) p++; |
| 176 | if (*p == '#' || *p == '\n') continue; |
| 177 | q= p; |
| 178 | while (*q && !whitespace(*q)) q++; |
| 179 | for (ccip=configcommandinfos; |
| 180 | ccip->name && strncmp(ccip->name,p,q-p); |
| 181 | ccip++); |
| 182 | if (!ccip->name) { |
| 183 | diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p); |
| 184 | continue; |
| 185 | } |
| 186 | while (whitespace(*q)) q++; |
| 187 | ccip->fn(ads,filename,lno,q); |
| 188 | } |
| 189 | if (ferror(file)) { |
| 190 | diag(ads,"%s:%d: read error: %s",filename,lno,strerror(errno)); |
| 191 | } |
| 192 | fclose(file); |
| 193 | } |
| 194 | |
| 195 | static const char *instrum_getenv(adns_state ads, const char *envvar) { |
| 196 | const char *value; |
| 197 | |
| 198 | value= getenv(envvar); |
| 199 | if (!value) debug(ads,"environment variable %s not set",envvar); |
| 200 | else debug(ads,"environment variable %s set to `%s'",envvar,value); |
| 201 | return value; |
| 202 | } |
| 203 | |
| 204 | static void readconfigenv(adns_state ads, const char *envvar) { |
| 205 | const char *filename; |
| 206 | |
| 207 | if (ads->iflags & adns_if_noenv) { |
| 208 | debug(ads,"not checking environment variable `%s'",envvar); |
| 209 | return; |
| 210 | } |
| 211 | filename= instrum_getenv(ads,envvar); |
| 212 | if (filename) readconfig(ads,filename); |
| 213 | } |
| 214 | |
| 215 | int adns_init(adns_state *ads_r, adns_initflags flags) { |
| 216 | adns_state ads; |
| 217 | const char *res_options, *adns_res_options; |
| 218 | struct protoent *proto; |
| 219 | struct sockaddr_in udpaddr; |
| 220 | int udpaddrlen, r; |
| 221 | |
| 222 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
| 223 | ads->input.head= ads->input.tail= 0; |
| 224 | ads->timew.head= ads->timew.tail= 0; |
| 225 | ads->childw.head= ads->childw.tail= 0; |
| 226 | ads->output.head= ads->output.tail= 0; |
| 227 | ads->nextid= 0x311f; |
| 228 | ads->udpsocket= -1; |
| 229 | ads->qbufavail= 0; |
| 230 | ads->qbuf= 0; |
| 231 | ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0; |
| 232 | ads->tcpbuf= 0; |
| 233 | ads->iflags= flags; |
| 234 | ads->nservers= 0; |
| 235 | ads->iflags= flags; |
| 236 | |
| 237 | res_options= instrum_getenv(ads,"RES_OPTIONS"); |
| 238 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); |
| 239 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
| 240 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
| 241 | |
| 242 | readconfig(ads,"/etc/resolv.conf"); |
| 243 | readconfigenv(ads,"RES_CONF"); |
| 244 | readconfigenv(ads,"ADNS_RES_CONF"); |
| 245 | |
| 246 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
| 247 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
| 248 | |
| 249 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); |
| 250 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); |
| 251 | |
| 252 | if (!ads->nservers) { |
| 253 | struct in_addr ia; |
| 254 | if (ads->iflags & adns_if_debug) |
| 255 | fprintf(stderr,"adns: no nameservers, using localhost\n"); |
| 256 | ia.s_addr= INADDR_LOOPBACK; |
| 257 | addserver(ads,ia); |
| 258 | } |
| 259 | |
| 260 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } |
| 261 | ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto); |
| 262 | if (!ads->udpsocket) { r= errno; goto x_closeudp; } |
| 263 | |
| 264 | memset(&udpaddr,0,sizeof(udpaddr)); |
| 265 | udpaddr.sin_family= AF_INET; |
| 266 | udpaddr.sin_addr.s_addr= INADDR_ANY; |
| 267 | udpaddr.sin_port= 0; |
| 268 | r= bind(ads->udpsocket,&udpaddr,sizeof(udpaddr)); |
| 269 | if (r) { r= errno; goto x_closeudp; } |
| 270 | |
| 271 | udpaddrlen= sizeof(udpaddr); |
| 272 | r= getsockname(ads->udpsocket,&udpaddr,&udpaddrlen); |
| 273 | if (r) { r= errno; goto x_closeudp; } |
| 274 | if (udpaddr.sin_family != AF_INET) { |
| 275 | diag(ads,"network API error: UDP socket not AF_INET but %lu", |
| 276 | (unsigned long)udpaddr.sin_family); |
| 277 | r= EPROTOTYPE; goto x_closeudp; |
| 278 | } |
| 279 | debug(ads,"UDP socket is %s:%u",inet_ntoa(udpaddr.sin_addr),ntohs(udpaddr.sin_port)); |
| 280 | |
| 281 | *ads_r= ads; |
| 282 | return 0; |
| 283 | |
| 284 | x_closeudp: |
| 285 | close(ads->udpsocket); |
| 286 | x_free: |
| 287 | free(ads); |
| 288 | return r; |
| 289 | } |
| 290 | |
| 291 | static void query_fail(adns_state ads, adns_query qu, adns_status stat) { |
| 292 | adns_answer *ans; |
| 293 | |
| 294 | ans= qu->answer; |
| 295 | if (!ans) ans= malloc(sizeof(*qu->answer)); |
| 296 | if (ans) { |
| 297 | ans->status= stat; |
| 298 | ans->cname= 0; |
| 299 | ans->type= qu->type; |
| 300 | ans->nrrs= 0; |
| 301 | } |
| 302 | qu->answer= ans; |
| 303 | LIST_LINK_TAIL(ads->input,qu); |
| 304 | } |
| 305 | |
| 306 | int adns_finish(adns_state ads) { |
| 307 | abort(); /* FIXME */ |
| 308 | } |
| 309 | |
| 310 | void adns_interest(adns_state ads, int *maxfd, |
| 311 | fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
| 312 | struct timeval **tv_io, struct timeval *tvbuf) { |
| 313 | abort(); /* FIXME */ |
| 314 | } |
| 315 | |
| 316 | static void autosys(adns_state ads) { |
| 317 | if (ads->iflags & adns_if_noautosys) return; |
| 318 | adns_callback(ads,-1,0,0,0); |
| 319 | } |
| 320 | |
| 321 | void adns_cancel(adns_state ads, adns_query query) { |
| 322 | abort(); /* FIXME */ |
| 323 | } |
| 324 | |
| 325 | int adns_callback(adns_state ads, int maxfd, |
| 326 | const fd_set *readfds, const fd_set *writefds, |
| 327 | const fd_set *exceptfds) { |
| 328 | abort(); /* FIXME */ |
| 329 | } |
| 330 | |
| 331 | static int internal_check(adns_state ads, |
| 332 | adns_query *query_io, |
| 333 | adns_answer *answer, |
| 334 | void *context_r) { |
| 335 | abort(); /* FIXME */ |
| 336 | } |
| 337 | |
| 338 | int adns_wait(adns_state ads, |
| 339 | adns_query *query_io, |
| 340 | adns_answer *answer, |
| 341 | void *context_r) { |
| 342 | int r, maxfd, rsel, rcb; |
| 343 | fd_set readfds, writefds, exceptfds; |
| 344 | struct timeval tvbuf, *tvp; |
| 345 | |
| 346 | for (;;) { |
| 347 | r= internal_check(ads,query_io,answer,context_r); |
| 348 | if (r && r != EWOULDBLOCK) return r; |
| 349 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
| 350 | maxfd= 0; tvp= 0; |
| 351 | adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); |
| 352 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
| 353 | if (rsel==-1) return r; |
| 354 | rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); |
| 355 | assert(rcb==rsel); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | int adns_check(adns_state ads, |
| 360 | adns_query *query_io, |
| 361 | adns_answer *answer, |
| 362 | void *context_r) { |
| 363 | autosys(ads); |
| 364 | return internal_check(ads,query_io,answer,context_r); |
| 365 | } |
| 366 | |
| 367 | int adns_synchronous(adns_state ads, |
| 368 | const char *owner, |
| 369 | adns_rrtype type, |
| 370 | adns_queryflags flags, |
| 371 | adns_answer *answer) { |
| 372 | adns_query qu; |
| 373 | int r; |
| 374 | |
| 375 | r= adns_submit(ads,owner,type,flags,0,&qu); |
| 376 | if (r) return r; |
| 377 | |
| 378 | do { |
| 379 | r= adns_wait(ads,&qu,answer,0); |
| 380 | } while (r==EINTR); |
| 381 | if (r) adns_cancel(ads,qu); |
| 382 | return r; |
| 383 | } |
| 384 | |
| 385 | static int mkquery(adns_state ads, |
| 386 | const char *owner, |
| 387 | adns_rrtype type) { |
| 388 | abort(); |
| 389 | } |
| 390 | |
| 391 | int adns_submit(adns_state ads, |
| 392 | const char *owner, |
| 393 | adns_rrtype type, |
| 394 | adns_queryflags flags, |
| 395 | void *context, |
| 396 | adns_query *query_r) { |
| 397 | adns_query qu; |
| 398 | adns_status stat; |
| 399 | int ol; |
| 400 | |
| 401 | stat= 0; |
| 402 | ol= strlen(owner); |
| 403 | if (ol>MAXDNAME+1) { stat= adns_s_invaliddomain; ol= 0; } |
| 404 | if (ol>0 && owner[ol-1]=='.') { flags &= ~adns_f_search; ol--; } |
| 405 | qu= malloc(sizeof(*qu)+ol+1); if (!qu) return errno; |
| 406 | qu->next= qu->back= qu->parent= qu->child= 0; |
| 407 | qu->id= ads->nextid++; |
| 408 | qu->type= type; |
| 409 | qu->answer= 0; |
| 410 | qu->flags= flags; |
| 411 | qu->context= context; |
| 412 | qu->udpretries= 0; |
| 413 | qu->sentudp= qu->senttcp= 0; |
| 414 | qu->nextserver= 0; |
| 415 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
| 416 | |
| 417 | |
| 418 | mkquery(ads,owner,type); |
| 419 | |
| 420 | if (stat) { |
| 421 | query_fail(ads,qu,stat); |
| 422 | } else { |
| 423 | LIST_LINK_TAIL(ads->input,qu); |
| 424 | autosys(ads); |
| 425 | } |
| 426 | *query_r= qu; |
| 427 | return 0; |
| 428 | } |