JSONDB.cpp 5.0 KB

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