JSONDB.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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,const std::string &b)
  105. {
  106. std::vector<std::string> dl(OSUtils::listDirectory(p.c_str()));
  107. for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
  108. if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
  109. this->get(b + di->substr(0,di->length() - 5),0);
  110. } else {
  111. this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
  112. }
  113. }
  114. }
  115. bool JSONDB::_isValidObjectName(const std::string &n)
  116. {
  117. if (n.length() == 0)
  118. return false;
  119. const char *p = n.c_str();
  120. char c;
  121. // For security reasons we should not allow dots, backslashes, or other path characters or potential path characters.
  122. while ((c = *(p++))) {
  123. if (!( ((c >= 'a')&&(c <= 'z')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= '0')&&(c <= '9')) || (c == '/') || (c == '_') || (c == '~') || (c == '-') ))
  124. return false;
  125. }
  126. return true;
  127. }
  128. std::string JSONDB::_genPath(const std::string &n,bool create)
  129. {
  130. std::vector<std::string> pt(OSUtils::split(n.c_str(),"/","",""));
  131. if (pt.size() == 0)
  132. return std::string();
  133. std::string p(_basePath);
  134. if (create) OSUtils::mkdir(p.c_str());
  135. for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i<j;++i) {
  136. p.push_back(ZT_PATH_SEPARATOR);
  137. p.append(pt[i]);
  138. if (create) OSUtils::mkdir(p.c_str());
  139. }
  140. p.push_back(ZT_PATH_SEPARATOR);
  141. p.append(pt[pt.size()-1]);
  142. p.append(".json");
  143. return p;
  144. }
  145. } // namespace ZeroTier