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