auth_srv.cc 22 KB

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