Browse Source

Delete support in harnessed mode.

Adam Ierymenko 8 years ago
parent
commit
174ba8884e

+ 16 - 10
controller/EmbeddedNetworkController.cpp

@@ -645,16 +645,8 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST(
 								}
 								}
 
 
 								// Member is being de-authorized, so spray Revocation objects to all online members
 								// Member is being de-authorized, so spray Revocation objects to all online members
-								if (!newAuth) {
-									Revocation rev((uint32_t)_node->prng(),nwid,0,now,ZT_REVOCATION_FLAG_FAST_PROPAGATE,Address(address),Revocation::CREDENTIAL_TYPE_COM);
-									rev.sign(_signingId);
-
-									Mutex::Lock _l(_memberStatus_m);
-									for(auto i=_memberStatus.begin();i!=_memberStatus.end();++i) {
-										if ((i->first.networkId == nwid)&&(i->second.online(now)))
-											_node->ncSendRevocation(Address(i->first.nodeId),rev);
-									}
-								}
+								if (!newAuth)
+									onNetworkMemberDeauthorize(nwid,address);
 							}
 							}
 						}
 						}
 
 
@@ -1154,6 +1146,20 @@ void EmbeddedNetworkController::onNetworkMemberUpdate(const uint64_t networkId,c
 	} catch ( ... ) {}
 	} catch ( ... ) {}
 }
 }
 
 
+void EmbeddedNetworkController::onNetworkMemberDeauthorize(const uint64_t networkId,const uint64_t memberId)
+{
+	const uint64_t now = OSUtils::now();
+	Revocation rev((uint32_t)_node->prng(),networkId,0,now,ZT_REVOCATION_FLAG_FAST_PROPAGATE,Address(memberId),Revocation::CREDENTIAL_TYPE_COM);
+	rev.sign(_signingId);
+	{
+		Mutex::Lock _l(_memberStatus_m);
+		for(auto i=_memberStatus.begin();i!=_memberStatus.end();++i) {
+			if ((i->first.networkId == networkId)&&(i->second.online(now)))
+				_node->ncSendRevocation(Address(i->first.nodeId),rev);
+		}
+	}
+}
+
 void EmbeddedNetworkController::threadMain()
 void EmbeddedNetworkController::threadMain()
 	throw()
 	throw()
 {
 {

+ 1 - 0
controller/EmbeddedNetworkController.hpp

@@ -96,6 +96,7 @@ public:
 	// Called on update via POST or by JSONDB on external update of network or network member records
 	// Called on update via POST or by JSONDB on external update of network or network member records
 	void onNetworkUpdate(const uint64_t networkId);
 	void onNetworkUpdate(const uint64_t networkId);
 	void onNetworkMemberUpdate(const uint64_t networkId,const uint64_t memberId);
 	void onNetworkMemberUpdate(const uint64_t networkId,const uint64_t memberId);
+	void onNetworkMemberDeauthorize(const uint64_t networkId,const uint64_t memberId);
 
 
 	void threadMain()
 	void threadMain()
 		throw();
 		throw();

+ 35 - 9
controller/JSONDB.cpp

@@ -294,9 +294,9 @@ void JSONDB::threadMain()
 
 
 							if (obj.is_array()) {
 							if (obj.is_array()) {
 								for(unsigned long i=0;i<obj.size();++i)
 								for(unsigned long i=0;i<obj.size();++i)
-									_add(obj[i]);
+									_addOrUpdate(obj[i]);
 							} else if (obj.is_object()) {
 							} else if (obj.is_object()) {
-								_add(obj);
+								_addOrUpdate(obj);
 							}
 							}
 						} catch ( ... ) {} // ignore malformed JSON
 						} catch ( ... ) {} // ignore malformed JSON
 
 
@@ -399,14 +399,14 @@ void JSONDB::threadMain()
 #endif
 #endif
 }
 }
 
 
