auth_srv.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // Copyright (C) 2009 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. #include <config.h>
  15. #include <netinet/in.h>
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <iostream>
  19. #include <vector>
  20. #include <memory>
  21. #include <boost/bind.hpp>
  22. #include <asiolink/asiolink.h>
  23. #include <config/ccsession.h>
  24. #include <cc/data.h>
  25. #include <exceptions/exceptions.h>
  26. #include <util/buffer.h>
  27. #include <dns/edns.h>
  28. #include <dns/exceptions.h>
  29. #include <dns/messagerenderer.h>
  30. #include <dns/name.h>
  31. #include <dns/question.h>
  32. #include <dns/opcode.h>
  33. #include <dns/rcode.h>
  34. #include <dns/rrset.h>
  35. #include <dns/rrttl.h>
  36. #include <dns/message.h>
  37. #include <dns/tsig.h>
  38. #include <datasrc/query.h>
  39. #include <datasrc/data_source.h>
  40. #include <datasrc/memory_datasrc.h>
  41. #include <datasrc/static_datasrc.h>
  42. #include <datasrc/sqlite3_datasrc.h>
  43. #include <xfr/xfrout_client.h>
  44. #include <auth/common.h>
  45. #include <auth/auth_config.h>
  46. #include <auth/auth_srv.h>
  47. #include <auth/query.h>
  48. #include <auth/statistics.h>
  49. #include <auth/auth_log.h>
  50. using namespace std;
  51. using namespace isc;
  52. using namespace isc::cc;
  53. using namespace isc::datasrc;
  54. using namespace isc::dns;
  55. using namespace isc::util;
  56. using namespace isc::auth;
  57. using namespace isc::dns::rdata;
  58. using namespace isc::data;
  59. using namespace isc::config;
  60. using namespace isc::xfr;
  61. using namespace isc::asiolink;
  62. using namespace isc::asiodns;
  63. using namespace isc::server_common::portconfig;
  64. using boost::shared_ptr;
  65. class AuthSrvImpl {
  66. private:
  67. // prohibit copy
  68. AuthSrvImpl(const AuthSrvImpl& source);
  69. AuthSrvImpl& operator=(const AuthSrvImpl& source);
  70. public:
  71. AuthSrvImpl(const bool use_cache, AbstractXfroutClient& xfrout_client);
  72. ~AuthSrvImpl();
  73. isc::data::ConstElementPtr setDbFile(isc::data::ConstElementPtr config);
  74. bool processNormalQuery(const IOMessage& io_message, MessagePtr message,
  75. OutputBufferPtr buffer,
  76. auto_ptr<TSIGContext> tsig_context);
  77. bool processAxfrQuery(const IOMessage& io_message, MessagePtr message,
  78. OutputBufferPtr buffer,
  79. auto_ptr<TSIGContext> tsig_context);
  80. bool processNotify(const IOMessage& io_message, MessagePtr message,
  81. OutputBufferPtr buffer,
  82. auto_ptr<TSIGContext> tsig_context);
  83. IOService io_service_;
  84. /// Currently non-configurable, but will be.
  85. static const uint16_t DEFAULT_LOCAL_UDPSIZE = 4096;
  86. /// These members are public because AuthSrv accesses them directly.
  87. ModuleCCSession* config_session_;
  88. AbstractSession* xfrin_session_;
  89. /// In-memory data source. Currently class IN only for simplicity.
  90. const RRClass memory_datasrc_class_;
  91. AuthSrv::MemoryDataSrcPtr memory_datasrc_;
  92. /// Hot spot cache
  93. isc::datasrc::HotCache cache_;
  94. /// Interval timer for periodic submission of statistics counters.
  95. IntervalTimer statistics_timer_;
  96. /// Query counters for statistics
  97. AuthCounters counters_;
  98. /// Addresses we listen on
  99. AddressList listen_addresses_;
  100. /// The TSIG keyring
  101. const shared_ptr<TSIGKeyRing>* keyring_;
  102. private:
  103. std::string db_file_;
  104. MetaDataSrc data_sources_;
  105. /// We keep a pointer to the currently running sqlite datasource
  106. /// so that we can specifically remove that one should the database
  107. /// file change
  108. ConstDataSrcPtr cur_datasrc_;
  109. bool xfrout_connected_;
  110. AbstractXfroutClient& xfrout_client_;
  111. /// Increment query counter
  112. void incCounter(const int protocol);
  113. };
  114. AuthSrvImpl::AuthSrvImpl(const bool use_cache,
  115. AbstractXfroutClient& xfrout_client) :
  116. config_session_(NULL),
  117. xfrin_session_(NULL),
  118. memory_datasrc_class_(RRClass::IN()),
  119. statistics_timer_(io_service_),
  120. counters_(),
  121. keyring_(NULL),
  122. xfrout_connected_(false),
  123. xfrout_client_(xfrout_client)
  124. {
  125. // cur_datasrc_ is automatically initialized by the default constructor,
  126. // effectively being an empty (sqlite) data source. once ccsession is up
  127. // the datasource will be set by the configuration setting
  128. // add static data source
  129. data_sources_.addDataSrc(ConstDataSrcPtr(new StaticDataSrc));
  130. // enable or disable the cache
  131. cache_.setEnabled(use_cache);
  132. }
  133. AuthSrvImpl::~AuthSrvImpl() {
  134. if (xfrout_connected_) {
  135. xfrout_client_.disconnect();
  136. xfrout_connected_ = false;
  137. }
  138. }
  139. // This is a derived class of \c DNSLookup, to serve as a
  140. // callback in the asiolink module. It calls
  141. // AuthSrv::processMessage() on a single DNS message.
  142. class MessageLookup : public DNSLookup {
  143. public:
  144. MessageLookup(AuthSrv* srv) : server_(srv) {}
  145. virtual void operator()(const IOMessage& io_message,
  146. MessagePtr message,
  147. MessagePtr answer_message,
  148. OutputBufferPtr buffer,
  149. DNSServer* server) const
  150. {
  151. (void) answer_message;
  152. server_->processMessage(io_message, message, buffer, server);
  153. }
  154. private:
  155. AuthSrv* server_;
  156. };
  157. // This is a derived class of \c DNSAnswer, to serve as a callback in the
  158. // asiolink module. We actually shouldn't do anything in this class because
  159. // we build complete response messages in the process methods; otherwise
  160. // the response message will contain trailing garbage. In future, we should
  161. // probably even drop the reliance on DNSAnswer. We don't need the coroutine
  162. // tricks provided in that framework, and its overhead would be significant
  163. // in terms of performance consideration for the authoritative server
  164. // implementation.
  165. class MessageAnswer : public DNSAnswer {
  166. public:
  167. MessageAnswer(AuthSrv*) {}
  168. virtual void operator()(const IOMessage&, MessagePtr,
  169. MessagePtr, OutputBufferPtr) const
  170. {}
  171. };
  172. // This is a derived class of \c SimpleCallback, to serve
  173. // as a callback in the asiolink module. It checks for queued
  174. // configuration messages, and executes them if found.
  175. class ConfigChecker : public SimpleCallback {
  176. public:
  177. ConfigChecker(AuthSrv* srv) : server_(srv) {}
  178. virtual void operator()(const IOMessage&) const {
  179. if (server_->getConfigSession()->hasQueuedMsgs()) {
  180. server_->getConfigSession()->checkCommand();
  181. }
  182. }
  183. private:
  184. AuthSrv* server_;
  185. };
  186. AuthSrv::AuthSrv(const bool use_cache, AbstractXfroutClient& xfrout_client) :
  187. impl_(new AuthSrvImpl(use_cache, xfrout_client)),
  188. checkin_(new ConfigChecker(this)),
  189. dns_lookup_(new MessageLookup(this)),
  190. dns_answer_(new MessageAnswer(this))
  191. {}
  192. void
  193. AuthSrv::stop() {
  194. impl_->io_service_.stop();
  195. }
  196. AuthSrv::~AuthSrv() {
  197. delete impl_;
  198. delete checkin_;
  199. delete dns_lookup_;
  200. delete dns_answer_;
  201. }
  202. namespace {
  203. class QuestionInserter {
  204. public:
  205. QuestionInserter(MessagePtr message) : message_(message) {}
  206. void operator()(const QuestionPtr question) {
  207. message_->addQuestion(question);
  208. }
  209. MessagePtr message_;
  210. };
  211. void
  212. makeErrorMessage(MessagePtr message, OutputBufferPtr buffer,
  213. const Rcode& rcode,
  214. std::auto_ptr<TSIGContext> tsig_context =
  215. std::auto_ptr<TSIGContext>())
  216. {
  217. // extract the parameters that should be kept.
  218. // XXX: with the current implementation, it's not easy to set EDNS0
  219. // depending on whether the query had it. So we'll simply omit it.
  220. const qid_t qid = message->getQid();
  221. const bool rd = message->getHeaderFlag(Message::HEADERFLAG_RD);
  222. const bool cd = message->getHeaderFlag(Message::HEADERFLAG_CD);
  223. const Opcode& opcode = message->getOpcode();
  224. vector<QuestionPtr> questions;
  225. // If this is an error to a query or notify, we should also copy the
  226. // question section.
  227. if (opcode == Opcode::QUERY() || opcode == Opcode::NOTIFY()) {
  228. questions.assign(message->beginQuestion(), message->endQuestion());
  229. }
  230. message->clear(Message::RENDER);
  231. message->setQid(qid);
  232. message->setOpcode(opcode);
  233. message->setHeaderFlag(Message::HEADERFLAG_QR);
  234. if (rd) {
  235. message->setHeaderFlag(Message::HEADERFLAG_RD);
  236. }
  237. if (cd) {
  238. message->setHeaderFlag(Message::HEADERFLAG_CD);
  239. }
  240. for_each(questions.begin(), questions.end(), QuestionInserter(message));
  241. message->setRcode(rcode);
  242. MessageRenderer renderer(*buffer);
  243. if (tsig_context.get() != NULL) {
  244. message->toWire(renderer, *tsig_context);
  245. } else {
  246. message->toWire(renderer);
  247. }
  248. LOG_DEBUG(auth_logger, DBG_AUTH_MESSAGES, AUTH_SEND_ERROR_RESPONSE)
  249. .arg(message->toText());
  250. }
  251. }
  252. IOService&
  253. AuthSrv::getIOService() {
  254. return (impl_->io_service_);
  255. }
  256. void
  257. AuthSrv::setCacheSlots(const size_t slots) {
  258. impl_->cache_.setSlots(slots);
  259. }
  260. size_t
  261. AuthSrv::getCacheSlots() const {
  262. return (impl_->cache_.getSlots());
  263. }
  264. void
  265. AuthSrv::setXfrinSession(AbstractSession* xfrin_session) {
  266. impl_->xfrin_session_ = xfrin_session;
  267. }
  268. void
  269. AuthSrv::setConfigSession(ModuleCCSession* config_session) {
  270. impl_->config_session_ = config_session;
  271. }
  272. void
  273. AuthSrv::setStatisticsSession(AbstractSession* statistics_session) {
  274. impl_->counters_.setStatisticsSession(statistics_session);
  275. }
  276. ModuleCCSession*
  277. AuthSrv::getConfigSession() const {
  278. return (impl_->config_session_);
  279. }
  280. AuthSrv::MemoryDataSrcPtr
  281. AuthSrv::getMemoryDataSrc(const RRClass& rrclass) {
  282. // XXX: for simplicity, we only support the IN class right now.
  283. if (rrclass != impl_->memory_datasrc_class_) {
  284. isc_throw(InvalidParameter,
  285. "Memory data source is not supported for RR class "
  286. << rrclass);
  287. }
  288. return (impl_->memory_datasrc_);
  289. }
  290. void
  291. AuthSrv::setMemoryDataSrc(const isc::dns::RRClass& rrclass,
  292. MemoryDataSrcPtr memory_datasrc)
  293. {
  294. // XXX: see above
  295. if (rrclass != impl_->memory_datasrc_class_) {
  296. isc_throw(InvalidParameter,
  297. "Memory data source is not supported for RR class "
  298. << rrclass);
  299. } else if (!impl_->memory_datasrc_ && memory_datasrc) {
  300. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_MEM_DATASRC_ENABLED)
  301. .arg(rrclass);
  302. } else if (impl_->memory_datasrc_ && !memory_datasrc) {
  303. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_MEM_DATASRC_DISABLED)
  304. .arg(rrclass);
  305. }
  306. impl_->memory_datasrc_ = memory_datasrc;
  307. }
  308. uint32_t
  309. AuthSrv::getStatisticsTimerInterval() const {
  310. return (impl_->statistics_timer_.getInterval() / 1000);
  311. }
  312. void
  313. AuthSrv::setStatisticsTimerInterval(uint32_t interval) {
  314. if (interval == impl_->statistics_timer_.getInterval()) {
  315. return;
  316. }
  317. if (interval > 86400) {
  318. // It can't occur since the value is checked in
  319. // statisticsIntervalConfig::build().
  320. isc_throw(InvalidParameter, "Too long interval: " << interval);
  321. }
  322. if (interval == 0) {
  323. impl_->statistics_timer_.cancel();
  324. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_STATS_TIMER_DISABLED);
  325. } else {
  326. impl_->statistics_timer_.setup(boost::bind(&AuthSrv::submitStatistics,
  327. this),
  328. interval * 1000);
  329. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_STATS_TIMER_SET)
  330. .arg(interval);
  331. }
  332. }
  333. void
  334. AuthSrv::processMessage(const IOMessage& io_message, MessagePtr message,
  335. OutputBufferPtr buffer, DNSServer* server)
  336. {
  337. InputBuffer request_buffer(io_message.getData(), io_message.getDataSize());
  338. // First, check the header part. If we fail even for the base header,
  339. // just drop the message.
  340. try {
  341. message->parseHeader(request_buffer);
  342. // Ignore all responses.
  343. if (message->getHeaderFlag(Message::HEADERFLAG_QR)) {
  344. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_RESPONSE_RECEIVED);
  345. server->resume(false);
  346. return;
  347. }
  348. } catch (const Exception& ex) {
  349. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_HEADER_PARSE_FAIL)
  350. .arg(ex.what());
  351. server->resume(false);
  352. return;
  353. }
  354. try {
  355. // Parse the message.
  356. message->fromWire(request_buffer);
  357. } catch (const DNSProtocolError& error) {
  358. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_PACKET_PROTOCOL_ERROR)
  359. .arg(error.getRcode().toText()).arg(error.what());
  360. makeErrorMessage(message, buffer, error.getRcode());
  361. server->resume(true);
  362. return;
  363. } catch (const Exception& ex) {
  364. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_PACKET_PARSE_ERROR)
  365. .arg(ex.what());
  366. makeErrorMessage(message, buffer, Rcode::SERVFAIL());
  367. server->resume(true);
  368. return;
  369. } // other exceptions will be handled at a higher layer.
  370. LOG_DEBUG(auth_logger, DBG_AUTH_MESSAGES, AUTH_PACKET_RECEIVED)
  371. .arg(message->toText());
  372. // Perform further protocol-level validation.
  373. // TSIG first
  374. // If this is set to something, we know we need to answer with TSIG as well
  375. std::auto_ptr<TSIGContext> tsig_context;
  376. const TSIGRecord* tsig_record(message->getTSIGRecord());
  377. TSIGError tsig_error(TSIGError::NOERROR());
  378. // Do we do TSIG?
  379. // The keyring can be null if we're in test
  380. if (impl_->keyring_ != NULL && tsig_record != NULL) {
  381. tsig_context.reset(new TSIGContext(tsig_record->getName(),
  382. tsig_record->getRdata().
  383. getAlgorithm(),
  384. **impl_->keyring_));
  385. tsig_error = tsig_context->verify(tsig_record, io_message.getData(),
  386. io_message.getDataSize());
  387. }
  388. bool sendAnswer = true;
  389. if (tsig_error != TSIGError::NOERROR()) {
  390. makeErrorMessage(message, buffer, tsig_error.toRcode(), tsig_context);
  391. } else if (message->getOpcode() == Opcode::NOTIFY()) {
  392. sendAnswer = impl_->processNotify(io_message, message, buffer,
  393. tsig_context);
  394. } else if (message->getOpcode() != Opcode::QUERY()) {
  395. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_UNSUPPORTED_OPCODE)
  396. .arg(message->getOpcode().toText());
  397. makeErrorMessage(message, buffer, Rcode::NOTIMP(), tsig_context);
  398. } else if (message->getRRCount(Message::SECTION_QUESTION) != 1) {
  399. makeErrorMessage(message, buffer, Rcode::FORMERR(), tsig_context);
  400. } else {
  401. ConstQuestionPtr question = *message->beginQuestion();
  402. const RRType &qtype = question->getType();
  403. if (qtype == RRType::AXFR()) {
  404. sendAnswer = impl_->processAxfrQuery(io_message, message, buffer,
  405. tsig_context);
  406. } else if (qtype == RRType::IXFR()) {
  407. makeErrorMessage(message, buffer, Rcode::NOTIMP(), tsig_context);
  408. } else {
  409. sendAnswer = impl_->processNormalQuery(io_message, message, buffer,
  410. tsig_context);
  411. }
  412. }
  413. server->resume(sendAnswer);
  414. }
  415. bool
  416. AuthSrvImpl::processNormalQuery(const IOMessage& io_message, MessagePtr message,
  417. OutputBufferPtr buffer,
  418. auto_ptr<TSIGContext> tsig_context)
  419. {
  420. ConstEDNSPtr remote_edns = message->getEDNS();
  421. const bool dnssec_ok = remote_edns && remote_edns->getDNSSECAwareness();
  422. const uint16_t remote_bufsize = remote_edns ? remote_edns->getUDPSize() :
  423. Message::DEFAULT_MAX_UDPSIZE;
  424. message->makeResponse();
  425. message->setHeaderFlag(Message::HEADERFLAG_AA);
  426. message->setRcode(Rcode::NOERROR());
  427. // Increment query counter.
  428. incCounter(io_message.getSocket().getProtocol());
  429. if (remote_edns) {
  430. EDNSPtr local_edns = EDNSPtr(new EDNS());
  431. local_edns->setDNSSECAwareness(dnssec_ok);
  432. local_edns->setUDPSize(AuthSrvImpl::DEFAULT_LOCAL_UDPSIZE);
  433. message->setEDNS(local_edns);
  434. }
  435. try {
  436. // If a memory data source is configured call the separate
  437. // Query::process()
  438. const ConstQuestionPtr question = *message->beginQuestion();
  439. if (memory_datasrc_ && memory_datasrc_class_ == question->getClass()) {
  440. const RRType& qtype = question->getType();
  441. const Name& qname = question->getName();
  442. auth::Query(*memory_datasrc_, qname, qtype, *message).process();
  443. } else {
  444. datasrc::Query query(*message, cache_, dnssec_ok);
  445. data_sources_.doQuery(query);
  446. }
  447. } catch (const Exception& ex) {
  448. LOG_ERROR(auth_logger, AUTH_PROCESS_FAIL).arg(ex.what());
  449. makeErrorMessage(message, buffer, Rcode::SERVFAIL());
  450. return (true);
  451. }
  452. MessageRenderer renderer(*buffer);
  453. const bool udp_buffer =
  454. (io_message.getSocket().getProtocol() == IPPROTO_UDP);
  455. renderer.setLengthLimit(udp_buffer ? remote_bufsize : 65535);
  456. if (tsig_context.get() != NULL) {
  457. message->toWire(renderer, *tsig_context);
  458. } else {
  459. message->toWire(renderer);
  460. }
  461. LOG_DEBUG(auth_logger, DBG_AUTH_MESSAGES, AUTH_SEND_NORMAL_RESPONSE)
  462. .arg(renderer.getLength()).arg(message->toText());
  463. return (true);
  464. }
  465. bool
  466. AuthSrvImpl::processAxfrQuery(const IOMessage& io_message, MessagePtr message,
  467. OutputBufferPtr buffer,
  468. auto_ptr<TSIGContext> tsig_context)
  469. {
  470. // Increment query counter.
  471. incCounter(io_message.getSocket().getProtocol());
  472. if (io_message.getSocket().getProtocol() == IPPROTO_UDP) {
  473. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_AXFR_UDP);
  474. makeErrorMessage(message, buffer, Rcode::FORMERR(), tsig_context);
  475. return (true);
  476. }
  477. try {
  478. if (!xfrout_connected_) {
  479. xfrout_client_.connect();
  480. xfrout_connected_ = true;
  481. }
  482. xfrout_client_.sendXfroutRequestInfo(
  483. io_message.getSocket().getNative(),
  484. io_message.getData(),
  485. io_message.getDataSize());
  486. } catch (const XfroutError& err) {
  487. if (xfrout_connected_) {
  488. // disconnect() may trigger an exception, but since we try it
  489. // only if we've successfully opened it, it shouldn't happen in
  490. // normal condition. Should this occur, we'll propagate it to the
  491. // upper layer.
  492. xfrout_client_.disconnect();
  493. xfrout_connected_ = false;
  494. }
  495. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_AXFR_ERROR)
  496. .arg(err.what());
  497. makeErrorMessage(message, buffer, Rcode::SERVFAIL(), tsig_context);
  498. return (true);
  499. }
  500. return (false);
  501. }
  502. bool
  503. AuthSrvImpl::processNotify(const IOMessage& io_message, MessagePtr message,
  504. OutputBufferPtr buffer,
  505. std::auto_ptr<TSIGContext> tsig_context)
  506. {
  507. // The incoming notify must contain exactly one question for SOA of the
  508. // zone name.
  509. if (message->getRRCount(Message::SECTION_QUESTION) != 1) {
  510. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_NOTIFY_QUESTIONS)
  511. .arg(message->getRRCount(Message::SECTION_QUESTION));
  512. makeErrorMessage(message, buffer, Rcode::FORMERR(), tsig_context);
  513. return (true);
  514. }
  515. ConstQuestionPtr question = *message->beginQuestion();
  516. if (question->getType() != RRType::SOA()) {
  517. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_NOTIFY_RRTYPE)
  518. .arg(question->getType().toText());
  519. makeErrorMessage(message, buffer, Rcode::FORMERR(), tsig_context);
  520. return (true);
  521. }
  522. // According to RFC 1996, rcode should be "no error" and AA bit should be
  523. // on, but we don't check these conditions. This behavior is compatible
  524. // with BIND 9.
  525. // TODO check with the conf-mgr whether current server is the auth of the
  526. // zone
  527. // In the code that follows, we simply ignore the notify if any internal
  528. // error happens rather than returning (e.g.) SERVFAIL. RFC 1996 is
  529. // silent about such cases, but there doesn't seem to be anything we can
  530. // improve at the primary server side by sending an error anyway.
  531. if (xfrin_session_ == NULL) {
  532. LOG_DEBUG(auth_logger, DBG_AUTH_DETAIL, AUTH_NO_XFRIN);
  533. return (false);
  534. }
  535. const string remote_ip_address =
  536. io_message.getRemoteEndpoint().getAddress().toText();
  537. static const string command_template_start =
  538. "{\"command\": [\"notify\", {\"zone_name\" : \"";
  539. static const string command_template_master = "\", \"master\" : \"";
  540. static const string command_template_rrclass = "\", \"zone_class\" : \"";
  541. static const string command_template_end = "\"}]}";
  542. try {
  543. ConstElementPtr notify_command = Element::fromJSON(
  544. command_template_start + question->getName().toText() +
  545. command_template_master + remote_ip_address +
  546. command_template_rrclass + question->getClass().toText() +
  547. command_template_end);
  548. const unsigned int seq =
  549. xfrin_session_->group_sendmsg(notify_command, "Zonemgr",
  550. "*", "*");
  551. ConstElementPtr env, answer, parsed_answer;
  552. xfrin_session_->group_recvmsg(env, answer, false, seq);
  553. int rcode;
  554. parsed_answer = parseAnswer(rcode, answer);
  555. if (rcode != 0) {
  556. LOG_ERROR(auth_logger, AUTH_ZONEMGR_ERROR)
  557. .arg(parsed_answer->str());
  558. return (false);
  559. }
  560. } catch (const Exception& ex) {
  561. LOG_ERROR(auth_logger, AUTH_ZONEMGR_COMMS).arg(ex.what());
  562. return (false);
  563. }
  564. message->makeResponse();
  565. message->setHeaderFlag(Message::HEADERFLAG_AA);
  566. message->setRcode(Rcode::NOERROR());
  567. MessageRenderer renderer(*buffer);
  568. if (tsig_context.get() != NULL) {
  569. message->toWire(renderer, *tsig_context);
  570. } else {
  571. message->toWire(renderer);
  572. }
  573. return (true);
  574. }
  575. void
  576. AuthSrvImpl::incCounter(const int protocol) {
  577. // Increment query counter.
  578. if (protocol == IPPROTO_UDP) {
  579. counters_.inc(AuthCounters::COUNTER_UDP_QUERY);
  580. } else if (protocol == IPPROTO_TCP) {
  581. counters_.inc(AuthCounters::COUNTER_TCP_QUERY);
  582. } else {
  583. // unknown protocol
  584. isc_throw(Unexpected, "Unknown protocol: " << protocol);
  585. }
  586. }
  587. ConstElementPtr
  588. AuthSrvImpl::setDbFile(ConstElementPtr config) {
  589. ConstElementPtr answer = isc::config::createAnswer();
  590. if (config && config->contains("database_file")) {
  591. db_file_ = config->get("database_file")->stringValue();
  592. } else if (config_session_ != NULL) {
  593. bool is_default;
  594. string item("database_file");
  595. ConstElementPtr value = config_session_->getValue(is_default, item);
  596. ElementPtr final = Element::createMap();
  597. // If the value is the default, and we are running from
  598. // a specific directory ('from build'), we need to use
  599. // a different value than the default (which may not exist)
  600. // (btw, this should not be done here in the end, i think
  601. // the from-source script should have a check for this,
  602. // but for that we need offline access to config, so for
  603. // now this is a decent solution)
  604. if (is_default && getenv("B10_FROM_BUILD")) {
  605. value = Element::create(string(getenv("B10_FROM_BUILD")) +
  606. "/bind10_zones.sqlite3");
  607. }
  608. final->set(item, value);
  609. config = final;
  610. db_file_ = value->stringValue();
  611. } else {
  612. return (answer);
  613. }
  614. LOG_DEBUG(auth_logger, DBG_AUTH_OPS, AUTH_DATA_SOURCE).arg(db_file_);
  615. // create SQL data source
  616. // Note: the following step is tricky to be exception-safe and to ensure
  617. // exception guarantee: We first need to perform all operations that can
  618. // fail, while acquiring resources in the RAII manner. We then perform
  619. // delete and swap operations which should not fail.
  620. DataSrcPtr datasrc_ptr(DataSrcPtr(new Sqlite3DataSrc));
  621. datasrc_ptr->init(config);
  622. data_sources_.addDataSrc(datasrc_ptr);
  623. // The following code should be exception free.
  624. if (cur_datasrc_ != NULL) {
  625. data_sources_.removeDataSrc(cur_datasrc_);
  626. }
  627. cur_datasrc_ = datasrc_ptr;
  628. return (answer);
  629. }
  630. ConstElementPtr
  631. AuthSrv::updateConfig(ConstElementPtr new_config) {
  632. try {
  633. // the ModuleCCSession has already checked if we have
  634. // the correct ElementPtr type as specified in our .spec file
  635. if (new_config) {
  636. configureAuthServer(*this, new_config);
  637. }
  638. return (impl_->setDbFile(new_config));
  639. } catch (const isc::Exception& error) {
  640. LOG_ERROR(auth_logger, AUTH_CONFIG_UPDATE_FAIL).arg(error.what());
  641. return (isc::config::createAnswer(1, error.what()));
  642. }
  643. }
  644. bool AuthSrv::submitStatistics() const {
  645. return (impl_->counters_.submitStatistics());
  646. }
  647. uint64_t
  648. AuthSrv::getCounter(const AuthCounters::CounterType type) const {
  649. return (impl_->counters_.getCounter(type));
  650. }
  651. const AddressList&
  652. AuthSrv::getListenAddresses() const {
  653. return (impl_->listen_addresses_);
  654. }
  655. void
  656. AuthSrv::setListenAddresses(const AddressList& addresses) {
  657. installListenAddresses(addresses, impl_->listen_addresses_, *dnss_);
  658. }
  659. void
  660. AuthSrv::setDNSService(isc::asiodns::DNSService& dnss) {
  661. dnss_ = &dnss;
  662. }
  663. void
  664. AuthSrv::setTSIGKeyRing(const shared_ptr<TSIGKeyRing>* keyring) {
  665. impl_->keyring_ = keyring;
  666. }