auth_srv.cc 25 KB

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