No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / named / lwdgrbn.c
blobecd2c66aa68b990aaf63472c8844fe6e7b4aaa11
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: lwdgrbn.c,v 1.22 2009/09/02 23:48:01 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <isc/mem.h>
27 #include <isc/socket.h>
28 #include <isc/string.h> /* Required for HP/UX (and others?) */
29 #include <isc/util.h>
31 #include <dns/db.h>
32 #include <dns/lookup.h>
33 #include <dns/rdata.h>
34 #include <dns/rdataset.h>
35 #include <dns/rdatasetiter.h>
36 #include <dns/result.h>
37 #include <dns/view.h>
39 #include <named/types.h>
40 #include <named/lwdclient.h>
41 #include <named/lwresd.h>
42 #include <named/lwsearch.h>
44 static void start_lookup(ns_lwdclient_t *);
46 static isc_result_t
47 fill_array(int *pos, dns_rdataset_t *rdataset,
48 int size, unsigned char **rdatas, lwres_uint16_t *rdatalen)
50 dns_rdata_t rdata;
51 isc_result_t result;
52 isc_region_t r;
54 UNUSED(size);
56 dns_rdata_init(&rdata);
57 for (result = dns_rdataset_first(rdataset);
58 result == ISC_R_SUCCESS;
59 result = dns_rdataset_next(rdataset))
61 INSIST(*pos < size);
62 dns_rdataset_current(rdataset, &rdata);
63 dns_rdata_toregion(&rdata, &r);
64 rdatas[*pos] = r.base;
65 rdatalen[*pos] = r.length;
66 dns_rdata_reset(&rdata);
67 (*pos)++;
69 if (result == ISC_R_NOMORE)
70 result = ISC_R_SUCCESS;
71 return (result);
74 static isc_result_t
75 iterate_node(lwres_grbnresponse_t *grbn, dns_db_t *db, dns_dbnode_t *node,
76 isc_mem_t *mctx)
78 int used = 0, count;
79 int size = 8, oldsize = 0;
80 unsigned char **rdatas = NULL, **oldrdatas = NULL, **newrdatas = NULL;
81 lwres_uint16_t *lens = NULL, *oldlens = NULL, *newlens = NULL;
82 dns_rdatasetiter_t *iter = NULL;
83 dns_rdataset_t set;
84 dns_ttl_t ttl = ISC_INT32_MAX;
85 lwres_uint32_t flags = LWRDATA_VALIDATED;
86 isc_result_t result = ISC_R_NOMEMORY;
88 result = dns_db_allrdatasets(db, node, NULL, 0, &iter);
89 if (result != ISC_R_SUCCESS)
90 goto out;
92 rdatas = isc_mem_get(mctx, size * sizeof(*rdatas));
93 if (rdatas == NULL)
94 goto out;
95 lens = isc_mem_get(mctx, size * sizeof(*lens));
96 if (lens == NULL)
97 goto out;
99 for (result = dns_rdatasetiter_first(iter);
100 result == ISC_R_SUCCESS;
101 result = dns_rdatasetiter_next(iter))
103 result = ISC_R_NOMEMORY;
104 dns_rdataset_init(&set);
105 dns_rdatasetiter_current(iter, &set);
107 if (set.type != dns_rdatatype_rrsig) {
108 dns_rdataset_disassociate(&set);
109 continue;
112 count = dns_rdataset_count(&set);
113 if (used + count > size) {
114 /* copy & reallocate */
115 oldsize = size;
116 oldrdatas = rdatas;
117 oldlens = lens;
118 rdatas = NULL;
119 lens = NULL;
121 size *= 2;
123 rdatas = isc_mem_get(mctx, size * sizeof(*rdatas));
124 if (rdatas == NULL)
125 goto out;
126 lens = isc_mem_get(mctx, size * sizeof(*lens));
127 if (lens == NULL)
128 goto out;
129 memcpy(rdatas, oldrdatas, used * sizeof(*rdatas));
130 memcpy(lens, oldlens, used * sizeof(*lens));
131 isc_mem_put(mctx, oldrdatas,
132 oldsize * sizeof(*oldrdatas));
133 isc_mem_put(mctx, oldlens, oldsize * sizeof(*oldlens));
134 oldrdatas = NULL;
135 oldlens = NULL;
137 if (set.ttl < ttl)
138 ttl = set.ttl;
139 if (set.trust != dns_trust_secure)
140 flags &= (~LWRDATA_VALIDATED);
141 result = fill_array(&used, &set, size, rdatas, lens);
142 dns_rdataset_disassociate(&set);
143 if (result != ISC_R_SUCCESS)
144 goto out;
146 if (result == ISC_R_NOMORE)
147 result = ISC_R_SUCCESS;
148 if (result != ISC_R_SUCCESS)
149 goto out;
150 dns_rdatasetiter_destroy(&iter);
153 * If necessary, shrink and copy the arrays.
155 if (size != used) {
156 result = ISC_R_NOMEMORY;
157 newrdatas = isc_mem_get(mctx, used * sizeof(*rdatas));
158 if (newrdatas == NULL)
159 goto out;
160 newlens = isc_mem_get(mctx, used * sizeof(*lens));
161 if (newlens == NULL)
162 goto out;
163 memcpy(newrdatas, rdatas, used * sizeof(*rdatas));
164 memcpy(newlens, lens, used * sizeof(*lens));
165 isc_mem_put(mctx, rdatas, size * sizeof(*rdatas));
166 isc_mem_put(mctx, lens, size * sizeof(*lens));
167 grbn->rdatas = newrdatas;
168 grbn->rdatalen = newlens;
169 } else {
170 grbn->rdatas = rdatas;
171 grbn->rdatalen = lens;
173 grbn->nrdatas = used;
174 grbn->ttl = ttl;
175 grbn->flags = flags;
176 return (ISC_R_SUCCESS);
178 out:
179 dns_rdatasetiter_destroy(&iter);
180 if (rdatas != NULL)
181 isc_mem_put(mctx, rdatas, size * sizeof(*rdatas));
182 if (lens != NULL)
183 isc_mem_put(mctx, lens, size * sizeof(*lens));
184 if (oldrdatas != NULL)
185 isc_mem_put(mctx, oldrdatas, oldsize * sizeof(*oldrdatas));
186 if (oldlens != NULL)
187 isc_mem_put(mctx, oldlens, oldsize * sizeof(*oldlens));
188 if (newrdatas != NULL)
189 isc_mem_put(mctx, newrdatas, used * sizeof(*oldrdatas));
190 return (result);
193 static void
194 lookup_done(isc_task_t *task, isc_event_t *event) {
195 ns_lwdclient_t *client;
196 ns_lwdclientmgr_t *cm;
197 dns_lookupevent_t *levent;
198 lwres_buffer_t lwb;
199 dns_name_t *name;
200 dns_rdataset_t *rdataset;
201 dns_rdataset_t *sigrdataset;
202 isc_result_t result;
203 lwres_result_t lwresult;
204 isc_region_t r;
205 isc_buffer_t b;
206 lwres_grbnresponse_t *grbn;
207 int i;
209 UNUSED(task);
211 lwb.base = NULL;
212 client = event->ev_arg;
213 cm = client->clientmgr;
214 INSIST(client->lookup == (dns_lookup_t *)event->ev_sender);
216 levent = (dns_lookupevent_t *)event;
217 grbn = &client->grbn;
219 ns_lwdclient_log(50, "lookup event result = %s",
220 isc_result_totext(levent->result));
222 result = levent->result;
223 if (result != ISC_R_SUCCESS) {
224 dns_lookup_destroy(&client->lookup);
225 isc_event_free(&event);
226 levent = NULL;
228 switch (result) {
229 case DNS_R_NXDOMAIN:
230 case DNS_R_NCACHENXDOMAIN:
231 result = ns_lwsearchctx_next(&client->searchctx);
232 if (result != ISC_R_SUCCESS)
233 lwresult = LWRES_R_NOTFOUND;
234 else {
235 start_lookup(client);
236 return;
238 break;
239 case DNS_R_NXRRSET:
240 case DNS_R_NCACHENXRRSET:
241 lwresult = LWRES_R_TYPENOTFOUND;
242 break;
243 default:
244 lwresult = LWRES_R_FAILURE;
246 ns_lwdclient_errorpktsend(client, lwresult);
247 return;
250 name = levent->name;
251 b = client->recv_buffer;
253 grbn->flags = 0;
255 grbn->nrdatas = 0;
256 grbn->rdatas = NULL;
257 grbn->rdatalen = NULL;
259 grbn->nsigs = 0;
260 grbn->sigs = NULL;
261 grbn->siglen = NULL;
263 result = dns_name_totext(name, ISC_TRUE, &client->recv_buffer);
264 if (result != ISC_R_SUCCESS)
265 goto out;
266 grbn->realname = (char *)isc_buffer_used(&b);
267 grbn->realnamelen = isc_buffer_usedlength(&client->recv_buffer) -
268 isc_buffer_usedlength(&b);
269 ns_lwdclient_log(50, "found name '%.*s'", grbn->realnamelen,
270 grbn->realname);
272 grbn->rdclass = cm->view->rdclass;
273 grbn->rdtype = client->rdtype;
275 rdataset = levent->rdataset;
276 if (rdataset != NULL) {
277 /* The normal case */
278 grbn->nrdatas = dns_rdataset_count(rdataset);
279 grbn->rdatas = isc_mem_get(cm->mctx, grbn->nrdatas *
280 sizeof(unsigned char *));
281 if (grbn->rdatas == NULL)
282 goto out;
283 grbn->rdatalen = isc_mem_get(cm->mctx, grbn->nrdatas *
284 sizeof(lwres_uint16_t));
285 if (grbn->rdatalen == NULL)
286 goto out;
288 i = 0;
289 result = fill_array(&i, rdataset, grbn->nrdatas, grbn->rdatas,
290 grbn->rdatalen);
291 if (result != ISC_R_SUCCESS)
292 goto out;
293 INSIST(i == grbn->nrdatas);
294 grbn->ttl = rdataset->ttl;
295 if (rdataset->trust == dns_trust_secure)
296 grbn->flags |= LWRDATA_VALIDATED;
297 } else {
298 /* The SIG query case */
299 result = iterate_node(grbn, levent->db, levent->node,
300 cm->mctx);
301 if (result != ISC_R_SUCCESS)
302 goto out;
304 ns_lwdclient_log(50, "filled in %d rdata%s", grbn->nrdatas,
305 (grbn->nrdatas == 1) ? "" : "s");
307 sigrdataset = levent->sigrdataset;
308 if (sigrdataset != NULL) {
309 grbn->nsigs = dns_rdataset_count(sigrdataset);
310 grbn->sigs = isc_mem_get(cm->mctx, grbn->nsigs *
311 sizeof(unsigned char *));
312 if (grbn->sigs == NULL)
313 goto out;
314 grbn->siglen = isc_mem_get(cm->mctx, grbn->nsigs *
315 sizeof(lwres_uint16_t));
316 if (grbn->siglen == NULL)
317 goto out;
319 i = 0;
320 result = fill_array(&i, sigrdataset, grbn->nsigs, grbn->sigs,
321 grbn->siglen);
322 if (result != ISC_R_SUCCESS)
323 goto out;
324 INSIST(i == grbn->nsigs);
325 ns_lwdclient_log(50, "filled in %d signature%s", grbn->nsigs,
326 (grbn->nsigs == 1) ? "" : "s");
329 dns_lookup_destroy(&client->lookup);
330 isc_event_free(&event);
333 * Render the packet.
335 client->pkt.recvlength = LWRES_RECVLENGTH;
336 client->pkt.authtype = 0; /* XXXMLG */
337 client->pkt.authlength = 0;
338 client->pkt.result = LWRES_R_SUCCESS;
340 lwresult = lwres_grbnresponse_render(cm->lwctx,
341 grbn, &client->pkt, &lwb);
342 if (lwresult != LWRES_R_SUCCESS)
343 goto out;
345 isc_mem_put(cm->mctx, grbn->rdatas,
346 grbn->nrdatas * sizeof(unsigned char *));
347 isc_mem_put(cm->mctx, grbn->rdatalen,
348 grbn->nrdatas * sizeof(lwres_uint16_t));
350 if (grbn->sigs != NULL)
351 isc_mem_put(cm->mctx, grbn->sigs,
352 grbn->nsigs * sizeof(unsigned char *));
353 if (grbn->siglen != NULL)
354 isc_mem_put(cm->mctx, grbn->siglen,
355 grbn->nsigs * sizeof(lwres_uint16_t));
357 r.base = lwb.base;
358 r.length = lwb.used;
359 client->sendbuf = r.base;
360 client->sendlength = r.length;
361 result = ns_lwdclient_sendreply(client, &r);
362 if (result != ISC_R_SUCCESS)
363 goto out2;
365 NS_LWDCLIENT_SETSEND(client);
367 return;
369 out:
370 if (grbn->rdatas != NULL)
371 isc_mem_put(cm->mctx, grbn->rdatas,
372 grbn->nrdatas * sizeof(unsigned char *));
373 if (grbn->rdatalen != NULL)
374 isc_mem_put(cm->mctx, grbn->rdatalen,
375 grbn->nrdatas * sizeof(lwres_uint16_t));
377 if (grbn->sigs != NULL)
378 isc_mem_put(cm->mctx, grbn->sigs,
379 grbn->nsigs * sizeof(unsigned char *));
380 if (grbn->siglen != NULL)
381 isc_mem_put(cm->mctx, grbn->siglen,
382 grbn->nsigs * sizeof(lwres_uint16_t));
383 out2:
384 if (client->lookup != NULL)
385 dns_lookup_destroy(&client->lookup);
386 if (lwb.base != NULL)
387 lwres_context_freemem(cm->lwctx, lwb.base, lwb.length);
389 if (event != NULL)
390 isc_event_free(&event);
392 ns_lwdclient_log(50, "error constructing getrrsetbyname response");
393 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
396 static void
397 start_lookup(ns_lwdclient_t *client) {
398 isc_result_t result;
399 ns_lwdclientmgr_t *cm;
400 dns_fixedname_t absname;
402 cm = client->clientmgr;
404 INSIST(client->lookup == NULL);
406 dns_fixedname_init(&absname);
407 result = ns_lwsearchctx_current(&client->searchctx,
408 dns_fixedname_name(&absname));
410 * This will return failure if relative name + suffix is too long.
411 * In this case, just go on to the next entry in the search path.
413 if (result != ISC_R_SUCCESS)
414 start_lookup(client);
416 result = dns_lookup_create(cm->mctx,
417 dns_fixedname_name(&absname),
418 client->rdtype, cm->view,
419 client->options, cm->task, lookup_done,
420 client, &client->lookup);
421 if (result != ISC_R_SUCCESS) {
422 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
423 return;
427 static void
428 init_grbn(ns_lwdclient_t *client) {
429 client->grbn.rdclass = 0;
430 client->grbn.rdtype = 0;
431 client->grbn.ttl = 0;
432 client->grbn.nrdatas = 0;
433 client->grbn.realname = NULL;
434 client->grbn.realnamelen = 0;
435 client->grbn.rdatas = 0;
436 client->grbn.rdatalen = 0;
437 client->grbn.base = NULL;
438 client->grbn.baselen = 0;
439 isc_buffer_init(&client->recv_buffer, client->buffer, LWRES_RECVLENGTH);
442 void
443 ns_lwdclient_processgrbn(ns_lwdclient_t *client, lwres_buffer_t *b) {
444 lwres_grbnrequest_t *req;
445 isc_result_t result;
446 ns_lwdclientmgr_t *cm;
447 isc_buffer_t namebuf;
449 REQUIRE(NS_LWDCLIENT_ISRECVDONE(client));
450 INSIST(client->byaddr == NULL);
452 cm = client->clientmgr;
453 req = NULL;
455 result = lwres_grbnrequest_parse(cm->lwctx,
456 b, &client->pkt, &req);
457 if (result != LWRES_R_SUCCESS)
458 goto out;
459 if (req->name == NULL)
460 goto out;
462 client->options = 0;
463 if (req->rdclass != cm->view->rdclass)
464 goto out;
466 if (req->rdclass == dns_rdataclass_any ||
467 req->rdtype == dns_rdatatype_any)
468 goto out;
470 client->rdtype = req->rdtype;
472 isc_buffer_init(&namebuf, req->name, req->namelen);
473 isc_buffer_add(&namebuf, req->namelen);
475 dns_fixedname_init(&client->query_name);
476 result = dns_name_fromtext(dns_fixedname_name(&client->query_name),
477 &namebuf, NULL, 0, NULL);
478 if (result != ISC_R_SUCCESS)
479 goto out;
480 ns_lwsearchctx_init(&client->searchctx,
481 cm->listener->manager->search,
482 dns_fixedname_name(&client->query_name),
483 cm->listener->manager->ndots);
484 ns_lwsearchctx_first(&client->searchctx);
486 ns_lwdclient_log(50, "client %p looking for type %d",
487 client, client->rdtype);
490 * We no longer need to keep this around.
492 lwres_grbnrequest_free(cm->lwctx, &req);
495 * Initialize the real name and alias arrays in the reply we're
496 * going to build up.
498 init_grbn(client);
501 * Start the find.
503 start_lookup(client);
505 return;
508 * We're screwed. Return an error packet to our caller.
510 out:
511 if (req != NULL)
512 lwres_grbnrequest_free(cm->lwctx, &req);
514 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);