auth_srv.cc 23 KB

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