data_source.cc 33 KB

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