1 
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16 
17 #include "ares_setup.h"
18 
19 #ifdef HAVE_SYS_SOCKET_H
20 #  include <sys/socket.h>
21 #endif
22 #ifdef HAVE_NETINET_IN_H
23 #  include <netinet/in.h>
24 #endif
25 #ifdef HAVE_NETDB_H
26 #  include <netdb.h>
27 #endif
28 #ifdef HAVE_ARPA_INET_H
29 #  include <arpa/inet.h>
30 #endif
31 #ifdef HAVE_ARPA_NAMESER_H
32 #  include <arpa/nameser.h>
33 #else
34 #  include "nameser.h"
35 #endif
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 #  include <arpa/nameser_compat.h>
38 #endif
39 
40 #ifdef HAVE_STRINGS_H
41 #  include <strings.h>
42 #endif
43 
44 #include <stdlib.h>
45 #include <string.h>
46 #ifdef HAVE_LIMITS_H
47 #  include <limits.h>
48 #endif
49 
50 #include "ares.h"
51 #include "ares_dns.h"
52 #include "ares_private.h"
53 
ares_parse_a_reply(const unsigned char * abuf,int alen,struct hostent ** host,struct ares_addrttl * addrttls,int * naddrttls)54 int ares_parse_a_reply(const unsigned char *abuf, int alen,
55                        struct hostent **host,
56                        struct ares_addrttl *addrttls, int *naddrttls)
57 {
58   unsigned int qdcount, ancount;
59   int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
60   int cname_ttl = INT_MAX;  /* the TTL imposed by the CNAME chain */
61   int naliases;
62   long len;
63   const unsigned char *aptr;
64   char *hostname, *rr_name, *rr_data, **aliases;
65   struct in_addr *addrs;
66   struct hostent *hostent;
67   const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
68 
69   /* Set *host to NULL for all failure cases. */
70   if (host)
71     *host = NULL;
72   /* Same with *naddrttls. */
73   if (naddrttls)
74     *naddrttls = 0;
75 
76   /* Give up if abuf doesn't have room for a header. */
77   if (alen < HFIXEDSZ)
78     return ARES_EBADRESP;
79 
80   /* Fetch the question and answer count from the header. */
81   qdcount = DNS_HEADER_QDCOUNT(abuf);
82   ancount = DNS_HEADER_ANCOUNT(abuf);
83   if (qdcount != 1)
84     return ARES_EBADRESP;
85 
86   /* Expand the name from the question, and skip past the question. */
87   aptr = abuf + HFIXEDSZ;
88   status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
89   if (status != ARES_SUCCESS)
90     return status;
91   if (aptr + len + QFIXEDSZ > abuf + alen)
92     {
93       free(hostname);
94       return ARES_EBADRESP;
95     }
96   aptr += len + QFIXEDSZ;
97 
98   if (host)
99     {
100       /* Allocate addresses and aliases; ancount gives an upper bound for
101          both. */
102       addrs = malloc(ancount * sizeof(struct in_addr));
103       if (!addrs)
104         {
105           free(hostname);
106           return ARES_ENOMEM;
107         }
108       aliases = malloc((ancount + 1) * sizeof(char *));
109       if (!aliases)
110         {
111           free(hostname);
112           free(addrs);
113           return ARES_ENOMEM;
114         }
115     }
116   else
117     {
118       addrs = NULL;
119       aliases = NULL;
120     }
121 
122   naddrs = 0;
123   naliases = 0;
124 
125   /* Examine each answer resource record (RR) in turn. */
126   for (i = 0; i < (int)ancount; i++)
127     {
128       /* Decode the RR up to the data field. */
129       status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
130       if (status != ARES_SUCCESS)
131         break;
132       aptr += len;
133       if (aptr + RRFIXEDSZ > abuf + alen)
134         {
135           free(rr_name);
136           status = ARES_EBADRESP;
137           break;
138         }
139       rr_type = DNS_RR_TYPE(aptr);
140       rr_class = DNS_RR_CLASS(aptr);
141       rr_len = DNS_RR_LEN(aptr);
142       rr_ttl = DNS_RR_TTL(aptr);
143       aptr += RRFIXEDSZ;
144 
145       if (rr_class == C_IN && rr_type == T_A
146           && rr_len == sizeof(struct in_addr)
147           && strcasecmp(rr_name, hostname) == 0)
148         {
149           if (addrs)
150             {
151               if (aptr + sizeof(struct in_addr) > abuf + alen)
152               {
153                 free(rr_name);
154                 status = ARES_EBADRESP;
155                 break;
156               }
157               memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
158             }
159           if (naddrs < max_addr_ttls)
160             {
161               struct ares_addrttl * const at = &addrttls[naddrs];
162               if (aptr + sizeof(struct in_addr) > abuf + alen)
163               {
164                 free(rr_name);
165                 status = ARES_EBADRESP;
166                 break;
167               }
168               memcpy(&at->ipaddr, aptr,  sizeof(struct in_addr));
169               at->ttl = rr_ttl;
170             }
171           naddrs++;
172           status = ARES_SUCCESS;
173         }
174 
175       if (rr_class == C_IN && rr_type == T_CNAME)
176         {
177           /* Record the RR name as an alias. */
178           if (aliases)
179             aliases[naliases] = rr_name;
180           else
181             free(rr_name);
182           naliases++;
183 
184           /* Decode the RR data and replace the hostname with it. */
185           status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
186                                                   &len);
187           if (status != ARES_SUCCESS)
188             break;
189           free(hostname);
190           hostname = rr_data;
191 
192           /* Take the min of the TTLs we see in the CNAME chain. */
193           if (cname_ttl > rr_ttl)
194             cname_ttl = rr_ttl;
195         }
196       else
197         free(rr_name);
198 
199       aptr += rr_len;
200       if (aptr > abuf + alen)
201         {
202           status = ARES_EBADRESP;
203           break;
204         }
205     }
206 
207   if (status == ARES_SUCCESS && naddrs == 0 && naliases == 0)
208     /* the check for naliases to be zero is to make sure CNAME responses
209        don't get caught here */
210     status = ARES_ENODATA;
211   if (status == ARES_SUCCESS)
212     {
213       /* We got our answer. */
214       if (naddrttls)
215         {
216           const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
217           for (i = 0; i < n; i++)
218             {
219               /* Ensure that each A TTL is no larger than the CNAME TTL. */
220               if (addrttls[i].ttl > cname_ttl)
221                 addrttls[i].ttl = cname_ttl;
222             }
223           *naddrttls = n;
224         }
225       if (aliases)
226         aliases[naliases] = NULL;
227       if (host)
228         {
229           /* Allocate memory to build the host entry. */
230           hostent = malloc(sizeof(struct hostent));
231           if (hostent)
232             {
233               hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
234               if (hostent->h_addr_list)
235                 {
236                   /* Fill in the hostent and return successfully. */
237                   hostent->h_name = hostname;
238                   hostent->h_aliases = aliases;
239                   hostent->h_addrtype = AF_INET;
240                   hostent->h_length = sizeof(struct in_addr);
241                   for (i = 0; i < naddrs; i++)
242                     hostent->h_addr_list[i] = (char *) &addrs[i];
243                   hostent->h_addr_list[naddrs] = NULL;
244                   if (!naddrs && addrs)
245                     free(addrs);
246                   *host = hostent;
247                   return ARES_SUCCESS;
248                 }
249               free(hostent);
250             }
251           status = ARES_ENOMEM;
252         }
253      }
254   if (aliases)
255     {
256       for (i = 0; i < naliases; i++)
257         free(aliases[i]);
258       free(aliases);
259     }
260   free(addrs);
261   free(hostname);
262   return status;
263 }
264