83c343110ad54f76c34ee97330dc8222ca6ef338
[adns] / src / reply.c
1 /*
2 * reply.c
3 * - main handling and parsing routine for received datagrams
4 */
5 /*
6 * This file is part of adns, which is
7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
8 * Copyright (C) 1999-2000,2003,2006 Tony Finch
9 * Copyright (C) 1991 Massachusetts Institute of Technology
10 * (See the file INSTALL for full details.)
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation.
24 */
25
26 #include <stdlib.h>
27
28 #include "internal.h"
29
30 void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
31 int serv, int viatcp, struct timeval now) {
32 int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
33 int id, f1, f2, qdcount, ancount, nscount, arcount;
34 int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
35 int rrtype, rrclass, rdlength, rdstart;
36 int anstart, nsstart;
37 int ownermatched, l, nrrs;
38 unsigned long ttl, soattl;
39 const typeinfo *typei;
40 adns_query qu, nqu;
41 dns_rcode rcode;
42 adns_status st;
43 vbuf tempvb;
44 byte *newquery, *rrsdata;
45 parseinfo pai;
46
47 if (dglen<DNS_HDRSIZE) {
48 adns__diag(ads,serv,0,"received datagram"
49 " too short for message header (%d)",dglen);
50 return;
51 }
52 cbyte= 0;
53 GET_W(cbyte,id);
54 GET_B(cbyte,f1);
55 GET_B(cbyte,f2);
56 GET_W(cbyte,qdcount);
57 GET_W(cbyte,ancount);
58 GET_W(cbyte,nscount);
59 GET_W(cbyte,arcount);
60 assert(cbyte == DNS_HDRSIZE);
61
62 flg_qr= f1&0x80;
63 opcode= (f1&0x78)>>3;
64 flg_tc= f1&0x02;
65 flg_rd= f1&0x01;
66 flg_ra= f2&0x80;
67 rcode= (f2&0x0f);
68
69 cname_here= 0;
70
71 if (!flg_qr) {
72 adns__diag(ads,serv,0,"server sent us a query, not a response");
73 return;
74 }
75 if (opcode) {
76 adns__diag(ads,serv,0,"server sent us unknown opcode"
77 " %d (wanted 0=QUERY)",opcode);
78 return;
79 }
80
81 qu= 0;
82 /* See if we can find the relevant query, or leave qu=0 otherwise ... */
83
84 if (qdcount == 1) {
85 for (qu= viatcp ? ads->tcpw.head : ads->udpw.head; qu; qu= nqu) {
86 nqu= qu->next;
87 if (qu->id != id) continue;
88 if (dglen < qu->query_dglen) continue;
89 if (memcmp(qu->query_dgram+DNS_HDRSIZE,
90 dgram+DNS_HDRSIZE,
91 qu->query_dglen-DNS_HDRSIZE))
92 continue;
93 if (viatcp) {
94 assert(qu->state == query_tcpw);
95 } else {
96 assert(qu->state == query_tosend);
97 if (!(qu->udpsent & (1<<serv))) continue;
98 }
99 break;
100 }
101 if (qu) {
102 /* We're definitely going to do something with this query now */
103 if (viatcp) LIST_UNLINK(ads->tcpw,qu);
104 else LIST_UNLINK(ads->udpw,qu);
105 }
106 }
107
108 /* If we're going to ignore the packet, we return as soon as we have
109 * failed the query (if any) and printed the warning message (if
110 * any).
111 */
112 switch (rcode) {
113 case rcode_noerror:
114 case rcode_nxdomain:
115 break;
116 case rcode_formaterror:
117 adns__warn(ads,serv,qu,"server cannot understand our query"
118 " (Format Error)");
119 if (qu) adns__query_fail(qu,adns_s_rcodeformaterror);
120 return;
121 case rcode_servfail:
122 if (qu) adns__query_fail(qu,adns_s_rcodeservfail);
123 else adns__debug(ads,serv,qu,"server failure on unidentifiable query");
124 return;
125 case rcode_notimp:
126 adns__warn(ads,serv,qu,"server claims not to implement our query");
127 if (qu) adns__query_fail(qu,adns_s_rcodenotimplemented);
128 return;
129 case rcode_refused:
130 adns__debug(ads,serv,qu,"server refused our query");
131 if (qu) adns__query_fail(qu,adns_s_rcoderefused);
132 return;
133 default:
134 adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
135 if (qu) adns__query_fail(qu,adns_s_rcodeunknown);
136 return;
137 }
138
139 if (!qu) {
140 if (!qdcount) {
141 adns__diag(ads,serv,0,"server sent reply without quoting our question");
142 } else if (qdcount>1) {
143 adns__diag(ads,serv,0,"server claimed to answer %d"
144 " questions with one message", qdcount);
145 } else if (ads->iflags & adns_if_debug) {
146 adns__vbuf_init(&tempvb);
147 adns__debug(ads,serv,0,"reply not found, id %02x, query owner %s",
148 id, adns__diag_domain(ads,serv,0,&tempvb,
149 dgram,dglen,DNS_HDRSIZE));
150 adns__vbuf_free(&tempvb);
151 }
152 return;
153 }
154
155 /* We're definitely going to do something with this packet and this
156 * query now. */
157
158 anstart= qu->query_dglen;
159
160 /* Now, take a look at the answer section, and see if it is complete.
161 * If it has any CNAMEs we stuff them in the answer.
162 */
163 wantedrrs= 0;
164 cbyte= anstart;
165 for (rri= 0; rri<ancount; rri++) {
166 rrstart= cbyte;
167 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
168 &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
169 &ownermatched);
170 if (st) { adns__query_fail(qu,st); return; }
171 if (rrtype == -1) goto x_truncated;
172
173 if (rrclass != DNS_CLASS_IN) {
174 adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d"
175 " (expected IN=%d)", rrclass,DNS_CLASS_IN);
176 continue;
177 }
178 if (!ownermatched) {
179 if (ads->iflags & adns_if_debug) {
180 adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
181 adns__diag_domain(ads,serv,qu, &qu->vb,
182 dgram,dglen,rrstart));
183 }
184 continue;
185 }
186 if (rrtype == adns_r_cname &&
187 (qu->answer->type & adns_rrt_typemask) != adns_r_cname) {
188 if (qu->flags & adns_qf_cname_forbid) {
189 adns__query_fail(qu,adns_s_prohibitedcname);
190 return;
191 } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
192 adns__debug(ads,serv,qu,"allegedly canonical name %s"
193 " is actually alias for %s", qu->answer->cname,
194 adns__diag_domain(ads,serv,qu, &qu->vb,
195 dgram,dglen,rdstart));
196 adns__query_fail(qu,adns_s_prohibitedcname);
197 return;
198 } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
199 adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
200 adns__diag_domain(ads,serv,qu, &qu->vb,
201 dgram,dglen,rdstart));
202 } else {
203 qu->cname_begin= rdstart;
204 qu->cname_dglen= dglen;
205 st= adns__parse_domain(ads,serv,qu, &qu->vb,
206 qu->flags & adns_qf_quotefail_cname
207 ? 0 : pdf_quoteok,
208 dgram,dglen, &rdstart,rdstart+rdlength);
209 if (!qu->vb.used) goto x_truncated;
210 if (st) { adns__query_fail(qu,st); return; }
211 l= strlen(qu->vb.buf)+1;
212 qu->answer->cname= adns__alloc_preserved(qu,l);
213 if (!qu->answer->cname) {
214 adns__query_fail(qu,adns_s_nomemory);
215 return;
216 }
217
218 qu->cname_dgram= adns__alloc_mine(qu,dglen);
219 memcpy(qu->cname_dgram,dgram,dglen);
220
221 memcpy(qu->answer->cname,qu->vb.buf,l);
222 cname_here= 1;
223 adns__update_expires(qu,ttl,now);
224 /* If we find the answer section truncated after this point we restart
225 * the query at the CNAME; if beforehand then we obviously have to use
226 * TCP. If there is no truncation we can use the whole answer if
227 * it contains the relevant info.
228 */
229 }
230 } else if (rrtype == (qu->answer->type & adns_rrt_typemask)) {
231 wantedrrs++;
232 } else {
233 adns__debug(ads,serv,qu,"ignoring answer RR"
234 " with irrelevant type %d",rrtype);
235 }
236 }
237
238 /* We defer handling truncated responses here, in case there was a CNAME
239 * which we could use.
240 */
241 if (flg_tc) goto x_truncated;
242
243 nsstart= cbyte;
244
245 if (!wantedrrs) {
246 /* Oops, NODATA or NXDOMAIN or perhaps a referral
247 * (which would be a problem) */
248
249 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records
250 * in authority section */
251 foundsoa= 0; soattl= 0; foundns= 0;
252 for (rri= 0; rri<nscount; rri++) {
253 rrstart= cbyte;
254 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
255 &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
256 if (st) { adns__query_fail(qu,st); return; }
257 if (rrtype==-1) goto x_truncated;
258 if (rrclass != DNS_CLASS_IN) {
259 adns__diag(ads,serv,qu,
260 "ignoring authority RR with wrong class %d"
261 " (expected IN=%d)", rrclass,DNS_CLASS_IN);
262 continue;
263 }
264 if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
265 else if (rrtype == adns_r_ns_raw) { foundns= 1; }
266 }
267
268 if (rcode == rcode_nxdomain) {
269 /* We still wanted to look for the SOA so we could find the TTL. */
270 adns__update_expires(qu,soattl,now);
271
272 if (qu->flags & adns_qf_search && !qu->cname_dgram) {
273 adns__search_next(ads,qu,now);
274 } else {
275 adns__query_fail(qu,adns_s_nxdomain);
276 }
277 return;
278 }
279
280 if (foundsoa || !foundns) {
281 /* Aha ! A NODATA response, good. */
282 adns__update_expires(qu,soattl,now);
283 adns__query_fail(qu,adns_s_nodata);
284 return;
285 }
286
287 /* Now what ? No relevant answers, no SOA, and at least some NS's.
288 * Looks like a referral. Just one last chance ... if we came across
289 * a CNAME in this datagram then we should probably do our own CNAME
290 * lookup now in the hope that we won't get a referral again.
291 */
292 if (cname_here) goto x_restartquery;
293
294 /* Bloody hell, I thought we asked for recursion ? */
295 if (!flg_ra) {
296 adns__diag(ads,serv,qu,"server is not willing"
297 " to do recursive lookups for us");
298 adns__query_fail(qu,adns_s_norecurse);
299 } else {
300 if (!flg_rd)
301 adns__diag(ads,serv,qu,"server thinks"
302 " we didn't ask for recursive lookup");
303 else
304 adns__debug(ads,serv,qu,"server claims to do recursion,"
305 " but gave us a referral");
306 adns__query_fail(qu,adns_s_invalidresponse);
307 }
308 return;
309 }
310
311 /* Now, we have some RRs which we wanted. */
312
313 qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->answer->rrsz*wantedrrs);
314 if (!qu->answer->rrs.untyped) {
315 adns__query_fail(qu,adns_s_nomemory);
316 return;
317 }
318
319 typei= qu->typei;
320 cbyte= anstart;
321 rrsdata= qu->answer->rrs.bytes;
322
323 pai.ads= qu->ads;
324 pai.qu= qu;
325 pai.serv= serv;
326 pai.dgram= dgram;
327 pai.dglen= dglen;
328 pai.nsstart= nsstart;
329 pai.nscount= nscount;
330 pai.arcount= arcount;
331 pai.now= now;
332
333 for (rri=0, nrrs=0; rri<ancount; rri++) {
334 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
335 &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
336 &ownermatched);
337 assert(!st); assert(rrtype != -1);
338 if (rrclass != DNS_CLASS_IN ||
339 rrtype != (qu->answer->type & adns_rrt_typemask) ||
340 !ownermatched)
341 continue;
342 adns__update_expires(qu,ttl,now);
343 st= typei->parse(&pai, rdstart,rdstart+rdlength,
344 rrsdata+nrrs*qu->answer->rrsz);
345 if (st) { adns__query_fail(qu,st); return; }
346 if (rdstart==-1) goto x_truncated;
347 nrrs++;
348 }
349 assert(nrrs==wantedrrs);
350 qu->answer->nrrs= nrrs;
351
352 /* This may have generated some child queries ... */
353 if (qu->children.head) {
354 qu->state= query_childw;
355 LIST_LINK_TAIL(ads->childw,qu);
356 return;
357 }
358 adns__query_done(qu);
359 return;
360
361 x_truncated:
362
363 if (!flg_tc) {
364 adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
365 adns__query_fail(qu,adns_s_invalidresponse);
366 return;
367 }
368 qu->flags |= adns_qf_usevc;
369
370 x_restartquery:
371 if (qu->cname_dgram) {
372 st= adns__mkquery_frdgram(qu->ads,&qu->vb,&qu->id,
373 qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
374 qu->answer->type, qu->flags);
375 if (st) { adns__query_fail(qu,st); return; }
376
377 newquery= realloc(qu->query_dgram,qu->vb.used);
378 if (!newquery) { adns__query_fail(qu,adns_s_nomemory); return; }
379
380 qu->query_dgram= newquery;
381 qu->query_dglen= qu->vb.used;
382 memcpy(newquery,qu->vb.buf,qu->vb.used);
383 }
384
385 if (qu->state == query_tcpw) qu->state= query_tosend;
386 qu->retries= 0;
387 adns__reset_preserved(qu);
388 adns__query_send(qu,now);
389 }