lease_mgr_factory.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2012-2013 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 "config.h"
  15. #include <dhcpsrv/dhcpsrv_log.h>
  16. #include <dhcpsrv/lease_mgr_factory.h>
  17. #include <dhcpsrv/memfile_lease_mgr.h>
  18. #ifdef HAVE_MYSQL
  19. #include <dhcpsrv/mysql_lease_mgr.h>
  20. #endif
  21. #include <boost/algorithm/string.hpp>
  22. #include <boost/foreach.hpp>
  23. #include <boost/scoped_ptr.hpp>
  24. #include <algorithm>
  25. #include <iostream>
  26. #include <iterator>
  27. #include <map>
  28. #include <sstream>
  29. #include <utility>
  30. using namespace std;
  31. namespace isc {
  32. namespace dhcp {
  33. boost::scoped_ptr<LeaseMgr>&
  34. LeaseMgrFactory::getLeaseMgrPtr() {
  35. static boost::scoped_ptr<LeaseMgr> leaseMgrPtr;
  36. return (leaseMgrPtr);
  37. }
  38. LeaseMgr::ParameterMap
  39. LeaseMgrFactory::parse(const std::string& dbaccess) {
  40. LeaseMgr::ParameterMap mapped_tokens;
  41. if (!dbaccess.empty()) {
  42. vector<string> tokens;
  43. // We need to pass a string to is_any_of, not just char*. Otherwise
  44. // there are cryptic warnings on Debian6 running g++ 4.4 in
  45. // /usr/include/c++/4.4/bits/stl_algo.h:2178 "array subscript is above
  46. // array bounds"
  47. boost::split(tokens, dbaccess, boost::is_any_of(string("\t ")));
  48. BOOST_FOREACH(std::string token, tokens) {
  49. size_t pos = token.find("=");
  50. if (pos != string::npos) {
  51. string name = token.substr(0, pos);
  52. string value = token.substr(pos + 1);
  53. mapped_tokens.insert(make_pair(name, value));
  54. } else {
  55. LOG_ERROR(dhcpsrv_logger, DHCPSRV_INVALID_ACCESS).arg(dbaccess);
  56. isc_throw(InvalidParameter, "Cannot parse " << token
  57. << ", expected format is name=value");
  58. }
  59. }
  60. }
  61. return (mapped_tokens);
  62. }
  63. std::string
  64. LeaseMgrFactory::redactedAccessString(const LeaseMgr::ParameterMap& parameters) {
  65. // Reconstruct the access string: start of with an empty string, then
  66. // work through all the parameters in the original string and add them.
  67. std::string access;
  68. for (LeaseMgr::ParameterMap::const_iterator i = parameters.begin();
  69. i != parameters.end(); ++i) {
  70. // Separate second and subsequent tokens are preceded by a space.
  71. if (!access.empty()) {
  72. access += " ";
  73. }
  74. // Append name of parameter...
  75. access += i->first;
  76. access += "=";
  77. // ... and the value, except in the case of the password, where a
  78. // redacted value is appended.
  79. if (i->first == std::string("password")) {
  80. access += "*****";
  81. } else {
  82. access += i->second;
  83. }
  84. }
  85. return (access);
  86. }
  87. void
  88. LeaseMgrFactory::create(const std::string& dbaccess) {
  89. const std::string type = "type";
  90. // Parse the access string and create a redacted string for logging.
  91. LeaseMgr::ParameterMap parameters = parse(dbaccess);
  92. std::string redacted = redactedAccessString(parameters);
  93. // Is "type" present?
  94. if (parameters.find(type) == parameters.end()) {
  95. LOG_ERROR(dhcpsrv_logger, DHCPSRV_NOTYPE_DB).arg(dbaccess);
  96. isc_throw(InvalidParameter, "Database configuration parameters do not "
  97. "contain the 'type' keyword");
  98. }
  99. // Yes, check what it is.
  100. #ifdef HAVE_MYSQL
  101. if (parameters[type] == string("mysql")) {
  102. LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_DB).arg(redacted);
  103. getLeaseMgrPtr().reset(new MySqlLeaseMgr(parameters));
  104. return;
  105. }
  106. #endif
  107. if (parameters[type] == string("memfile")) {
  108. LOG_INFO(dhcpsrv_logger, DHCPSRV_MEMFILE_DB).arg(redacted);
  109. getLeaseMgrPtr().reset(new Memfile_LeaseMgr(parameters));
  110. return;
  111. }
  112. // Get here on no match
  113. LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
  114. isc_throw(InvalidType, "Database access parameter 'type' does "
  115. "not specify a supported database backend");
  116. }
  117. void
  118. LeaseMgrFactory::destroy() {
  119. // Destroy current lease manager. This is a no-op if no lease manager
  120. // is available.
  121. if (getLeaseMgrPtr()) {
  122. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CLOSE_DB)
  123. .arg(getLeaseMgrPtr()->getType());
  124. }
  125. getLeaseMgrPtr().reset();
  126. }
  127. LeaseMgr&
  128. LeaseMgrFactory::instance() {
  129. LeaseMgr* lmptr = getLeaseMgrPtr().get();
  130. if (lmptr == NULL) {
  131. isc_throw(NoLeaseManager, "no current lease manager is available");
  132. }
  133. return (*lmptr);
  134. }
  135. }; // namespace dhcp
  136. }; // namespace isc