-bool JSONDB::_add(const nlohmann::json &j)
+bool JSONDB::_addOrUpdate(const nlohmann::json &j)
 {
 {
 	try {
 	try {
 		if (j.is_object()) {
 		if (j.is_object()) {
 			std::string id(OSUtils::jsonString(j["id"],"0"));
 			std::string id(OSUtils::jsonString(j["id"],"0"));
-			std::string objtype(OSUtils::jsonString(j["objtype"],""));
-
+			const std::string objtype(OSUtils::jsonString(j["objtype"],""));
 			if ((id.length() == 16)&&(objtype == "network")) {
 			if ((id.length() == 16)&&(objtype == "network")) {
+
 				const uint64_t nwid = Utils::hexStrToU64(id.c_str());
 				const uint64_t nwid = Utils::hexStrToU64(id.c_str());
 				if (nwid) {
 				if (nwid) {
 					bool update;
 					bool update;
@@ -421,23 +421,49 @@ bool JSONDB::_add(const nlohmann::json &j)
 					_recomputeSummaryInfo(nwid);
 					_recomputeSummaryInfo(nwid);
 					return true;
 					return true;
 				}
 				}
+
 			} else if ((id.length() == 10)&&(objtype == "member")) {
 			} else if ((id.length() == 10)&&(objtype == "member")) {
+
 				const uint64_t mid = Utils::hexStrToU64(id.c_str());
 				const uint64_t mid = Utils::hexStrToU64(id.c_str());
 				const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str());
 				const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str());
 				if ((mid)&&(nwid)) {
 				if ((mid)&&(nwid)) {
-					bool update;
+					bool update = false;
+					bool deauth = false;
 					{
 					{
 						Mutex::Lock _l(_networks_m);
 						Mutex::Lock _l(_networks_m);
 						std::vector<uint8_t> &m = _networks[nwid].members[mid];
 						std::vector<uint8_t> &m = _networks[nwid].members[mid];
-						update = !m.empty();
+						if (!m.empty()) {
+							update = true;
+							nlohmann::json oldm(nlohmann::json::from_msgpack(m));
+							deauth = ((OSUtils::jsonBool(oldm["authorized"],false))&&(!OSUtils::jsonBool(j["authorized"],false)));
+						}
 						m = nlohmann::json::to_msgpack(j);
 						m = nlohmann::json::to_msgpack(j);
 						_members[mid].insert(nwid);
 						_members[mid].insert(nwid);
 					}
 					}
-					if (update)
+					if (update) {
 						_parent->onNetworkMemberUpdate(nwid,mid);
 						_parent->onNetworkMemberUpdate(nwid,mid);
+						if (deauth)
+							_parent->onNetworkMemberDeauthorize(nwid,mid);
+					}
 					_recomputeSummaryInfo(nwid);
 					_recomputeSummaryInfo(nwid);
 					return true;
 					return true;
 				}
 				}
+
+			} else if (objtype == "_delete") { // pseudo-object-type, only used in Central harnessed mode
+
+				const std::string deleteType(OSUtils::jsonString(j["deleteType"],""));
+				id = OSUtils::jsonString(j["deleteId"],"");
+				if ((deleteType == "network")&&(id.length() == 16)) {
+					eraseNetwork(Utils::hexStrToU64(id.c_str()));
+				} else if ((deleteType == "member")&&(id.length() == 10)) {
+					const std::string networkId(OSUtils::jsonString(j["deleteNetworkId"],""));
+					const uint64_t nwid = Utils::hexStrToU64(networkId.c_str());
+					const uint64_t mid = Utils::hexStrToU64(id.c_str());
+					if (networkId.length() == 16)
+						eraseNetworkMember(nwid,mid,true);
+					_parent->onNetworkMemberDeauthorize(nwid,mid);
+				}
+
 			}
 			}
 		}
 		}
 	} catch ( ... ) {}
 	} catch ( ... ) {}
@@ -455,7 +481,7 @@ bool JSONDB::_load(const std::string &p)
 			std::string buf;
 			std::string buf;
 			if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) {
 			if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) {
 				try {
 				try {
-					_add(OSUtils::jsonParse(buf));
+					_addOrUpdate(OSUtils::jsonParse(buf));
 				} catch ( ... ) {}
 				} catch ( ... ) {}
 			}
 			}
 		} else {
 		} else {

+ 1 - 1
controller/JSONDB.hpp

@@ -157,7 +157,7 @@ public:
 		throw();
 		throw();
 
 
 private:
 private:
-	bool _add(const nlohmann::json &j);
+	bool _addOrUpdate(const nlohmann::json &j);
 	bool _load(const std::string &p);
 	bool _load(const std::string &p);
 	void _recomputeSummaryInfo(const uint64_t networkId);
 	void _recomputeSummaryInfo(const uint64_t networkId);
 	std::string _genPath(const std::string &n,bool create);
 	std::string _genPath(const std::string &n,bool create);