r20640: Commit part 2/2
[Samba.git] / source / heimdal / lib / roken / resolve.c
blob0f45bc57b2218736a4b027ef945c5dd56501a118
1 /*
2 * Copyright (c) 1995 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include "roken.h"
38 #ifdef HAVE_ARPA_NAMESER_H
39 #include <arpa/nameser.h>
40 #endif
41 #ifdef HAVE_RESOLV_H
42 #include <resolv.h>
43 #endif
44 #include "resolve.h"
46 #include <assert.h>
48 RCSID("$Id: resolve.c,v 1.55 2006/04/14 13:56:00 lha Exp $");
50 #ifdef _AIX /* AIX have broken res_nsearch() in 5.1 (5.0 also ?) */
51 #undef HAVE_RES_NSEARCH
52 #endif
54 #define DECL(X) {#X, rk_ns_t_##X}
56 static struct stot{
57 const char *name;
58 int type;
59 }stot[] = {
60 DECL(a),
61 DECL(aaaa),
62 DECL(ns),
63 DECL(cname),
64 DECL(soa),
65 DECL(ptr),
66 DECL(mx),
67 DECL(txt),
68 DECL(afsdb),
69 DECL(sig),
70 DECL(key),
71 DECL(srv),
72 DECL(naptr),
73 DECL(sshfp),
74 DECL(ds),
75 {NULL, 0}
78 int _resolve_debug = 0;
80 int ROKEN_LIB_FUNCTION
81 dns_string_to_type(const char *name)
83 struct stot *p = stot;
84 for(p = stot; p->name; p++)
85 if(strcasecmp(name, p->name) == 0)
86 return p->type;
87 return -1;
90 const char * ROKEN_LIB_FUNCTION
91 dns_type_to_string(int type)
93 struct stot *p = stot;
94 for(p = stot; p->name; p++)
95 if(type == p->type)
96 return p->name;
97 return NULL;
100 #if (defined(HAVE_RES_SEARCH) || defined(HAVE_RES_NSEARCH)) && defined(HAVE_DN_EXPAND)
102 static void
103 dns_free_rr(struct resource_record *rr)
105 if(rr->domain)
106 free(rr->domain);
107 if(rr->u.data)
108 free(rr->u.data);
109 free(rr);
112 void ROKEN_LIB_FUNCTION
113 dns_free_data(struct dns_reply *r)
115 struct resource_record *rr;
116 if(r->q.domain)
117 free(r->q.domain);
118 for(rr = r->head; rr;){
119 struct resource_record *tmp = rr;
120 rr = rr->next;
121 dns_free_rr(tmp);
123 free (r);
126 static int
127 parse_record(const unsigned char *data, const unsigned char *end_data,
128 const unsigned char **pp, struct resource_record **ret_rr)
130 struct resource_record *rr;
131 int type, class, ttl, size;
132 int status;
133 char host[MAXDNAME];
134 const unsigned char *p = *pp;
136 *ret_rr = NULL;
138 status = dn_expand(data, end_data, p, host, sizeof(host));
139 if(status < 0)
140 return -1;
141 if (p + status + 10 > end_data)
142 return -1;
144 p += status;
145 type = (p[0] << 8) | p[1];
146 p += 2;
147 class = (p[0] << 8) | p[1];
148 p += 2;
149 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
150 p += 4;
151 size = (p[0] << 8) | p[1];
152 p += 2;
154 if (p + size > end_data)
155 return -1;
157 rr = calloc(1, sizeof(*rr));
158 if(rr == NULL)
159 return -1;
160 rr->domain = strdup(host);
161 if(rr->domain == NULL) {
162 dns_free_rr(rr);
163 return -1;
165 rr->type = type;
166 rr->class = class;
167 rr->ttl = ttl;
168 rr->size = size;
169 switch(type){
170 case rk_ns_t_ns:
171 case rk_ns_t_cname:
172 case rk_ns_t_ptr:
173 status = dn_expand(data, end_data, p, host, sizeof(host));
174 if(status < 0) {
175 dns_free_rr(rr);
176 return -1;
178 rr->u.txt = strdup(host);
179 if(rr->u.txt == NULL) {
180 dns_free_rr(rr);
181 return -1;
183 break;
184 case rk_ns_t_mx:
185 case rk_ns_t_afsdb:{
186 size_t hostlen;
188 status = dn_expand(data, end_data, p + 2, host, sizeof(host));
189 if(status < 0){
190 dns_free_rr(rr);
191 return -1;
193 if (status + 2 > size) {
194 dns_free_rr(rr);
195 return -1;
198 hostlen = strlen(host);
199 rr->u.mx = (struct mx_record*)malloc(sizeof(struct mx_record) +
200 hostlen);
201 if(rr->u.mx == NULL) {
202 dns_free_rr(rr);
203 return -1;
205 rr->u.mx->preference = (p[0] << 8) | p[1];
206 strlcpy(rr->u.mx->domain, host, hostlen + 1);
207 break;
209 case rk_ns_t_srv:{
210 size_t hostlen;
211 status = dn_expand(data, end_data, p + 6, host, sizeof(host));
212 if(status < 0){
213 dns_free_rr(rr);
214 return -1;
216 if (status + 6 > size) {
217 dns_free_rr(rr);
218 return -1;
221 hostlen = strlen(host);
222 rr->u.srv =
223 (struct srv_record*)malloc(sizeof(struct srv_record) +
224 hostlen);
225 if(rr->u.srv == NULL) {
226 dns_free_rr(rr);
227 return -1;
229 rr->u.srv->priority = (p[0] << 8) | p[1];
230 rr->u.srv->weight = (p[2] << 8) | p[3];
231 rr->u.srv->port = (p[4] << 8) | p[5];
232 strlcpy(rr->u.srv->target, host, hostlen + 1);
233 break;
235 case rk_ns_t_txt:{
236 if(size == 0 || size < *p + 1) {
237 dns_free_rr(rr);
238 return -1;
240 rr->u.txt = (char*)malloc(*p + 1);
241 if(rr->u.txt == NULL) {
242 dns_free_rr(rr);
243 return -1;
245 strncpy(rr->u.txt, (const char*)(p + 1), *p);
246 rr->u.txt[*p] = '\0';
247 break;
249 case rk_ns_t_key : {
250 size_t key_len;
252 if (size < 4) {
253 dns_free_rr(rr);
254 return -1;
257 key_len = size - 4;
258 rr->u.key = malloc (sizeof(*rr->u.key) + key_len - 1);
259 if (rr->u.key == NULL) {
260 dns_free_rr(rr);
261 return -1;
264 rr->u.key->flags = (p[0] << 8) | p[1];
265 rr->u.key->protocol = p[2];
266 rr->u.key->algorithm = p[3];
267 rr->u.key->key_len = key_len;
268 memcpy (rr->u.key->key_data, p + 4, key_len);
269 break;
271 case rk_ns_t_sig : {
272 size_t sig_len, hostlen;
274 if(size <= 18) {
275 dns_free_rr(rr);
276 return -1;
278 status = dn_expand (data, end_data, p + 18, host, sizeof(host));
279 if (status < 0) {
280 dns_free_rr(rr);
281 return -1;
283 if (status + 18 > size) {
284 dns_free_rr(rr);
285 return -1;
288 /* the signer name is placed after the sig_data, to make it
289 easy to free this structure; the size calculation below
290 includes the zero-termination if the structure itself.
291 don't you just love C?
293 sig_len = size - 18 - status;
294 hostlen = strlen(host);
295 rr->u.sig = malloc(sizeof(*rr->u.sig)
296 + hostlen + sig_len);
297 if (rr->u.sig == NULL) {
298 dns_free_rr(rr);
299 return -1;
301 rr->u.sig->type = (p[0] << 8) | p[1];
302 rr->u.sig->algorithm = p[2];
303 rr->u.sig->labels = p[3];
304 rr->u.sig->orig_ttl = (p[4] << 24) | (p[5] << 16)
305 | (p[6] << 8) | p[7];
306 rr->u.sig->sig_expiration = (p[8] << 24) | (p[9] << 16)
307 | (p[10] << 8) | p[11];
308 rr->u.sig->sig_inception = (p[12] << 24) | (p[13] << 16)
309 | (p[14] << 8) | p[15];
310 rr->u.sig->key_tag = (p[16] << 8) | p[17];
311 rr->u.sig->sig_len = sig_len;
312 memcpy (rr->u.sig->sig_data, p + 18 + status, sig_len);
313 rr->u.sig->signer = &rr->u.sig->sig_data[sig_len];
314 strlcpy(rr->u.sig->signer, host, hostlen + 1);
315 break;
318 case rk_ns_t_cert : {
319 size_t cert_len;
321 if (size < 5) {
322 dns_free_rr(rr);
323 return -1;
326 cert_len = size - 5;
327 rr->u.cert = malloc (sizeof(*rr->u.cert) + cert_len - 1);
328 if (rr->u.cert == NULL) {
329 dns_free_rr(rr);
330 return -1;
333 rr->u.cert->type = (p[0] << 8) | p[1];
334 rr->u.cert->tag = (p[2] << 8) | p[3];
335 rr->u.cert->algorithm = p[4];
336 rr->u.cert->cert_len = cert_len;
337 memcpy (rr->u.cert->cert_data, p + 5, cert_len);
338 break;
340 case rk_ns_t_sshfp : {
341 size_t sshfp_len;
343 if (size < 2) {
344 dns_free_rr(rr);
345 return -1;
348 sshfp_len = size - 2;
350 rr->u.sshfp = malloc (sizeof(*rr->u.sshfp) + sshfp_len - 1);
351 if (rr->u.sshfp == NULL) {
352 dns_free_rr(rr);
353 return -1;
356 rr->u.sshfp->algorithm = p[0];
357 rr->u.sshfp->type = p[1];
358 rr->u.sshfp->sshfp_len = sshfp_len;
359 memcpy (rr->u.sshfp->sshfp_data, p + 2, sshfp_len);
360 break;
362 case rk_ns_t_ds: {
363 size_t digest_len;
365 if (size < 4) {
366 dns_free_rr(rr);
367 return -1;
370 digest_len = size - 4;
372 rr->u.ds = malloc (sizeof(*rr->u.ds) + digest_len - 1);
373 if (rr->u.ds == NULL) {
374 dns_free_rr(rr);
375 return -1;
378 rr->u.ds->key_tag = (p[0] << 8) | p[1];
379 rr->u.ds->algorithm = p[2];
380 rr->u.ds->digest_type = p[3];
381 rr->u.ds->digest_len = digest_len;
382 memcpy (rr->u.ds->digest_data, p + 4, digest_len);
383 break;
385 default:
386 rr->u.data = (unsigned char*)malloc(size);
387 if(size != 0 && rr->u.data == NULL) {
388 dns_free_rr(rr);
389 return -1;
391 if (size)
392 memcpy(rr->u.data, p, size);
394 *pp = p + size;
395 *ret_rr = rr;
397 return 0;
400 #ifndef TEST_RESOLVE
401 static
402 #endif
403 struct dns_reply*
404 parse_reply(const unsigned char *data, size_t len)
406 const unsigned char *p;
407 int status;
408 int i;
409 char host[MAXDNAME];
410 const unsigned char *end_data = data + len;
411 struct dns_reply *r;
412 struct resource_record **rr;
414 r = calloc(1, sizeof(*r));
415 if (r == NULL)
416 return NULL;
418 p = data;
420 r->h.id = (p[0] << 8) | p[1];
421 r->h.flags = 0;
422 if (p[2] & 0x01)
423 r->h.flags |= rk_DNS_HEADER_RESPONSE_FLAG;
424 r->h.opcode = (p[2] >> 1) & 0xf;
425 if (p[2] & 0x20)
426 r->h.flags |= rk_DNS_HEADER_AUTHORITIVE_ANSWER;
427 if (p[2] & 0x40)
428 r->h.flags |= rk_DNS_HEADER_TRUNCATED_MESSAGE;
429 if (p[2] & 0x80)
430 r->h.flags |= rk_DNS_HEADER_RECURSION_DESIRED;
431 if (p[3] & 0x01)
432 r->h.flags |= rk_DNS_HEADER_RECURSION_AVAILABLE;
433 if (p[3] & 0x04)
434 r->h.flags |= rk_DNS_HEADER_AUTHORITIVE_ANSWER;
435 if (p[3] & 0x08)
436 r->h.flags |= rk_DNS_HEADER_CHECKING_DISABLED;
437 r->h.response_code = (p[3] >> 4) & 0xf;
438 r->h.qdcount = (p[4] << 8) | p[5];
439 r->h.ancount = (p[6] << 8) | p[7];
440 r->h.nscount = (p[8] << 8) | p[9];
441 r->h.arcount = (p[10] << 8) | p[11];
443 p += 12;
445 if(r->h.qdcount != 1) {
446 free(r);
447 return NULL;
449 status = dn_expand(data, end_data, p, host, sizeof(host));
450 if(status < 0){
451 dns_free_data(r);
452 return NULL;
454 r->q.domain = strdup(host);
455 if(r->q.domain == NULL) {
456 dns_free_data(r);
457 return NULL;
459 if (p + status + 4 > end_data) {
460 dns_free_data(r);
461 return NULL;
463 p += status;
464 r->q.type = (p[0] << 8 | p[1]);
465 p += 2;
466 r->q.class = (p[0] << 8 | p[1]);
467 p += 2;
469 rr = &r->head;
470 for(i = 0; i < r->h.ancount; i++) {
471 if(parse_record(data, end_data, &p, rr) != 0) {
472 dns_free_data(r);
473 return NULL;
475 rr = &(*rr)->next;
477 for(i = 0; i < r->h.nscount; i++) {
478 if(parse_record(data, end_data, &p, rr) != 0) {
479 dns_free_data(r);
480 return NULL;
482 rr = &(*rr)->next;
484 for(i = 0; i < r->h.arcount; i++) {
485 if(parse_record(data, end_data, &p, rr) != 0) {
486 dns_free_data(r);
487 return NULL;
489 rr = &(*rr)->next;
491 *rr = NULL;
492 return r;
495 static struct dns_reply *
496 dns_lookup_int(const char *domain, int rr_class, int rr_type)
498 struct dns_reply *r;
499 unsigned char *reply = NULL;
500 int size;
501 int len;
502 #ifdef HAVE_RES_NSEARCH
503 struct __res_state state;
504 memset(&state, 0, sizeof(state));
505 if(res_ninit(&state))
506 return NULL; /* is this the best we can do? */
507 #elif defined(HAVE__RES)
508 u_long old_options = 0;
509 #endif
511 size = 0;
512 len = 1000;
513 do {
514 if (reply) {
515 free(reply);
516 reply = NULL;
518 if (size <= len)
519 size = len;
520 if (_resolve_debug) {
521 #ifdef HAVE_RES_NSEARCH
522 state.options |= RES_DEBUG;
523 #elif defined(HAVE__RES)
524 old_options = _res.options;
525 _res.options |= RES_DEBUG;
526 #endif
527 fprintf(stderr, "dns_lookup(%s, %d, %s), buffer size %d\n", domain,
528 rr_class, dns_type_to_string(rr_type), size);
530 reply = malloc(size);
531 if (reply == NULL) {
532 #ifdef HAVE_RES_NSEARCH
533 res_nclose(&state);
534 #endif
535 return NULL;
537 #ifdef HAVE_RES_NSEARCH
538 len = res_nsearch(&state, domain, rr_class, rr_type, reply, size);
539 #else
540 len = res_search(domain, rr_class, rr_type, reply, size);
541 #endif
542 if (_resolve_debug) {
543 #if defined(HAVE__RES) && !defined(HAVE_RES_NSEARCH)
544 _res.options = old_options;
545 #endif
546 fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n",
547 domain, rr_class, dns_type_to_string(rr_type), len);
549 if (len < 0) {
550 #ifdef HAVE_RES_NSEARCH
551 #ifdef HAVE_RES_NDESTROY
552 res_ndestroy(&state);
553 #else
554 res_nclose(&state);
555 #endif
556 #endif
557 free(reply);
558 return NULL;
560 } while (size < len && len < rk_DNS_MAX_PACKET_SIZE);
561 #ifdef HAVE_RES_NSEARCH
562 res_nclose(&state);
563 #endif
565 len = min(len, size);
566 r = parse_reply(reply, len);
567 free(reply);
568 return r;
571 struct dns_reply * ROKEN_LIB_FUNCTION
572 dns_lookup(const char *domain, const char *type_name)
574 int type;
576 type = dns_string_to_type(type_name);
577 if(type == -1) {
578 if(_resolve_debug)
579 fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n",
580 type_name);
581 return NULL;
583 return dns_lookup_int(domain, C_IN, type);
586 static int
587 compare_srv(const void *a, const void *b)
589 const struct resource_record *const* aa = a, *const* bb = b;
591 if((*aa)->u.srv->priority == (*bb)->u.srv->priority)
592 return ((*aa)->u.srv->weight - (*bb)->u.srv->weight);
593 return ((*aa)->u.srv->priority - (*bb)->u.srv->priority);
596 #ifndef HAVE_RANDOM
597 #define random() rand()
598 #endif
600 /* try to rearrange the srv-records by the algorithm in RFC2782 */
601 void ROKEN_LIB_FUNCTION
602 dns_srv_order(struct dns_reply *r)
604 struct resource_record **srvs, **ss, **headp;
605 struct resource_record *rr;
606 int num_srv = 0;
608 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
609 int state[256 / sizeof(int)];
610 char *oldstate;
611 #endif
613 for(rr = r->head; rr; rr = rr->next)
614 if(rr->type == rk_ns_t_srv)
615 num_srv++;
617 if(num_srv == 0)
618 return;
620 srvs = malloc(num_srv * sizeof(*srvs));
621 if(srvs == NULL)
622 return; /* XXX not much to do here */
624 /* unlink all srv-records from the linked list and put them in
625 a vector */
626 for(ss = srvs, headp = &r->head; *headp; )
627 if((*headp)->type == rk_ns_t_srv) {
628 *ss = *headp;
629 *headp = (*headp)->next;
630 (*ss)->next = NULL;
631 ss++;
632 } else
633 headp = &(*headp)->next;
635 /* sort them by priority and weight */
636 qsort(srvs, num_srv, sizeof(*srvs), compare_srv);
638 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
639 oldstate = initstate(time(NULL), (char*)state, sizeof(state));
640 #endif
642 headp = &r->head;
644 for(ss = srvs; ss < srvs + num_srv; ) {
645 int sum, rnd, count;
646 struct resource_record **ee, **tt;
647 /* find the last record with the same priority and count the
648 sum of all weights */
649 for(sum = 0, tt = ss; tt < srvs + num_srv; tt++) {
650 assert(*tt != NULL);
651 if((*tt)->u.srv->priority != (*ss)->u.srv->priority)
652 break;
653 sum += (*tt)->u.srv->weight;
655 ee = tt;
656 /* ss is now the first record of this priority and ee is the
657 first of the next */
658 while(ss < ee) {
659 rnd = random() % (sum + 1);
660 for(count = 0, tt = ss; ; tt++) {
661 if(*tt == NULL)
662 continue;
663 count += (*tt)->u.srv->weight;
664 if(count >= rnd)
665 break;
668 assert(tt < ee);
670 /* insert the selected record at the tail (of the head) of
671 the list */
672 (*tt)->next = *headp;
673 *headp = *tt;
674 headp = &(*tt)->next;
675 sum -= (*tt)->u.srv->weight;
676 *tt = NULL;
677 while(ss < ee && *ss == NULL)
678 ss++;
682 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
683 setstate(oldstate);
684 #endif
685 free(srvs);
686 return;
689 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
691 struct dns_reply * ROKEN_LIB_FUNCTION
692 dns_lookup(const char *domain, const char *type_name)
694 return NULL;
697 void ROKEN_LIB_FUNCTION
698 dns_free_data(struct dns_reply *r)
702 void ROKEN_LIB_FUNCTION
703 dns_srv_order(struct dns_reply *r)
707 #endif