]> andersk Git - openssh.git/blame - openbsd-compat/getrrsetbyname.c
- (dtucker) [openbsd-compat/strlcat.c] Sync OpenBSD revs 1.11 - 1.12 (removal
[openssh.git] / openbsd-compat / getrrsetbyname.c
CommitLineData
d74671e4 1/* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
2
ebb049f1 3/* $OpenBSD: getrrsetbyname.c,v 1.10 2005/03/30 02:58:28 tedu Exp $ */
4460d509 4
5/*
6 * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * Portions Copyright (c) 1999-2001 Internet Software Consortium.
33 *
34 * Permission to use, copy, modify, and distribute this software for any
35 * purpose with or without fee is hereby granted, provided that the above
36 * copyright notice and this permission notice appear in all copies.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
39 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
41 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
42 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
43 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
44 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
45 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 */
47
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)
63struct __res_state _res;
4460d509 64
ebb049f1 65/* Necessary functions and macros */
4460d509 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
252ff4df 107#ifndef HAVE__GETSHORT
4460d509 108static 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}
03f5da4c 117#elif defined(HAVE_DECL__GETSHORT) && (HAVE_DECL__GETSHORT == 0)
118u_int16_t _getshort(register const u_char *);
252ff4df 119#endif
4460d509 120
252ff4df 121#ifndef HAVE__GETLONG
4460d509 122static 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}
03f5da4c 131#elif defined(HAVE_DECL__GETLONG) && (HAVE_DECL__GETLONG == 0)
132u_int32_t _getlong(register const u_char *);
252ff4df 133#endif
4460d509 134
ebb049f1 135/* ************** */
136
137#define ANSWER_BUFFER_SIZE 1024*64
138
139struct dns_query {
140 char *name;
141 u_int16_t type;
142 u_int16_t class;
143 struct dns_query *next;
144};
145
146struct 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
156struct 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
164static struct dns_response *parse_dns_response(const u_char *, int);
165static struct dns_query *parse_dns_qsection(const u_char *, int,
166 const u_char **, int);
167static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
168 int);
169
170static void free_dns_query(struct dns_query *);
171static void free_dns_rr(struct dns_rr *);
172static void free_dns_response(struct dns_response *);
173
174static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
175
4460d509 176int
177getrrsetbyname(const char *hostname, unsigned int rdclass,
178 unsigned int rdtype, unsigned int flags,
179 struct rrsetinfo **res)
180{
ebb049f1 181 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
4460d509 182 int result;
183 struct rrsetinfo *rrset = NULL;
ebb049f1 184 struct dns_response *response = NULL;
4460d509 185 struct dns_rr *rr;
186 struct rdatainfo *rdata;
bffa6723 187 int length;
188 unsigned int index_ans, index_sig;
189 u_char answer[ANSWER_BUFFER_SIZE];
4460d509 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 */
ebb049f1 210 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
4460d509 211 result = ERRSET_FAIL;
212 goto fail;
213 }
214
215#ifdef DEBUG
ebb049f1 216 _resp->options |= RES_DEBUG;
4460d509 217#endif /* DEBUG */
218
219#ifdef RES_USE_DNSSEC
220 /* turn on DNSSEC if EDNS0 is configured */
ebb049f1 221 if (_resp->options & RES_USE_EDNS0)
222 _resp->options |= RES_USE_DNSSEC;
4460d509 223#endif /* RES_USE_DNSEC */
224
225 /* make query */
bffa6723 226 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
227 answer, sizeof(answer));
4460d509 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 /* check for authenticated data */
266 if (response->header.ad == 1)
267 rrset->rri_flags |= RRSET_VALIDATED;
268
269 /* copy name from answer section */
ebb049f1 270 rrset->rri_name = strdup(response->answer->name);
4460d509 271 if (rrset->rri_name == NULL) {
272 result = ERRSET_NOMEMORY;
273 goto fail;
274 }
4460d509 275
276 /* count answers */
277 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
278 rrset->rri_rdtype);
279 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
280 T_SIG);
281
282 /* allocate memory for answers */
283 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
284 sizeof(struct rdatainfo));
285 if (rrset->rri_rdatas == NULL) {
286 result = ERRSET_NOMEMORY;
287 goto fail;
288 }
289
290 /* allocate memory for signatures */
291 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
ebb049f1 292 if (rrset->rri_sigs == NULL) {
4460d509 293 result = ERRSET_NOMEMORY;
294 goto fail;
295 }
296
297 /* copy answers & signatures */
298 for (rr = response->answer, index_ans = 0, index_sig = 0;
299 rr; rr = rr->next) {
300
301 rdata = NULL;
302
303 if (rr->class == rrset->rri_rdclass &&
304 rr->type == rrset->rri_rdtype)
305 rdata = &rrset->rri_rdatas[index_ans++];
306
307 if (rr->class == rrset->rri_rdclass &&
308 rr->type == T_SIG)
309 rdata = &rrset->rri_sigs[index_sig++];
310
311 if (rdata) {
312 rdata->rdi_length = rr->size;
313 rdata->rdi_data = malloc(rr->size);
314
315 if (rdata->rdi_data == NULL) {
316 result = ERRSET_NOMEMORY;
317 goto fail;
318 }
319 memcpy(rdata->rdi_data, rr->rdata, rr->size);
320 }
321 }
ebb049f1 322 free_dns_response(response);
4460d509 323
324 *res = rrset;
325 return (ERRSET_SUCCESS);
326
327fail:
328 if (rrset != NULL)
329 freerrset(rrset);
ebb049f1 330 if (response != NULL)
331 free_dns_response(response);
4460d509 332 return (result);
333}
334
335void
336freerrset(struct rrsetinfo *rrset)
337{
338 u_int16_t i;
339
340 if (rrset == NULL)
341 return;
342
343 if (rrset->rri_rdatas) {
344 for (i = 0; i < rrset->rri_nrdatas; i++) {
345 if (rrset->rri_rdatas[i].rdi_data == NULL)
346 break;
347 free(rrset->rri_rdatas[i].rdi_data);
348 }
349 free(rrset->rri_rdatas);
350 }
351
352 if (rrset->rri_sigs) {
353 for (i = 0; i < rrset->rri_nsigs; i++) {
354 if (rrset->rri_sigs[i].rdi_data == NULL)
355 break;
356 free(rrset->rri_sigs[i].rdi_data);
357 }
358 free(rrset->rri_sigs);
359 }
360
361 if (rrset->rri_name)
362 free(rrset->rri_name);
363 free(rrset);
364}
365
366/*
367 * DNS response parsing routines
368 */
369static struct dns_response *
bffa6723 370parse_dns_response(const u_char *answer, int size)
4460d509 371{
372 struct dns_response *resp;
bffa6723 373 const u_char *cp;
4460d509 374
375 /* allocate memory for the response */
376 resp = calloc(1, sizeof(*resp));
377 if (resp == NULL)
378 return (NULL);
379
380 /* initialize current pointer */
381 cp = answer;
382
383 /* copy header */
384 memcpy(&resp->header, cp, HFIXEDSZ);
385 cp += HFIXEDSZ;
386
387 /* fix header byte order */
388 resp->header.qdcount = ntohs(resp->header.qdcount);
389 resp->header.ancount = ntohs(resp->header.ancount);
390 resp->header.nscount = ntohs(resp->header.nscount);
391 resp->header.arcount = ntohs(resp->header.arcount);
392
393 /* there must be at least one query */
394 if (resp->header.qdcount < 1) {
395 free_dns_response(resp);
396 return (NULL);
397 }
398
399 /* parse query section */
400 resp->query = parse_dns_qsection(answer, size, &cp,
401 resp->header.qdcount);
402 if (resp->header.qdcount && resp->query == NULL) {
403 free_dns_response(resp);
404 return (NULL);
405 }
406
407 /* parse answer section */
408 resp->answer = parse_dns_rrsection(answer, size, &cp,
409 resp->header.ancount);
410 if (resp->header.ancount && resp->answer == NULL) {
411 free_dns_response(resp);
412 return (NULL);
413 }
414
415 /* parse authority section */
416 resp->authority = parse_dns_rrsection(answer, size, &cp,
417 resp->header.nscount);
418 if (resp->header.nscount && resp->authority == NULL) {
419 free_dns_response(resp);
420 return (NULL);
421 }
422
423 /* parse additional section */
424 resp->additional = parse_dns_rrsection(answer, size, &cp,
425 resp->header.arcount);
426 if (resp->header.arcount && resp->additional == NULL) {
427 free_dns_response(resp);
428 return (NULL);
429 }
430
431 return (resp);
432}
433
434static struct dns_query *
bffa6723 435parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
4460d509 436{
437 struct dns_query *head, *curr, *prev;
438 int i, length;
439 char name[MAXDNAME];
440
441 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
442
443 /* allocate and initialize struct */
444 curr = calloc(1, sizeof(struct dns_query));
445 if (curr == NULL) {
446 free_dns_query(head);
447 return (NULL);
448 }
449 if (head == NULL)
450 head = curr;
451 if (prev != NULL)
452 prev->next = curr;
453
454 /* name */
455 length = dn_expand(answer, answer + size, *cp, name,
456 sizeof(name));
457 if (length < 0) {
458 free_dns_query(head);
459 return (NULL);
460 }
461 curr->name = strdup(name);
462 if (curr->name == NULL) {
463 free_dns_query(head);
464 return (NULL);
465 }
466 *cp += length;
467
468 /* type */
469 curr->type = _getshort(*cp);
470 *cp += INT16SZ;
471
472 /* class */
473 curr->class = _getshort(*cp);
474 *cp += INT16SZ;
475 }
476
477 return (head);
478}
479
480static struct dns_rr *
ebb049f1 481parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
482 int count)
4460d509 483{
484 struct dns_rr *head, *curr, *prev;
485 int i, length;
486 char name[MAXDNAME];
487
488 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
489
490 /* allocate and initialize struct */
491 curr = calloc(1, sizeof(struct dns_rr));
492 if (curr == NULL) {
493 free_dns_rr(head);
494 return (NULL);
495 }
496 if (head == NULL)
497 head = curr;
498 if (prev != NULL)
499 prev->next = curr;
500
501 /* name */
502 length = dn_expand(answer, answer + size, *cp, name,
503 sizeof(name));
504 if (length < 0) {
505 free_dns_rr(head);
506 return (NULL);
507 }
508 curr->name = strdup(name);
509 if (curr->name == NULL) {
510 free_dns_rr(head);
511 return (NULL);
512 }
513 *cp += length;
514
515 /* type */
516 curr->type = _getshort(*cp);
517 *cp += INT16SZ;
518
519 /* class */
520 curr->class = _getshort(*cp);
521 *cp += INT16SZ;
522
523 /* ttl */
524 curr->ttl = _getlong(*cp);
525 *cp += INT32SZ;
526
527 /* rdata size */
528 curr->size = _getshort(*cp);
529 *cp += INT16SZ;
530
531 /* rdata itself */
532 curr->rdata = malloc(curr->size);
533 if (curr->rdata == NULL) {
534 free_dns_rr(head);
535 return (NULL);
536 }
537 memcpy(curr->rdata, *cp, curr->size);
538 *cp += curr->size;
539 }
540
541 return (head);
542}
543
544static void
545free_dns_query(struct dns_query *p)
546{
547 if (p == NULL)
548 return;
549
550 if (p->name)
551 free(p->name);
552 free_dns_query(p->next);
553 free(p);
554}
555
556static void
557free_dns_rr(struct dns_rr *p)
558{
559 if (p == NULL)
560 return;
561
562 if (p->name)
563 free(p->name);
564 if (p->rdata)
565 free(p->rdata);
566 free_dns_rr(p->next);
567 free(p);
568}
569
570static void
571free_dns_response(struct dns_response *p)
572{
573 if (p == NULL)
574 return;
575
576 free_dns_query(p->query);
577 free_dns_rr(p->answer);
578 free_dns_rr(p->authority);
579 free_dns_rr(p->additional);
580 free(p);
581}
582
583static int
584count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
585{
586 int n = 0;
587
588 while(p) {
589 if (p->class == class && p->type == type)
590 n++;
591 p = p->next;
592 }
593
594 return (n);
595}
596
c369b7c3 597#endif /* !defined(HAVE_GETRRSETBYNAME) */
This page took 0.283678 seconds and 5 git commands to generate.