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