data_source.cc 27 KB

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