1 
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17 
18 #include "ares_setup.h"
19 
20 #ifdef HAVE_NETINET_IN_H
21 #  include <netinet/in.h>
22 #endif
23 #ifdef HAVE_NETDB_H
24 #  include <netdb.h>
25 #endif
26 #ifdef HAVE_ARPA_INET_H
27 #  include <arpa/inet.h>
28 #endif
29 #ifdef HAVE_ARPA_NAMESER_H
30 #  include <arpa/nameser.h>
31 #else
32 #  include "nameser.h"
33 #endif
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 #  include <arpa/nameser_compat.h>
36 #endif
37 
38 #ifdef HAVE_STRINGS_H
39 #  include <strings.h>
40 #endif
41 
42 #include "ares.h"
43 #include "ares_dns.h"
44 #include "ares_data.h"
45 #include "ares_private.h"
46 
47 static int
ares__parse_txt_reply(const unsigned char * abuf,int alen,int ex,void ** txt_out)48 ares__parse_txt_reply (const unsigned char *abuf, int alen,
49                        int ex, void **txt_out)
50 {
51   size_t substr_len;
52   unsigned int qdcount, ancount, i;
53   const unsigned char *aptr;
54   const unsigned char *strptr;
55   int status, rr_type, rr_class, rr_len, rr_ttl;
56   long len;
57   char *hostname = NULL, *rr_name = NULL;
58   struct ares_txt_ext *txt_head = NULL;
59   struct ares_txt_ext *txt_last = NULL;
60   struct ares_txt_ext *txt_curr;
61 
62   /* Set *txt_out to NULL for all failure cases. */
63   *txt_out = NULL;
64 
65   /* Give up if abuf doesn't have room for a header. */
66   if (alen < HFIXEDSZ)
67     return ARES_EBADRESP;
68 
69   /* Fetch the question and answer count from the header. */
70   qdcount = DNS_HEADER_QDCOUNT (abuf);
71   ancount = DNS_HEADER_ANCOUNT (abuf);
72   if (qdcount != 1)
73     return ARES_EBADRESP;
74   if (ancount == 0)
75     return ARES_ENODATA;
76 
77   /* Expand the name from the question, and skip past the question. */
78   aptr = abuf + HFIXEDSZ;
79   status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
80   if (status != ARES_SUCCESS)
81     return status;
82 
83   if (aptr + len + QFIXEDSZ > abuf + alen)
84     {
85       ares_free (hostname);
86       return ARES_EBADRESP;
87     }
88   aptr += len + QFIXEDSZ;
89 
90   /* Examine each answer resource record (RR) in turn. */
91   for (i = 0; i < ancount; i++)
92     {
93       /* Decode the RR up to the data field. */
94       status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
95       if (status != ARES_SUCCESS)
96         {
97           break;
98         }
99       aptr += len;
100       if (aptr + RRFIXEDSZ > abuf + alen)
101         {
102           status = ARES_EBADRESP;
103           break;
104         }
105       rr_type = DNS_RR_TYPE (aptr);
106       rr_class = DNS_RR_CLASS (aptr);
107       rr_len = DNS_RR_LEN (aptr);
108       rr_ttl = DNS_RR_TTL (aptr);
109       aptr += RRFIXEDSZ;
110       if (aptr + rr_len > abuf + alen)
111         {
112           status = ARES_EBADRESP;
113           break;
114         }
115 
116       /* Check if we are really looking at a TXT record */
117       if (rr_class == C_IN && rr_type == T_TXT)
118         {
119           /*
120            * There may be multiple substrings in a single TXT record. Each
121            * substring may be up to 255 characters in length, with a
122            * "length byte" indicating the size of the substring payload.
123            * RDATA contains both the length-bytes and payloads of all
124            * substrings contained therein.
125            */
126 
127           strptr = aptr;
128           while (strptr < (aptr + rr_len))
129             {
130               substr_len = (unsigned char)*strptr;
131               if (strptr + substr_len + 1 > aptr + rr_len)
132                 {
133                   status = ARES_EBADRESP;
134                   break;
135                 }
136 
137               /* Allocate storage for this TXT answer appending it to the list */
138               txt_curr = ares_malloc_data(ex ? ARES_DATATYPE_TXT_EXT :
139                                                ARES_DATATYPE_TXT_REPLY);
140               if (!txt_curr)
141                 {
142                   status = ARES_ENOMEM;
143                   break;
144                 }
145               if (txt_last)
146                 {
147                   txt_last->next = txt_curr;
148                 }
149               else
150                 {
151                   txt_head = txt_curr;
152                 }
153               txt_last = txt_curr;
154 
155               if (ex)
156                 txt_curr->record_start = (strptr == aptr);
157               txt_curr->length = substr_len;
158               txt_curr->txt = ares_malloc (substr_len + 1/* Including null byte */);
159               if (txt_curr->txt == NULL)
160                 {
161                   status = ARES_ENOMEM;
162                   break;
163                 }
164 
165               ++strptr;
166               memcpy ((char *) txt_curr->txt, strptr, substr_len);
167 
168               /* Make sure we NULL-terminate */
169               txt_curr->txt[substr_len] = 0;
170               txt_curr->ttl = rr_ttl;
171 
172               strptr += substr_len;
173             }
174         }
175 
176       /* Propagate any failures */
177       if (status != ARES_SUCCESS)
178         {
179           break;
180         }
181 
182       /* Don't lose memory in the next iteration */
183       ares_free (rr_name);
184       rr_name = NULL;
185 
186       /* Move on to the next record */
187       aptr += rr_len;
188     }
189 
190   if (hostname)
191     ares_free (hostname);
192   if (rr_name)
193     ares_free (rr_name);
194 
195   /* clean up on error */
196   if (status != ARES_SUCCESS)
197     {
198       if (txt_head)
199         ares_free_data (txt_head);
200       return status;
201     }
202 
203   /* everything looks fine, return the data */
204   *txt_out = txt_head;
205 
206   return ARES_SUCCESS;
207 }
208 
209 int
ares_parse_txt_reply(const unsigned char * abuf,int alen,struct ares_txt_reply ** txt_out)210 ares_parse_txt_reply (const unsigned char *abuf, int alen,
211                       struct ares_txt_reply **txt_out)
212 {
213   return ares__parse_txt_reply(abuf, alen, 0, (void **) txt_out);
214 }
215 
216 
217 int
ares_parse_txt_reply_ext(const unsigned char * abuf,int alen,struct ares_txt_ext ** txt_out)218 ares_parse_txt_reply_ext (const unsigned char *abuf, int alen,
219                           struct ares_txt_ext **txt_out)
220 {
221   return ares__parse_txt_reply(abuf, alen, 1, (void **) txt_out);
222 }
223