data_source.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2015 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 <dhcpsrv/data_source.h>
  15. #include <dhcpsrv/dhcpsrv_log.h>
  16. #include <exceptions/exceptions.h>
  17. #include <boost/algorithm/string.hpp>
  18. #include <boost/foreach.hpp>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <vector>
  21. using namespace std;
  22. namespace isc {
  23. namespace dhcp {
  24. std::string DataSource::getParameter(const std::string& name) const {
  25. ParameterMap::const_iterator param = parameters_.find(name);
  26. if (param == parameters_.end()) {
  27. isc_throw(BadValue, "Parameter not found");
  28. }
  29. return (param->second);
  30. }
  31. DataSource::ParameterMap
  32. DataSource::parse(const std::string& dbaccess) {
  33. DataSource::ParameterMap mapped_tokens;
  34. if (!dbaccess.empty()) {
  35. vector<string> tokens;
  36. // We need to pass a string to is_any_of, not just char*. Otherwise
  37. // there are cryptic warnings on Debian6 running g++ 4.4 in
  38. // /usr/include/c++/4.4/bits/stl_algo.h:2178 "array subscript is above
  39. // array bounds"
  40. boost::split(tokens, dbaccess, boost::is_any_of(string("\t ")));
  41. BOOST_FOREACH(std::string token, tokens) {
  42. size_t pos = token.find("=");
  43. if (pos != string::npos) {
  44. string name = token.substr(0, pos);
  45. string value = token.substr(pos + 1);
  46. mapped_tokens.insert(make_pair(name, value));
  47. } else {
  48. LOG_ERROR(dhcpsrv_logger, DHCPSRV_INVALID_ACCESS).arg(dbaccess);
  49. isc_throw(InvalidParameter, "Cannot parse " << token
  50. << ", expected format is name=value");
  51. }
  52. }
  53. }
  54. return (mapped_tokens);
  55. }
  56. std::string
  57. DataSource::redactedAccessString(const DataSource::ParameterMap& parameters) {
  58. // Reconstruct the access string: start of with an empty string, then
  59. // work through all the parameters in the original string and add them.
  60. std::string access;
  61. for (DataSource::ParameterMap::const_iterator i = parameters.begin();
  62. i != parameters.end(); ++i) {
  63. // Separate second and subsequent tokens are preceded by a space.
  64. if (!access.empty()) {
  65. access += " ";
  66. }
  67. // Append name of parameter...
  68. access += i->first;
  69. access += "=";
  70. // ... and the value, except in the case of the password, where a
  71. // redacted value is appended.
  72. if (i->first == std::string("password")) {
  73. access += "*****";
  74. } else {
  75. access += i->second;
  76. }
  77. }
  78. return (access);
  79. }
  80. };
  81. };