|
| 1 | + |
| 2 | +/* Copyright 2020 by <danny.sonnenschein@platynum.ch> |
| 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 | +#ifdefHAVE_NETINET_IN_H |
| 20 | +# include<netinet/in.h> |
| 21 | +#endif |
| 22 | +#ifdefHAVE_NETDB_H |
| 23 | +# include<netdb.h> |
| 24 | +#endif |
| 25 | +#ifdefHAVE_ARPA_INET_H |
| 26 | +# include<arpa/inet.h> |
| 27 | +#endif |
| 28 | +#ifdefHAVE_ARPA_NAMESER_H |
| 29 | +# include<arpa/nameser.h> |
| 30 | +#else |
| 31 | +# include"nameser.h" |
| 32 | +#endif |
| 33 | +#ifdefHAVE_ARPA_NAMESER_COMPAT_H |
| 34 | +# include<arpa/nameser_compat.h> |
| 35 | +#endif |
| 36 | + |
| 37 | +#ifdefHAVE_STRINGS_H |
| 38 | +# include<strings.h> |
| 39 | +#endif |
| 40 | + |
| 41 | +#include"ares.h" |
| 42 | +#include"ares_dns.h" |
| 43 | +#include"ares_data.h" |
| 44 | +#include"ares_private.h" |
| 45 | + |
| 46 | +#ifndefT_CAA |
| 47 | +# defineT_CAA 257 /* Certification Authority Authorization */ |
| 48 | +#endif |
| 49 | + |
| 50 | +int |
| 51 | +ares_parse_caa_reply (constunsigned char*abuf, intalen, |
| 52 | +structares_caa_reply**caa_out) |
| 53 | +{ |
| 54 | +unsigned intqdcount, ancount, i; |
| 55 | +constunsigned char*aptr; |
| 56 | +constunsigned char*strptr; |
| 57 | +intstatus, rr_type, rr_class, rr_len; |
| 58 | +longlen; |
| 59 | +char*hostname=NULL, *rr_name=NULL; |
| 60 | +structares_caa_reply*caa_head=NULL; |
| 61 | +structares_caa_reply*caa_last=NULL; |
| 62 | +structares_caa_reply*caa_curr; |
| 63 | + |
| 64 | +/* Set *caa_out to NULL for all failure cases. */ |
| 65 | +*caa_out=NULL; |
| 66 | + |
| 67 | +/* Give up if abuf doesn't have room for a header. */ |
| 68 | +if (alen<HFIXEDSZ) |
| 69 | +returnARES_EBADRESP; |
| 70 | + |
| 71 | +/* Fetch the question and answer count from the header. */ |
| 72 | +qdcount=DNS_HEADER_QDCOUNT (abuf); |
| 73 | +ancount=DNS_HEADER_ANCOUNT (abuf); |
| 74 | +if (qdcount!=1) |
| 75 | +returnARES_EBADRESP; |
| 76 | +if (ancount==0) |
| 77 | +returnARES_ENODATA; |
| 78 | + |
| 79 | +/* Expand the name from the question, and skip past the question. */ |
| 80 | +aptr=abuf+HFIXEDSZ; |
| 81 | +status=ares_expand_name (aptr, abuf, alen, &hostname, &len); |
| 82 | +if (status!=ARES_SUCCESS) |
| 83 | +returnstatus; |
| 84 | + |
| 85 | +if (aptr+len+QFIXEDSZ>abuf+alen) |
| 86 | + { |
| 87 | +ares_free (hostname); |
| 88 | +returnARES_EBADRESP; |
| 89 | + } |
| 90 | +aptr+=len+QFIXEDSZ; |
| 91 | + |
| 92 | +/* Examine each answer resource record (RR) in turn. */ |
| 93 | +for (i=0; i<ancount; i++) |
| 94 | + { |
| 95 | +/* Decode the RR up to the data field. */ |
| 96 | +status=ares_expand_name (aptr, abuf, alen, &rr_name, &len); |
| 97 | +if (status!=ARES_SUCCESS) |
| 98 | + { |
| 99 | +break; |
| 100 | + } |
| 101 | +aptr+=len; |
| 102 | +if (aptr+RRFIXEDSZ>abuf+alen) |
| 103 | + { |
| 104 | +status=ARES_EBADRESP; |
| 105 | +break; |
| 106 | + } |
| 107 | +rr_type=DNS_RR_TYPE (aptr); |
| 108 | +rr_class=DNS_RR_CLASS (aptr); |
| 109 | +rr_len=DNS_RR_LEN (aptr); |
| 110 | +aptr+=RRFIXEDSZ; |
| 111 | +if (aptr+rr_len>abuf+alen) |
| 112 | + { |
| 113 | +status=ARES_EBADRESP; |
| 114 | +break; |
| 115 | + } |
| 116 | + |
| 117 | +/* Check if we are really looking at a CAA record */ |
| 118 | +if ((rr_class==C_IN||rr_class==C_CHAOS) &&rr_type==T_CAA) |
| 119 | + { |
| 120 | +strptr=aptr; |
| 121 | + |
| 122 | +/* Allocate storage for this CAA answer appending it to the list */ |
| 123 | +caa_curr=ares_malloc_data(ARES_DATATYPE_CAA_REPLY); |
| 124 | +if (!caa_curr) |
| 125 | + { |
| 126 | +status=ARES_ENOMEM; |
| 127 | +break; |
| 128 | + } |
| 129 | +if (caa_last) |
| 130 | + { |
| 131 | +caa_last->next=caa_curr; |
| 132 | + } |
| 133 | +else |
| 134 | + { |
| 135 | +caa_head=caa_curr; |
| 136 | + } |
| 137 | +caa_last=caa_curr; |
| 138 | +if (rr_len<2) |
| 139 | + { |
| 140 | +status=ARES_EBADRESP; |
| 141 | +break; |
| 142 | + } |
| 143 | +caa_curr->critical= (int)*strptr++; |
| 144 | +caa_curr->plength= (int)*strptr++; |
| 145 | +if (caa_curr->plength <= 0|| (int)caa_curr->plength >= rr_len-2) |
| 146 | + { |
| 147 | +status=ARES_EBADRESP; |
| 148 | +break; |
| 149 | + } |
| 150 | +caa_curr->property=ares_malloc (caa_curr->plength+1/* Including null byte */); |
| 151 | +if (caa_curr->property==NULL) |
| 152 | + { |
| 153 | +status=ARES_ENOMEM; |
| 154 | +break; |
| 155 | + } |
| 156 | +memcpy ((char*) caa_curr->property, strptr, caa_curr->plength); |
| 157 | +/* Make sure we NULL-terminate */ |
| 158 | +caa_curr->property[caa_curr->plength] =0; |
| 159 | +strptr+=caa_curr->plength; |
| 160 | + |
| 161 | +caa_curr->length=rr_len-caa_curr->plength-2; |
| 162 | +if (caa_curr->length <= 0) |
| 163 | + { |
| 164 | +status=ARES_EBADRESP; |
| 165 | +break; |
| 166 | + } |
| 167 | +caa_curr->value=ares_malloc (caa_curr->length+1/* Including null byte */); |
| 168 | +if (caa_curr->value==NULL) |
| 169 | + { |
| 170 | +status=ARES_ENOMEM; |
| 171 | +break; |
| 172 | + } |
| 173 | +memcpy ((char*) caa_curr->value, strptr, caa_curr->length); |
| 174 | +/* Make sure we NULL-terminate */ |
| 175 | +caa_curr->value[caa_curr->length] =0; |
| 176 | + } |
| 177 | + |
| 178 | +/* Propagate any failures */ |
| 179 | +if (status!=ARES_SUCCESS) |
| 180 | + { |
| 181 | +break; |
| 182 | + } |
| 183 | + |
| 184 | +/* Don't lose memory in the next iteration */ |
| 185 | +ares_free (rr_name); |
| 186 | +rr_name=NULL; |
| 187 | + |
| 188 | +/* Move on to the next record */ |
| 189 | +aptr+=rr_len; |
| 190 | + } |
| 191 | + |
| 192 | +if (hostname) |
| 193 | +ares_free (hostname); |
| 194 | +if (rr_name) |
| 195 | +ares_free (rr_name); |
| 196 | + |
| 197 | +/* clean up on error */ |
| 198 | +if (status!=ARES_SUCCESS) |
| 199 | + { |
| 200 | +if (caa_head) |
| 201 | +ares_free_data (caa_head); |
| 202 | +returnstatus; |
| 203 | + } |
| 204 | + |
| 205 | +/* everything looks fine, return the data */ |
| 206 | +*caa_out=caa_head; |
| 207 | + |
| 208 | +returnARES_SUCCESS; |
| 209 | +} |
0 commit comments