auth_srv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. #ifndef __AUTH_SRV_H
  15. #define __AUTH_SRV_H 1
  16. #include <string>
  17. #include <cc/data.h>
  18. #include <config/ccsession.h>
  19. #include <datasrc/factory.h>
  20. #include <dns/message.h>
  21. #include <dns/opcode.h>
  22. #include <util/buffer.h>
  23. #include <asiodns/dns_server.h>
  24. #include <asiodns/dns_service.h>
  25. #include <asiodns/dns_lookup.h>
  26. #include <asiodns/dns_answer.h>
  27. #include <asiolink/io_message.h>
  28. #include <asiolink/io_service.h>
  29. #include <asiolink/simple_callback.h>
  30. #include <asiolink/asiolink.h>
  31. #include <server_common/portconfig.h>
  32. #include <auth/statistics.h>
  33. namespace isc {
  34. namespace util {
  35. namespace io {
  36. class BaseSocketSessionForwarder;
  37. }
  38. }
  39. namespace datasrc {
  40. class InMemoryClient;
  41. }
  42. namespace xfr {
  43. class AbstractXfroutClient;
  44. }
  45. namespace dns {
  46. class TSIGKeyRing;
  47. }
  48. }
  49. /// \brief The implementation class for the \c AuthSrv class using the pimpl
  50. /// idiom.
  51. class AuthSrvImpl;
  52. /// \brief The authoritative nameserver class.
  53. ///
  54. /// \c AuthSrv is a concrete class that implements authoritative DNS server
  55. /// protocol processing.
  56. /// An \c AuthSrv object is primarily responsible for handling incoming DNS
  57. /// requests: It parses the request and dispatches subsequent processing to
  58. /// the corresponding module (which may be an internal library or a separate
  59. /// process) depending on the request type. For normal queries, the
  60. /// \c AuthSrv object searches configured data sources for the answer to the
  61. /// query, and builds a response containing the answer.
  62. ///
  63. /// This class uses the "pimpl" idiom, and hides detailed implementation
  64. /// through the \c impl_ pointer (which points to an instance of the
  65. /// \c AuthSrvImpl class). An \c AuthSrv object is supposed to exist for quite
  66. /// a long period, and only a few \c AuthSrv objects will be created (in fact,
  67. /// in this current implementation there will only be one object), so the
  68. /// construction overhead of this approach should be acceptable.
  69. ///
  70. /// The design of this class is still in flux. It's quite likely to change
  71. /// in future versions.
  72. ///
  73. class AuthSrv {
  74. ///
  75. /// \name Constructors, Assignment Operator and Destructor.
  76. ///
  77. /// Note: The copy constructor and the assignment operator are
  78. /// intentionally defined as private.
  79. //@{
  80. private:
  81. AuthSrv(const AuthSrv& source);
  82. AuthSrv& operator=(const AuthSrv& source);
  83. public:
  84. /// The constructor.
  85. ///
  86. /// \param use_cache Whether to enable hot spot cache for lookup results.
  87. /// \param xfrout_client Communication interface with a separate xfrout
  88. /// process. It's normally a reference to an xfr::XfroutClient object,
  89. /// but can refer to a local mock object for testing (or other
  90. /// experimental) purposes.
  91. AuthSrv(const bool use_cache,
  92. isc::xfr::AbstractXfroutClient& xfrout_client,
  93. isc::util::io::BaseSocketSessionForwarder& ddns_forwarder);
  94. ~AuthSrv();
  95. //@}
  96. /// Stop the server.
  97. ///
  98. /// It stops the internal event loop of the server and subsequently
  99. /// returns the control to the top level context.
  100. ///
  101. /// This method should never throw an exception.
  102. void stop();
  103. /// \brief Process an incoming DNS message, then signal 'server' to resume
  104. ///
  105. /// A DNS query (or other message) has been received by a \c DNSServer
  106. /// object. Find an answer, then post the \c DNSServer object on the
  107. /// I/O service queue and return. When the server resumes, it can
  108. /// send the reply.
  109. ///
  110. /// \param io_message The raw message received
  111. /// \param message the \c Message object
  112. /// \param buffer an \c OutputBuffer for the resposne
  113. /// \param server Pointer to the \c DNSServer
  114. ///
  115. /// \throw isc::Unexpected Protocol type of \a message is unexpected
  116. void processMessage(const isc::asiolink::IOMessage& io_message,
  117. isc::dns::Message& message,
  118. isc::util::OutputBuffer& buffer,
  119. isc::asiodns::DNSServer* server);
  120. /// \brief Updates the data source for the \c AuthSrv object.
  121. ///
  122. /// This method installs or replaces the data source that the \c AuthSrv
  123. /// object refers to for query processing.
  124. /// Although the method name is generic, the only thing it does is to
  125. /// update the data source information.
  126. /// If there is a data source installed, it will be replaced with the
  127. /// new one.
  128. ///
  129. /// In the current implementation, the SQLite data source and InMemoryClient
  130. /// are assumed.
  131. /// We can enable memory data source and get the path of SQLite database by
  132. /// the \c config parameter. If we disabled memory data source, the SQLite
  133. /// data source will be used.
  134. ///
  135. /// On success this method returns a data \c Element (in the form of a
  136. /// pointer like object) indicating the successful result,
  137. /// i.e., {"result": [0]}.
  138. /// Otherwise, it returns a data \c Element explaining the error:
  139. /// {"result": [1, <error-description>]}.
  140. ///
  141. /// This method is mostly exception free (error conditions are represented
  142. /// via the return value). But it may still throw a standard exception
  143. /// if memory allocation fails inside the method.
  144. /// When a standard exception is thrown or an implementation specific
  145. /// exception is triggered and caught internally, this function provides
  146. /// the strong exception guarantee: Unless everything succeeds, currently
  147. /// installed data source (if any) won't be replaced.
  148. ///
  149. /// \param config An immutable pointer-like object to a data \c Element,
  150. /// possibly containing the data source information to be used.
  151. /// \return An immutable pointer-like object to a data \c Element
  152. /// containing the result of the update operation.
  153. isc::data::ConstElementPtr updateConfig(isc::data::ConstElementPtr config);
  154. /// \brief Returns the command and configuration session for the
  155. /// \c AuthSrv.
  156. ///
  157. /// This method never throws an exception.
  158. ///
  159. /// \return A pointer to \c ModuleCCSession object stored in the
  160. /// \c AuthSrv object. In this implementation it could be NULL.
  161. isc::config::ModuleCCSession* getConfigSession() const;
  162. /// \brief Set the command and configuration session for the \c AuthSrv.
  163. ///
  164. /// Note: this interface is tentative. We'll revisit the ASIO and session
  165. /// frameworks, at which point the session will probably be passed on
  166. /// construction of the server.
  167. /// In the current implementation, this method is expected to be called
  168. /// exactly once as part of initialization. If this method is called
  169. /// multiple times, previously specified session is silently overridden.
  170. ///
  171. /// This method never throws an exception.
  172. ///
  173. /// \param config_session A pointer to \c ModuleCCSession object to receive
  174. /// control commands and configuration updates.
  175. void setConfigSession(isc::config::ModuleCCSession* config_session);
  176. /// \brief Return this object's ASIO IO Service queue
  177. isc::asiolink::IOService& getIOService();
  178. /// \brief Return pointer to the DNS Lookup callback function
  179. isc::asiodns::DNSLookup* getDNSLookupProvider() const { return (dns_lookup_); }
  180. /// \brief Return pointer to the DNS Answer callback function
  181. isc::asiodns::DNSAnswer* getDNSAnswerProvider() const { return (dns_answer_); }
  182. /// \brief Return pointer to the Checkin callback function
  183. isc::asiolink::SimpleCallback* getCheckinProvider() const { return (checkin_); }
  184. /// \brief Set or update the size (number of slots) of hot spot cache.
  185. ///
  186. /// If the specified size is 0, it means the size will be unlimited.
  187. /// The specified size is recorded even if the cache is disabled; the
  188. /// new size will be effective when the cache is enabled.
  189. ///
  190. /// This method never throws an exception.
  191. ///
  192. /// \param slots The number of cache slots.
  193. void setCacheSlots(const size_t slots);
  194. /// \brief Get the current size (number of slots) of hot spot cache.
  195. ///
  196. /// It always returns the recorded size regardless of the cache is enabled.
  197. ///
  198. /// This method never throws an exception.
  199. ///
  200. /// \return The current number of cache slots.
  201. size_t getCacheSlots() const;
  202. /// \brief Set the communication session with a separate process for
  203. /// outgoing zone transfers.
  204. ///
  205. /// Note: this interface is tentative. We'll revisit the ASIO and session
  206. /// frameworks, at which point the session will probably be passed on
  207. /// construction of the server.
  208. ///
  209. /// \param xfrin_session A Session object over which NOTIFY message
  210. /// information is exchanged with a XFRIN handler.
  211. /// The session must be established before setting in the server
  212. /// object.
  213. /// Ownership isn't transferred: the caller is responsible for keeping
  214. /// this object to be valid while the server object is working and for
  215. /// disconnecting the session and destroying the object when the server
  216. /// is shutdown.
  217. ///
  218. void setXfrinSession(isc::cc::AbstractSession* xfrin_session);
  219. /// Returns the in-memory data source configured for the \c AuthSrv,
  220. /// if any, as a pointer.
  221. ///
  222. /// This is mostly a convenience function around
  223. /// \c getInMemoryClientContainer, which saves the caller the step
  224. /// of having to call getInstance().
  225. /// The pointer is of course only valid as long as the container
  226. /// exists.
  227. ///
  228. /// The in-memory data source is configured per RR class. However,
  229. /// the data source may not be available for all RR classes.
  230. /// If it is not available for the specified RR class, an exception of
  231. /// class \c InvalidParameter will be thrown.
  232. /// This method never throws an exception otherwise.
  233. ///
  234. /// Even for supported RR classes, the in-memory data source is not
  235. /// configured by default. In that case a NULL (shared) pointer will
  236. /// be returned.
  237. ///
  238. /// \param rrclass The RR class of the requested in-memory data source.
  239. /// \return A pointer to the in-memory data source, if configured;
  240. /// otherwise NULL.
  241. isc::datasrc::DataSourceClient* getInMemoryClient(
  242. const isc::dns::RRClass& rrclass);
  243. /// Returns the DataSourceClientContainer of the in-memory datasource
  244. ///
  245. /// \exception InvalidParameter if the given class does not match
  246. /// the one in the memory data source, or if the memory
  247. /// datasource has not been set (callers can check with
  248. /// \c hasMemoryDataSource())
  249. ///
  250. /// \param rrclass The RR class of the requested in-memory data source.
  251. /// \return A shared pointer to the in-memory data source, if configured;
  252. /// otherwise an empty shared pointer.
  253. isc::datasrc::DataSourceClientContainerPtr getInMemoryClientContainer(
  254. const isc::dns::RRClass& rrclass);
  255. /// Checks if the in-memory data source has been set.
  256. ///
  257. /// Right now, only one datasource at a time is effectively supported.
  258. /// This is a helper method to check whether it is the in-memory one.
  259. /// This is mostly useful for current testing, and is expected to be
  260. /// removed (or changed in behaviour) soon, when the general
  261. /// multi-data-source framework is completed.
  262. ///
  263. /// \return True if the in-memory datasource has been set.
  264. bool hasInMemoryClient() const;
  265. /// Sets or replaces the in-memory data source of the specified RR class.
  266. ///
  267. /// Some RR classes may not be supported, in which case an exception
  268. /// of class \c InvalidParameter will be thrown.
  269. /// This method never throws an exception otherwise.
  270. ///
  271. /// If there is already an in memory data source configured, it will be
  272. /// replaced with the newly specified one.
  273. /// \c memory_client can be an empty shared pointer, in which case it
  274. /// will (re)disable the in-memory data source.
  275. ///
  276. /// \param rrclass The RR class of the in-memory data source to be set.
  277. /// \param memory_client A (shared) pointer to \c InMemoryClient to be set.
  278. void setInMemoryClient(const isc::dns::RRClass& rrclass,
  279. isc::datasrc::DataSourceClientContainerPtr memory_client);
  280. /// \brief Set the communication session with Statistics.
  281. ///
  282. /// This function never throws an exception as far as
  283. /// AuthCounters::setStatisticsSession() doesn't throw.
  284. ///
  285. /// Note: this interface is tentative. We'll revisit the ASIO and
  286. /// session frameworks, at which point the session will probably
  287. /// be passed on construction of the server.
  288. ///
  289. /// \param statistics_session A Session object over which statistics
  290. /// information is exchanged with statistics module.
  291. /// The session must be established before setting in the server
  292. /// object.
  293. /// Ownership isn't transferred: the caller is responsible for keeping
  294. /// this object to be valid while the server object is working and for
  295. /// disconnecting the session and destroying the object when the server
  296. /// is shutdown.
  297. void setStatisticsSession(isc::cc::AbstractSession* statistics_session);
  298. /// Return the interval of periodic submission of statistics in seconds.
  299. ///
  300. /// If the statistics submission is disabled, it returns 0.
  301. ///
  302. /// This method never throws an exception.
  303. uint32_t getStatisticsTimerInterval() const;
  304. /// Set the interval of periodic submission of statistics.
  305. ///
  306. /// If the specified value is non 0, the \c AuthSrv object will submit
  307. /// its statistics to the statistics module every \c interval seconds.
  308. /// If it's 0, and \c AuthSrv currently submits statistics, the submission
  309. /// will be disabled. \c interval must be equal to or shorter than 86400
  310. /// seconds (1 day).
  311. ///
  312. /// This method should normally not throw an exception; however, its
  313. /// underlying library routines may involve resource allocation, and
  314. /// when it fails it would result in a corresponding standard exception.
  315. ///
  316. /// \param interval The submission interval in seconds if non 0;
  317. /// or a value of 0 to disable the submission.
  318. void setStatisticsTimerInterval(uint32_t interval);
  319. /// \brief Submit statistics counters to statistics module.
  320. ///
  321. /// This function can throw an exception from
  322. /// AuthCounters::submitStatistics().
  323. ///
  324. /// \return true on success, false on failure (e.g. session timeout,
  325. /// session error).
  326. bool submitStatistics() const;
  327. /// \brief Get the value of counter in the AuthCounters.
  328. ///
  329. /// This function calls AuthCounters::getCounter() and
  330. /// returns its return value.
  331. ///
  332. /// This function never throws an exception as far as
  333. /// AuthCounters::getCounter() doesn't throw.
  334. ///
  335. /// Note: Currently this function is for testing purpose only.
  336. ///
  337. /// \param type Type of a counter to get the value of
  338. ///
  339. /// \return the value of the counter.
  340. uint64_t getCounter(const AuthCounters::ServerCounterType type) const;
  341. /// \brief Get the value of per Opcode counter in the Auth Counters.
  342. ///
  343. /// This function calls AuthCounters::getCounter(isc::dns::Opcode) and
  344. /// returns its return value.
  345. ///
  346. /// \note This is a tentative interface as an attempt of experimentally
  347. /// supporting more statistics counters. This should eventually be more
  348. /// generalized. In any case, this method is mainly for testing.
  349. ///
  350. /// \throw None
  351. /// \param opcode The opcode of the counter to get the value of
  352. /// \return the value of the counter.
  353. uint64_t getCounter(const isc::dns::Opcode opcode) const;
  354. /// \brief Get the value of per Rcode counter in the Auth Counters.
  355. ///
  356. /// This function calls AuthCounters::getCounter(isc::dns::Rcode) and
  357. /// returns its return value.
  358. ///
  359. /// \note This is a tentative interface as an attempt of experimentally
  360. /// supporting more statistics counters. This should eventually be more
  361. /// generalized. In any case, this method is mainly for testing.
  362. ///
  363. /// \throw None
  364. /// \param rcode The rcode of the counter to get the value of
  365. /// \return the value of the counter.
  366. uint64_t getCounter(const isc::dns::Rcode rcode) const;
  367. /**
  368. * \brief Set and get the addresses we listen on.
  369. */
  370. void setListenAddresses(const isc::server_common::portconfig::AddressList&
  371. addreses);
  372. const isc::server_common::portconfig::AddressList& getListenAddresses()
  373. const;
  374. /// \brief Assign an ASIO DNS Service queue to this Auth object
  375. void setDNSService(isc::asiodns::DNSServiceBase& dnss);
  376. /// \brief Sets the keyring used for verifying and signing
  377. ///
  378. /// The parameter is pointer to shared pointer, because the automatic
  379. /// reloading routines of tsig keys replace the actual keyring object.
  380. /// It is expected the pointer will point to some statically-allocated
  381. /// object, it doesn't take ownership of it.
  382. void setTSIGKeyRing(const boost::shared_ptr<isc::dns::TSIGKeyRing>*
  383. keyring);
  384. /// \brief Create the internal forwarder for DDNS update messages
  385. ///
  386. /// Until this method is called (it is called when the
  387. /// start_ddns_forwarder command is sent to b10-auth), b10-auth will
  388. /// respond to UPDATE messages with a NOTIMP rcode.
  389. /// If the internal forwarder was already created, it is destroyed and
  390. /// created again. This is useful for instance when b10-ddns is shut
  391. /// down and restarted.
  392. void createDDNSForwarder();
  393. /// \brief Destroy the internal forwarder for DDNS update messages
  394. ///
  395. /// After this method has been called (it is called when the
  396. /// stop_ddns_forwarder command is sent to b10-auth), DDNS Update
  397. /// messages are no longer forwarded internally, but b10-auth will
  398. /// immediately respond with a NOTIMP rcode.
  399. /// If there was no forwarder yet, this method does nothing.
  400. void destroyDDNSForwarder();
  401. private:
  402. AuthSrvImpl* impl_;
  403. isc::asiolink::SimpleCallback* checkin_;
  404. isc::asiodns::DNSLookup* dns_lookup_;
  405. isc::asiodns::DNSAnswer* dns_answer_;
  406. isc::asiodns::DNSServiceBase* dnss_;
  407. };
  408. #endif // __AUTH_SRV_H
  409. // Local Variables:
  410. // mode: c++
  411. // End: