auth_srv.cc 24 KB

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