data_source.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 <iomanip>
  17. #include <iostream>
  18. #include <vector>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/foreach.hpp>
  21. #include <dns/base32.h>
  22. #include <dns/buffer.h>
  23. #include <dns/message.h>
  24. #include <dns/name.h>
  25. #include <dns/rdataclass.h>
  26. #include <dns/rrset.h>
  27. #include <dns/rrsetlist.h>
  28. #include <dns/sha1.h>
  29. #include <cc/data.h>
  30. #include "data_source.h"
  31. #include "query.h"
  32. #define RETERR(x) do { \
  33. DataSrc::Result r = (x); \
  34. if (r != DataSrc::SUCCESS) \
  35. return (r); \
  36. } while (0)
  37. using namespace std;
  38. using namespace isc::dns;
  39. using namespace isc::dns::rdata;
  40. namespace isc {
  41. namespace auth {
  42. typedef boost::shared_ptr<const Nsec3Param> ConstNsec3ParamPtr;
  43. // Add a task to the query task queue to look up additional data
  44. // (i.e., address records for the names included in NS or MX records)
  45. static void
  46. getAdditional(Query& q, RRsetPtr rrset) {
  47. if (!q.wantAdditional()) {
  48. return;
  49. }
  50. RdataIteratorPtr it = rrset->getRdataIterator();
  51. for (it->first(); !it->isLast(); it->next()) {
  52. const Rdata& rd(it->getCurrent());
  53. QueryTaskPtr newtask = QueryTaskPtr();
  54. if (rrset->getType() == RRType::NS()) {
  55. const generic::NS& ns = dynamic_cast<const generic::NS&>(rd);
  56. newtask = QueryTaskPtr(new QueryTask(ns.getNSName(), q.qclass(),
  57. Section::ADDITIONAL(),
  58. QueryTask::GLUE_QUERY,
  59. QueryTask::GETADDITIONAL));
  60. } else if (rrset->getType() == RRType::MX()) {
  61. const generic::MX& mx = dynamic_cast<const generic::MX&>(rd);
  62. newtask = QueryTaskPtr(new QueryTask(mx.getMXName(), q.qclass(),
  63. Section::ADDITIONAL(),
  64. QueryTask::NOGLUE_QUERY,
  65. QueryTask::GETADDITIONAL));
  66. }
  67. if (newtask) {
  68. q.tasks().push(newtask);
  69. }
  70. }
  71. }
  72. // Synthesize a CNAME answer, for the benefit of clients that don't
  73. // understand DNAME
  74. static void
  75. synthesizeCname(Query& q, QueryTaskPtr task, RRsetPtr rrset, RRsetList& target)
  76. {
  77. RdataIteratorPtr it = rrset->getRdataIterator();
  78. // More than one DNAME RR in the RRset is illegal, so we only have
  79. // to process the first one.
  80. it->first();
  81. if (it->isLast()) {
  82. return;
  83. }
  84. const Rdata& rd(it->getCurrent());
  85. const generic::DNAME& dname = dynamic_cast<const generic::DNAME&>(rd);
  86. const Name& dname_target(dname.getDname());
  87. RRsetPtr cname(new RRset(task->qname, task->qclass, RRType::CNAME(),
  88. rrset->getTTL()));
  89. const int qnlen = task->qname.getLabelCount();
  90. const int dnlen = rrset->getName().getLabelCount();
  91. assert(qnlen > dnlen);
  92. const Name& prefix(task->qname.split(0, qnlen - dnlen));
  93. cname->addRdata(generic::CNAME(prefix.concatenate(dname_target)));
  94. target.addRRset(cname);
  95. }
  96. // Add a task to the query task queue to look up the data pointed
  97. // to by a CNAME record
  98. static void
  99. chaseCname(Query& q, QueryTaskPtr task, RRsetPtr rrset)
  100. {
  101. RdataIteratorPtr it = rrset->getRdataIterator();
  102. // More than one CNAME RR in the RRset is illegal, so we only have
  103. // to process the first one.
  104. it->first();
  105. if (it->isLast()) {
  106. return;
  107. }
  108. q.tasks().push(QueryTaskPtr(
  109. new QueryTask(dynamic_cast<const generic::CNAME&>
  110. (it->getCurrent()).getCname(),
  111. task->qclass,
  112. task->qtype,
  113. Section::ANSWER(),
  114. QueryTask::FOLLOWCNAME)));
  115. }
  116. // Perform the query specified in a QueryTask object
  117. static DataSrc::Result
  118. doQueryTask(const DataSrc* ds, const Name* zonename, Query& q, QueryTask& task,
  119. RRsetList& target)
  120. {
  121. switch (task.op) {
  122. case QueryTask::AUTH_QUERY:
  123. return (ds->findRRset(q, task.qname, task.qclass, task.qtype,
  124. target, task.flags, zonename));
  125. case QueryTask::SIMPLE_QUERY:
  126. return (ds->findExactRRset(q, task.qname, task.qclass, task.qtype,
  127. target, task.flags, zonename));
  128. case QueryTask::GLUE_QUERY:
  129. case QueryTask::NOGLUE_QUERY:
  130. return (ds->findAddrs(q, task.qname, task.qclass, target,
  131. task.flags, zonename));
  132. case QueryTask::REF_QUERY:
  133. return (ds->findReferral(q, task.qname, task.qclass, target,
  134. task.flags, zonename));
  135. }
  136. // Not reached
  137. return (DataSrc::ERROR);
  138. }
  139. // Copy referral information into the authority section of a message
  140. static inline void
  141. copyAuth(Query& q, RRsetList& auth)
  142. {
  143. BOOST_FOREACH(RRsetPtr rrset, auth) {
  144. if (rrset->getType() == RRType::DNAME()) {
  145. continue;
  146. }
  147. if (rrset->getType() == RRType::DS() && !q.wantDnssec()) {
  148. continue;
  149. }
  150. q.message().addRRset(Section::AUTHORITY(), rrset, q.wantDnssec());
  151. getAdditional(q, rrset);
  152. }
  153. }
  154. // Query for referrals (i.e., NS/DS or DNAME) at a given name
  155. static inline bool
  156. refQuery(const Name& name, Query& q, QueryTaskPtr task,
  157. const DataSrc* ds, const Name* zonename, RRsetList& target)
  158. {
  159. QueryTask newtask(name, q.qclass(), QueryTask::REF_QUERY);
  160. if (doQueryTask(ds, zonename, q, newtask, target) != DataSrc::SUCCESS) {
  161. // Lookup failed
  162. return (false);
  163. }
  164. // Referral bit is expected, so clear it when checking flags
  165. if ((newtask.flags & ~DataSrc::REFERRAL) != 0) {
  166. return (false);
  167. }
  168. return (true);
  169. }
  170. // Match downward, from the zone apex to the query name, looking for
  171. // referrals.
  172. static inline bool
  173. hasDelegation(const DataSrc* ds, const Name* zonename, Query& q,
  174. QueryTaskPtr task)
  175. {
  176. int nlen = task->qname.getLabelCount();
  177. int diff = nlen - zonename->getLabelCount();
  178. if (diff > 1) {
  179. bool found = false;
  180. RRsetList ref;
  181. for (int i = diff; i > 1; --i) {
  182. const Name sub(task->qname.split(i - 1, nlen - i));
  183. if (refQuery(sub, q, task, ds, zonename, ref)) {
  184. found = true;
  185. break;
  186. }
  187. }
  188. // Found a referral while getting additional data
  189. // for something other than NS; we skip it.
  190. if (found && task->op == QueryTask::NOGLUE_QUERY) {
  191. return (true);
  192. }
  193. // Found a referral while getting answer data;
  194. // send a delegation.
  195. if (found) {
  196. if (RRsetPtr r = ref[RRType::DNAME()]) {
  197. RRsetList syn;
  198. q.message().addRRset(Section::ANSWER(), r, q.wantDnssec());
  199. q.message().setHeaderFlag(MessageFlag::AA());
  200. synthesizeCname(q, task, r, syn);
  201. if (syn.size() == 1) {
  202. q.message().addRRset(Section::ANSWER(),
  203. syn[RRType::CNAME()],
  204. q.wantDnssec());
  205. chaseCname(q, task, syn[RRType::CNAME()]);
  206. return (true);
  207. }
  208. }
  209. copyAuth(q, ref);
  210. return (true);
  211. }
  212. }
  213. // We appear to have authoritative data; set the header
  214. // flag. (We may clear it later if we find a referral
  215. // at the actual qname node.)
  216. if (task->op == QueryTask::AUTH_QUERY &&
  217. task->state == QueryTask::GETANSWER) {
  218. q.message().setHeaderFlag(MessageFlag::AA());
  219. }
  220. return (false);
  221. }
  222. static inline DataSrc::Result
  223. addSOA(Query& q, const Name* zonename, const DataSrc* ds) {
  224. Message& m = q.message();
  225. RRsetList soa;
  226. QueryTask newtask(*zonename, q.qclass(), RRType::SOA(),
  227. QueryTask::SIMPLE_QUERY);
  228. RETERR(doQueryTask(ds, zonename, q, newtask, soa));
  229. if (newtask.flags != 0) {
  230. return (DataSrc::ERROR);
  231. }
  232. m.addRRset(Section::AUTHORITY(), soa[RRType::SOA()], q.wantDnssec());
  233. return (DataSrc::SUCCESS);
  234. }
  235. static inline DataSrc::Result
  236. addNSEC(Query& q, const QueryTaskPtr task, const Name& name,
  237. const Name& zonename, const DataSrc* ds)
  238. {
  239. RRsetList nsec;
  240. Message& m = q.message();
  241. QueryTask newtask(name, task->qclass, RRType::NSEC(),
  242. QueryTask::SIMPLE_QUERY);
  243. RETERR(doQueryTask(ds, &zonename, q, newtask, nsec));
  244. if (newtask.flags == 0) {
  245. m.addRRset(Section::AUTHORITY(), nsec[RRType::NSEC()], true);
  246. }
  247. return (DataSrc::SUCCESS);
  248. }
  249. static inline DataSrc::Result
  250. getNsec3(Query& q, const DataSrc* ds, const Name& zonename, string& hash,
  251. RRsetPtr& target)
  252. {
  253. RRsetList rl;
  254. RETERR(ds->findCoveringNSEC3(q, zonename, hash, rl));
  255. target = rl[RRType::NSEC3()];
  256. return (DataSrc::SUCCESS);
  257. }
  258. static ConstNsec3ParamPtr
  259. getNsec3Param(Query& q, const DataSrc* ds, const Name& zonename) {
  260. DataSrc::Result result;
  261. RRsetList nsec3param;
  262. QueryTask newtask(zonename, q.qclass(), RRType::NSEC3PARAM(),
  263. QueryTask::SIMPLE_QUERY);
  264. result = doQueryTask(ds, &zonename, q, newtask, nsec3param);
  265. newtask.flags &= ~DataSrc::REFERRAL;
  266. if (result != DataSrc::SUCCESS || newtask.flags != 0) {
  267. return (ConstNsec3ParamPtr());
  268. }
  269. RRsetPtr rrset = nsec3param[RRType::NSEC3PARAM()];
  270. if (!rrset) {
  271. return (ConstNsec3ParamPtr());
  272. }
  273. // XXX: currently only one NSEC3 chain per zone is supported;
  274. // we will need to revisit this.
  275. RdataIteratorPtr it = rrset->getRdataIterator();
  276. it->first();
  277. if (it->isLast()) {
  278. return (ConstNsec3ParamPtr());
  279. }
  280. const generic::NSEC3PARAM& np =
  281. dynamic_cast<const generic::NSEC3PARAM&>(it->getCurrent());
  282. return (ConstNsec3ParamPtr(new Nsec3Param(np.getHashalg(), np.getFlags(),
  283. np.getIterations(),
  284. np.getSalt())));
  285. }
  286. static inline DataSrc::Result
  287. proveNX(Query& q, QueryTaskPtr task, const DataSrc* ds, const Name& zonename)
  288. {
  289. Message& m = q.message();
  290. ConstNsec3ParamPtr nsec3 = getNsec3Param(q, ds, zonename);
  291. if (nsec3 != NULL) {
  292. // Attach the NSEC3 record covering the QNAME
  293. RRsetPtr rrset;
  294. string hash1(nsec3->getHash(task->qname)), hash2;
  295. RETERR(getNsec3(q, ds, zonename, hash1, rrset));
  296. m.addRRset(Section::AUTHORITY(), rrset, true);
  297. // If this is an NXRRSET or NOERROR/NODATA, we're done
  298. if ((task->flags & DataSrc::TYPE_NOT_FOUND) != 0) {
  299. return (DataSrc::SUCCESS);
  300. }
  301. // Find the closest provable enclosing name for QNAME
  302. Name enclosure(zonename);
  303. int nlen = task->qname.getLabelCount();
  304. int diff = nlen - enclosure.getLabelCount();
  305. for (int i = 1; i <= diff; ++i) {
  306. enclosure = task->qname.split(i, nlen - i);
  307. string nodehash(nsec3->getHash(enclosure));
  308. if (nodehash == hash1) {
  309. break;
  310. }
  311. hash2 = nodehash;
  312. RRsetList rl;
  313. // hash2 will be overwritten with the actual hash found;
  314. // we don't want to use one until we find an exact match
  315. RETERR(getNsec3(q, ds, zonename, hash2, rrset));
  316. if (hash2 == nodehash) {
  317. m.addRRset(Section::AUTHORITY(), rrset, true);
  318. break;
  319. }
  320. }
  321. // Now add a covering NSEC3 for a wildcard under the
  322. // closest provable enclosing name
  323. string hash3(nsec3->getHash(Name("*").concatenate(enclosure)));
  324. RETERR(getNsec3(q, ds, zonename, hash3, rrset));
  325. if (hash3 != hash1 && hash3 != hash2) {
  326. m.addRRset(Section::AUTHORITY(), rrset, true);
  327. }
  328. } else {
  329. Name nsecname(task->qname);
  330. if ((task->flags & DataSrc::NAME_NOT_FOUND) != 0) {
  331. ds->findPreviousName(q, task->qname, nsecname, &zonename);
  332. }
  333. RETERR(addNSEC(q, task, nsecname, zonename, ds));
  334. if ((task->flags & DataSrc::TYPE_NOT_FOUND) != 0 ||
  335. nsecname == zonename)
  336. {
  337. return (DataSrc::SUCCESS);
  338. }
  339. RETERR(addNSEC(q, task, zonename, zonename, ds));
  340. }
  341. return (DataSrc::SUCCESS);
  342. }
  343. // Attempt a wildcard lookup
  344. static inline DataSrc::Result
  345. tryWildcard(Query& q, QueryTaskPtr task, const DataSrc* ds,
  346. const Name* zonename, bool& found)
  347. {
  348. Message& m = q.message();
  349. DataSrc::Result result;
  350. found = false;
  351. if ((task->flags & DataSrc::NAME_NOT_FOUND) == 0 ||
  352. (task->state != QueryTask::GETANSWER &&
  353. task->state != QueryTask::FOLLOWCNAME)) {
  354. return (DataSrc::SUCCESS);
  355. }
  356. int nlen = task->qname.getLabelCount();
  357. int diff = nlen - zonename->getLabelCount();
  358. if (diff < 1) {
  359. return (DataSrc::SUCCESS);
  360. }
  361. RRsetList wild;
  362. Name star("*");
  363. uint32_t rflags = 0;
  364. for (int i = 1; i <= diff; ++i) {
  365. const Name& wname(star.concatenate(task->qname.split(i, nlen - i)));
  366. QueryTask newtask(wname, task->qclass, task->qtype,
  367. QueryTask::SIMPLE_QUERY);
  368. result = doQueryTask(ds, zonename, q, newtask, wild);
  369. if (result == DataSrc::SUCCESS &&
  370. (newtask.flags == 0 || (newtask.flags & DataSrc::CNAME_FOUND))) {
  371. rflags = newtask.flags;
  372. found = true;
  373. break;
  374. }
  375. }
  376. // A wildcard was found. Add the data to the answer
  377. // section (but with the name changed to match the
  378. // qname), and then continue as if this were a normal
  379. // answer: if a CNAME, chase the target, otherwise
  380. // add authority.
  381. if (found) {
  382. if ((rflags & DataSrc::CNAME_FOUND) != 0) {
  383. if (RRsetPtr rrset = wild[RRType::CNAME()]) {
  384. rrset->setName(task->qname);
  385. m.addRRset(Section::ANSWER(), rrset, q.wantDnssec());
  386. chaseCname(q, task, rrset);
  387. }
  388. } else {
  389. BOOST_FOREACH (RRsetPtr rrset, wild) {
  390. rrset->setName(task->qname);
  391. m.addRRset(Section::ANSWER(), rrset, q.wantDnssec());
  392. }
  393. RRsetList auth;
  394. if (! refQuery(*zonename, q, task, ds, zonename, auth)) {
  395. return (DataSrc::ERROR);
  396. }
  397. copyAuth(q, auth);
  398. }
  399. }
  400. return (DataSrc::SUCCESS);
  401. }
  402. //
  403. // doQuery: Processes a query.
  404. //
  405. void
  406. DataSrc::doQuery(Query& q)
  407. {
  408. Message& m = q.message();
  409. vector<RRsetPtr> additional;
  410. m.clearHeaderFlag(MessageFlag::AA());
  411. while (!q.tasks().empty()) {
  412. QueryTaskPtr task = q.tasks().front();
  413. q.tasks().pop();
  414. // These task types should never be on the task queue.
  415. if (task->op == QueryTask::SIMPLE_QUERY ||
  416. task->op == QueryTask::REF_QUERY) {
  417. m.setRcode(Rcode::SERVFAIL());
  418. return;
  419. }
  420. // Find the closest enclosing zone for which we are authoritative,
  421. // and the concrete data source which is authoritative for it.
  422. // (Note that RRtype DS queries need to go to the parent.)
  423. NameMatch match(task->qtype == RRType::DS() ?
  424. task->qname.split(1, task->qname.getLabelCount() - 1) :
  425. task->qname);
  426. findClosestEnclosure(match, task->qclass);
  427. const DataSrc* datasource = match.bestDataSrc();
  428. const Name* zonename = match.closestName();
  429. assert((datasource == NULL && zonename == NULL) ||
  430. (datasource != NULL && zonename != NULL));
  431. RRsetList data;
  432. Result result = SUCCESS;
  433. if (datasource) {
  434. // For these query task types, if there is more than
  435. // one level between the zone name and qname, we need to
  436. // check the intermediate nodes for referrals.
  437. if ((task->op == QueryTask::AUTH_QUERY ||
  438. task->op == QueryTask::NOGLUE_QUERY) &&
  439. hasDelegation(datasource, zonename, q, task)) {
  440. continue;
  441. }
  442. result = doQueryTask(datasource, zonename, q, *task, data);
  443. if (result != SUCCESS) {
  444. m.setRcode(Rcode::SERVFAIL());
  445. return;
  446. }
  447. // Query found a referral; let's find out if that was expected--
  448. // i.e., if an NS was at the zone apex, or if we were querying
  449. // specifically for the DS or DNAME record.
  450. if ((task->flags & REFERRAL) != 0 &&
  451. (zonename->getLabelCount() == task->qname.getLabelCount() ||
  452. task->qtype == RRType::DS() ||
  453. task->qtype == RRType::DNAME())) {
  454. task->flags &= ~REFERRAL;
  455. }
  456. } else {
  457. task->flags = NO_SUCH_ZONE;
  458. // No such zone. If we're chasing cnames or adding additional
  459. // data, that's okay, but if doing an original query, return
  460. // REFUSED.
  461. if (task->state == QueryTask::GETANSWER) {
  462. m.setRcode(Rcode::REFUSED());
  463. return;
  464. }
  465. continue;
  466. }
  467. if (result == SUCCESS && task->flags == 0) {
  468. bool have_ns = false, need_auth = false;
  469. switch (task->state) {
  470. case QueryTask::GETANSWER:
  471. case QueryTask::FOLLOWCNAME:
  472. BOOST_FOREACH(RRsetPtr rrset, data) {
  473. m.addRRset(task->section, rrset, q.wantDnssec());
  474. if (q.tasks().empty()) {
  475. need_auth = true;
  476. }
  477. getAdditional(q, rrset);
  478. if (rrset->getType() == RRType::NS()) {
  479. have_ns = true;
  480. }
  481. }
  482. q.setStatus(Query::ANSWERED);
  483. if (need_auth && !have_ns) {
  484. // Data found, no additional processing needed.
  485. // Add the NS records for the enclosing zone to
  486. // the authority section.
  487. RRsetList auth;
  488. if (! refQuery(*zonename, q, task, datasource,
  489. zonename, auth)) {
  490. m.setRcode(Rcode::SERVFAIL());
  491. return;
  492. }
  493. copyAuth(q, auth);
  494. }
  495. continue;
  496. case QueryTask::GETADDITIONAL:
  497. // Got additional data. Do not add it to the message
  498. // yet; instead store it and copy it in at the end
  499. // (this allow RRSIGs to be omitted if necessary).
  500. BOOST_FOREACH(RRsetPtr rrset, data) {
  501. if (q.status() == Query::ANSWERED &&
  502. rrset->getName() == q.qname() &&
  503. rrset->getType() == q.qtype()) {
  504. continue;
  505. }
  506. additional.push_back(rrset);
  507. }
  508. continue;
  509. default:
  510. isc_throw (Unexpected, "unexpected query state");
  511. }
  512. } else if (result == ERROR || result == NOT_IMPLEMENTED) {
  513. m.setRcode(Rcode::SERVFAIL());
  514. return;
  515. } else if ((task->flags & CNAME_FOUND) != 0) {
  516. // The qname node contains a CNAME. Add a new task to the
  517. // queue to look up its target.
  518. if (RRsetPtr rrset = data[RRType::CNAME()]) {
  519. m.addRRset(task->section, rrset, q.wantDnssec());
  520. chaseCname(q, task, rrset);
  521. }
  522. continue;
  523. } else if ((task->flags & REFERRAL) != 0) {
  524. // The qname node contains an out-of-zone referral.
  525. if (task->state == QueryTask::GETANSWER) {
  526. RRsetList auth;
  527. m.clearHeaderFlag(MessageFlag::AA());
  528. if (!refQuery(task->qname, q, task, datasource, zonename,
  529. auth)) {
  530. m.setRcode(Rcode::SERVFAIL());
  531. return;
  532. }
  533. BOOST_FOREACH (RRsetPtr rrset, auth) {
  534. if (rrset->getType() == RRType::NS()) {
  535. m.addRRset(Section::AUTHORITY(), rrset, q.wantDnssec());
  536. } else if (rrset->getType() == task->qtype) {
  537. m.addRRset(Section::ANSWER(), rrset, q.wantDnssec());
  538. } else if (rrset->getType() == RRType::DS() &&
  539. q.wantDnssec()) {
  540. m.addRRset(Section::AUTHORITY(), rrset, true);
  541. }
  542. getAdditional(q, rrset);
  543. }
  544. }
  545. continue;
  546. } else if ((task->flags & (NAME_NOT_FOUND|TYPE_NOT_FOUND)) != 0) {
  547. // No data found at this qname/qtype.
  548. // If we were looking for answer data, not additional,
  549. // and the name was not found, we need to find out whether
  550. // there are any relevant wildcards.
  551. bool wildcard_found = false;
  552. result = tryWildcard(q, task, datasource, zonename, wildcard_found);
  553. if (result != SUCCESS) {
  554. m.setRcode(Rcode::SERVFAIL());
  555. return;
  556. }
  557. if (wildcard_found) {
  558. continue;
  559. }
  560. // If we've reached this point, there is definitely no answer.
  561. // If we were chasing cnames or adding additional data, that's
  562. // okay, but if we were doing an original query, reply with the
  563. // SOA in the authority section. For NAME_NOT_FOUND, set
  564. // NXDOMAIN, and also add the previous NSEC to the authority
  565. // section. For TYPE_NOT_FOUND, do not set an error rcode,
  566. // and send the current NSEC in the authority section.
  567. if (task->state == QueryTask::GETANSWER) {
  568. if ((task->flags & NAME_NOT_FOUND) != 0) {
  569. m.setRcode(Rcode::NXDOMAIN());
  570. }
  571. result = addSOA(q, zonename, datasource);
  572. if (result != SUCCESS) {
  573. m.setRcode(Rcode::SERVFAIL());
  574. return;
  575. }
  576. }
  577. Name nsecname(task->qname);
  578. if ((task->flags & NAME_NOT_FOUND) != 0) {
  579. datasource->findPreviousName(q, task->qname, nsecname,
  580. zonename);
  581. }
  582. if (q.wantDnssec()) {
  583. result = proveNX(q, task, datasource, *zonename);
  584. if (result != DataSrc::SUCCESS) {
  585. m.setRcode(Rcode::SERVFAIL());
  586. return;
  587. }
  588. }
  589. return;
  590. } else {
  591. // Should never be reached!
  592. m.setRcode(Rcode::SERVFAIL());
  593. return;
  594. }
  595. }
  596. // We're done, so now copy in the additional data:
  597. // data first, then signatures. (If we run out of
  598. // space, signatures in additional section are
  599. // optional.)
  600. BOOST_FOREACH(RRsetPtr rrset, additional) {
  601. m.addRRset(Section::ADDITIONAL(), rrset, false);
  602. }
  603. if (q.wantDnssec()) {
  604. BOOST_FOREACH(RRsetPtr rrset, additional) {
  605. if (rrset->getRRsig()) {
  606. m.addRRset(Section::ADDITIONAL(), rrset->getRRsig(), false);
  607. }
  608. }
  609. }
  610. }
  611. DataSrc::Result
  612. DataSrc::findAddrs(const Query& q, const Name& qname, const RRClass& qclass,
  613. RRsetList& target, uint32_t& flags,
  614. const Name* zonename) const
  615. {
  616. Result r;
  617. bool a = false, aaaa = false;
  618. flags = 0;
  619. r = findExactRRset(q, qname, qclass, RRType::A(), target, flags, zonename);
  620. if (r == SUCCESS && flags == 0) {
  621. a = true;
  622. }
  623. flags = 0;
  624. r = findExactRRset(q, qname, qclass, RRType::AAAA(), target, flags,
  625. zonename);
  626. if (r == SUCCESS && flags == 0) {
  627. aaaa = true;
  628. }
  629. if (!a && !aaaa) {
  630. flags = TYPE_NOT_FOUND;
  631. } else {
  632. flags = 0;
  633. }
  634. return (SUCCESS);
  635. }
  636. DataSrc::Result
  637. DataSrc::findReferral(const Query& q, const Name& qname, const RRClass& qclass,
  638. RRsetList& target, uint32_t& flags,
  639. const Name* zonename) const
  640. {
  641. Result r;
  642. bool ns = false, ds = false, dname = false;
  643. flags = 0;
  644. r = findExactRRset(q, qname, qclass, RRType::NS(), target, flags, zonename);
  645. if (r == SUCCESS && flags == 0) {
  646. ns = true;
  647. } else if ((flags & (NO_SUCH_ZONE|NAME_NOT_FOUND))) {
  648. return (SUCCESS);
  649. }
  650. flags = 0;
  651. r = findExactRRset(q, qname, qclass, RRType::DS(), target, flags, zonename);
  652. if (r == SUCCESS && flags == 0) {
  653. ds = true;
  654. } else if ((flags & (NO_SUCH_ZONE|NAME_NOT_FOUND))) {
  655. return (SUCCESS);
  656. }
  657. flags = 0;
  658. r = findExactRRset(q, qname, qclass, RRType::DNAME(), target, flags,
  659. zonename);
  660. if (r == SUCCESS && flags == 0) {
  661. dname = true;
  662. } else if ((flags & (NO_SUCH_ZONE|NAME_NOT_FOUND))) {
  663. return (SUCCESS);
  664. }
  665. if (!ns && !dname && !ds) {
  666. flags = TYPE_NOT_FOUND;
  667. } else {
  668. flags = 0;
  669. }
  670. return (SUCCESS);
  671. }
  672. void
  673. MetaDataSrc::addDataSrc(ConstDataSrcPtr data_src)
  674. {
  675. if (getClass() != RRClass::ANY() && data_src->getClass() != getClass()) {
  676. isc_throw(Unexpected, "class mismatch");
  677. }
  678. data_sources.push_back(data_src);
  679. }
  680. void
  681. MetaDataSrc::removeDataSrc(ConstDataSrcPtr data_src)
  682. {
  683. std::vector<ConstDataSrcPtr>::iterator it, itr;
  684. for (it = data_sources.begin(); it != data_sources.end(); it++) {
  685. if (*it == data_src) {
  686. itr = it;
  687. }
  688. }
  689. data_sources.erase(itr);
  690. }
  691. void
  692. MetaDataSrc::findClosestEnclosure(NameMatch& match, const RRClass& qclass) const
  693. {
  694. if (qclass == RRClass::ANY()) {
  695. isc_throw(Unexpected, "invalid query class");
  696. }
  697. if (getClass() != RRClass::ANY() && getClass() != qclass) {
  698. return;
  699. }
  700. BOOST_FOREACH (ConstDataSrcPtr data_src, data_sources) {
  701. if (data_src->getClass() != qclass) {
  702. continue;
  703. }
  704. data_src->findClosestEnclosure(match, qclass);
  705. }
  706. }
  707. NameMatch::~NameMatch()
  708. {
  709. delete closest_name_;
  710. }
  711. void
  712. NameMatch::update(const DataSrc& new_source, const Name& container)
  713. {
  714. if (closest_name_ == NULL) {
  715. closest_name_ = new Name(container);
  716. best_source_ = &new_source;
  717. return;
  718. }
  719. if (container.compare(*closest_name_).getRelation() ==
  720. NameComparisonResult::SUBDOMAIN) {
  721. Name* newname = new Name(container);
  722. delete closest_name_;
  723. closest_name_ = newname;
  724. best_source_ = &new_source;
  725. }
  726. }
  727. Nsec3Param::Nsec3Param(uint8_t a, uint8_t f, uint16_t i,
  728. const std::vector<uint8_t>& s) :
  729. algorithm_(a), flags_(f), iterations_(i), salt_(s)
  730. {}
  731. string
  732. Nsec3Param::getHash(const Name& name) const {
  733. OutputBuffer buf(0);
  734. name.toWire(buf);
  735. uint8_t digest[SHA1_HASHSIZE];
  736. uint8_t* input = (uint8_t*) buf.getData();
  737. size_t inlength = buf.getLength();
  738. uint8_t saltlen = salt_.size();
  739. uint8_t salt[saltlen];
  740. for (int i = 0; i < saltlen; ++i) {
  741. salt[i] = salt_[i];
  742. }
  743. int n = 0;
  744. SHA1Context sha;
  745. do {
  746. SHA1Reset(&sha);
  747. SHA1Input(&sha, input, inlength);
  748. SHA1Input(&sha, salt, saltlen);
  749. SHA1Result(&sha, digest);
  750. input = digest;
  751. inlength = SHA1_HASHSIZE;
  752. } while (n++ < iterations_);
  753. vector<uint8_t> result;
  754. for (int i = 0; i < SHA1_HASHSIZE; ++i) {
  755. result.push_back(digest[i]);
  756. }
  757. return (encodeBase32(result));
  758. }
  759. }
  760. }