Licensing: Update copyright dates for Ian Jackson
[adns] / src / reply.c
CommitLineData
e576be50 1/*
2 * reply.c
3 * - main handling and parsing routine for received datagrams
4 */
5/*
ae8cc977 6 * This file is part of adns, which is
26e1c3d6 7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
ae8cc977 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.)
e576be50 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
7f8bbe29 14 * the Free Software Foundation; either version 3, or (at your option)
e576be50 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
8c09a4c6 23 * along with this program; if not, write to the Free Software Foundation.
e576be50 24 */
4353a5c4 25
e062dcae 26#include <stdlib.h>
4353a5c4 27
e062dcae 28#include "internal.h"
b9de380c 29
98a3f706 30void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
ebf4877a 31 int serv, int viatcp, struct timeval now) {
86e7b8d9 32 int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
3955725c 33 int id, f1, f2, qdcount, ancount, nscount, arcount;
34 int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
e062dcae 35 int rrtype, rrclass, rdlength, rdstart;
faa52594 36 int anstart, nsstart;
88372443 37 int ownermatched, l, nrrs;
73dba56e 38 unsigned long ttl, soattl;
e062dcae 39 const typeinfo *typei;
98a3f706 40 adns_query qu, nqu;
41 dns_rcode rcode;
b9de380c 42 adns_status st;
86e7b8d9 43 vbuf tempvb;
e062dcae 44 byte *newquery, *rrsdata;
1dfe95d8 45 parseinfo pai;
ec477b9e 46
98a3f706 47 if (dglen<DNS_HDRSIZE) {
609133ee 48 adns__diag(ads,serv,0,"received datagram"
49 " too short for message header (%d)",dglen);
ec477b9e 50 return;
51 }
9557e604 52 cbyte= 0;
b9de380c 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);
98a3f706 60 assert(cbyte == DNS_HDRSIZE);
ec477b9e 61
31144a72 62 flg_qr= f1&0x80;
63 opcode= (f1&0x78)>>3;
e062dcae 64 flg_tc= f1&0x02;
31144a72 65 flg_rd= f1&0x01;
0ba0614a 66 flg_ra= f2&0x80;
86e7b8d9 67 rcode= (f2&0x0f);
0ba0614a 68
86e7b8d9 69 cname_here= 0;
70
9557e604 71 if (!flg_qr) {
3955725c 72 adns__diag(ads,serv,0,"server sent us a query, not a response");
ec477b9e 73 return;
74 }
31144a72 75 if (opcode) {
609133ee 76 adns__diag(ads,serv,0,"server sent us unknown opcode"
77 " %d (wanted 0=QUERY)",opcode);
ec477b9e 78 return;
79 }
16d7d2b5 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;
ebf4877a 100 }
16d7d2b5 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);
86e7b8d9 105 }
ec477b9e 106 }
98a3f706 107
16d7d2b5 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 */
b9de380c 112 switch (rcode) {
113 case rcode_noerror:
114 case rcode_nxdomain:
ec477b9e 115 break;
b9de380c 116 case rcode_formaterror:
609133ee 117 adns__warn(ads,serv,qu,"server cannot understand our query"
118 " (Format Error)");
16d7d2b5 119 if (qu) adns__query_fail(qu,adns_s_rcodeformaterror);
ec477b9e 120 return;
98a3f706 121 case rcode_servfail:
16d7d2b5 122 if (qu) adns__query_fail(qu,adns_s_rcodeservfail);
c3ccdc54 123 else adns__debug(ads,serv,qu,"server failure on unidentifiable query");
ec477b9e 124 return;
b9de380c 125 case rcode_notimp:
31144a72 126 adns__warn(ads,serv,qu,"server claims not to implement our query");
16d7d2b5 127 if (qu) adns__query_fail(qu,adns_s_rcodenotimplemented);
b9de380c 128 return;
129 case rcode_refused:
d00d4b99 130 adns__debug(ads,serv,qu,"server refused our query");
16d7d2b5 131 if (qu) adns__query_fail(qu,adns_s_rcoderefused);
b9de380c 132 return;
133 default:
31144a72 134 adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
16d7d2b5 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) {
609133ee 143 adns__diag(ads,serv,0,"server claimed to answer %d"
144 " questions with one message", qdcount);
16d7d2b5 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",
609133ee 148 id, adns__diag_domain(ads,serv,0,&tempvb,
149 dgram,dglen,DNS_HDRSIZE));
16d7d2b5 150 adns__vbuf_free(&tempvb);
151 }
b9de380c 152 return;
153 }
154
609133ee 155 /* We're definitely going to do something with this packet and this
156 * query now. */
16d7d2b5 157
158 anstart= qu->query_dglen;
16d7d2b5 159
b9de380c 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;
86e7b8d9 164 cbyte= anstart;
b9de380c 165 for (rri= 0; rri<ancount; rri++) {
166 rrstart= cbyte;
3955725c 167 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
73dba56e 168 &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
3955725c 169 &ownermatched);
86e7b8d9 170 if (st) { adns__query_fail(qu,st); return; }
0ba0614a 171 if (rrtype == -1) goto x_truncated;
172
b9de380c 173 if (rrclass != DNS_CLASS_IN) {
609133ee 174 adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d"
175 " (expected IN=%d)", rrclass,DNS_CLASS_IN);
b9de380c 176 continue;
177 }
0ba0614a 178 if (!ownermatched) {
98a3f706 179 if (ads->iflags & adns_if_debug) {
31144a72 180 adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
609133ee 181 adns__diag_domain(ads,serv,qu, &qu->vb,
182 dgram,dglen,rrstart));
0ba0614a 183 }
b9de380c 184 continue;
185 }
2a6d6736 186 if (rrtype == adns_r_cname &&
2c6eb096 187 (qu->answer->type & adns_rrt_typemask) != adns_r_cname) {
4b707d8b 188 if (qu->flags & adns_qf_cname_forbid) {
189 adns__query_fail(qu,adns_s_prohibitedcname);
190 return;
636b69b1 191 } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
609133ee 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));
05db5bce 196 adns__query_fail(qu,adns_s_prohibitedcname);
197 return;
636b69b1 198 } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
199 adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
609133ee 200 adns__diag_domain(ads,serv,qu, &qu->vb,
201 dgram,dglen,rdstart));
636b69b1 202 } else {
31144a72 203 qu->cname_begin= rdstart;
31144a72 204 qu->cname_dglen= dglen;
828d89bd 205 st= adns__parse_domain(ads,serv,qu, &qu->vb,
ac015392 206 qu->flags & adns_qf_quotefail_cname
207 ? 0 : pdf_quoteok,
31144a72 208 dgram,dglen, &rdstart,rdstart+rdlength);
3955725c 209 if (!qu->vb.used) goto x_truncated;
210 if (st) { adns__query_fail(qu,st); return; }
211 l= strlen(qu->vb.buf)+1;
8b3d55e3 212 qu->answer->cname= adns__alloc_preserved(qu,l);
609133ee 213 if (!qu->answer->cname) {
214 adns__query_fail(qu,adns_s_nomemory);
215 return;
216 }
e062dcae 217
218 qu->cname_dgram= adns__alloc_mine(qu,dglen);
219 memcpy(qu->cname_dgram,dgram,dglen);
220
3955725c 221 memcpy(qu->answer->cname,qu->vb.buf,l);
86e7b8d9 222 cname_here= 1;
73dba56e 223 adns__update_expires(qu,ttl,now);
31144a72 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 */
31144a72 229 }
2c6eb096 230 } else if (rrtype == (qu->answer->type & adns_rrt_typemask)) {
b9de380c 231 wantedrrs++;
232 } else {
609133ee 233 adns__debug(ads,serv,qu,"ignoring answer RR"
234 " with irrelevant type %d",rrtype);
b9de380c 235 }
236 }
237
e062dcae 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
b9de380c 243 nsstart= cbyte;
244
245 if (!wantedrrs) {
609133ee 246 /* Oops, NODATA or NXDOMAIN or perhaps a referral
247 * (which would be a problem) */
b9de380c 248
609133ee 249 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records
250 * in authority section */
73dba56e 251 foundsoa= 0; soattl= 0; foundns= 0;
b9de380c 252 for (rri= 0; rri<nscount; rri++) {
0ba0614a 253 rrstart= cbyte;
3955725c 254 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
73dba56e 255 &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
3955725c 256 if (st) { adns__query_fail(qu,st); return; }
0ba0614a 257 if (rrtype==-1) goto x_truncated;
258 if (rrclass != DNS_CLASS_IN) {
31144a72 259 adns__diag(ads,serv,qu,
609133ee 260 "ignoring authority RR with wrong class %d"
261 " (expected IN=%d)", rrclass,DNS_CLASS_IN);
0ba0614a 262 continue;
263 }
73dba56e 264 if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
0ba0614a 265 else if (rrtype == adns_r_ns_raw) { foundns= 1; }
b9de380c 266 }
73dba56e 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);
660d7d3b 271
ac015392 272 if (qu->flags & adns_qf_search && !qu->cname_dgram) {
660d7d3b 273 adns__search_next(ads,qu,now);
274 } else {
275 adns__query_fail(qu,adns_s_nxdomain);
276 }
73dba56e 277 return;
278 }
0ba0614a 279
280 if (foundsoa || !foundns) {
281 /* Aha ! A NODATA response, good. */
73dba56e 282 adns__update_expires(qu,soattl,now);
3955725c 283 adns__query_fail(qu,adns_s_nodata);
0ba0614a 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
31144a72 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.
0ba0614a 291 */
e062dcae 292 if (cname_here) goto x_restartquery;
0ba0614a 293
294 /* Bloody hell, I thought we asked for recursion ? */
295 if (!flg_ra) {
609133ee 296 adns__diag(ads,serv,qu,"server is not willing"
297 " to do recursive lookups for us");
3955725c 298 adns__query_fail(qu,adns_s_norecurse);
31144a72 299 } else {
d8ae4748 300 if (!flg_rd)
609133ee 301 adns__diag(ads,serv,qu,"server thinks"
302 " we didn't ask for recursive lookup");
d8ae4748 303 else
609133ee 304 adns__debug(ads,serv,qu,"server claims to do recursion,"
305 " but gave us a referral");
ea1e31e3 306 adns__query_fail(qu,adns_s_invalidresponse);
0ba0614a 307 }
b9de380c 308 return;
309 }
310
0ba0614a 311 /* Now, we have some RRs which we wanted. */
b9de380c 312
042b0909 313 qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->answer->rrsz*wantedrrs);
609133ee 314 if (!qu->answer->rrs.untyped) {
315 adns__query_fail(qu,adns_s_nomemory);
316 return;
317 }
98a3f706 318
e062dcae 319 typei= qu->typei;
98a3f706 320 cbyte= anstart;
e062dcae 321 rrsdata= qu->answer->rrs.bytes;
1dfe95d8 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;
7da21070 331 pai.now= now;
1dfe95d8 332
333 for (rri=0, nrrs=0; rri<ancount; rri++) {
3955725c 334 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
73dba56e 335 &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
3955725c 336 &ownermatched);
98a3f706 337 assert(!st); assert(rrtype != -1);
338 if (rrclass != DNS_CLASS_IN ||
2c6eb096 339 rrtype != (qu->answer->type & adns_rrt_typemask) ||
31144a72 340 !ownermatched)
98a3f706 341 continue;
73dba56e 342 adns__update_expires(qu,ttl,now);
042b0909
MW
343 st= typei->parse(&pai, rdstart,rdstart+rdlength,
344 rrsdata+nrrs*qu->answer->rrsz);
3955725c 345 if (st) { adns__query_fail(qu,st); return; }
31144a72 346 if (rdstart==-1) goto x_truncated;
e062dcae 347 nrrs++;
98a3f706 348 }
e062dcae 349 assert(nrrs==wantedrrs);
350 qu->answer->nrrs= nrrs;
b9de380c 351
98a3f706 352 /* This may have generated some child queries ... */
353 if (qu->children.head) {
f7f83b4a 354 qu->state= query_childw;
98a3f706 355 LIST_LINK_TAIL(ads->childw,qu);
356 return;
b9de380c 357 }
3955725c 358 adns__query_done(qu);
98a3f706 359 return;
0ba0614a 360
e576be50 361 x_truncated:
e062dcae 362
98a3f706 363 if (!flg_tc) {
31144a72 364 adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
ea1e31e3 365 adns__query_fail(qu,adns_s_invalidresponse);
98a3f706 366 return;
367 }
98a3f706 368 qu->flags |= adns_qf_usevc;
e062dcae 369
370 x_restartquery:
e062dcae 371 if (qu->cname_dgram) {
372 st= adns__mkquery_frdgram(qu->ads,&qu->vb,&qu->id,
609133ee 373 qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
2c6eb096 374 qu->answer->type, qu->flags);
e062dcae 375 if (st) { adns__query_fail(qu,st); return; }
376
377 newquery= realloc(qu->query_dgram,qu->vb.used);
ea1e31e3 378 if (!newquery) { adns__query_fail(qu,adns_s_nomemory); return; }
e062dcae 379
380 qu->query_dgram= newquery;
381 qu->query_dglen= qu->vb.used;
382 memcpy(newquery,qu->vb.buf,qu->vb.used);
383 }
384
f7f83b4a 385 if (qu->state == query_tcpw) qu->state= query_tosend;
386 qu->retries= 0;
8b3d55e3 387 adns__reset_preserved(qu);
d8c062fa 388 adns__query_send(qu,now);
98a3f706 389}