JSONDB.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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({{}});
  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,false));
  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.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
  33. e.lastCheck = OSUtils::now();
  34. e.obj = obj;
  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 ((lm)&&(e->second.lastModifiedOnDisk != lm)) {
  54. if (OSUtils::readFile(path.c_str(),buf)) {
  55. try {
  56. e->second.lastModifiedOnDisk = lm;
  57. e->second.lastCheck = now;
  58. e->second.obj = nlohmann::json::parse(buf);
  59. } catch ( ... ) {
  60. e->second.obj = _EMPTY_JSON;
  61. }
  62. }
  63. }
  64. return e->second.obj;
  65. } else {
  66. std::string path(_genPath(n,false));
  67. if (!path.length())
  68. return _EMPTY_JSON;
  69. if (!OSUtils::readFile(path.c_str(),buf))
  70. return _EMPTY_JSON;
  71. const uint64_t lm = OSUtils::getLastModified(path.c_str());
  72. if (!lm)
  73. return _EMPTY_JSON;
  74. _E &e2 = _db[n];
  75. e2.lastModifiedOnDisk = lm;
  76. e2.lastCheck = now;
  77. try {
  78. e2.obj = nlohmann::json::parse(buf);
  79. } catch ( ... ) {
  80. e2.obj = _EMPTY_JSON;
  81. }
  82. return e2.obj;
  83. }
  84. }
  85. bool JSONDB::_isValidObjectName(const std::string &n)
  86. {
  87. if (n.length() == 0)
  88. return false;
  89. const char *p = n.c_str();
  90. char c;
  91. // For security reasons we should not allow dots, backslashes, or other path characters or potential path characters.
  92. while ((c = *(p++))) {
  93. if (!( ((c >= 'a')&&(c <= 'z')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= '0')&&(c <= '9')) || (c == '/') || (c == '_') || (c == '~') || (c == '-') ))
  94. return false;
  95. }
  96. return true;
  97. }
  98. std::string JSONDB::_genPath(const std::string &n,bool create)
  99. {
  100. std::vector<std::string> pt(Utils::split(n.c_str(),"/","",""));
  101. if (pt.size() == 0)
  102. return std::string();
  103. if (pt.size() == 1)
  104. return pt[0];
  105. std::string p(_basePath);
  106. if (create) OSUtils::mkdir(p.c_str());
  107. for(unsigned long i=0,j=pt.size()-1;i<j;++i) {
  108. p.push_back(ZT_PATH_SEPARATOR);
  109. p.append(pt[i]);
  110. if (create) OSUtils::mkdir(p.c_str());
  111. }
  112. p.push_back(ZT_PATH_SEPARATOR);
  113. p.append(pt[pt.size()-1]);
  114. p.append(".json");
  115. return p;
  116. }
  117. void JSONDB::_reloadAll(const std::string &path)
  118. {
  119. }
  120. } // namespace ZeroTier