• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MIT License
2  *
3  * Copyright (c) 2023 Brad House
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * SPDX-License-Identifier: MIT
25  */
26 
27 #include "ares_setup.h"
28 
29 #ifdef HAVE_NETINET_IN_H
30 #  include <netinet/in.h>
31 #endif
32 #ifdef HAVE_NETDB_H
33 #  include <netdb.h>
34 #endif
35 
36 #include "ares.h"
37 #include "ares_private.h"
38 
ares_parse_ptr_reply(const unsigned char * abuf,int alen_int,const void * addr,int addrlen,int family,struct hostent ** host)39 int ares_parse_ptr_reply(const unsigned char *abuf, int alen_int,
40                          const void *addr, int addrlen, int family,
41                          struct hostent **host)
42 {
43   ares_status_t      status;
44   size_t             alen;
45   size_t             ptrcount = 0;
46   struct hostent    *hostent  = NULL;
47   const char        *hostname = NULL;
48   const char        *ptrname  = NULL;
49   ares_dns_record_t *dnsrec   = NULL;
50   size_t             i;
51   size_t             ancount;
52 
53   *host = NULL;
54 
55   if (alen_int < 0) {
56     return ARES_EBADRESP;
57   }
58 
59   alen = (size_t)alen_int;
60 
61   status = ares_dns_parse(abuf, alen, 0, &dnsrec);
62   if (status != ARES_SUCCESS) {
63     goto done;
64   }
65 
66   /* Fetch name from query as we will use it to compare later on.  Old code
67    * did this check, so we'll retain it. */
68   status = ares_dns_record_query_get(dnsrec, 0, &ptrname, NULL, NULL);
69   if (status != ARES_SUCCESS) {
70     goto done;
71   }
72 
73   ancount = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
74   if (ancount == 0) {
75     status = ARES_ENODATA;
76     goto done;
77   }
78 
79   /* Response structure */
80   hostent = ares_malloc(sizeof(*hostent));
81   if (hostent == NULL) {
82     status = ARES_ENOMEM;
83     goto done;
84   }
85 
86   memset(hostent, 0, sizeof(*hostent));
87 
88   hostent->h_addr_list = ares_malloc(2 * sizeof(*hostent->h_addr_list));
89   if (hostent->h_addr_list == NULL) {
90     status = ARES_ENOMEM;
91     goto done;
92   }
93   memset(hostent->h_addr_list, 0, 2 * sizeof(*hostent->h_addr_list));
94   if (addr != NULL && addrlen > 0) {
95     hostent->h_addr_list[0] = ares_malloc((size_t)addrlen);
96     if (hostent->h_addr_list[0] == NULL) {
97       status = ARES_ENOMEM;
98       goto done;
99     }
100     memcpy(hostent->h_addr_list[0], addr, (size_t)addrlen);
101   }
102   hostent->h_addrtype = (HOSTENT_ADDRTYPE_TYPE)family;
103   hostent->h_length   = (HOSTENT_LENGTH_TYPE)addrlen;
104 
105   /* Preallocate the maximum number + 1 */
106   hostent->h_aliases = ares_malloc((ancount + 1) * sizeof(*hostent->h_aliases));
107   if (hostent->h_aliases == NULL) {
108     status = ARES_ENOMEM;
109     goto done;
110   }
111   memset(hostent->h_aliases, 0, (ancount + 1) * sizeof(*hostent->h_aliases));
112 
113 
114   /* Cycle through answers */
115   for (i = 0; i < ancount; i++) {
116     const ares_dns_rr_t *rr =
117       ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
118 
119     if (rr == NULL) {
120       /* Shouldn't be possible */
121       status = ARES_EBADRESP;
122       goto done;
123     }
124 
125     if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN) {
126       continue;
127     }
128 
129     /* Any time we see a CNAME, replace our ptrname with its value */
130     if (ares_dns_rr_get_type(rr) == ARES_REC_TYPE_CNAME) {
131       ptrname = ares_dns_rr_get_str(rr, ARES_RR_CNAME_CNAME);
132       if (ptrname == NULL) {
133         status = ARES_EBADRESP;
134         goto done;
135       }
136     }
137 
138     /* Handling for PTR records below this, otherwise skip */
139     if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_PTR) {
140       continue;
141     }
142 
143     /* Issue #683
144      * Old code compared the name in the rr to the ptrname, but I think this
145      * is wrong since it was proven wrong for A & AAAA records.  Leaving
146      * this code commented out for future reference
147      *
148      * rname = ares_dns_rr_get_name(rr);
149      * if (rname == NULL) {
150      *   status = ARES_EBADRESP;
151      *   goto done;
152      * }
153      * if (strcasecmp(ptrname, rname) != 0) {
154      *   continue;
155      * }
156      */
157 
158     /* Save most recent PTR record as the hostname */
159     hostname = ares_dns_rr_get_str(rr, ARES_RR_PTR_DNAME);
160     if (hostname == NULL) {
161       status = ARES_EBADRESP;
162       goto done;
163     }
164 
165     /* Append as an alias */
166     hostent->h_aliases[ptrcount] = ares_strdup(hostname);
167     if (hostent->h_aliases[ptrcount] == NULL) {
168       status = ARES_ENOMEM;
169       goto done;
170     }
171     ptrcount++;
172   }
173 
174   if (ptrcount == 0) {
175     status = ARES_ENODATA;
176     goto done;
177   } else {
178     status = ARES_SUCCESS;
179   }
180 
181   /* Fill in hostname */
182   hostent->h_name = ares_strdup(hostname);
183   if (hostent->h_name == NULL) {
184     status = ARES_ENOMEM;
185     goto done;
186   }
187 
188 done:
189   if (status != ARES_SUCCESS) {
190     ares_free_hostent(hostent);
191     /* Compatibility */
192     if (status == ARES_EBADNAME) {
193       status = ARES_EBADRESP;
194     }
195   } else {
196     *host = hostent;
197   }
198   ares_dns_record_destroy(dnsrec);
199   return (int)status;
200 }
201