JSONDB.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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::put(const std::string &n,const nlohmann::json &obj)
  22. {
  23. if (!_isValidObjectName(n))
  24. return false;
  25. std::string path(_genPath(n,true));
  26. if (!path.length())
  27. return false;
  28. std::string buf(obj.dump(2));
  29. if (!OSUtils::writeFile(path.c_str(),buf))
  30. return false;
  31. _E &e = _db[n];
  32. e.obj = obj;
  33. e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
  34. e.lastCheck = OSUtils::now();
  35. return true;
  36. }
  37. const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceCheck)
  38. {
  39. if (!_isValidObjectName(n))
  40. return _EMPTY_JSON;
  41. const uint64_t now = OSUtils::now();
  42. std::string buf;
  43. std::map<std::string,_E>::iterator e(_db.find(n));
  44. if (e != _db.end()) {
  45. if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
  46. return e->second.obj;
  47. std::string path(_genPath(n,false));
  48. if (!path.length()) // sanity check
  49. return _EMPTY_JSON;
  50. // We are somewhat tolerant to momentary disk failures here. This may
  51. // occur over e.g. EC2's elastic filesystem (NFS).
  52. const uint64_t lm = OSUtils::getLastModified(path.c_str());
  53. if (e->second.lastModifiedOnDisk != lm) {
  54. if (OSUtils::readFile(path.c_str(),buf)) {
  55. try {
  56. e->second.obj = nlohmann::json::parse(buf);
  57. e->second.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP
  58. e->second.lastCheck = now;
  59. } catch ( ... ) {} // parse errors result in "holding pattern" behavior
  60. }
  61. }
  62. return e->second.obj;
  63. } else {
  64. std::string path(_genPath(n,false));
  65. if (!path.length())
  66. return _EMPTY_JSON;
  67. if (!OSUtils::readFile(path.c_str(),buf))
  68. return _EMPTY_JSON;
  69. const uint64_t lm = OSUtils::getLastModified(path.c_str());
  70. _E &e2 = _db[n];
  71. try {
  72. e2.obj = nlohmann::json::parse(buf);
  73. } catch ( ... ) {
  74. e2.obj = _EMPTY_JSON;
  75. }
  76. e2.lastModifiedOnDisk = lm;
  77. e2.lastCheck = now;
  78. return e2.obj;
  79. }
  80. }
  81. void JSONDB::erase(const std::string &n)
  82. {
  83. if (!_isValidObjectName(n))
  84. return;
  85. std::string path(_genPath(n,true));
  86. if (!path.length())
  87. return;
  88. OSUtils::rm(path.c_str());
  89. _db.erase(n);
  90. }
  91. void JSONDB::_reload(const std::string &p)
  92. {
  93. std::map<std::string,char> l(OSUtils::listDirectoryFull(p.c_str()));
  94. for(std::map<std::string,char>::iterator li(l.begin());li!=l.end();++li) {
  95. if (li->second == 'f') {
  96. // assume p starts with _basePath, which it always does -- will throw otherwise
  97. std::string n(p.substr(_basePath.length()));
  98. while ((n.length() > 0)&&(n[0] == ZT_PATH_SEPARATOR)) n = n.substr(1);
  99. if (ZT_PATH_SEPARATOR != '/') std::replace(n.begin(),n.end(),ZT_PATH_SEPARATOR,'/');
  100. if ((n.length() > 0)&&(n[n.length() - 1] != '/')) n.push_back('/');
  101. n.append(li->first);
  102. if ((n.length() > 5)&&(n.substr(n.length() - 5) == ".json")) {
  103. this->get(n.substr(0,n.length() - 5),0); // causes load and cache or update
  104. }
  105. } else if (li->second == 'd') {
  106. this->_reload(p + ZT_PATH_SEPARATOR + li->first);
  107. }
  108. }
  109. }
  110. bool JSONDB::_isValidObjectName(const std::string &n)
  111. {
  112. if (n.length() == 0)
  113. return false;
  114. const char *p = n.c_str();
  115. char c;
  116. // For security reasons we should not allow dots, backslashes, or other path characters or potential path characters.
  117. while ((c = *(p++))) {
  118. if (!( ((c >= 'a')&&(c <= 'z')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= '0')&&(c <= '9')) || (c == '/') || (c == '_') || (c == '~') || (c == '-') ))
  119. return false;
  120. }
  121. return true;
  122. }
  123. std::string JSONDB::_genPath(const std::string &n,bool create)
  124. {
  125. std::vector<std::string> pt(OSUtils::split(n.c_str(),"/","",""));
  126. if (pt.size() == 0)
  127. return std::string();
  128. std::string p(_basePath);
  129. if (create) OSUtils::mkdir(p.c_str());
  130. for(unsigned long i=0,j=pt.size()-1;i<j;++i) {
  131. p.push_back(ZT_PATH_SEPARATOR);
  132. p.append(pt[i]);
  133. if (create) OSUtils::mkdir(p.c_str());
  134. }
  135. p.push_back(ZT_PATH_SEPARATOR);
  136. p.append(pt[pt.size()-1]);
  137. p.append(".json");
  138. return p;
  139. }
  140. } // namespace ZeroTier