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