123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- /*
- * Copyright (c)2013-2020 ZeroTier, Inc.
- *
- * Use of this software is governed by the Business Source License included
- * in the LICENSE.TXT file in the project's root directory.
- *
- * Change Date: 2025-01-01
- *
- * On the date above, in accordance with the Business Source License, use
- * of this software will be governed by version 2.0 of the Apache License.
- */
- /****/
- #include "DB.hpp"
- #include "EmbeddedNetworkController.hpp"
- #include <chrono>
- #include <algorithm>
- #include <stdexcept>
- using json = nlohmann::json;
- namespace ZeroTier {
- void DB::initNetwork(nlohmann::json &network)
- {
- if (!network.count("private")) network["private"] = true;
- if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
- if (!network.count("name")) network["name"] = "";
- if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
- if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
- if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
- if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
- if (!network.count("authTokens")) network["authTokens"] = {{}};
- if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
- if (!network.count("tags")) network["tags"] = nlohmann::json::array();
- if (!network.count("routes")) network["routes"] = nlohmann::json::array();
- if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
- if (!network.count("mtu")) network["mtu"] = ZT_DEFAULT_MTU;
- if (!network.count("remoteTraceTarget")) network["remoteTraceTarget"] = nlohmann::json();
- if (!network.count("removeTraceLevel")) network["remoteTraceLevel"] = 0;
- if (!network.count("rulesSource")) network["rulesSource"] = "";
- if (!network.count("rules")) {
- // If unspecified, rules are set to allow anything and behave like a flat L2 segment
- network["rules"] = {{
- { "not",false },
- { "or", false },
- { "type","ACTION_ACCEPT" }
- }};
- }
- network["objtype"] = "network";
- }
- void DB::initMember(nlohmann::json &member)
- {
- if (!member.count("authorized")) member["authorized"] = false;
- if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
- if (!member.count("activeBridge")) member["activeBridge"] = false;
- if (!member.count("tags")) member["tags"] = nlohmann::json::array();
- if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
- if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
- if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
- if (!member.count("revision")) member["revision"] = 0ULL;
- if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
- if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
- if (!member.count("lastAuthorizedCredentialType")) member["lastAuthorizedCredentialType"] = nlohmann::json();
- if (!member.count("lastAuthorizedCredential")) member["lastAuthorizedCredential"] = nlohmann::json();
- if (!member.count("vMajor")) member["vMajor"] = -1;
- if (!member.count("vMinor")) member["vMinor"] = -1;
- if (!member.count("vRev")) member["vRev"] = -1;
- if (!member.count("vProto")) member["vProto"] = -1;
- if (!member.count("remoteTraceTarget")) member["remoteTraceTarget"] = nlohmann::json();
- if (!member.count("removeTraceLevel")) member["remoteTraceLevel"] = 0;
- member["objtype"] = "member";
- }
- void DB::cleanNetwork(nlohmann::json &network)
- {
- network.erase("clock");
- network.erase("authorizedMemberCount");
- network.erase("activeMemberCount");
- network.erase("totalMemberCount");
- network.erase("lastModified");
- }
- void DB::cleanMember(nlohmann::json &member)
- {
- member.erase("clock");
- member.erase("physicalAddr");
- member.erase("recentLog");
- member.erase("lastModified");
- member.erase("lastRequestMetaData");
- }
- DB::DB() {}
- DB::~DB() {}
- bool DB::get(const uint64_t networkId,nlohmann::json &network)
- {
- waitForReady();
- std::shared_ptr<_Network> nw;
- {
- std::lock_guard<std::mutex> l(_networks_l);
- auto nwi = _networks.find(networkId);
- if (nwi == _networks.end())
- return false;
- nw = nwi->second;
- }
- {
- std::lock_guard<std::mutex> l2(nw->lock);
- network = nw->config;
- }
- return true;
- }
- bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member)
- {
- waitForReady();
- std::shared_ptr<_Network> nw;
- {
- std::lock_guard<std::mutex> l(_networks_l);
- auto nwi = _networks.find(networkId);
- if (nwi == _networks.end())
- return false;
- nw = nwi->second;
- }
- {
- std::lock_guard<std::mutex> l2(nw->lock);
- network = nw->config;
- auto m = nw->members.find(memberId);
- if (m == nw->members.end())
- return false;
- member = m->second;
- }
- return true;
- }
- bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info)
- {
- waitForReady();
- std::shared_ptr<_Network> nw;
- {
- std::lock_guard<std::mutex> l(_networks_l);
- auto nwi = _networks.find(networkId);
- if (nwi == _networks.end())
- return false;
- nw = nwi->second;
- }
- {
- std::lock_guard<std::mutex> l2(nw->lock);
- network = nw->config;
- _fillSummaryInfo(nw,info);
- auto m = nw->members.find(memberId);
- if (m == nw->members.end())
- return false;
- member = m->second;
- }
- return true;
- }
- bool DB::get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members)
- {
- waitForReady();
- std::shared_ptr<_Network> nw;
- {
- std::lock_guard<std::mutex> l(_networks_l);
- auto nwi = _networks.find(networkId);
- if (nwi == _networks.end())
- return false;
- nw = nwi->second;
- }
- {
- std::lock_guard<std::mutex> l2(nw->lock);
- network = nw->config;
- for(auto m=nw->members.begin();m!=nw->members.end();++m)
- members.push_back(m->second);
- }
- return true;
- }
- void DB::networks(std::set<uint64_t> &networks)
- {
- waitForReady();
- std::lock_guard<std::mutex> l(_networks_l);
- for(auto n=_networks.begin();n!=_networks.end();++n)
- networks.insert(n->first);
- }
- void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners)
- {
- uint64_t memberId = 0;
- uint64_t networkId = 0;
- bool isAuth = false;
- bool wasAuth = false;
- std::shared_ptr<_Network> nw;
- if (old.is_object()) {
- memberId = DB::jsonIntHex(old["id"],0ULL);
- networkId = DB::jsonIntHex(old["nwid"],0ULL);
- if ((memberId)&&(networkId)) {
- {
- std::lock_guard<std::mutex> l(_networks_l);
- auto nw2 = _networks.find(networkId);
- if (nw2 != _networks.end())
- nw = nw2->second;
- }
- if (nw) {
- std::lock_guard<std::mutex> l(nw->lock);
- if (DB::jsonBool(old["activeBridge"],false))
- nw->activeBridgeMembers.erase(memberId);
- wasAuth = DB::jsonBool(old["authorized"],false);
- if (wasAuth)
- nw->authorizedMembers.erase(memberId);
- json &ips = old["ipAssignments"];
- if (ips.is_array()) {
- for(unsigned long i=0;i<ips.size();++i) {
- json &ipj = ips[i];
- if (ipj.is_string()) {
- const std::string ips = ipj;
- InetAddress ipa(ips.c_str());
- ipa.setPort(0);
- nw->allocatedIps.erase(ipa);
- }
- }
- }
- }
- }
- }
- if (memberConfig.is_object()) {
- if (!nw) {
- memberId = DB::jsonIntHex(memberConfig["id"],0ULL);
- networkId = DB::jsonIntHex(memberConfig["nwid"],0ULL);
- if ((!memberId)||(!networkId))
- return;
- std::lock_guard<std::mutex> l(_networks_l);
- std::shared_ptr<_Network> &nw2 = _networks[networkId];
- if (!nw2)
- nw2.reset(new _Network);
- nw = nw2;
- }
- {
- std::lock_guard<std::mutex> l(nw->lock);
- nw->members[memberId] = memberConfig;
- if (DB::jsonBool(memberConfig["activeBridge"],false))
- nw->activeBridgeMembers.insert(memberId);
- isAuth = DB::jsonBool(memberConfig["authorized"],false);
- if (isAuth)
- nw->authorizedMembers.insert(memberId);
- json &ips = memberConfig["ipAssignments"];
- if (ips.is_array()) {
- for(unsigned long i=0;i<ips.size();++i) {
- json &ipj = ips[i];
- if (ipj.is_string()) {
- const std::string ips = ipj;
- InetAddress ipa(ips.c_str());
- ipa.setPort(0);
- nw->allocatedIps.insert(ipa);
- }
- }
- }
- if (!isAuth) {
- const int64_t ldt = (int64_t)DB::jsonInt(memberConfig["lastDeauthorizedTime"],0ULL);
- if (ldt > nw->mostRecentDeauthTime)
- nw->mostRecentDeauthTime = ldt;
- }
- }
- if (notifyListeners) {
- std::lock_guard<std::mutex> ll(_changeListeners_l);
- for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
- (*i)->onNetworkMemberUpdate(this,networkId,memberId,memberConfig);
- }
- }
- } else if (memberId) {
- if (nw) {
- std::lock_guard<std::mutex> l(nw->lock);
- nw->members.erase(memberId);
- }
- if (networkId) {
- std::lock_guard<std::mutex> l(_networks_l);
- auto er = _networkByMember.equal_range(memberId);
- for(auto i=er.first;i!=er.second;++i) {
- if (i->second == networkId) {
- _networkByMember.erase(i);
- break;
- }
- }
- }
- }
- if ((notifyListeners)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId))) {
- std::lock_guard<std::mutex> ll(_changeListeners_l);
- for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
- (*i)->onNetworkMemberDeauthorize(this,networkId,memberId);
- }
- }
- }
- void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners)
- {
- if (networkConfig.is_object()) {
- const std::string ids = networkConfig["id"];
- const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
- if (networkId) {
- std::shared_ptr<_Network> nw;
- {
- std::lock_guard<std::mutex> l(_networks_l);
- std::shared_ptr<_Network> &nw2 = _networks[networkId];
- if (!nw2)
- nw2.reset(new _Network);
- nw = nw2;
- }
- {
- std::lock_guard<std::mutex> l2(nw->lock);
- nw->config = networkConfig;
- }
- if (notifyListeners) {
- std::lock_guard<std::mutex> ll(_changeListeners_l);
- for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
- (*i)->onNetworkUpdate(this,networkId,networkConfig);
- }
- }
- }
- } else if (old.is_object()) {
- const std::string ids = old["id"];
- const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
- if (networkId) {
- std::lock_guard<std::mutex> l(_networks_l);
- _networks.erase(networkId);
- }
- }
- }
- void DB::_fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
- {
- for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab) {
- const Address aba(*ab);
- if (nw->authorizedMembers.count(aba) != 0)
- info.activeBridges.push_back(aba);
- }
- std::sort(info.activeBridges.begin(),info.activeBridges.end());
- for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
- info.allocatedIps.push_back(*ip);
- std::sort(info.allocatedIps.begin(),info.allocatedIps.end());
- info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
- info.totalMemberCount = (unsigned long)nw->members.size();
- info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
- }
- nlohmann::json DB::jsonParse(const std::string &buf) { return nlohmann::json::parse(buf.c_str()); }
- std::string DB::jsonDump(const nlohmann::json &j,int indentation) { return j.dump(indentation); }
- uint64_t DB::jsonInt(const nlohmann::json &jv,const uint64_t dfl)
- {
- try {
- if (jv.is_number()) {
- return (uint64_t)jv;
- } else if (jv.is_string()) {
- std::string s = jv;
- return (uint64_t)strtoull(s.c_str(),nullptr,10);
- } else if (jv.is_boolean()) {
- return ((bool)jv ? 1ULL : 0ULL);
- }
- } catch ( ... ) {}
- return dfl;
- }
- uint64_t DB::jsonIntHex(const nlohmann::json &jv,const uint64_t dfl)
- {
- try {
- if (jv.is_number()) {
- return (uint64_t)jv;
- } else if (jv.is_string()) {
- std::string s = jv;
- return Utils::hexStrToU64(s.c_str());
- } else if (jv.is_boolean()) {
- return ((bool)jv ? 1ULL : 0ULL);
- }
- } catch ( ... ) {}
- return dfl;
- }
- bool DB::jsonBool(const nlohmann::json &jv,const bool dfl)
- {
- try {
- if (jv.is_boolean()) {
- return (bool)jv;
- } else if (jv.is_number()) {
- return ((uint64_t)jv > 0ULL);
- } else if (jv.is_string()) {
- std::string s = jv;
- if (s.length() > 0) {
- switch(s[0]) {
- case 't':
- case 'T':
- case '1':
- return true;
- }
- }
- return false;
- }
- } catch ( ... ) {}
- return dfl;
- }
- std::string DB::jsonString(const nlohmann::json &jv,const char *dfl)
- {
- try {
- if (jv.is_string()) {
- return jv;
- } else if (jv.is_number()) {
- char tmp[64];
- OSUtils::ztsnprintf(tmp,sizeof(tmp),"%llu",(uint64_t)jv);
- return tmp;
- } else if (jv.is_boolean()) {
- return ((bool)jv ? std::string("1") : std::string("0"));
- }
- } catch ( ... ) {}
- return std::string((dfl) ? dfl : "");
- }
- } // namespace ZeroTier
|