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