JSONDB.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #define ZT_JSONDB_HTTP_TIMEOUT 60000
  20. namespace ZeroTier {
  21. static const nlohmann::json _EMPTY_JSON(nlohmann::json::object());
  22. static const std::map<std::string,std::string> _ZT_JSONDB_GET_HEADERS;
  23. JSONDB::JSONDB(const std::string &basePath) :
  24. _basePath(basePath)
  25. {
  26. if ((_basePath.length() > 7)&&(_basePath.substr(0,7) == "http://")) {
  27. // TODO: this doesn't yet support IPv6 since bracketed address notiation isn't supported.
  28. // Typically it's used with 127.0.0.1 anyway.
  29. std::string hn = _basePath.substr(7);
  30. std::size_t hnend = hn.find_first_of('/');
  31. if (hnend != std::string::npos)
  32. hn = hn.substr(0,hnend);
  33. std::size_t hnsep = hn.find_last_of(':');
  34. if (hnsep != std::string::npos)
  35. hn[hnsep] = '/';
  36. _httpAddr.fromString(hn);
  37. if (hnend != std::string::npos)
  38. _basePath = _basePath.substr(7 + hnend);
  39. if (_basePath.length() == 0)
  40. _basePath = "/";
  41. if (_basePath[0] != '/')
  42. _basePath = std::string("/") + _basePath;
  43. } else {
  44. OSUtils::mkdir(_basePath.c_str());
  45. OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions
  46. }
  47. _reload(_basePath,std::string());
  48. }
  49. bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
  50. {
  51. if (!_isValidObjectName(n))
  52. return false;
  53. if (_httpAddr) {
  54. std::map<std::string,std::string> headers;
  55. std::string body;
  56. std::map<std::string,std::string> reqHeaders;
  57. char tmp[64];
  58. Utils::snprintf(tmp,sizeof(tmp),"%lu",(unsigned long)obj.length());
  59. reqHeaders["Content-Length"] = tmp;
  60. reqHeaders["Content-Type"] = "application/json";
  61. const unsigned int sc = Http::PUT(1048576,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),reqHeaders,obj.data(),obj.length(),headers,body);
  62. return (sc == 200);
  63. } else {
  64. const std::string path(_genPath(n,true));
  65. if (!path.length())
  66. return false;
  67. return OSUtils::writeFile(path.c_str(),obj);
  68. }
  69. }
  70. bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
  71. {
  72. const bool r = writeRaw(n,OSUtils::jsonDump(obj));
  73. _db[n].obj = obj;
  74. return r;
  75. }
  76. const nlohmann::json &JSONDB::get(const std::string &n)
  77. {
  78. if (!_isValidObjectName(n))
  79. return _EMPTY_JSON;
  80. std::map<std::string,_E>::iterator e(_db.find(n));
  81. if (e != _db.end())
  82. return e->second.obj;
  83. std::string buf;
  84. if (_httpAddr) {
  85. std::map<std::string,std::string> headers;
  86. const unsigned int sc = Http::GET(1048576,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),_ZT_JSONDB_GET_HEADERS,headers,buf);
  87. if (sc != 200)
  88. return _EMPTY_JSON;
  89. } else {
  90. const std::string path(_genPath(n,false));
  91. if (!path.length())
  92. return _EMPTY_JSON;
  93. if (!OSUtils::readFile(path.c_str(),buf))
  94. return _EMPTY_JSON;
  95. }
  96. try {
  97. _E &e2 = _db[n];
  98. e2.obj = OSUtils::jsonParse(buf);
  99. return e2.obj;
  100. } catch ( ... ) {
  101. _db.erase(n);
  102. return _EMPTY_JSON;
  103. }
  104. }
  105. void JSONDB::erase(const std::string &n)
  106. {
  107. if (!_isValidObjectName(n))
  108. return;
  109. if (_httpAddr) {
  110. std::string body;
  111. std::map<std::string,std::string> headers;
  112. Http::DEL(1048576,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
  113. } else {
  114. std::string path(_genPath(n,true));
  115. if (!path.length())
  116. return;
  117. OSUtils::rm(path.c_str());
  118. }
  119. _db.erase(n);
  120. }
  121. void JSONDB::_reload(const std::string &p,const std::string &b)
  122. {
  123. if (_httpAddr) {
  124. std::string body;
  125. std::map<std::string,std::string> headers;
  126. const unsigned int sc = Http::GET(2147483647,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),_basePath.c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
  127. if (sc == 200) {
  128. try {
  129. nlohmann::json dbImg(OSUtils::jsonParse(body));
  130. std::string tmp;
  131. if (dbImg.is_object()) {
  132. for(nlohmann::json::iterator i(dbImg.begin());i!=dbImg.end();++i) {
  133. if (i.value().is_object()) {
  134. tmp = i.key();
  135. _db[tmp].obj = i.value();
  136. }
  137. }
  138. }
  139. } catch ( ... ) {
  140. // TODO: report error?
  141. }
  142. }
  143. } else {
  144. std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
  145. for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
  146. if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
  147. this->get(b + di->substr(0,di->length() - 5));
  148. } else {
  149. this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
  150. }
  151. }
  152. }
  153. }
  154. bool JSONDB::_isValidObjectName(const std::string &n)
  155. {
  156. if (n.length() == 0)
  157. return false;
  158. const char *p = n.c_str();
  159. char c;
  160. // For security reasons we should not allow dots, backslashes, or other path characters or potential path characters.
  161. while ((c = *(p++))) {
  162. if (!( ((c >= 'a')&&(c <= 'z')) || ((c >= 'A')&&(c <= 'Z')) || ((c >= '0')&&(c <= '9')) || (c == '/') || (c == '_') || (c == '~') || (c == '-') ))
  163. return false;
  164. }
  165. return true;
  166. }
  167. std::string JSONDB::_genPath(const std::string &n,bool create)
  168. {
  169. std::vector<std::string> pt(OSUtils::split(n.c_str(),"/","",""));
  170. if (pt.size() == 0)
  171. return std::string();
  172. char sep;
  173. if (_httpAddr) {
  174. sep = '/';
  175. create = false;
  176. } else {
  177. sep = ZT_PATH_SEPARATOR;
  178. }
  179. std::string p(_basePath);
  180. if (create) OSUtils::mkdir(p.c_str());
  181. for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i<j;++i) {
  182. p.push_back(sep);
  183. p.append(pt[i]);
  184. if (create) OSUtils::mkdir(p.c_str());
  185. }
  186. p.push_back(sep);
  187. p.append(pt[pt.size()-1]);
  188. p.append(".json");
  189. return p;
  190. }
  191. } // namespace ZeroTier