auth_srv.h 16 KB

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