JSONDB.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "JSONDB.hpp"
  19. namespace ZeroTier {
  20. static const nlohmann::json _EMPTY_JSON(nlohmann::json::object());
  21. bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
  22. {
  23. if (!_isValidObjectName(n))
  24. return false;
  25. const std::string path(_genPath(n,true));
  26. if (!path.length())
  27. return false;
  28. const std::string buf(obj);
  29. if (!OSUtils::writeFile(path.c_str(),buf))
  30. return false;
  31. return true;
  32. }
  33. bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
  34. {
  35. if (!_isValidObjectName(n))
  36. return false;
  37. const std::string path(_genPath(n,true));
  38. if (!path.length())
  39. return false;
  40. const std::string buf(OSUtils::jsonDump(obj));
  41. if (!OSUtils::writeFile(path.c_str(),buf))
  42. return false;
  43. _E &e = _db[n];
  44. e.obj = obj;
  45. return true;
  46. }
  47. const nlohmann::json &JSONDB::get(const std::string &n)
  48. {
  49. if (!_isValidObjectName(n))
  50. return _EMPTY_JSON;
  51. std::map<std::string,_E>::iterator e(_db.find(n));
  52. if (e != _db.end())
  53. return e->second.obj;
  54. const std::string path(_genPath(n,false));
  55. if (!path.length())
  56. return _EMPTY_JSON;
  57. std::string buf;
  58. if (!OSUtils::readFile(path.c_str(),buf))
  59. return _EMPTY_JSON;
  60. _E &e2 = _db[n];
  61. try {
  62. e2.obj = OSUtils::jsonParse(buf);
  63. } catch ( ... ) {
  64. e2.obj = _EMPTY_JSON;
  65. buf = "{}";
  66. }
  67. return e2.obj;
  68. }
  69. void JSONDB::erase(const std::string &n)
  70. {
  71. if (!_isValidObjectName(n))
  72. return;
  73. std::string path(_genPath(n,true));
  74. if (!path.length())
  75. return;
  76. OSUtils::rm(path.c_str());
  77. _db.erase(n);
  78. }
  79. void JSONDB::_reload(const std::string &p,const std::string &b)
  80. {
  81. std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
  82. for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
  83. printf("%s\n",di->c_str());
  84. if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
  85. this->get(b + di->substr(0,di->length() - 5));
  86. } else {
  87. this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
  88. }
  89. }
  90. }
  91. bool JSONDB::_isValidObjectName(const std::string &n)
  92. {
  93. if (n.length() == 0)
  94. return false;
  95. const char *p = n.c_str();
  96. char c;
  97. // For security reasons we should not allow dots, backslashes, or other path characters or potential path characters.
  98. while ((c = *(p++))) {
  99. if (!( ((c >= 'a')&&(c <= 'z')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= '0')&&(c <= '9')) || (c == '/') || (c == '_') || (c == '~') || (c == '-') ))
  100. return false;
  101. }
  102. return true;
  103. }
  104. std::string JSONDB::_genPath(const std::string &n,bool create)
  105. {
  106. std::vector<std::string> pt(OSUtils::split(n.c_str(),"/","",""));
  107. if (pt.size() == 0)
  108. return std::string();
  109. std::string p(_basePath);
  110. if (create) OSUtils::mkdir(p.c_str());
  111. for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i<j;++i) {
  112. p.push_back(ZT_PATH_SEPARATOR);
  113. p.append(pt[i]);
  114. if (create) OSUtils::mkdir(p.c_str());
  115. }
  116. p.push_back(ZT_PATH_SEPARATOR);
  117. p.append(pt[pt.size()-1]);
  118. p.append(".json");
  119. return p;
  120. }
  121. } // namespace ZeroTier