xref: /freebsd/crypto/heimdal/lib/roken/resolve.c (revision 8373020d34ceb1ac55d8f43333c1ca3680185b39)
1 /*
2  * Copyright (c) 1995 - 2002 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.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
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"
45 
46 #include <assert.h>
47 
48 RCSID("$Id: resolve.c,v 1.33 2002/08/28 20:07:24 joda Exp $");
49 
50 #if defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND)
51 
52 #define DECL(X) {#X, T_##X}
53 
54 static struct stot{
55     const char *name;
56     int type;
57 }stot[] = {
58     DECL(A),
59     DECL(NS),
60     DECL(CNAME),
61     DECL(SOA),
62     DECL(PTR),
63     DECL(MX),
64     DECL(TXT),
65     DECL(AFSDB),
66     DECL(SIG),
67     DECL(KEY),
68     DECL(SRV),
69     DECL(NAPTR),
70     {NULL, 	0}
71 };
72 
73 int _resolve_debug = 0;
74 
75 int
76 dns_string_to_type(const char *name)
77 {
78     struct stot *p = stot;
79     for(p = stot; p->name; p++)
80 	if(strcasecmp(name, p->name) == 0)
81 	    return p->type;
82     return -1;
83 }
84 
85 const char *
86 dns_type_to_string(int type)
87 {
88     struct stot *p = stot;
89     for(p = stot; p->name; p++)
90 	if(type == p->type)
91 	    return p->name;
92     return NULL;
93 }
94 
95 void
96 dns_free_data(struct dns_reply *r)
97 {
98     struct resource_record *rr;
99     if(r->q.domain)
100 	free(r->q.domain);
101     for(rr = r->head; rr;){
102 	struct resource_record *tmp = rr;
103 	if(rr->domain)
104 	    free(rr->domain);
105 	if(rr->u.data)
106 	    free(rr->u.data);
107 	rr = rr->next;
108 	free(tmp);
109     }
110     free (r);
111 }
112 
113 static struct dns_reply*
114 parse_reply(unsigned char *data, int len)
115 {
116     const unsigned char *p;
117     char host[128];
118     int status;
119     const unsigned char *end_data = data + len;
120     struct dns_reply *r;
121     struct resource_record **rr;
122 
123     r = calloc(1, sizeof(*r));
124     if (r == NULL)
125 	return NULL;
126 
127     p = data;
128 #if 0
129     /* doesn't work on Crays */
130     memcpy(&r->h, p, sizeof(HEADER));
131     p += sizeof(HEADER);
132 #else
133     memcpy(&r->h, p, 12); /* XXX this will probably be mostly garbage */
134     p += 12;
135 #endif
136     status = dn_expand(data, end_data, p, host, sizeof(host));
137     if(status < 0){
138 	dns_free_data(r);
139 	return NULL;
140     }
141     r->q.domain = strdup(host);
142     if(r->q.domain == NULL) {
143 	dns_free_data(r);
144 	return NULL;
145     }
146     if (p + status + 4 > end_data) {
147 	dns_free_data(r);
148 	return NULL;
149     }
150     p += status;
151     r->q.type = (p[0] << 8 | p[1]);
152     p += 2;
153     r->q.class = (p[0] << 8 | p[1]);
154     p += 2;
155     rr = &r->head;
156     while(p < end_data){
157 	int type, class, ttl, size;
158 	status = dn_expand(data, end_data, p, host, sizeof(host));
159 	if(status < 0){
160 	    dns_free_data(r);
161 	    return NULL;
162 	}
163 	if (p + status + 10 > end_data) {
164 	    dns_free_data(r);
165 	    return NULL;
166 	}
167 	p += status;
168 	type = (p[0] << 8) | p[1];
169 	p += 2;
170 	class = (p[0] << 8) | p[1];
171 	p += 2;
172 	ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
173 	p += 4;
174 	size = (p[0] << 8) | p[1];
175 	p += 2;
176 
177 	if (p + size > end_data) {
178 	    dns_free_data(r);
179 	    return NULL;
180 	}
181 
182 	*rr = (struct resource_record*)calloc(1,
183 					      sizeof(struct resource_record));
184 	if(*rr == NULL) {
185 	    dns_free_data(r);
186 	    return NULL;
187 	}
188 	(*rr)->domain = strdup(host);
189 	if((*rr)->domain == NULL) {
190 	    dns_free_data(r);
191 	    return NULL;
192 	}
193 	(*rr)->type = type;
194 	(*rr)->class = class;
195 	(*rr)->ttl = ttl;
196 	(*rr)->size = size;
197 	switch(type){
198 	case T_NS:
199 	case T_CNAME:
200 	case T_PTR:
201 	    status = dn_expand(data, end_data, p, host, sizeof(host));
202 	    if(status < 0){
203 		dns_free_data(r);
204 		return NULL;
205 	    }
206 	    (*rr)->u.txt = strdup(host);
207 	    if((*rr)->u.txt == NULL) {
208 		dns_free_data(r);
209 		return NULL;
210 	    }
211 	    break;
212 	case T_MX:
213 	case T_AFSDB:{
214 	    status = dn_expand(data, end_data, p + 2, host, sizeof(host));
215 	    if(status < 0){
216 		dns_free_data(r);
217 		return NULL;
218 	    }
219 	    if (status + 2 > size) {
220 		dns_free_data(r);
221 		return NULL;
222 	    }
223 
224 	    (*rr)->u.mx = (struct mx_record*)malloc(sizeof(struct mx_record) +
225 						    strlen(host));
226 	    if((*rr)->u.mx == NULL) {
227 		dns_free_data(r);
228 		return NULL;
229 	    }
230 	    (*rr)->u.mx->preference = (p[0] << 8) | p[1];
231 	    strcpy((*rr)->u.mx->domain, host);
232 	    break;
233 	}
234 	case T_SRV:{
235 	    status = dn_expand(data, end_data, p + 6, host, sizeof(host));
236 	    if(status < 0){
237 		dns_free_data(r);
238 		return NULL;
239 	    }
240 	    if (status + 6 > size) {
241 		dns_free_data(r);
242 		return NULL;
243 	    }
244 
245 	    (*rr)->u.srv =
246 		(struct srv_record*)malloc(sizeof(struct srv_record) +
247 					   strlen(host));
248 	    if((*rr)->u.srv == NULL) {
249 		dns_free_data(r);
250 		return NULL;
251 	    }
252 	    (*rr)->u.srv->priority = (p[0] << 8) | p[1];
253 	    (*rr)->u.srv->weight = (p[2] << 8) | p[3];
254 	    (*rr)->u.srv->port = (p[4] << 8) | p[5];
255 	    strcpy((*rr)->u.srv->target, host);
256 	    break;
257 	}
258 	case T_TXT:{
259 	    (*rr)->u.txt = (char*)malloc(size + 1);
260 	    if((*rr)->u.txt == NULL) {
261 		dns_free_data(r);
262 		return NULL;
263 	    }
264 	    strncpy((*rr)->u.txt, (char*)p + 1, *p);
265 	    (*rr)->u.txt[*p] = 0;
266 	    break;
267 	}
268 	case T_KEY : {
269 	    size_t key_len;
270 
271 	    if (size < 4) {
272 		dns_free_data (r);
273 		return NULL;
274 	    }
275 
276 	    key_len = size - 4;
277 	    (*rr)->u.key = malloc (sizeof(*(*rr)->u.key) + key_len - 1);
278 	    if ((*rr)->u.key == NULL) {
279 		dns_free_data (r);
280 		return NULL;
281 	    }
282 
283 	    (*rr)->u.key->flags     = (p[0] << 8) | p[1];
284 	    (*rr)->u.key->protocol  = p[2];
285 	    (*rr)->u.key->algorithm = p[3];
286 	    (*rr)->u.key->key_len   = key_len;
287 	    memcpy ((*rr)->u.key->key_data, p + 4, key_len);
288 	    break;
289 	}
290 	case T_SIG : {
291 	    size_t sig_len;
292 
293 	    status = dn_expand (data, end_data, p + 18, host, sizeof(host));
294 	    if (status < 0) {
295 		dns_free_data (r);
296 		return NULL;
297 	    }
298 	    if (status + 18 > size) {
299 		dns_free_data(r);
300 		return NULL;
301 	    }
302 
303 	    sig_len = len - 18 - status;
304 	    (*rr)->u.sig = malloc(sizeof(*(*rr)->u.sig)
305 				  + strlen(host) + sig_len);
306 	    if ((*rr)->u.sig == NULL) {
307 		dns_free_data (r);
308 		return NULL;
309 	    }
310 	    (*rr)->u.sig->type           = (p[0] << 8) | p[1];
311 	    (*rr)->u.sig->algorithm      = p[2];
312 	    (*rr)->u.sig->labels         = p[3];
313 	    (*rr)->u.sig->orig_ttl       = (p[4] << 24) | (p[5] << 16)
314 		| (p[6] << 8) | p[7];
315 	    (*rr)->u.sig->sig_expiration = (p[8] << 24) | (p[9] << 16)
316 		| (p[10] << 8) | p[11];
317 	    (*rr)->u.sig->sig_inception  = (p[12] << 24) | (p[13] << 16)
318 		| (p[14] << 8) | p[15];
319 	    (*rr)->u.sig->key_tag        = (p[16] << 8) | p[17];
320 	    (*rr)->u.sig->sig_len        = sig_len;
321 	    memcpy ((*rr)->u.sig->sig_data, p + 18 + status, sig_len);
322 	    (*rr)->u.sig->signer         = &(*rr)->u.sig->sig_data[sig_len];
323 	    strcpy((*rr)->u.sig->signer, host);
324 	    break;
325 	}
326 
327 	case T_CERT : {
328 	    size_t cert_len;
329 
330 	    if (size < 5) {
331 		dns_free_data(r);
332 		return NULL;
333 	    }
334 
335 	    cert_len = size - 5;
336 	    (*rr)->u.cert = malloc (sizeof(*(*rr)->u.cert) + cert_len - 1);
337 	    if ((*rr)->u.cert == NULL) {
338 		dns_free_data (r);
339 		return NULL;
340 	    }
341 
342 	    (*rr)->u.cert->type      = (p[0] << 8) | p[1];
343 	    (*rr)->u.cert->tag       = (p[2] << 8) | p[3];
344 	    (*rr)->u.cert->algorithm = p[4];
345 	    (*rr)->u.cert->cert_len  = cert_len;
346 	    memcpy ((*rr)->u.cert->cert_data, p + 5, cert_len);
347 	    break;
348 	}
349 	default:
350 	    (*rr)->u.data = (unsigned char*)malloc(size);
351 	    if(size != 0 && (*rr)->u.data == NULL) {
352 		dns_free_data(r);
353 		return NULL;
354 	    }
355 	    memcpy((*rr)->u.data, p, size);
356 	}
357 	p += size;
358 	rr = &(*rr)->next;
359     }
360     *rr = NULL;
361     return r;
362 }
363 
364 static struct dns_reply *
365 dns_lookup_int(const char *domain, int rr_class, int rr_type)
366 {
367     unsigned char reply[1024];
368     int len;
369 #ifdef HAVE__RES
370     u_long old_options = 0;
371 #endif
372 
373     if (_resolve_debug) {
374 #ifdef HAVE__RES
375         old_options = _res.options;
376 	_res.options |= RES_DEBUG;
377 #endif
378 	fprintf(stderr, "dns_lookup(%s, %d, %s)\n", domain,
379 		rr_class, dns_type_to_string(rr_type));
380     }
381     len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
382     if (_resolve_debug) {
383 #ifdef HAVE__RES
384         _res.options = old_options;
385 #endif
386 	fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n",
387 		domain, rr_class, dns_type_to_string(rr_type), len);
388     }
389     if(len < 0) {
390 	return NULL;
391     } else {
392 	len = min(len, sizeof(reply));
393 	return parse_reply(reply, len);
394     }
395 }
396 
397 struct dns_reply *
398 dns_lookup(const char *domain, const char *type_name)
399 {
400     int type;
401 
402     type = dns_string_to_type(type_name);
403     if(type == -1) {
404 	if(_resolve_debug)
405 	    fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n",
406 		    type_name);
407 	return NULL;
408     }
409     return dns_lookup_int(domain, C_IN, type);
410 }
411 
412 static int
413 compare_srv(const void *a, const void *b)
414 {
415     const struct resource_record *const* aa = a, *const* bb = b;
416 
417     if((*aa)->u.srv->priority == (*bb)->u.srv->priority)
418 	return ((*aa)->u.srv->weight - (*bb)->u.srv->weight);
419     return ((*aa)->u.srv->priority - (*bb)->u.srv->priority);
420 }
421 
422 #ifndef HAVE_RANDOM
423 #define random() rand()
424 #endif
425 
426 /* try to rearrange the srv-records by the algorithm in RFC2782 */
427 void
428 dns_srv_order(struct dns_reply *r)
429 {
430     struct resource_record **srvs, **ss, **headp;
431     struct resource_record *rr;
432     int num_srv = 0;
433 
434 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
435     int state[256 / sizeof(int)];
436     char *oldstate;
437 #endif
438 
439     for(rr = r->head; rr; rr = rr->next)
440 	if(rr->type == T_SRV)
441 	    num_srv++;
442 
443     if(num_srv == 0)
444 	return;
445 
446     srvs = malloc(num_srv * sizeof(*srvs));
447     if(srvs == NULL)
448 	return; /* XXX not much to do here */
449 
450     /* unlink all srv-records from the linked list and put them in
451        a vector */
452     for(ss = srvs, headp = &r->head; *headp; )
453 	if((*headp)->type == T_SRV) {
454 	    *ss = *headp;
455 	    *headp = (*headp)->next;
456 	    (*ss)->next = NULL;
457 	    ss++;
458 	} else
459 	    headp = &(*headp)->next;
460 
461     /* sort them by priority and weight */
462     qsort(srvs, num_srv, sizeof(*srvs), compare_srv);
463 
464 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
465     oldstate = initstate(time(NULL), (char*)state, sizeof(state));
466 #endif
467 
468     headp = &r->head;
469 
470     for(ss = srvs; ss < srvs + num_srv; ) {
471 	int sum, rnd, count;
472 	struct resource_record **ee, **tt;
473 	/* find the last record with the same priority and count the
474            sum of all weights */
475 	for(sum = 0, tt = ss; tt < srvs + num_srv; tt++) {
476 	    if(*tt == NULL)
477 		continue;
478 	    if((*tt)->u.srv->priority != (*ss)->u.srv->priority)
479 		break;
480 	    sum += (*tt)->u.srv->weight;
481 	}
482 	ee = tt;
483 	/* ss is now the first record of this priority and ee is the
484            first of the next */
485 	while(ss < ee) {
486 	    rnd = random() % (sum + 1);
487 	    for(count = 0, tt = ss; ; tt++) {
488 		if(*tt == NULL)
489 		    continue;
490 		count += (*tt)->u.srv->weight;
491 		if(count >= rnd)
492 		    break;
493 	    }
494 
495 	    assert(tt < ee);
496 
497 	    /* insert the selected record at the tail (of the head) of
498                the list */
499 	    (*tt)->next = *headp;
500 	    *headp = *tt;
501 	    headp = &(*tt)->next;
502 	    sum -= (*tt)->u.srv->weight;
503 	    *tt = NULL;
504 	    while(ss < ee && *ss == NULL)
505 		ss++;
506 	}
507     }
508 
509 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
510     setstate(oldstate);
511 #endif
512     free(srvs);
513     return;
514 }
515 
516 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
517 
518 struct dns_reply *
519 dns_lookup(const char *domain, const char *type_name)
520 {
521     return NULL;
522 }
523 
524 void
525 dns_free_data(struct dns_reply *r)
526 {
527 }
528 
529 void
530 dns_srv_order(struct dns_reply *r)
531 {
532 }
533 
534 #endif
535 
536 #ifdef TEST
537 int
538 main(int argc, char **argv)
539 {
540     struct dns_reply *r;
541     struct resource_record *rr;
542     r = dns_lookup(argv[1], argv[2]);
543     if(r == NULL){
544 	printf("No reply.\n");
545 	return 1;
546     }
547     if(r->q.type == T_SRV)
548 	dns_srv_order(r);
549 
550     for(rr = r->head; rr;rr=rr->next){
551 	printf("%s %s %d ", rr->domain, dns_type_to_string(rr->type), rr->ttl);
552 	switch(rr->type){
553 	case T_NS:
554 	case T_CNAME:
555 	case T_PTR:
556 	    printf("%s\n", (char*)rr->u.data);
557 	    break;
558 	case T_A:
559 	    printf("%s\n", inet_ntoa(*rr->u.a));
560 	    break;
561 	case T_MX:
562 	case T_AFSDB:{
563 	    printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain);
564 	    break;
565 	}
566 	case T_SRV:{
567 	    struct srv_record *srv = rr->u.srv;
568 	    printf("%d %d %d %s\n", srv->priority, srv->weight,
569 		   srv->port, srv->target);
570 	    break;
571 	}
572 	case T_TXT: {
573 	    printf("%s\n", rr->u.txt);
574 	    break;
575 	}
576 	case T_SIG : {
577 	    struct sig_record *sig = rr->u.sig;
578 	    const char *type_string = dns_type_to_string (sig->type);
579 
580 	    printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
581 		    sig->type, type_string ? type_string : "",
582 		    sig->algorithm, sig->labels, sig->orig_ttl,
583 		    sig->sig_expiration, sig->sig_inception, sig->key_tag,
584 		    sig->signer);
585 	    break;
586 	}
587 	case T_KEY : {
588 	    struct key_record *key = rr->u.key;
589 
590 	    printf ("flags %u, protocol %u, algorithm %u\n",
591 		    key->flags, key->protocol, key->algorithm);
592 	    break;
593 	}
594 	default:
595 	    printf("\n");
596 	    break;
597 	}
598     }
599 
600     return 0;
601 }
602 #endif
603