DB.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "DB.hpp"
  14. #include "EmbeddedNetworkController.hpp"
  15. #include <chrono>
  16. #include <algorithm>
  17. #include <stdexcept>
  18. using json = nlohmann::json;
  19. namespace ZeroTier {
  20. void DB::initNetwork(nlohmann::json &network)
  21. {
  22. if (!network.count("private")) network["private"] = true;
  23. if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
  24. if (!network.count("name")) network["name"] = "";
  25. if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
  26. if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
  27. if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
  28. if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
  29. if (!network.count("authTokens")) network["authTokens"] = {{}};
  30. if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
  31. if (!network.count("tags")) network["tags"] = nlohmann::json::array();
  32. if (!network.count("routes")) network["routes"] = nlohmann::json::array();
  33. if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
  34. if (!network.count("mtu")) network["mtu"] = ZT_DEFAULT_MTU;
  35. if (!network.count("remoteTraceTarget")) network["remoteTraceTarget"] = nlohmann::json();
  36. if (!network.count("removeTraceLevel")) network["remoteTraceLevel"] = 0;
  37. if (!network.count("rulesSource")) network["rulesSource"] = "";
  38. if (!network.count("rules")) {
  39. // If unspecified, rules are set to allow anything and behave like a flat L2 segment
  40. network["rules"] = {{
  41. { "not",false },
  42. { "or", false },
  43. { "type","ACTION_ACCEPT" }
  44. }};
  45. }
  46. network["objtype"] = "network";
  47. }
  48. void DB::initMember(nlohmann::json &member)
  49. {
  50. if (!member.count("authorized")) member["authorized"] = false;
  51. if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
  52. if (!member.count("activeBridge")) member["activeBridge"] = false;
  53. if (!member.count("tags")) member["tags"] = nlohmann::json::array();
  54. if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
  55. if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
  56. if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
  57. if (!member.count("revision")) member["revision"] = 0ULL;
  58. if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
  59. if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
  60. if (!member.count("lastAuthorizedCredentialType")) member["lastAuthorizedCredentialType"] = nlohmann::json();
  61. if (!member.count("lastAuthorizedCredential")) member["lastAuthorizedCredential"] = nlohmann::json();
  62. if (!member.count("vMajor")) member["vMajor"] = -1;
  63. if (!member.count("vMinor")) member["vMinor"] = -1;
  64. if (!member.count("vRev")) member["vRev"] = -1;
  65. if (!member.count("vProto")) member["vProto"] = -1;
  66. if (!member.count("remoteTraceTarget")) member["remoteTraceTarget"] = nlohmann::json();
  67. if (!member.count("removeTraceLevel")) member["remoteTraceLevel"] = 0;
  68. member["objtype"] = "member";
  69. }
  70. void DB::cleanNetwork(nlohmann::json &network)
  71. {
  72. network.erase("clock");
  73. network.erase("authorizedMemberCount");
  74. network.erase("activeMemberCount");
  75. network.erase("totalMemberCount");
  76. network.erase("lastModified");
  77. }
  78. void DB::cleanMember(nlohmann::json &member)
  79. {
  80. member.erase("clock");
  81. member.erase("physicalAddr");
  82. member.erase("recentLog");
  83. member.erase("lastModified");
  84. member.erase("lastRequestMetaData");
  85. }
  86. DB::DB() {}
  87. DB::~DB() {}
  88. bool DB::get(const uint64_t networkId,nlohmann::json &network)
  89. {
  90. waitForReady();
  91. std::shared_ptr<_Network> nw;
  92. {
  93. std::lock_guard<std::mutex> l(_networks_l);
  94. auto nwi = _networks.find(networkId);
  95. if (nwi == _networks.end())
  96. return false;
  97. nw = nwi->second;
  98. }
  99. {
  100. std::lock_guard<std::mutex> l2(nw->lock);
  101. network = nw->config;
  102. }
  103. return true;
  104. }
  105. bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member)
  106. {
  107. waitForReady();
  108. std::shared_ptr<_Network> nw;
  109. {
  110. std::lock_guard<std::mutex> l(_networks_l);
  111. auto nwi = _networks.find(networkId);
  112. if (nwi == _networks.end())
  113. return false;
  114. nw = nwi->second;
  115. }
  116. {
  117. std::lock_guard<std::mutex> l2(nw->lock);
  118. network = nw->config;
  119. auto m = nw->members.find(memberId);
  120. if (m == nw->members.end())
  121. return false;
  122. member = m->second;
  123. }
  124. return true;
  125. }
  126. bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info)
  127. {
  128. waitForReady();
  129. std::shared_ptr<_Network> nw;
  130. {
  131. std::lock_guard<std::mutex> l(_networks_l);
  132. auto nwi = _networks.find(networkId);
  133. if (nwi == _networks.end())
  134. return false;
  135. nw = nwi->second;
  136. }
  137. {
  138. std::lock_guard<std::mutex> l2(nw->lock);
  139. network = nw->config;
  140. _fillSummaryInfo(nw,info);
  141. auto m = nw->members.find(memberId);
  142. if (m == nw->members.end())
  143. return false;
  144. member = m->second;
  145. }
  146. return true;
  147. }
  148. bool DB::get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members)
  149. {
  150. waitForReady();
  151. std::shared_ptr<_Network> nw;
  152. {
  153. std::lock_guard<std::mutex> l(_networks_l);
  154. auto nwi = _networks.find(networkId);
  155. if (nwi == _networks.end())
  156. return false;
  157. nw = nwi->second;
  158. }
  159. {
  160. std::lock_guard<std::mutex> l2(nw->lock);
  161. network = nw->config;
  162. for(auto m=nw->members.begin();m!=nw->members.end();++m)
  163. members.push_back(m->second);
  164. }
  165. return true;
  166. }
  167. void DB::networks(std::set<uint64_t> &networks)
  168. {
  169. waitForReady();
  170. std::lock_guard<std::mutex> l(_networks_l);
  171. for(auto n=_networks.begin();n!=_networks.end();++n)
  172. networks.insert(n->first);
  173. }
  174. void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners)
  175. {
  176. uint64_t memberId = 0;
  177. uint64_t networkId = 0;
  178. bool isAuth = false;
  179. bool wasAuth = false;
  180. std::shared_ptr<_Network> nw;
  181. if (old.is_object()) {
  182. memberId = DB::jsonIntHex(old["id"],0ULL);
  183. networkId = DB::jsonIntHex(old["nwid"],0ULL);
  184. if ((memberId)&&(networkId)) {
  185. {
  186. std::lock_guard<std::mutex> l(_networks_l);
  187. auto nw2 = _networks.find(networkId);
  188. if (nw2 != _networks.end())
  189. nw = nw2->second;
  190. }
  191. if (nw) {
  192. std::lock_guard<std::mutex> l(nw->lock);
  193. if (DB::jsonBool(old["activeBridge"],false))
  194. nw->activeBridgeMembers.erase(memberId);
  195. wasAuth = DB::jsonBool(old["authorized"],false);
  196. if (wasAuth)
  197. nw->authorizedMembers.erase(memberId);
  198. json &ips = old["ipAssignments"];
  199. if (ips.is_array()) {
  200. for(unsigned long i=0;i<ips.size();++i) {
  201. json &ipj = ips[i];
  202. if (ipj.is_string()) {
  203. const std::string ips = ipj;
  204. InetAddress ipa(ips.c_str());
  205. ipa.setPort(0);
  206. nw->allocatedIps.erase(ipa);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. if (memberConfig.is_object()) {
  214. if (!nw) {
  215. memberId = DB::jsonIntHex(memberConfig["id"],0ULL);
  216. networkId = DB::jsonIntHex(memberConfig["nwid"],0ULL);
  217. if ((!memberId)||(!networkId))
  218. return;
  219. std::lock_guard<std::mutex> l(_networks_l);
  220. std::shared_ptr<_Network> &nw2 = _networks[networkId];
  221. if (!nw2)
  222. nw2.reset(new _Network);
  223. nw = nw2;
  224. }
  225. {
  226. std::lock_guard<std::mutex> l(nw->lock);
  227. nw->members[memberId] = memberConfig;
  228. if (DB::jsonBool(memberConfig["activeBridge"],false))
  229. nw->activeBridgeMembers.insert(memberId);
  230. isAuth = DB::jsonBool(memberConfig["authorized"],false);
  231. if (isAuth)
  232. nw->authorizedMembers.insert(memberId);
  233. json &ips = memberConfig["ipAssignments"];
  234. if (ips.is_array()) {
  235. for(unsigned long i=0;i<ips.size();++i) {
  236. json &ipj = ips[i];
  237. if (ipj.is_string()) {
  238. const std::string ips = ipj;
  239. InetAddress ipa(ips.c_str());
  240. ipa.setPort(0);
  241. nw->allocatedIps.insert(ipa);
  242. }
  243. }
  244. }
  245. if (!isAuth) {
  246. const int64_t ldt = (int64_t)DB::jsonInt(memberConfig["lastDeauthorizedTime"],0ULL);
  247. if (ldt > nw->mostRecentDeauthTime)
  248. nw->mostRecentDeauthTime = ldt;
  249. }
  250. }
  251. if (notifyListeners) {
  252. std::lock_guard<std::mutex> ll(_changeListeners_l);
  253. for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
  254. (*i)->onNetworkMemberUpdate(this,networkId,memberId,memberConfig);
  255. }
  256. }
  257. } else if (memberId) {
  258. if (nw) {
  259. std::lock_guard<std::mutex> l(nw->lock);
  260. nw->members.erase(memberId);
  261. }
  262. if (networkId) {
  263. std::lock_guard<std::mutex> l(_networks_l);
  264. auto er = _networkByMember.equal_range(memberId);
  265. for(auto i=er.first;i!=er.second;++i) {
  266. if (i->second == networkId) {
  267. _networkByMember.erase(i);
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. if ((notifyListeners)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId))) {
  274. std::lock_guard<std::mutex> ll(_changeListeners_l);
  275. for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
  276. (*i)->onNetworkMemberDeauthorize(this,networkId,memberId);
  277. }
  278. }
  279. }
  280. void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners)
  281. {
  282. if (networkConfig.is_object()) {
  283. const std::string ids = networkConfig["id"];
  284. const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
  285. if (networkId) {
  286. std::shared_ptr<_Network> nw;
  287. {
  288. std::lock_guard<std::mutex> l(_networks_l);
  289. std::shared_ptr<_Network> &nw2 = _networks[networkId];
  290. if (!nw2)
  291. nw2.reset(new _Network);
  292. nw = nw2;
  293. }
  294. {
  295. std::lock_guard<std::mutex> l2(nw->lock);
  296. nw->config = networkConfig;
  297. }
  298. if (notifyListeners) {
  299. std::lock_guard<std::mutex> ll(_changeListeners_l);
  300. for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
  301. (*i)->onNetworkUpdate(this,networkId,networkConfig);
  302. }
  303. }
  304. }
  305. } else if (old.is_object()) {
  306. const std::string ids = old["id"];
  307. const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
  308. if (networkId) {
  309. std::lock_guard<std::mutex> l(_networks_l);
  310. _networks.erase(networkId);
  311. }
  312. }
  313. }
  314. void DB::_fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
  315. {
  316. for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab) {
  317. const Address aba(*ab);
  318. if (nw->authorizedMembers.count(aba) != 0)
  319. info.activeBridges.push_back(aba);
  320. }
  321. std::sort(info.activeBridges.begin(),info.activeBridges.end());
  322. for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
  323. info.allocatedIps.push_back(*ip);
  324. std::sort(info.allocatedIps.begin(),info.allocatedIps.end());
  325. info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
  326. info.totalMemberCount = (unsigned long)nw->members.size();
  327. info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
  328. }
  329. nlohmann::json DB::jsonParse(const std::string &buf) { return nlohmann::json::parse(buf.c_str()); }
  330. std::string DB::jsonDump(const nlohmann::json &j,int indentation) { return j.dump(indentation); }
  331. uint64_t DB::jsonInt(const nlohmann::json &jv,const uint64_t dfl)
  332. {
  333. try {
  334. if (jv.is_number()) {
  335. return (uint64_t)jv;
  336. } else if (jv.is_string()) {
  337. std::string s = jv;
  338. return (uint64_t)strtoull(s.c_str(),nullptr,10);
  339. } else if (jv.is_boolean()) {
  340. return ((bool)jv ? 1ULL : 0ULL);
  341. }
  342. } catch ( ... ) {}
  343. return dfl;
  344. }
  345. uint64_t DB::jsonIntHex(const nlohmann::json &jv,const uint64_t dfl)
  346. {
  347. try {
  348. if (jv.is_number()) {
  349. return (uint64_t)jv;
  350. } else if (jv.is_string()) {
  351. std::string s = jv;
  352. return Utils::hexStrToU64(s.c_str());
  353. } else if (jv.is_boolean()) {
  354. return ((bool)jv ? 1ULL : 0ULL);
  355. }
  356. } catch ( ... ) {}
  357. return dfl;
  358. }
  359. bool DB::jsonBool(const nlohmann::json &jv,const bool dfl)
  360. {
  361. try {
  362. if (jv.is_boolean()) {
  363. return (bool)jv;
  364. } else if (jv.is_number()) {
  365. return ((uint64_t)jv > 0ULL);
  366. } else if (jv.is_string()) {
  367. std::string s = jv;
  368. if (s.length() > 0) {
  369. switch(s[0]) {
  370. case 't':
  371. case 'T':
  372. case '1':
  373. return true;
  374. }
  375. }
  376. return false;
  377. }
  378. } catch ( ... ) {}
  379. return dfl;
  380. }
  381. std::string DB::jsonString(const nlohmann::json &jv,const char *dfl)
  382. {
  383. try {
  384. if (jv.is_string()) {
  385. return jv;
  386. } else if (jv.is_number()) {
  387. char tmp[64];
  388. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%llu",(uint64_t)jv);
  389. return tmp;
  390. } else if (jv.is_boolean()) {
  391. return ((bool)jv ? std::string("1") : std::string("0"));
  392. }
  393. } catch ( ... ) {}
  394. return std::string((dfl) ? dfl : "");
  395. }
  396. } // namespace ZeroTier