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