2017-11-07 22:44:46 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2017-11-07 22:44:46 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2017-11-07 22:44:46 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2017-11-07 22:44:46 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* 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.
|
2017-11-07 22:44:46 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
#include "DB.hpp"
|
|
|
|
#include "EmbeddedNetworkController.hpp"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2018-01-09 20:39:25 +00:00
|
|
|
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;
|
2019-08-08 22:56:27 +00:00
|
|
|
if (!network.count("rulesSource")) network["rulesSource"] = "";
|
2018-01-09 20:39:25 +00:00
|
|
|
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" }
|
|
|
|
}};
|
|
|
|
}
|
2020-08-04 20:52:57 +00:00
|
|
|
if (!network.count("dns")) network["dns"] = nlohmann::json::array();
|
2021-06-03 21:38:26 +00:00
|
|
|
if (!network.count("ssoEnabled")) network["ssoEnabled"] = false;
|
|
|
|
if (!network.count("clientId")) network["clientId"] = "";
|
|
|
|
if (!network.count("authorizationEndpoint")) network["authorizationEndpoint"] = "";
|
2020-08-04 20:52:57 +00:00
|
|
|
|
2018-01-09 20:39:25 +00:00
|
|
|
network["objtype"] = "network";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DB::initMember(nlohmann::json &member)
|
|
|
|
{
|
|
|
|
if (!member.count("authorized")) member["authorized"] = false;
|
2021-07-23 22:49:00 +00:00
|
|
|
if (!member.count("ssoExempt")) member["ssoExempt"] = false;
|
2018-01-09 20:39:25 +00:00
|
|
|
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();
|
2021-06-04 20:19:39 +00:00
|
|
|
if (!member.count("authenticationExpiryTime")) member["authenticationExpiryTime"] = 0LL;
|
2018-01-09 20:39:25 +00:00
|
|
|
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");
|
2021-05-28 21:08:24 +00:00
|
|
|
member.erase("authenticationURL"); // computed
|
|
|
|
member.erase("authenticationClientID"); // computed
|
2018-01-09 20:39:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
DB::DB() {}
|
2019-07-27 00:39:00 +00:00
|
|
|
DB::~DB() {}
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
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;
|
2021-05-28 21:08:24 +00:00
|
|
|
for(auto m=nw->members.begin();m!=nw->members.end();++m) {
|
2017-11-07 22:44:46 +00:00
|
|
|
members.push_back(m->second);
|
2021-05-28 21:08:24 +00:00
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void DB::networks(std::set<uint64_t> &networks)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
|
|
|
waitForReady();
|
|
|
|
std::lock_guard<std::mutex> l(_networks_l);
|
|
|
|
for(auto n=_networks.begin();n!=_networks.end();++n)
|
2019-08-06 15:42:54 +00:00
|
|
|
networks.insert(n->first);
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 16:17:40 +00:00
|
|
|
void DB::networkMemberSSOHasExpired(uint64_t nwid, int64_t now) {
|
|
|
|
std::lock_guard<std::mutex> l(_networks_l);
|
|
|
|
auto nw = _networks.find(nwid);
|
|
|
|
if (nw != _networks.end()) {
|
|
|
|
nw->second->mostRecentDeauthTime = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
|
|
|
uint64_t memberId = 0;
|
|
|
|
uint64_t networkId = 0;
|
|
|
|
bool isAuth = false;
|
|
|
|
bool wasAuth = false;
|
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
|
2018-03-09 06:33:08 +00:00
|
|
|
if (old.is_object()) {
|
|
|
|
memberId = OSUtils::jsonIntHex(old["id"],0ULL);
|
|
|
|
networkId = OSUtils::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 (OSUtils::jsonBool(old["activeBridge"],false))
|
|
|
|
nw->activeBridgeMembers.erase(memberId);
|
|
|
|
wasAuth = OSUtils::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 = OSUtils::jsonIntHex(memberConfig["id"],0ULL);
|
|
|
|
networkId = OSUtils::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 (OSUtils::jsonBool(memberConfig["activeBridge"],false))
|
|
|
|
nw->activeBridgeMembers.insert(memberId);
|
|
|
|
isAuth = OSUtils::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)OSUtils::jsonInt(memberConfig["lastDeauthorizedTime"],0ULL);
|
|
|
|
if (ldt > nw->mostRecentDeauthTime)
|
|
|
|
nw->mostRecentDeauthTime = ldt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
if (notifyListeners) {
|
2019-07-27 00:39:00 +00:00
|
|
|
std::lock_guard<std::mutex> ll(_changeListeners_l);
|
|
|
|
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
|
2019-08-06 12:51:50 +00:00
|
|
|
(*i)->onNetworkMemberUpdate(this,networkId,memberId,memberConfig);
|
2019-07-27 00:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
if ((notifyListeners)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId))) {
|
2019-07-27 00:39:00 +00:00
|
|
|
std::lock_guard<std::mutex> ll(_changeListeners_l);
|
|
|
|
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
|
2019-08-06 12:51:50 +00:00
|
|
|
(*i)->onNetworkMemberDeauthorize(this,networkId,memberId);
|
2019-07-27 00:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
2018-03-09 06:33:08 +00:00
|
|
|
if (networkConfig.is_object()) {
|
|
|
|
const std::string ids = networkConfig["id"];
|
2019-07-27 00:39:00 +00:00
|
|
|
const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
|
|
|
|
if (networkId) {
|
2018-03-09 06:33:08 +00:00
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(_networks_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
std::shared_ptr<_Network> &nw2 = _networks[networkId];
|
2018-03-09 06:33:08 +00:00
|
|
|
if (!nw2)
|
|
|
|
nw2.reset(new _Network);
|
|
|
|
nw = nw2;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l2(nw->lock);
|
|
|
|
nw->config = networkConfig;
|
|
|
|
}
|
2019-08-06 15:42:54 +00:00
|
|
|
if (notifyListeners) {
|
2019-07-27 00:39:00 +00:00
|
|
|
std::lock_guard<std::mutex> ll(_changeListeners_l);
|
|
|
|
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
|
2019-08-06 12:51:50 +00:00
|
|
|
(*i)->onNetworkUpdate(this,networkId,networkConfig);
|
2019-07-27 00:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
}
|
|
|
|
} else if (old.is_object()) {
|
|
|
|
const std::string ids = old["id"];
|
2019-07-27 00:39:00 +00:00
|
|
|
const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
|
|
|
|
if (networkId) {
|
2018-03-09 06:33:08 +00:00
|
|
|
std::lock_guard<std::mutex> l(_networks_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
_networks.erase(networkId);
|
2018-03-09 06:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DB::_fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
|
|
|
|
{
|
|
|
|
for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab)
|
|
|
|
info.activeBridges.push_back(Address(*ab));
|
2018-01-12 18:38:19 +00:00
|
|
|
std::sort(info.activeBridges.begin(),info.activeBridges.end());
|
2017-11-07 22:44:46 +00:00
|
|
|
for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
|
|
|
|
info.allocatedIps.push_back(*ip);
|
2018-01-12 18:38:19 +00:00
|
|
|
std::sort(info.allocatedIps.begin(),info.allocatedIps.end());
|
2017-11-07 22:44:46 +00:00
|
|
|
info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
|
|
|
|
info.totalMemberCount = (unsigned long)nw->members.size();
|
|
|
|
info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|