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