data_source.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <cassert>
  16. #include <iostream>
  17. #include <vector>
  18. #include <boost/foreach.hpp>
  19. #include <dns/base32.h>
  20. #include <dns/buffer.h>
  21. #include <dns/message.h>
  22. #include <dns/name.h>
  23. #include <dns/rdataclass.h>
  24. #include <dns/rrset.h>
  25. #include <dns/rrsetlist.h>
  26. #include <dns/sha1.h>
  27. #include <cc/data.h>
  28. #include "data_source.h"
  29. #include "query.h"
  30. using namespace std;
  31. using namespace isc::dns;
  32. using namespace isc::dns::rdata;
  33. namespace isc {
  34. namespace auth {
  35. // Add a task to the query task queue to look up additional data
  36. // (i.e., address records for the names included in NS or MX records)
  37. static void
  38. getAdditional(Query& q, RRsetPtr rrset) {
  39. if (!q.wantAdditional()) {
  40. return;
  41. }
  42. RdataIteratorPtr it = rrset->getRdataIterator();
  43. for (it->first(); !it->isLast(); it->next()) {
  44. const Rdata& rd(it->getCurrent());
  45. QueryTaskPtr newtask = QueryTaskPtr();
  46. if (rrset->getType() == RRType::NS()) {
  47. const generic::NS& ns = dynamic_cast<const generic::NS&>(rd);
  48. newtask = QueryTaskPtr(new QueryTask(ns.getNSName(), q.qclass(),
  49. Section::ADDITIONAL(),
  50. QueryTask::GLUE_QUERY,
  51. QueryTask::GETADDITIONAL));
  52. } else if (rrset->getType() == RRType::MX()) {
  53. const generic::MX& mx = dynamic_cast<const generic::MX&>(rd);
  54. newtask = QueryTaskPtr(new QueryTask(mx.getMXName(), q.qclass(),
  55. Section::ADDITIONAL(),
  56. QueryTask::NOGLUE_QUERY,
  57. QueryTask::GETADDITIONAL));
  58. }
  59. if (newtask) {
  60. q.tasks().push(newtask);
  61. }
  62. }
  63. }
  64. // Synthesize a CNAME answer, for the benefit of clients that don't
  65. // understand DNAME
  66. static void
  67. synthesizeCname(Query& q, QueryTaskPtr task, RRsetPtr rrset, RRsetList& target)
  68. {
  69. RdataIteratorPtr it = rrset->getRdataIterator();
  70. // More than one DNAME RR in the RRset is illegal, so we only have
  71. // to process the first one.
  72. it->first();
  73. if (it->isLast()) {
  74. return;
  75. }
  76. const Rdata& rd(it->getCurrent());
  77. const generic::DNAME& dname = dynamic_cast<const generic::DNAME&>(rd);
  78. const Name& dname_target(dname.getDname());
  79. RRsetPtr cname(new RRset(task->qname, task->qclass, RRType::CNAME(),
  80. rrset->getTTL()));
  81. const int qnlen = task->qname.getLabelCount();
  82. const int dnlen = rrset->getName().getLabelCount();
  83. assert(qnlen > dnlen);
  84. const Name& prefix(task->qname.split(0, qnlen - dnlen));
  85. cname->addRdata(generic::CNAME(prefix.concatenate(dname_target)));
  86. target.addRRset(cname);
  87. }
  88. // Add a task to the query task queue to look up the data pointed
  89. // to by a CNAME record
  90. static void
  91. chaseCname(Query& q, QueryTaskPtr task, RRsetPtr rrset)
  92. {
  93. RdataIteratorPtr it = rrset->getRdataIterator();
  94. // More than one CNAME RR in the RRset is illegal, so we only have
  95. // to process the first one.
  96. it->first();
  97. if (it->isLast()) {
  98. return;
  99. }
  100. q.tasks().push(QueryTaskPtr(
  101. new QueryTask(dynamic_cast<const generic::CNAME&>
  102. (it->getCurrent()).getCname(),
  103. task->qclass,
  104. task->qtype,
  105. Section::ANSWER(),
  106. QueryTask::FOLLOWCNAME)));
  107. }
  108. // Perform the query specified in a QueryTask object
  109. static DataSrc::Result
  110. doQueryTask(const DataSrc* ds, const Name* zonename, Query& q, QueryTask& task,
  111. RRsetList& target)
  112. {
  113. switch (task.op) {
  114. case QueryTask::AUTH_QUERY:
  115. return (ds->findRRset(q, task.qname, task.qclass, task.qtype,
  116. target, task.flags, zonename));
  117. case QueryTask::SIMPLE_QUERY:
  118. return (ds->findExactRRset(q, task.qname, task.qclass, task.qtype,
  119. target, task.flags, zonename));
  120. case QueryTask::GLUE_QUERY:
  121. case QueryTask::NOGLUE_QUERY:
  122. return (ds->findAddrs(q, task.qname, task.qclass, target,
  123. task.flags, zonename));
  124. case QueryTask::REF_QUERY:
  125. return (ds->findReferral(q, task.qname, task.qclass, target,
  126. task.flags, zonename));
  127. }
  128. // Not reached
  129. return (DataSrc::ERROR);
  130. }
  131. // Copy referral information into the authority section of a message
  132. static inline void
  133. copyAuth(Query& q, RRsetList& auth)
  134. {
  135. BOOST_FOREACH(RRsetPtr rrset, auth) {
  136. if (rrset->getType() == RRType::DNAME()) {
  137. continue;
  138. }
  139. if (rrset->getType() == RRType::DS() && !q.wantDnssec()) {
  140. continue;
  141. }
  142. q.message().addRRset(Section::AUTHORITY(), rrset, q.wantDnssec());
  143. getAdditional(q, rrset);
  144. }
  145. }
  146. // Query for referrals (i.e., NS/DS or DNAME) at a given name
  147. static inline bool
  148. refQuery(const Name& name, Query& q, QueryTaskPtr task,
  149. const DataSrc* ds, const Name* zonename, RRsetList& target)
  150. {
  151. QueryTask newtask(name, q.qclass(), QueryTask::REF_QUERY);
  152. if (doQueryTask(ds, zonename, q, newtask, target) != DataSrc::SUCCESS) {
  153. // Lookup failed
  154. return (false);
  155. }
  156. // Referral bit is expected, so clear it when checking flags
  157. if ((newtask.flags & ~DataSrc::REFERRAL) != 0) {
  158. return (false);
  159. }
  160. return (true);
  161. }
  162. // Match downward, from the zone apex to the query name, looking for
  163. // referrals.
  164. static inline bool
  165. hasDelegation(const DataSrc* ds, const Name* zonename, Query& q,
  166. QueryTaskPtr task)
  167. {
  168. int nlen = task->qname.getLabelCount();
  169. int diff = nlen - zonename->getLabelCount();
  170. if (diff > 1) {
  171. bool found = false;
  172. RRsetList ref;
  173. for (int i = diff; i > 1; --i) {
  174. const Name sub(task->qname.split(i - 1, nlen - i));
  175. if (refQuery(sub, q, task, ds, zonename, ref)) {
  176. found = true;
  177. break;
  178. }
  179. }
  180. // Found a referral while getting additional data
  181. // for something other than NS; we skip it.
  182. if (found && task->op == QueryTask::NOGLUE_QUERY) {
  183. return (true);
  184. }
  185. // Found a referral while getting answer data;
  186. // send a delegation.
  187. if (found) {
  188. if (RRsetPtr r = ref[RRType::DNAME()]) {
  189. RRsetList syn;
  190. q.message().addRRset(Section::ANSWER(), r, q.wantDnssec());
  191. q.message().setHeaderFlag(MessageFlag::AA());
  192. synthesizeCname(q, task, r, syn);
  193. if (syn.size() == 1) {
  194. q.message().addRRset(Section::ANSWER(),
  195. syn[RRType::CNAME()],
  196. q.wantDnssec());
  197. chaseCname(q, task, syn[RRType::CNAME()]);
  198. return (true);
  199. }
  200. }
  201. copyAuth(q, ref);
  202. return (true);
  203. }
  204. }
  205. // We appear to have authoritative data; set the header
  206. // flag. (We may clear it later if we find a referral
  207. // at the actual qname node.)
  208. if (task->op == QueryTask::AUTH_QUERY &&
  209. task->state == QueryTask::GETANSWER) {
  210. q.message().setHeaderFlag(MessageFlag::AA());
  211. }
  212. return (false);
  213. }
  214. // Attempt a wildcard lookup
  215. static inline DataSrc::Result
  216. tryWildcard(Query& q, QueryTaskPtr task, const DataSrc* ds,
  217. const Name* zonename, bool& found)
  218. {
  219. Message& m = q.message();
  220. DataSrc::Result result;
  221. found = false;
  222. if ((task->flags & DataSrc::NAME_NOT_FOUND) == 0 ||
  223. (task->state != QueryTask::GETANSWER &&
  224. task->state != QueryTask::FOLLOWCNAME)) {
  225. return (DataSrc::SUCCESS);
  226. }
  227. int nlen = task->qname.getLabelCount();
  228. int diff = nlen - zonename->getLabelCount();
  229. if (diff < 1) {
  230. return (DataSrc::SUCCESS);
  231. }
  232. RRsetList wild;
  233. Name star("*");
  234. uint32_t rflags = 0;
  235. for (int i = 1; i <= diff; ++i) {
  236. const Name& wname(star.concatenate(task->qname.split(i, nlen - i)));
  237. QueryTask newtask(wname, task->qclass, task->qtype,
  238. QueryTask::SIMPLE_QUERY);
  239. result = doQueryTask(ds, zonename, q, newtask, wild);
  240. if (result == DataSrc::SUCCESS &&
  241. (newtask.flags == 0 || (newtask.flags & DataSrc::CNAME_FOUND))) {
  242. rflags = newtask.flags;
  243. found = true;
  244. break;
  245. }
  246. }
  247. // A wildcard was found. Add the data to the answer
  248. // section (but with the name changed to match the
  249. // qname), and then continue as if this were a normal
  250. // answer: if a CNAME, chase the target, otherwise
  251. // add authority.
  252. if (found) {
  253. if ((rflags & DataSrc::CNAME_FOUND) != 0) {
  254. if (RRsetPtr rrset = wild[RRType::CNAME()]) {
  255. rrset->setName(task->qname);
  256. m.addRRset(Section::ANSWER(), rrset, q.wantDnssec());
  257. chaseCname(q, task, rrset);
  258. }
  259. } else {
  260. BOOST_FOREACH (RRsetPtr rrset, wild) {
  261. rrset->setName(task->qname);
  262. m.addRRset(Section::ANSWER(), rrset, q.wantDnssec());
  263. }
  264. RRsetList auth;
  265. if (! refQuery(*zonename, q, task, ds, zonename, auth)) {
  266. return (DataSrc::ERROR);
  267. }
  268. copyAuth(q, auth);
  269. }
  270. } else if (q.wantDnssec()) {
  271. // No wildcard found; add an NSEC to prove it
  272. RRsetList nsec;
  273. QueryTask newtask(*zonename, task->qclass, RRType::NSEC(),
  274. QueryTask::SIMPLE_QUERY);
  275. result = doQueryTask(ds, zonename, q, newtask, nsec);
  276. if (result != DataSrc::SUCCESS) {
  277. return (DataSrc::ERROR);
  278. }
  279. if (newtask.flags == 0) {
  280. m.addRRset(Section::AUTHORITY(), nsec[RRType::NSEC()], true);
  281. }
  282. }
  283. return (DataSrc::SUCCESS);
  284. }
  285. //
  286. // doQuery: Processes a query.
  287. //
  288. void
  289. DataSrc::doQuery(Query& q)
  290. {
  291. Message& m = q.message();
  292. vector<RRsetPtr> additional;
  293. m.clearHeaderFlag(MessageFlag::AA());
  294. while (!q.tasks().empty()) {
  295. QueryTaskPtr task = q.tasks().front();
  296. q.tasks().pop();
  297. // These task types should never be on the task queue.
  298. if (task->op == QueryTask::SIMPLE_QUERY ||
  299. task->op == QueryTask::REF_QUERY) {
  300. m.setRcode(Rcode::SERVFAIL());
  301. return;
  302. }
  303. // Find the closest enclosing zone for which we are authoritative,
  304. // and the concrete data source which is authoritative for it.
  305. // (Note that RRtype DS queries need to go to the parent.)
  306. Name matchname(task->qtype == RRType::DS() ?
  307. task->qname.split(1, task->qname.getLabelCount() - 1) :
  308. task->qname);
  309. NameMatch match(matchname);
  310. findClosestEnclosure(match);
  311. const DataSrc* datasource = match.bestDataSrc();
  312. const Name* zonename = match.closestName();
  313. assert((datasource == NULL && zonename == NULL) ||
  314. (datasource != NULL && zonename != NULL));
  315. RRsetList data;
  316. Result result = SUCCESS;
  317. if (datasource) {
  318. // For these query task types, if there is more than
  319. // one level between the zone name and qname, we need to
  320. // check the intermediate nodes for referrals.
  321. if ((task->op == QueryTask::AUTH_QUERY ||
  322. task->op == QueryTask::NOGLUE_QUERY) &&
  323. hasDelegation(datasource, zonename, q, task)) {
  324. continue;
  325. }
  326. result = doQueryTask(datasource, zonename, q, *task, data);
  327. if (result != SUCCESS) {
  328. m.setRcode(Rcode::SERVFAIL());
  329. return;
  330. }
  331. // Query found a referral; let's find out if that was expected--
  332. // i.e., if an NS was at the zone apex, or if we were querying
  333. // specifically for the DS or DNAME record.
  334. if ((task->flags & REFERRAL) != 0 &&
  335. (zonename->getLabelCount() == task->qname.getLabelCount() ||
  336. task->qtype == RRType::DS() ||
  337. task->qtype == RRType::DNAME())) {
  338. task->flags &= ~REFERRAL;
  339. }
  340. } else {
  341. task->flags = NO_SUCH_ZONE;
  342. // No such zone. If we're chasing cnames or adding additional
  343. // data, that's okay, but if doing an original query, return
  344. // REFUSED.
  345. if (task->state == QueryTask::GETANSWER) {
  346. m.setRcode(Rcode::REFUSED());
  347. return;
  348. }
  349. continue;
  350. }
  351. if (result == SUCCESS && task->flags == 0) {
  352. bool have_ns = false, need_auth = false;
  353. switch (task->state) {
  354. case QueryTask::GETANSWER:
  355. case QueryTask::FOLLOWCNAME:
  356. BOOST_FOREACH(RRsetPtr rrset, data) {
  357. m.addRRset(task->section, rrset, q.wantDnssec());
  358. if (q.tasks().empty()) {
  359. need_auth = true;
  360. }
  361. getAdditional(q, rrset);
  362. if (rrset->getType() == RRType::NS()) {
  363. have_ns = true;
  364. }
  365. }
  366. q.setStatus(Query::ANSWERED);
  367. if (need_auth && !have_ns) {
  368. // Data found, no additional processing needed.
  369. // Add the NS records for the enclosing zone to
  370. // the authority section.
  371. RRsetList auth;
  372. if (! refQuery(*zonename, q, task, datasource,
  373. zonename, auth)) {
  374. m.setRcode(Rcode::SERVFAIL());
  375. return;
  376. }
  377. copyAuth(q, auth);
  378. }
  379. continue;
  380. case QueryTask::GETADDITIONAL:
  381. // Got additional data. Do not add it to the message
  382. // yet; instead store it and copy it in at the end
  383. // (this allow RRSIGs to be omitted if necessary).
  384. BOOST_FOREACH(RRsetPtr rrset, data) {
  385. if (q.status() == Query::ANSWERED &&
  386. rrset->getName() == q.qname() &&
  387. rrset->getType() == q.qtype()) {
  388. continue;
  389. }
  390. additional.push_back(rrset);
  391. }
  392. continue;
  393. default:
  394. dns_throw (Unexpected, "unexpected query state");
  395. }
  396. } else if (result == ERROR || result == NOT_IMPLEMENTED) {
  397. m.setRcode(Rcode::SERVFAIL());
  398. return;
  399. } else if ((task->flags & CNAME_FOUND) != 0) {
  400. // The qname node contains a CNAME. Add a new task to the
  401. // queue to look up its target.
  402. if (RRsetPtr rrset = data[RRType::CNAME()]) {
  403. m.addRRset(task->section, rrset, q.wantDnssec());
  404. chaseCname(q, task, rrset);
  405. }
  406. continue;
  407. } else if ((task->flags & REFERRAL) != 0) {
  408. // The qname node contains an out-of-zone referral.
  409. if (task->state == QueryTask::GETANSWER) {
  410. RRsetList auth;
  411. m.clearHeaderFlag(MessageFlag::AA());
  412. if (!refQuery(task->qname, q, task, datasource, zonename,
  413. auth)) {
  414. m.setRcode(Rcode::SERVFAIL());
  415. return;
  416. }
  417. BOOST_FOREACH (RRsetPtr rrset, auth) {
  418. if (rrset->getType() == task->qtype) {
  419. m.addRRset(Section::ANSWER(), rrset, q.wantDnssec());
  420. } else if (rrset->getType() == RRType::DS() &&
  421. q.wantDnssec()) {
  422. m.addRRset(Section::AUTHORITY(), rrset, true);
  423. }
  424. getAdditional(q, rrset);
  425. }
  426. }
  427. continue;
  428. } else if ((task->flags & (NAME_NOT_FOUND|TYPE_NOT_FOUND)) != 0) {
  429. // No data found at this qname/qtype.
  430. // If we were looking for answer data, not additional,
  431. // and the name was not found, we need to find out whether
  432. // there are any relevant wildcards.
  433. bool wildcard_found = false;
  434. result = tryWildcard(q, task, datasource, zonename, wildcard_found);
  435. if (result != SUCCESS) {
  436. m.setRcode(Rcode::SERVFAIL());
  437. return;
  438. }
  439. if (wildcard_found) {
  440. continue;
  441. }
  442. // If we've reached this point, there is definitely no answer.
  443. // If we were chasing cnames or adding additional data, that's
  444. // okay, but if we were doing an original query, reply with the
  445. // SOA in the authority section. For NAME_NOT_FOUND, set
  446. // NXDOMAIN, and also add the previous NSEC to the authority
  447. // section. For TYPE_NOT_FOUND, do not set an error rcode,
  448. // and send the current NSEC in the authority section.
  449. Name nsecname(task->qname);
  450. if ((task->flags & NAME_NOT_FOUND) != 0) {
  451. datasource->findPreviousName(q, task->qname, nsecname,
  452. zonename);
  453. }
  454. if (task->state == QueryTask::GETANSWER) {
  455. if ((task->flags & NAME_NOT_FOUND) != 0) {
  456. m.setRcode(Rcode::NXDOMAIN());
  457. }
  458. RRsetList soa;
  459. QueryTask newtask(*zonename, task->qclass, RRType::SOA(),
  460. QueryTask::SIMPLE_QUERY);
  461. result = doQueryTask(datasource, zonename, q, newtask, soa);
  462. if (result != SUCCESS || newtask.flags != 0) {
  463. m.setRcode(Rcode::SERVFAIL());
  464. return;
  465. }
  466. m.addRRset(Section::AUTHORITY(), soa[RRType::SOA()],
  467. q.wantDnssec());
  468. }
  469. if (q.wantDnssec()) {
  470. RRsetList nsec;
  471. QueryTask newtask(nsecname, task->qclass,
  472. RRType::NSEC(), QueryTask::SIMPLE_QUERY);
  473. result = doQueryTask(datasource, zonename, q, newtask, nsec);
  474. if (result != SUCCESS) {
  475. m.setRcode(Rcode::SERVFAIL());
  476. return;
  477. }
  478. if (newtask.flags == 0) {
  479. m.addRRset(Section::AUTHORITY(), nsec[RRType::NSEC()],
  480. true);
  481. }
  482. }
  483. return;
  484. } else {
  485. // Should never be reached!
  486. m.setRcode(Rcode::SERVFAIL());
  487. return;
  488. }
  489. }
  490. // We're done, so now copy in the additional data:
  491. // data first, then signatures. (If we run out of
  492. // space, signatures in additional section are
  493. // optional.)
  494. BOOST_FOREACH(RRsetPtr rrset, additional) {
  495. m.addRRset(Section::ADDITIONAL(), rrset, false);
  496. }
  497. if (q.wantDnssec()) {
  498. BOOST_FOREACH(RRsetPtr rrset, additional) {
  499. if (rrset->getRRsig()) {
  500. m.addRRset(Section::ADDITIONAL(), rrset->getRRsig(), false);
  501. }
  502. }
  503. }
  504. }
  505. DataSrc::Result
  506. DataSrc::findAddrs(const Query& q, const Name& qname, const RRClass& qclass,
  507. RRsetList& target, uint32_t& flags,
  508. const Name* zonename) const
  509. {
  510. Result r;
  511. bool a = false, aaaa = false;
  512. flags = 0;
  513. r = findExactRRset(q, qname, qclass, RRType::A(), target, flags, zonename);
  514. if (r == SUCCESS && flags == 0) {
  515. a = true;
  516. }
  517. flags = 0;
  518. r = findExactRRset(q, qname, qclass, RRType::AAAA(), target, flags,
  519. zonename);
  520. if (r == SUCCESS && flags == 0) {
  521. aaaa = true;
  522. }
  523. if (!a && !aaaa) {
  524. flags = TYPE_NOT_FOUND;
  525. } else {
  526. flags = 0;
  527. }
  528. return (SUCCESS);
  529. }
  530. DataSrc::Result
  531. DataSrc::findReferral(const Query& q, const Name& qname, const RRClass& qclass,
  532. RRsetList& target, uint32_t& flags,
  533. const Name* zonename) const
  534. {
  535. Result r;
  536. bool ns = false, ds = false, dname = false;
  537. flags = 0;
  538. r = findExactRRset(q, qname, qclass, RRType::NS(), target, flags, zonename);
  539. if (r == SUCCESS && flags == 0) {
  540. ns = true;
  541. } else if ((flags & (NO_SUCH_ZONE|NAME_NOT_FOUND))) {
  542. return (SUCCESS);
  543. }
  544. flags = 0;
  545. r = findExactRRset(q, qname, qclass, RRType::DS(), target, flags, zonename);
  546. if (r == SUCCESS && flags == 0) {
  547. ds = true;
  548. } else if ((flags & (NO_SUCH_ZONE|NAME_NOT_FOUND))) {
  549. return (SUCCESS);
  550. }
  551. flags = 0;
  552. r = findExactRRset(q, qname, qclass, RRType::DNAME(), target, flags,
  553. zonename);
  554. if (r == SUCCESS && flags == 0) {
  555. dname = true;
  556. } else if ((flags & (NO_SUCH_ZONE|NAME_NOT_FOUND))) {
  557. return (SUCCESS);
  558. }
  559. if (!ns && !dname && !ds) {
  560. flags = TYPE_NOT_FOUND;
  561. } else {
  562. flags = 0;
  563. }
  564. return (SUCCESS);
  565. }
  566. void
  567. MetaDataSrc::addDataSrc(ConstDataSrcPtr data_src)
  568. {
  569. if (getClass() != RRClass::ANY() && data_src->getClass() != getClass()) {
  570. dns_throw(Unexpected, "class mismatch");
  571. }
  572. data_sources.push_back(data_src);
  573. }
  574. void
  575. MetaDataSrc::findClosestEnclosure(NameMatch& match) const
  576. {
  577. BOOST_FOREACH (ConstDataSrcPtr data_src, data_sources) {
  578. if (getClass() != RRClass::ANY() &&
  579. data_src->getClass() != getClass()) {
  580. continue;
  581. }
  582. data_src->findClosestEnclosure(match);
  583. }
  584. }
  585. NameMatch::~NameMatch()
  586. {
  587. delete closest_name_;
  588. }
  589. void
  590. NameMatch::update(const DataSrc& new_source, const Name& container)
  591. {
  592. if (closest_name_ == NULL) {
  593. closest_name_ = new Name(container);
  594. best_source_ = &new_source;
  595. return;
  596. }
  597. if (container.compare(*closest_name_).getRelation() ==
  598. NameComparisonResult::SUBDOMAIN) {
  599. Name* newname = new Name(container);
  600. delete closest_name_;
  601. closest_name_ = newname;
  602. best_source_ = &new_source;
  603. }
  604. }
  605. Nsec3Param::Nsec3Param(uint8_t a, uint8_t f, uint16_t i,
  606. std::vector<uint8_t>& s) :
  607. algorithm(a), flags(f), iterations(i), salt(s)
  608. {}
  609. string
  610. Nsec3Param::getHash(const Name& name) const {
  611. OutputBuffer buf(0);
  612. name.toWire(buf);
  613. buf.writeData(&salt[0], salt.size());
  614. uint8_t* in = (uint8_t*) buf.getData();
  615. size_t inlength = buf.getLength();
  616. uint8_t digest[SHA1_HASHSIZE];
  617. int n = 0;
  618. SHA1Context sha;
  619. do {
  620. SHA1Reset(&sha);
  621. SHA1Input(&sha, in, inlength);
  622. SHA1Result(&sha, digest);
  623. in = digest;
  624. inlength = SHA1_HASHSIZE;
  625. } while (n++ < iterations);
  626. vector<uint8_t> result;
  627. for (int i = 0; i < SHA1_HASHSIZE; ++i) {
  628. result.push_back(digest[i]);
  629. }
  630. return (encodeBase32(result));
  631. }
  632. }
  633. }