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