xref: /freebsd/crypto/openssh/openbsd-compat/getrrsetbyname.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /* $OpenBSD: getrrsetbyname.c,v 1.11 2007/10/11 18:36:41 jakob Exp $ */
2 
3 /*
4  * Copyright (c) 2001 Jakob Schlyter. 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  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Portions Copyright (c) 1999-2001 Internet Software Consortium.
31  *
32  * Permission to use, copy, modify, and distribute this software for any
33  * purpose with or without fee is hereby granted, provided that the above
34  * copyright notice and this permission notice appear in all copies.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
37  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
39  * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
40  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
41  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
42  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44  */
45 
46 /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
47 
48 #include "includes.h"
49 
50 #if !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS)
51 
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 
58 #include "getrrsetbyname.h"
59 
60 #if defined(HAVE_DECL_H_ERRNO) && !HAVE_DECL_H_ERRNO
61 extern int h_errno;
62 #endif
63 
64 /* We don't need multithread support here */
65 #ifdef _THREAD_PRIVATE
66 # undef _THREAD_PRIVATE
67 #endif
68 #define _THREAD_PRIVATE(a,b,c) (c)
69 
70 #ifndef HAVE__RES_EXTERN
71 struct __res_state _res;
72 #endif
73 
74 /* Necessary functions and macros */
75 
76 /*
77  * Inline versions of get/put short/long.  Pointer is advanced.
78  *
79  * These macros demonstrate the property of C whereby it can be
80  * portable or it can be elegant but rarely both.
81  */
82 
83 #ifndef INT32SZ
84 # define INT32SZ	4
85 #endif
86 #ifndef INT16SZ
87 # define INT16SZ	2
88 #endif
89 
90 #ifndef GETSHORT
91 #define GETSHORT(s, cp) { \
92 	u_char *t_cp = (u_char *)(cp); \
93 	(s) = ((u_int16_t)t_cp[0] << 8) \
94 	    | ((u_int16_t)t_cp[1]) \
95 	    ; \
96 	(cp) += INT16SZ; \
97 }
98 #endif
99 
100 #ifndef GETLONG
101 #define GETLONG(l, cp) { \
102 	u_char *t_cp = (u_char *)(cp); \
103 	(l) = ((u_int32_t)t_cp[0] << 24) \
104 	    | ((u_int32_t)t_cp[1] << 16) \
105 	    | ((u_int32_t)t_cp[2] << 8) \
106 	    | ((u_int32_t)t_cp[3]) \
107 	    ; \
108 	(cp) += INT32SZ; \
109 }
110 #endif
111 
112 /*
113  * If the system doesn't have _getshort/_getlong or that are not exactly what
114  * we need then use local replacements, avoiding name collisions.
115  */
116 #if !defined(HAVE__GETSHORT) || !defined(HAVE__GETLONG) || \
117     !defined(HAVE_DECL__GETSHORT) || HAVE_DECL__GETSHORT == 0 || \
118     !defined(HAVE_DECL__GETLONG) || HAVE_DECL__GETLONG == 0
119 #define _getshort(x) (_ssh_compat_getshort(x))
120 #define _getlong(x) (_ssh_compat_getlong(x))
121 /*
122  * Routines to insert/extract short/long's.
123  */
124 static u_int16_t
125 _getshort(const u_char *msgp)
126 {
127 	u_int16_t u;
128 
129 	GETSHORT(u, msgp);
130 	return (u);
131 }
132 
133 static u_int32_t
134 _getlong(const u_char *msgp)
135 {
136 	u_int32_t u;
137 
138 	GETLONG(u, msgp);
139 	return (u);
140 }
141 #endif
142 
143 /* ************** */
144 
145 #define ANSWER_BUFFER_SIZE 0xffff
146 
147 struct dns_query {
148 	char			*name;
149 	u_int16_t		type;
150 	u_int16_t		class;
151 	struct dns_query	*next;
152 };
153 
154 struct dns_rr {
155 	char			*name;
156 	u_int16_t		type;
157 	u_int16_t		class;
158 	u_int16_t		ttl;
159 	u_int16_t		size;
160 	void			*rdata;
161 	struct dns_rr		*next;
162 };
163 
164 struct dns_response {
165 	HEADER			header;
166 	struct dns_query	*query;
167 	struct dns_rr		*answer;
168 	struct dns_rr		*authority;
169 	struct dns_rr		*additional;
170 };
171 
172 static struct dns_response *parse_dns_response(const u_char *, int);
173 static struct dns_query *parse_dns_qsection(const u_char *, int,
174     const u_char **, int);
175 static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
176     int);
177 
178 static void free_dns_query(struct dns_query *);
179 static void free_dns_rr(struct dns_rr *);
180 static void free_dns_response(struct dns_response *);
181 
182 static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
183 
184 int
185 getrrsetbyname(const char *hostname, unsigned int rdclass,
186     unsigned int rdtype, unsigned int flags,
187     struct rrsetinfo **res)
188 {
189 	struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
190 	int result;
191 	struct rrsetinfo *rrset = NULL;
192 	struct dns_response *response = NULL;
193 	struct dns_rr *rr;
194 	struct rdatainfo *rdata;
195 	int length;
196 	unsigned int index_ans, index_sig;
197 	u_char answer[ANSWER_BUFFER_SIZE];
198 
199 	/* check for invalid class and type */
200 	if (rdclass > 0xffff || rdtype > 0xffff) {
201 		result = ERRSET_INVAL;
202 		goto fail;
203 	}
204 
205 	/* don't allow queries of class or type ANY */
206 	if (rdclass == 0xff || rdtype == 0xff) {
207 		result = ERRSET_INVAL;
208 		goto fail;
209 	}
210 
211 	/* don't allow flags yet, unimplemented */
212 	if (flags) {
213 		result = ERRSET_INVAL;
214 		goto fail;
215 	}
216 
217 	/* initialize resolver */
218 	if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
219 		result = ERRSET_FAIL;
220 		goto fail;
221 	}
222 
223 #ifdef DEBUG
224 	_resp->options |= RES_DEBUG;
225 #endif /* DEBUG */
226 
227 #ifdef RES_USE_DNSSEC
228 	/* turn on DNSSEC if EDNS0 is configured */
229 	if (_resp->options & RES_USE_EDNS0)
230 		_resp->options |= RES_USE_DNSSEC;
231 #endif /* RES_USE_DNSEC */
232 
233 	/* make query */
234 	length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
235 	    answer, sizeof(answer));
236 	if (length < 0) {
237 		switch(h_errno) {
238 		case HOST_NOT_FOUND:
239 			result = ERRSET_NONAME;
240 			goto fail;
241 		case NO_DATA:
242 			result = ERRSET_NODATA;
243 			goto fail;
244 		default:
245 			result = ERRSET_FAIL;
246 			goto fail;
247 		}
248 	}
249 
250 	/* parse result */
251 	response = parse_dns_response(answer, length);
252 	if (response == NULL) {
253 		result = ERRSET_FAIL;
254 		goto fail;
255 	}
256 
257 	if (response->header.qdcount != 1) {
258 		result = ERRSET_FAIL;
259 		goto fail;
260 	}
261 
262 	/* initialize rrset */
263 	rrset = calloc(1, sizeof(struct rrsetinfo));
264 	if (rrset == NULL) {
265 		result = ERRSET_NOMEMORY;
266 		goto fail;
267 	}
268 	rrset->rri_rdclass = response->query->class;
269 	rrset->rri_rdtype = response->query->type;
270 	rrset->rri_ttl = response->answer->ttl;
271 	rrset->rri_nrdatas = response->header.ancount;
272 
273 #ifdef HAVE_HEADER_AD
274 	/* check for authenticated data */
275 	if (response->header.ad == 1)
276 		rrset->rri_flags |= RRSET_VALIDATED;
277 #endif
278 
279 	/* copy name from answer section */
280 	rrset->rri_name = strdup(response->answer->name);
281 	if (rrset->rri_name == NULL) {
282 		result = ERRSET_NOMEMORY;
283 		goto fail;
284 	}
285 
286 	/* count answers */
287 	rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
288 	    rrset->rri_rdtype);
289 	rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
290 	    T_RRSIG);
291 
292 	/* allocate memory for answers */
293 	rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
294 	    sizeof(struct rdatainfo));
295 	if (rrset->rri_rdatas == NULL) {
296 		result = ERRSET_NOMEMORY;
297 		goto fail;
298 	}
299 
300 	/* allocate memory for signatures */
301 	if (rrset->rri_nsigs > 0) {
302 		rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
303 		if (rrset->rri_sigs == NULL) {
304 			result = ERRSET_NOMEMORY;
305 			goto fail;
306 		}
307 	}
308 
309 	/* copy answers & signatures */
310 	for (rr = response->answer, index_ans = 0, index_sig = 0;
311 	    rr; rr = rr->next) {
312 
313 		rdata = NULL;
314 
315 		if (rr->class == rrset->rri_rdclass &&
316 		    rr->type  == rrset->rri_rdtype)
317 			rdata = &rrset->rri_rdatas[index_ans++];
318 
319 		if (rr->class == rrset->rri_rdclass &&
320 		    rr->type  == T_RRSIG)
321 			rdata = &rrset->rri_sigs[index_sig++];
322 
323 		if (rdata) {
324 			rdata->rdi_length = rr->size;
325 			rdata->rdi_data   = malloc(rr->size);
326 
327 			if (rdata->rdi_data == NULL) {
328 				result = ERRSET_NOMEMORY;
329 				goto fail;
330 			}
331 			memcpy(rdata->rdi_data, rr->rdata, rr->size);
332 		}
333 	}
334 	free_dns_response(response);
335 
336 	*res = rrset;
337 	return (ERRSET_SUCCESS);
338 
339 fail:
340 	if (rrset != NULL)
341 		freerrset(rrset);
342 	if (response != NULL)
343 		free_dns_response(response);
344 	return (result);
345 }
346 
347 void
348 freerrset(struct rrsetinfo *rrset)
349 {
350 	u_int16_t i;
351 
352 	if (rrset == NULL)
353 		return;
354 
355 	if (rrset->rri_rdatas) {
356 		for (i = 0; i < rrset->rri_nrdatas; i++) {
357 			if (rrset->rri_rdatas[i].rdi_data == NULL)
358 				break;
359 			free(rrset->rri_rdatas[i].rdi_data);
360 		}
361 		free(rrset->rri_rdatas);
362 	}
363 
364 	if (rrset->rri_sigs) {
365 		for (i = 0; i < rrset->rri_nsigs; i++) {
366 			if (rrset->rri_sigs[i].rdi_data == NULL)
367 				break;
368 			free(rrset->rri_sigs[i].rdi_data);
369 		}
370 		free(rrset->rri_sigs);
371 	}
372 
373 	if (rrset->rri_name)
374 		free(rrset->rri_name);
375 	free(rrset);
376 }
377 
378 /*
379  * DNS response parsing routines
380  */
381 static struct dns_response *
382 parse_dns_response(const u_char *answer, int size)
383 {
384 	struct dns_response *resp;
385 	const u_char *cp;
386 
387 	/* allocate memory for the response */
388 	resp = calloc(1, sizeof(*resp));
389 	if (resp == NULL)
390 		return (NULL);
391 
392 	/* initialize current pointer */
393 	cp = answer;
394 
395 	/* copy header */
396 	memcpy(&resp->header, cp, HFIXEDSZ);
397 	cp += HFIXEDSZ;
398 
399 	/* fix header byte order */
400 	resp->header.qdcount = ntohs(resp->header.qdcount);
401 	resp->header.ancount = ntohs(resp->header.ancount);
402 	resp->header.nscount = ntohs(resp->header.nscount);
403 	resp->header.arcount = ntohs(resp->header.arcount);
404 
405 	/* there must be at least one query */
406 	if (resp->header.qdcount < 1) {
407 		free_dns_response(resp);
408 		return (NULL);
409 	}
410 
411 	/* parse query section */
412 	resp->query = parse_dns_qsection(answer, size, &cp,
413 	    resp->header.qdcount);
414 	if (resp->header.qdcount && resp->query == NULL) {
415 		free_dns_response(resp);
416 		return (NULL);
417 	}
418 
419 	/* parse answer section */
420 	resp->answer = parse_dns_rrsection(answer, size, &cp,
421 	    resp->header.ancount);
422 	if (resp->header.ancount && resp->answer == NULL) {
423 		free_dns_response(resp);
424 		return (NULL);
425 	}
426 
427 	/* parse authority section */
428 	resp->authority = parse_dns_rrsection(answer, size, &cp,
429 	    resp->header.nscount);
430 	if (resp->header.nscount && resp->authority == NULL) {
431 		free_dns_response(resp);
432 		return (NULL);
433 	}
434 
435 	/* parse additional section */
436 	resp->additional = parse_dns_rrsection(answer, size, &cp,
437 	    resp->header.arcount);
438 	if (resp->header.arcount && resp->additional == NULL) {
439 		free_dns_response(resp);
440 		return (NULL);
441 	}
442 
443 	return (resp);
444 }
445 
446 static struct dns_query *
447 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
448 {
449 	struct dns_query *head, *curr, *prev;
450 	int i, length;
451 	char name[MAXDNAME];
452 
453 	for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
454 
455 		/* allocate and initialize struct */
456 		curr = calloc(1, sizeof(struct dns_query));
457 		if (curr == NULL) {
458 			free_dns_query(head);
459 			return (NULL);
460 		}
461 		if (head == NULL)
462 			head = curr;
463 		if (prev != NULL)
464 			prev->next = curr;
465 
466 		/* name */
467 		length = dn_expand(answer, answer + size, *cp, name,
468 		    sizeof(name));
469 		if (length < 0) {
470 			free_dns_query(head);
471 			return (NULL);
472 		}
473 		curr->name = strdup(name);
474 		if (curr->name == NULL) {
475 			free_dns_query(head);
476 			return (NULL);
477 		}
478 		*cp += length;
479 
480 		/* type */
481 		curr->type = _getshort(*cp);
482 		*cp += INT16SZ;
483 
484 		/* class */
485 		curr->class = _getshort(*cp);
486 		*cp += INT16SZ;
487 	}
488 
489 	return (head);
490 }
491 
492 static struct dns_rr *
493 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
494     int count)
495 {
496 	struct dns_rr *head, *curr, *prev;
497 	int i, length;
498 	char name[MAXDNAME];
499 
500 	for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
501 
502 		/* allocate and initialize struct */
503 		curr = calloc(1, sizeof(struct dns_rr));
504 		if (curr == NULL) {
505 			free_dns_rr(head);
506 			return (NULL);
507 		}
508 		if (head == NULL)
509 			head = curr;
510 		if (prev != NULL)
511 			prev->next = curr;
512 
513 		/* name */
514 		length = dn_expand(answer, answer + size, *cp, name,
515 		    sizeof(name));
516 		if (length < 0) {
517 			free_dns_rr(head);
518 			return (NULL);
519 		}
520 		curr->name = strdup(name);
521 		if (curr->name == NULL) {
522 			free_dns_rr(head);
523 			return (NULL);
524 		}
525 		*cp += length;
526 
527 		/* type */
528 		curr->type = _getshort(*cp);
529 		*cp += INT16SZ;
530 
531 		/* class */
532 		curr->class = _getshort(*cp);
533 		*cp += INT16SZ;
534 
535 		/* ttl */
536 		curr->ttl = _getlong(*cp);
537 		*cp += INT32SZ;
538 
539 		/* rdata size */
540 		curr->size = _getshort(*cp);
541 		*cp += INT16SZ;
542 
543 		/* rdata itself */
544 		curr->rdata = malloc(curr->size);
545 		if (curr->rdata == NULL) {
546 			free_dns_rr(head);
547 			return (NULL);
548 		}
549 		memcpy(curr->rdata, *cp, curr->size);
550 		*cp += curr->size;
551 	}
552 
553 	return (head);
554 }
555 
556 static void
557 free_dns_query(struct dns_query *p)
558 {
559 	if (p == NULL)
560 		return;
561 
562 	if (p->name)
563 		free(p->name);
564 	free_dns_query(p->next);
565 	free(p);
566 }
567 
568 static void
569 free_dns_rr(struct dns_rr *p)
570 {
571 	if (p == NULL)
572 		return;
573 
574 	if (p->name)
575 		free(p->name);
576 	if (p->rdata)
577 		free(p->rdata);
578 	free_dns_rr(p->next);
579 	free(p);
580 }
581 
582 static void
583 free_dns_response(struct dns_response *p)
584 {
585 	if (p == NULL)
586 		return;
587 
588 	free_dns_query(p->query);
589 	free_dns_rr(p->answer);
590 	free_dns_rr(p->authority);
591 	free_dns_rr(p->additional);
592 	free(p);
593 }
594 
595 static int
596 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
597 {
598 	int n = 0;
599 
600 	while(p) {
601 		if (p->class == class && p->type == type)
602 			n++;
603 		p = p->next;
604 	}
605 
606 	return (n);
607 }
608 
609 #endif /*  !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS) */
610