dns.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2011 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. #include <exceptions/exceptions.h>
  15. #include <dns/name.h>
  16. #include <dns/tsigrecord.h>
  17. #include <cc/data.h>
  18. #include <acl/dns.h>
  19. #include <acl/ip_check.h>
  20. #include <acl/dnsname_check.h>
  21. #include <acl/loader.h>
  22. #include <acl/logic_check.h>
  23. #include <boost/shared_ptr.hpp>
  24. #include <boost/scoped_ptr.hpp>
  25. #include <memory>
  26. #include <string>
  27. #include <vector>
  28. using namespace std;
  29. using namespace isc::dns;
  30. using namespace isc::data;
  31. namespace isc {
  32. namespace acl {
  33. /// The specialization of \c IPCheck for access control with \c RequestContext.
  34. ///
  35. /// It returns \c true if the remote (source) IP address of the request
  36. /// matches the expression encapsulated in the \c IPCheck, and returns
  37. /// \c false if not.
  38. template <>
  39. bool
  40. IPCheck<dns::RequestContext>::matches(
  41. const dns::RequestContext& request) const
  42. {
  43. return (compare(request.remote_address.getData(),
  44. request.remote_address.getFamily()));
  45. }
  46. namespace dns {
  47. /// The specialization of \c NameCheck for access control with
  48. /// \c RequestContext.
  49. ///
  50. /// It returns \c true if the request contains a TSIG record and its key
  51. /// (owner) name is equal to the name stored in the check; otherwise
  52. /// it returns \c false.
  53. template<>
  54. bool
  55. NameCheck<RequestContext>::matches(const RequestContext& request) const {
  56. return (request.tsig != NULL && request.tsig->getName() == name_);
  57. }
  58. vector<string>
  59. internal::RequestCheckCreator::names() const {
  60. // Probably we should eventually build this vector in a more
  61. // sophisticated way. For now, it's simple enough to hardcode
  62. // everything.
  63. vector<string> supported_names;
  64. supported_names.push_back("from");
  65. supported_names.push_back("key");
  66. return (supported_names);
  67. }
  68. boost::shared_ptr<RequestCheck>
  69. internal::RequestCheckCreator::create(const string& name,
  70. ConstElementPtr definition,
  71. // unused:
  72. const acl::Loader<RequestContext>&)
  73. {
  74. if (!definition) {
  75. isc_throw(LoaderError,
  76. "NULL pointer is passed to RequestCheckCreator");
  77. }
  78. if (name == "from") {
  79. return (boost::shared_ptr<internal::RequestIPCheck>(
  80. new internal::RequestIPCheck(definition->stringValue())));
  81. } else if (name == "key") {
  82. return (boost::shared_ptr<internal::RequestKeyCheck>(
  83. new internal::RequestKeyCheck(
  84. Name(definition->stringValue()))));
  85. } else {
  86. // This case shouldn't happen (normally) as it should have been
  87. // rejected at the loader level. But we explicitly catch the case
  88. // and throw an exception for that.
  89. isc_throw(LoaderError, "Invalid check name for RequestCheck: " <<
  90. name);
  91. }
  92. }
  93. RequestLoader&
  94. getRequestLoader() {
  95. // To ensure that the singleton gets destroyed at the end of the
  96. // program's lifetime, we put it in a static scoped_ptr.
  97. static boost::scoped_ptr<RequestLoader> loader(NULL);
  98. if (loader.get() == NULL) {
  99. // Creator registration may throw, so we first store the new loader
  100. // in a second auto pointer in order to provide the strong exception
  101. // guarantee.
  102. auto_ptr<RequestLoader> loader_ptr =
  103. auto_ptr<RequestLoader>(new RequestLoader(REJECT));
  104. // Register default check creator(s)
  105. loader_ptr->registerCreator(
  106. boost::shared_ptr<internal::RequestCheckCreator>(
  107. new internal::RequestCheckCreator()));
  108. loader_ptr->registerCreator(
  109. boost::shared_ptr<NotCreator<RequestContext> >(
  110. new NotCreator<RequestContext>("NOT")));
  111. loader_ptr->registerCreator(
  112. boost::shared_ptr<LogicCreator<AnyOfSpec, RequestContext> >(
  113. new LogicCreator<AnyOfSpec, RequestContext>("ANY")));
  114. loader_ptr->registerCreator(
  115. boost::shared_ptr<LogicCreator<AllOfSpec, RequestContext> >(
  116. new LogicCreator<AllOfSpec, RequestContext>("ALL")));
  117. // From this point there shouldn't be any exception thrown
  118. loader.reset(loader_ptr.release());
  119. }
  120. return (*loader);
  121. }
  122. }
  123. }
  124. }