Bugfixes.
[adns] / src / reply.c
CommitLineData
98db6da3 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 (C) 1997, 1998 Ian Jackson
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
d05cc330 22
23#include "internal.h"
24
11c8bf9b 25static void cname_recurse(adns_query qu, adns_queryflags xflags) {
5c596e4d 26 abort(); /* FIXME */
54ed1d64 27}
28
5c596e4d 29void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
30 int serv, struct timeval now) {
31 int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns;
11c8bf9b 32 int id, f1, f2, qdcount, ancount, nscount, arcount;
33 int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
34 int rrtype, rrclass, rdlength, rdstart, ownermatched, l;
5c596e4d 35 int anstart, nsstart, arstart;
5c596e4d 36 adns_query qu, nqu;
37 dns_rcode rcode;
54ed1d64 38 adns_status st;
168e26fe 39
5c596e4d 40 if (dglen<DNS_HDRSIZE) {
11c8bf9b 41 adns__diag(ads,serv,0,"received datagram too short for message header (%d)",dglen);
168e26fe 42 return;
43 }
9a2b67d4 44 cbyte= 0;
54ed1d64 45 GET_W(cbyte,id);
46 GET_B(cbyte,f1);
47 GET_B(cbyte,f2);
48 GET_W(cbyte,qdcount);
49 GET_W(cbyte,ancount);
50 GET_W(cbyte,nscount);
51 GET_W(cbyte,arcount);
5c596e4d 52 assert(cbyte == DNS_HDRSIZE);
168e26fe 53
7f702335 54 flg_qr= f1&0x80;
55 opcode= (f1&0x78)>>3;
5c596e4d 56 flg_tc= f1&0x20;
7f702335 57 flg_rd= f1&0x01;
8312a1c2 58 flg_ra= f2&0x80;
7f702335 59 rcode= (f1&0x0f);
8312a1c2 60
9a2b67d4 61 if (!flg_qr) {
11c8bf9b 62 adns__diag(ads,serv,0,"server sent us a query, not a response");
168e26fe 63 return;
64 }
7f702335 65 if (opcode) {
11c8bf9b 66 adns__diag(ads,serv,0,"server sent us unknown opcode %d (wanted 0=QUERY)",opcode);
168e26fe 67 return;
68 }
69 if (!qdcount) {
11c8bf9b 70 adns__diag(ads,serv,0,"server sent reply without quoting our question");
168e26fe 71 return;
72 } else if (qdcount>1) {
11c8bf9b 73 adns__diag(ads,serv,0,"server claimed to answer %d questions with one message",
168e26fe 74 qdcount);
75 return;
76 }
5c596e4d 77 for (qu= ads->timew.head; qu; qu= nqu) {
168e26fe 78 nqu= qu->next;
79 if (qu->id != id) continue;
11c8bf9b 80 if (dglen < qu->query_dglen) continue;
81 if (memcmp(qu->query_dgram+DNS_HDRSIZE,
82 dgram+DNS_HDRSIZE,
83 qu->query_dglen-DNS_HDRSIZE))
54ed1d64 84 continue;
168e26fe 85 break;
86 }
11c8bf9b 87 anstart= qu->query_dglen;
168e26fe 88 if (!qu) {
11c8bf9b 89 adns__debug(ads,serv,0,"reply not found (id=%02x)",id);
168e26fe 90 return;
91 }
5c596e4d 92
93 LIST_UNLINK(ads->timew,qu);
94 /* We're definitely going to do something with this query now */
95
54ed1d64 96 switch (rcode) {
97 case rcode_noerror:
98 case rcode_nxdomain:
168e26fe 99 break;
54ed1d64 100 case rcode_formaterror:
7f702335 101 adns__warn(ads,serv,qu,"server cannot understand our query (Format Error)");
11c8bf9b 102 adns__query_fail(qu,adns_s_serverfaulty);
168e26fe 103 return;
5c596e4d 104 case rcode_servfail:
11c8bf9b 105 adns__query_fail(qu,adns_s_servfail);
168e26fe 106 return;
54ed1d64 107 case rcode_notimp:
7f702335 108 adns__warn(ads,serv,qu,"server claims not to implement our query");
11c8bf9b 109 adns__query_fail(qu,adns_s_notimplemented);
54ed1d64 110 return;
111 case rcode_refused:
7f702335 112 adns__warn(ads,serv,qu,"server refused our query");
11c8bf9b 113 adns__query_fail(qu,adns_s_refused);
54ed1d64 114 return;
115 default:
7f702335 116 adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
11c8bf9b 117 adns__query_fail(qu,adns_s_reasonunknown);
54ed1d64 118 return;
119 }
120
121 /* Now, take a look at the answer section, and see if it is complete.
122 * If it has any CNAMEs we stuff them in the answer.
123 */
124 wantedrrs= 0;
125 for (rri= 0; rri<ancount; rri++) {
126 rrstart= cbyte;
11c8bf9b 127 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
128 &rrtype,&rrclass,&rdlength,&rdstart,
129 &ownermatched);
130 if (st) adns__query_fail(qu,st);
8312a1c2 131 if (rrtype == -1) goto x_truncated;
132
54ed1d64 133 if (rrclass != DNS_CLASS_IN) {
7f702335 134 adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d (expected IN=%d)",
54ed1d64 135 rrclass,DNS_CLASS_IN);
136 continue;
137 }
8312a1c2 138 if (!ownermatched) {
5c596e4d 139 if (ads->iflags & adns_if_debug) {
7f702335 140 adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
11c8bf9b 141 adns__diag_domain(ads,serv,qu, &qu->vb,qu->flags,
142 dgram,dglen,rrstart));
8312a1c2 143 }
54ed1d64 144 continue;
145 }
7f702335 146 if (rrtype == adns_r_cname &&
147 (qu->typei->type & adns__rrt_typemask) != adns_r_cname) {
11c8bf9b 148 if (!qu->cname_dgram) { /* Ignore second and subsequent CNAMEs */
149 qu->cname_dgram= adns__alloc_mine(qu,dglen);
150 if (!qu->cname_dgram) return;
7f702335 151 qu->cname_begin= rdstart;
7f702335 152 qu->cname_dglen= dglen;
11c8bf9b 153 st= adns__parse_domain(ads,serv,qu, &qu->vb,qu->flags,
7f702335 154 dgram,dglen, &rdstart,rdstart+rdlength);
11c8bf9b 155 if (!qu->vb.used) goto x_truncated;
156 if (st) { adns__query_fail(qu,st); return; }
157 l= strlen(qu->vb.buf)+1;
158 qu->answer->cname= adns__alloc_interim(qu,l);
965c9782 159 if (!qu->answer->cname) return;
11c8bf9b 160 memcpy(qu->answer->cname,qu->vb.buf,l);
7f702335 161 /* If we find the answer section truncated after this point we restart
162 * the query at the CNAME; if beforehand then we obviously have to use
163 * TCP. If there is no truncation we can use the whole answer if
164 * it contains the relevant info.
165 */
166 } else {
167 adns__debug(ads,serv,qu,"ignoring duplicate CNAME (%s, as well as %s)",
11c8bf9b 168 adns__diag_domain(ads,serv,qu, &qu->vb,qu->flags,
169 dgram,dglen,rdstart),
170 qu->answer->cname);
7f702335 171 }
5c596e4d 172 } else if (rrtype == (qu->typei->type & adns__rrt_typemask)) {
54ed1d64 173 wantedrrs++;
174 } else {
7f702335 175 adns__debug(ads,serv,qu,"ignoring answer RR with irrelevant type %d",rrtype);
54ed1d64 176 }
177 }
178
179 /* If we got here then the answer section is intact. */
180 nsstart= cbyte;
181
182 if (!wantedrrs) {
183 /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
184
185 if (rcode == rcode_nxdomain) {
11c8bf9b 186 adns__query_fail(qu,adns_s_nxdomain);
54ed1d64 187 return;
188 }
189
190 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
8312a1c2 191 foundsoa= 0; foundns= 0;
54ed1d64 192 for (rri= 0; rri<nscount; rri++) {
8312a1c2 193 rrstart= cbyte;
11c8bf9b 194 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
195 &rrtype,&rrclass,&rdlength,&rdstart, 0);
196 if (st) { adns__query_fail(qu,st); return; }
8312a1c2 197 if (rrtype==-1) goto x_truncated;
198 if (rrclass != DNS_CLASS_IN) {
7f702335 199 adns__diag(ads,serv,qu,
200 "ignoring authority RR with wrong class %d (expected IN=%d)",
8312a1c2 201 rrclass,DNS_CLASS_IN);
202 continue;
203 }
204 if (rrtype == adns_r_soa_raw) { foundsoa= 1; break; }
205 else if (rrtype == adns_r_ns_raw) { foundns= 1; }
54ed1d64 206 }
8312a1c2 207
208 if (foundsoa || !foundns) {
209 /* Aha ! A NODATA response, good. */
11c8bf9b 210 adns__query_fail(qu,adns_s_nodata);
8312a1c2 211 return;
212 }
213
214 /* Now what ? No relevant answers, no SOA, and at least some NS's.
215 * Looks like a referral. Just one last chance ... if we came across
7f702335 216 * a CNAME in this datagram then we should probably do our own CNAME
217 * lookup now in the hope that we won't get a referral again.
8312a1c2 218 */
11c8bf9b 219 if (qu->cname_dgram == dgram) { cname_recurse(qu,0); return; }
8312a1c2 220
221 /* Bloody hell, I thought we asked for recursion ? */
7f702335 222 if (flg_rd) {
223 adns__diag(ads,serv,qu,"server thinks we didn't ask for recursive lookup");
224 }
8312a1c2 225 if (!flg_ra) {
7f702335 226 adns__diag(ads,serv,qu,"server is not willing to do recursive lookups for us");
11c8bf9b 227 adns__query_fail(qu,adns_s_norecurse);
7f702335 228 } else {
229 adns__diag(ads,serv,qu,"server claims to do recursion, but gave us a referral");
11c8bf9b 230 adns__query_fail(qu,adns_s_serverfaulty);
8312a1c2 231 }
54ed1d64 232 return;
233 }
234
8312a1c2 235 /* Now, we have some RRs which we wanted. */
54ed1d64 236
11c8bf9b 237 qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->typei->rrsz*wantedrrs);
238 if (!qu->answer->rrs.untyped) return;
5c596e4d 239
240 cbyte= anstart;
5c596e4d 241 arstart= -1;
242 for (rri=0; rri<ancount; rri++) {
11c8bf9b 243 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
244 &rrtype,&rrclass,&rdlength,&rdstart,
245 &ownermatched);
5c596e4d 246 assert(!st); assert(rrtype != -1);
247 if (rrclass != DNS_CLASS_IN ||
7f702335 248 rrtype != (qu->typei->type & adns__rrt_typemask) ||
249 !ownermatched)
5c596e4d 250 continue;
11c8bf9b 251 assert(qu->answer->nrrs<wantedrrs);
252 st= qu->typei->parse(qu,serv,
253 dgram,dglen, rdstart,rdstart+rdlength,
254 qu->answer->rrs.bytes+qu->answer->nrrs*qu->typei->rrsz);
255 if (st) { adns__query_fail(qu,st); return; }
7f702335 256 if (rdstart==-1) goto x_truncated;
5c596e4d 257 }
54ed1d64 258
5c596e4d 259 /* This may have generated some child queries ... */
260 if (qu->children.head) {
261 qu->state= query_child;
262 LIST_LINK_TAIL(ads->childw,qu);
263 return;
54ed1d64 264 }
168e26fe 265
11c8bf9b 266 adns__query_done(qu);
5c596e4d 267 return;
8312a1c2 268
98db6da3 269 x_truncated:
5c596e4d 270 if (!flg_tc) {
7f702335 271 adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
11c8bf9b 272 adns__query_fail(qu,adns_s_serverfaulty);
5c596e4d 273 return;
274 }
11c8bf9b 275 if (qu->cname_dgram) { cname_recurse(qu,adns_qf_usevc); return; }
276 adns__reset_cnameonly(qu);
5c596e4d 277 qu->flags |= adns_qf_usevc;
11c8bf9b 278 adns__query_udp(qu,now);
5c596e4d 279}