auth_srv.h 17 KB

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