00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023
00024 #include <isc/mem.h>
00025 #include <isc/socket.h>
00026 #include <isc/string.h>
00027 #include <isc/util.h>
00028
00029 #include <dns/db.h>
00030 #include <dns/lookup.h>
00031 #include <dns/rdata.h>
00032 #include <dns/rdataset.h>
00033 #include <dns/rdatasetiter.h>
00034 #include <dns/result.h>
00035 #include <dns/view.h>
00036
00037 #include <named/types.h>
00038 #include <named/lwdclient.h>
00039 #include <named/lwresd.h>
00040 #include <named/lwsearch.h>
00041
00042 static void start_lookup(ns_lwdclient_t *);
00043
00044 static isc_result_t
00045 fill_array(int *pos, dns_rdataset_t *rdataset,
00046 int size, unsigned char **rdatas, lwres_uint16_t *rdatalen)
00047 {
00048 dns_rdata_t rdata;
00049 isc_result_t result;
00050 isc_region_t r;
00051
00052 UNUSED(size);
00053
00054 dns_rdata_init(&rdata);
00055 for (result = dns_rdataset_first(rdataset);
00056 result == ISC_R_SUCCESS;
00057 result = dns_rdataset_next(rdataset))
00058 {
00059 INSIST(*pos < size);
00060 dns_rdataset_current(rdataset, &rdata);
00061 dns_rdata_toregion(&rdata, &r);
00062 rdatas[*pos] = r.base;
00063 rdatalen[*pos] = r.length;
00064 dns_rdata_reset(&rdata);
00065 (*pos)++;
00066 }
00067 if (result == ISC_R_NOMORE)
00068 result = ISC_R_SUCCESS;
00069 return (result);
00070 }
00071
00072 static isc_result_t
00073 iterate_node(lwres_grbnresponse_t *grbn, dns_db_t *db, dns_dbnode_t *node,
00074 isc_mem_t *mctx)
00075 {
00076 int used = 0, count;
00077 int size = 8, oldsize = 0;
00078 unsigned char **rdatas = NULL, **oldrdatas = NULL, **newrdatas = NULL;
00079 lwres_uint16_t *lens = NULL, *oldlens = NULL, *newlens = NULL;
00080 dns_rdatasetiter_t *iter = NULL;
00081 dns_rdataset_t set;
00082 dns_ttl_t ttl = ISC_INT32_MAX;
00083 lwres_uint32_t flags = LWRDATA_VALIDATED;
00084 isc_result_t result = ISC_R_NOMEMORY;
00085
00086 result = dns_db_allrdatasets(db, node, NULL, 0, &iter);
00087 if (result != ISC_R_SUCCESS)
00088 goto out;
00089
00090 rdatas = isc_mem_get(mctx, size * sizeof(*rdatas));
00091 if (rdatas == NULL)
00092 goto out;
00093 lens = isc_mem_get(mctx, size * sizeof(*lens));
00094 if (lens == NULL)
00095 goto out;
00096
00097 for (result = dns_rdatasetiter_first(iter);
00098 result == ISC_R_SUCCESS;
00099 result = dns_rdatasetiter_next(iter))
00100 {
00101 result = ISC_R_NOMEMORY;
00102 dns_rdataset_init(&set);
00103 dns_rdatasetiter_current(iter, &set);
00104
00105 if (set.type != dns_rdatatype_rrsig) {
00106 dns_rdataset_disassociate(&set);
00107 continue;
00108 }
00109
00110 count = dns_rdataset_count(&set);
00111 if (used + count > size) {
00112
00113 oldsize = size;
00114 oldrdatas = rdatas;
00115 oldlens = lens;
00116 rdatas = NULL;
00117 lens = NULL;
00118
00119 size *= 2;
00120
00121 rdatas = isc_mem_get(mctx, size * sizeof(*rdatas));
00122 if (rdatas == NULL)
00123 goto out;
00124 lens = isc_mem_get(mctx, size * sizeof(*lens));
00125 if (lens == NULL)
00126 goto out;
00127 memmove(rdatas, oldrdatas, used * sizeof(*rdatas));
00128 memmove(lens, oldlens, used * sizeof(*lens));
00129 isc_mem_put(mctx, oldrdatas,
00130 oldsize * sizeof(*oldrdatas));
00131 isc_mem_put(mctx, oldlens, oldsize * sizeof(*oldlens));
00132 oldrdatas = NULL;
00133 oldlens = NULL;
00134 }
00135 if (set.ttl < ttl)
00136 ttl = set.ttl;
00137 if (set.trust != dns_trust_secure)
00138 flags &= (~LWRDATA_VALIDATED);
00139 result = fill_array(&used, &set, size, rdatas, lens);
00140 dns_rdataset_disassociate(&set);
00141 if (result != ISC_R_SUCCESS)
00142 goto out;
00143 }
00144 if (result == ISC_R_NOMORE)
00145 result = ISC_R_SUCCESS;
00146 if (result != ISC_R_SUCCESS)
00147 goto out;
00148 dns_rdatasetiter_destroy(&iter);
00149
00150
00151
00152
00153 if (size != used) {
00154 result = ISC_R_NOMEMORY;
00155 newrdatas = isc_mem_get(mctx, used * sizeof(*rdatas));
00156 if (newrdatas == NULL)
00157 goto out;
00158 newlens = isc_mem_get(mctx, used * sizeof(*lens));
00159 if (newlens == NULL)
00160 goto out;
00161 memmove(newrdatas, rdatas, used * sizeof(*rdatas));
00162 memmove(newlens, lens, used * sizeof(*lens));
00163 isc_mem_put(mctx, rdatas, size * sizeof(*rdatas));
00164 isc_mem_put(mctx, lens, size * sizeof(*lens));
00165 grbn->rdatas = newrdatas;
00166 grbn->rdatalen = newlens;
00167 } else {
00168 grbn->rdatas = rdatas;
00169 grbn->rdatalen = lens;
00170 }
00171 grbn->nrdatas = used;
00172 grbn->ttl = ttl;
00173 grbn->flags = flags;
00174 return (ISC_R_SUCCESS);
00175
00176 out:
00177 dns_rdatasetiter_destroy(&iter);
00178 if (rdatas != NULL)
00179 isc_mem_put(mctx, rdatas, size * sizeof(*rdatas));
00180 if (lens != NULL)
00181 isc_mem_put(mctx, lens, size * sizeof(*lens));
00182 if (oldrdatas != NULL)
00183 isc_mem_put(mctx, oldrdatas, oldsize * sizeof(*oldrdatas));
00184 if (oldlens != NULL)
00185 isc_mem_put(mctx, oldlens, oldsize * sizeof(*oldlens));
00186 if (newrdatas != NULL)
00187 isc_mem_put(mctx, newrdatas, used * sizeof(*oldrdatas));
00188 return (result);
00189 }
00190
00191 static void
00192 lookup_done(isc_task_t *task, isc_event_t *event) {
00193 ns_lwdclient_t *client;
00194 ns_lwdclientmgr_t *cm;
00195 dns_lookupevent_t *levent;
00196 lwres_buffer_t lwb;
00197 dns_name_t *name;
00198 dns_rdataset_t *rdataset;
00199 dns_rdataset_t *sigrdataset;
00200 isc_result_t result;
00201 lwres_result_t lwresult;
00202 isc_region_t r;
00203 isc_buffer_t b;
00204 lwres_grbnresponse_t *grbn;
00205 int i;
00206
00207 REQUIRE(event != NULL);
00208
00209 UNUSED(task);
00210
00211 lwb.base = NULL;
00212 client = event->ev_arg;
00213 cm = client->clientmgr;
00214 INSIST(client->lookup == (dns_lookup_t *)event->ev_sender);
00215
00216 levent = (dns_lookupevent_t *)event;
00217 grbn = &client->grbn;
00218
00219 ns_lwdclient_log(50, "lookup event result = %s",
00220 isc_result_totext(levent->result));
00221
00222 result = levent->result;
00223 if (result != ISC_R_SUCCESS) {
00224 dns_lookup_destroy(&client->lookup);
00225 isc_event_free(&event);
00226 levent = NULL;
00227
00228 switch (result) {
00229 case DNS_R_NXDOMAIN:
00230 case DNS_R_NCACHENXDOMAIN:
00231 result = ns_lwsearchctx_next(&client->searchctx);
00232 if (result != ISC_R_SUCCESS)
00233 lwresult = LWRES_R_NOTFOUND;
00234 else {
00235 start_lookup(client);
00236 return;
00237 }
00238 break;
00239 case DNS_R_NXRRSET:
00240 case DNS_R_NCACHENXRRSET:
00241 lwresult = LWRES_R_TYPENOTFOUND;
00242 break;
00243 default:
00244 lwresult = LWRES_R_FAILURE;
00245 }
00246 ns_lwdclient_errorpktsend(client, lwresult);
00247 return;
00248 }
00249
00250 name = levent->name;
00251 b = client->recv_buffer;
00252
00253 grbn->flags = 0;
00254
00255 grbn->nrdatas = 0;
00256 grbn->rdatas = NULL;
00257 grbn->rdatalen = NULL;
00258
00259 grbn->nsigs = 0;
00260 grbn->sigs = NULL;
00261 grbn->siglen = NULL;
00262
00263 result = dns_name_totext(name, ISC_TRUE, &client->recv_buffer);
00264 if (result != ISC_R_SUCCESS)
00265 goto out;
00266 grbn->realname = (char *)isc_buffer_used(&b);
00267 grbn->realnamelen = isc_buffer_usedlength(&client->recv_buffer) -
00268 isc_buffer_usedlength(&b);
00269 ns_lwdclient_log(50, "found name '%.*s'", grbn->realnamelen,
00270 grbn->realname);
00271
00272 grbn->rdclass = cm->view->rdclass;
00273 grbn->rdtype = client->rdtype;
00274
00275 rdataset = levent->rdataset;
00276 if (rdataset != NULL) {
00277
00278 grbn->nrdatas = dns_rdataset_count(rdataset);
00279 grbn->rdatas = isc_mem_get(cm->mctx, grbn->nrdatas *
00280 sizeof(unsigned char *));
00281 if (grbn->rdatas == NULL)
00282 goto out;
00283 grbn->rdatalen = isc_mem_get(cm->mctx, grbn->nrdatas *
00284 sizeof(lwres_uint16_t));
00285 if (grbn->rdatalen == NULL)
00286 goto out;
00287
00288 i = 0;
00289 result = fill_array(&i, rdataset, grbn->nrdatas, grbn->rdatas,
00290 grbn->rdatalen);
00291 if (result != ISC_R_SUCCESS)
00292 goto out;
00293 INSIST(i == grbn->nrdatas);
00294 grbn->ttl = rdataset->ttl;
00295 if (rdataset->trust == dns_trust_secure)
00296 grbn->flags |= LWRDATA_VALIDATED;
00297 } else {
00298
00299 result = iterate_node(grbn, levent->db, levent->node,
00300 cm->mctx);
00301 if (result != ISC_R_SUCCESS)
00302 goto out;
00303 }
00304 ns_lwdclient_log(50, "filled in %d rdata%s", grbn->nrdatas,
00305 (grbn->nrdatas == 1) ? "" : "s");
00306
00307 sigrdataset = levent->sigrdataset;
00308 if (sigrdataset != NULL) {
00309 grbn->nsigs = dns_rdataset_count(sigrdataset);
00310 grbn->sigs = isc_mem_get(cm->mctx, grbn->nsigs *
00311 sizeof(unsigned char *));
00312 if (grbn->sigs == NULL)
00313 goto out;
00314 grbn->siglen = isc_mem_get(cm->mctx, grbn->nsigs *
00315 sizeof(lwres_uint16_t));
00316 if (grbn->siglen == NULL)
00317 goto out;
00318
00319 i = 0;
00320 result = fill_array(&i, sigrdataset, grbn->nsigs, grbn->sigs,
00321 grbn->siglen);
00322 if (result != ISC_R_SUCCESS)
00323 goto out;
00324 INSIST(i == grbn->nsigs);
00325 ns_lwdclient_log(50, "filled in %d signature%s", grbn->nsigs,
00326 (grbn->nsigs == 1) ? "" : "s");
00327 }
00328
00329
00330
00331
00332 client->pkt.recvlength = LWRES_RECVLENGTH;
00333 client->pkt.authtype = 0;
00334 client->pkt.authlength = 0;
00335 client->pkt.result = LWRES_R_SUCCESS;
00336
00337 lwresult = lwres_grbnresponse_render(cm->lwctx,
00338 grbn, &client->pkt, &lwb);
00339 if (lwresult != LWRES_R_SUCCESS)
00340 goto out;
00341
00342 isc_mem_put(cm->mctx, grbn->rdatas,
00343 grbn->nrdatas * sizeof(unsigned char *));
00344 isc_mem_put(cm->mctx, grbn->rdatalen,
00345 grbn->nrdatas * sizeof(lwres_uint16_t));
00346
00347 if (grbn->sigs != NULL)
00348 isc_mem_put(cm->mctx, grbn->sigs,
00349 grbn->nsigs * sizeof(unsigned char *));
00350 if (grbn->siglen != NULL)
00351 isc_mem_put(cm->mctx, grbn->siglen,
00352 grbn->nsigs * sizeof(lwres_uint16_t));
00353
00354 r.base = lwb.base;
00355 r.length = lwb.used;
00356 client->sendbuf = r.base;
00357 client->sendlength = r.length;
00358 result = ns_lwdclient_sendreply(client, &r);
00359 if (result != ISC_R_SUCCESS)
00360 goto out2;
00361
00362 NS_LWDCLIENT_SETSEND(client);
00363
00364 dns_lookup_destroy(&client->lookup);
00365 isc_event_free(&event);
00366
00367 return;
00368
00369 out:
00370 if (grbn->rdatas != NULL)
00371 isc_mem_put(cm->mctx, grbn->rdatas,
00372 grbn->nrdatas * sizeof(unsigned char *));
00373 if (grbn->rdatalen != NULL)
00374 isc_mem_put(cm->mctx, grbn->rdatalen,
00375 grbn->nrdatas * sizeof(lwres_uint16_t));
00376
00377 if (grbn->sigs != NULL)
00378 isc_mem_put(cm->mctx, grbn->sigs,
00379 grbn->nsigs * sizeof(unsigned char *));
00380 if (grbn->siglen != NULL)
00381 isc_mem_put(cm->mctx, grbn->siglen,
00382 grbn->nsigs * sizeof(lwres_uint16_t));
00383 out2:
00384 if (client->lookup != NULL)
00385 dns_lookup_destroy(&client->lookup);
00386 if (lwb.base != NULL)
00387 lwres_context_freemem(cm->lwctx, lwb.base, lwb.length);
00388
00389 isc_event_free(&event);
00390
00391 ns_lwdclient_log(50, "error constructing getrrsetbyname response");
00392 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
00393 }
00394
00395 static void
00396 start_lookup(ns_lwdclient_t *client) {
00397 isc_result_t result;
00398 ns_lwdclientmgr_t *cm;
00399 dns_fixedname_t absname;
00400
00401 cm = client->clientmgr;
00402
00403 INSIST(client->lookup == NULL);
00404
00405 dns_fixedname_init(&absname);
00406 result = ns_lwsearchctx_current(&client->searchctx,
00407 dns_fixedname_name(&absname));
00408
00409
00410
00411
00412 if (result != ISC_R_SUCCESS)
00413 start_lookup(client);
00414
00415 result = dns_lookup_create(cm->mctx,
00416 dns_fixedname_name(&absname),
00417 client->rdtype, cm->view,
00418 client->options, cm->task, lookup_done,
00419 client, &client->lookup);
00420 if (result != ISC_R_SUCCESS) {
00421 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
00422 return;
00423 }
00424 }
00425
00426 static void
00427 init_grbn(ns_lwdclient_t *client) {
00428 client->grbn.rdclass = 0;
00429 client->grbn.rdtype = 0;
00430 client->grbn.ttl = 0;
00431 client->grbn.nrdatas = 0;
00432 client->grbn.realname = NULL;
00433 client->grbn.realnamelen = 0;
00434 client->grbn.rdatas = 0;
00435 client->grbn.rdatalen = 0;
00436 client->grbn.base = NULL;
00437 client->grbn.baselen = 0;
00438 isc_buffer_init(&client->recv_buffer, client->buffer, LWRES_RECVLENGTH);
00439 }
00440
00441 void
00442 ns_lwdclient_processgrbn(ns_lwdclient_t *client, lwres_buffer_t *b) {
00443 lwres_grbnrequest_t *req;
00444 isc_result_t result;
00445 ns_lwdclientmgr_t *cm;
00446 isc_buffer_t namebuf;
00447
00448 REQUIRE(NS_LWDCLIENT_ISRECVDONE(client));
00449 INSIST(client->byaddr == NULL);
00450
00451 cm = client->clientmgr;
00452 req = NULL;
00453
00454 result = lwres_grbnrequest_parse(cm->lwctx,
00455 b, &client->pkt, &req);
00456 if (result != LWRES_R_SUCCESS)
00457 goto out;
00458 if (req->name == NULL)
00459 goto out;
00460
00461 client->options = 0;
00462 if (req->rdclass != cm->view->rdclass)
00463 goto out;
00464
00465 if (req->rdclass == dns_rdataclass_any ||
00466 req->rdtype == dns_rdatatype_any)
00467 goto out;
00468
00469 client->rdtype = req->rdtype;
00470
00471 isc_buffer_init(&namebuf, req->name, req->namelen);
00472 isc_buffer_add(&namebuf, req->namelen);
00473
00474 dns_fixedname_init(&client->query_name);
00475 result = dns_name_fromtext(dns_fixedname_name(&client->query_name),
00476 &namebuf, NULL, 0, NULL);
00477 if (result != ISC_R_SUCCESS)
00478 goto out;
00479 ns_lwsearchctx_init(&client->searchctx,
00480 cm->listener->manager->search,
00481 dns_fixedname_name(&client->query_name),
00482 cm->listener->manager->ndots);
00483 ns_lwsearchctx_first(&client->searchctx);
00484
00485 ns_lwdclient_log(50, "client %p looking for type %d",
00486 client, client->rdtype);
00487
00488
00489
00490
00491 lwres_grbnrequest_free(cm->lwctx, &req);
00492
00493
00494
00495
00496
00497 init_grbn(client);
00498
00499
00500
00501
00502 start_lookup(client);
00503
00504 return;
00505
00506
00507
00508
00509 out:
00510 if (req != NULL)
00511 lwres_grbnrequest_free(cm->lwctx, &req);
00512
00513 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
00514 }