2
0

DB.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c)2019 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. #ifndef ZT_CONTROLLER_DB_HPP
  14. #define ZT_CONTROLLER_DB_HPP
  15. //#define ZT_CONTROLLER_USE_LIBPQ
  16. #include "../node/Constants.hpp"
  17. #include "../node/Identity.hpp"
  18. #include "../node/InetAddress.hpp"
  19. #include "../osdep/OSUtils.hpp"
  20. #include "../osdep/BlockingQueue.hpp"
  21. #include <memory>
  22. #include <string>
  23. #include <thread>
  24. #include <unordered_map>
  25. #include <unordered_set>
  26. #include <vector>
  27. #include <atomic>
  28. #include <mutex>
  29. #include <set>
  30. #include <map>
  31. #include <nlohmann/json.hpp>
  32. #define ZT_MEMBER_AUTH_TIMEOUT_NOTIFY_BEFORE 25000
  33. namespace ZeroTier
  34. {
  35. struct AuthInfo
  36. {
  37. public:
  38. AuthInfo()
  39. : enabled(false)
  40. , version(0)
  41. , authenticationURL()
  42. , authenticationExpiryTime(0)
  43. , issuerURL()
  44. , centralAuthURL()
  45. , ssoNonce()
  46. , ssoState()
  47. , ssoClientID()
  48. {}
  49. bool enabled;
  50. uint64_t version;
  51. std::string authenticationURL;
  52. uint64_t authenticationExpiryTime;
  53. std::string issuerURL;
  54. std::string centralAuthURL;
  55. std::string ssoNonce;
  56. std::string ssoState;
  57. std::string ssoClientID;
  58. };
  59. /**
  60. * Base class with common infrastructure for all controller DB implementations
  61. */
  62. class DB
  63. {
  64. public:
  65. class ChangeListener
  66. {
  67. public:
  68. ChangeListener() {}
  69. virtual ~ChangeListener() {}
  70. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network) {}
  71. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
  72. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId) {}
  73. };
  74. struct NetworkSummaryInfo
  75. {
  76. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  77. std::vector<Address> activeBridges;
  78. std::vector<InetAddress> allocatedIps;
  79. unsigned long authorizedMemberCount;
  80. unsigned long totalMemberCount;
  81. int64_t mostRecentDeauthTime;
  82. };
  83. static void initNetwork(nlohmann::json &network);
  84. static void initMember(nlohmann::json &member);
  85. static void cleanNetwork(nlohmann::json &network);
  86. static void cleanMember(nlohmann::json &member);
  87. DB();
  88. virtual ~DB();
  89. virtual bool waitForReady() = 0;
  90. virtual bool isReady() = 0;
  91. inline bool hasNetwork(const uint64_t networkId) const
  92. {
  93. std::lock_guard<std::mutex> l(_networks_l);
  94. return (_networks.find(networkId) != _networks.end());
  95. }
  96. bool get(const uint64_t networkId,nlohmann::json &network);
  97. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  98. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  99. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  100. void networks(std::set<uint64_t> &networks);
  101. template<typename F>
  102. inline void each(F f)
  103. {
  104. nlohmann::json nullJson;
  105. std::lock_guard<std::mutex> lck(_networks_l);
  106. for(auto nw=_networks.begin();nw!=_networks.end();++nw) {
  107. f(nw->first,nw->second->config,0,nullJson); // first provide network with 0 for member ID
  108. for(auto m=nw->second->members.begin();m!=nw->second->members.end();++m) {
  109. f(nw->first,nw->second->config,m->first,m->second);
  110. }
  111. }
  112. }
  113. virtual bool save(nlohmann::json &record,bool notifyListeners) = 0;
  114. virtual void eraseNetwork(const uint64_t networkId) = 0;
  115. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
  116. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) = 0;
  117. virtual AuthInfo getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL) { return AuthInfo(); }
  118. inline void addListener(DB::ChangeListener *const listener)
  119. {
  120. std::lock_guard<std::mutex> l(_changeListeners_l);
  121. _changeListeners.push_back(listener);
  122. }
  123. protected:
  124. static inline bool _compareRecords(const nlohmann::json &a,const nlohmann::json &b)
  125. {
  126. if (a.is_object() == b.is_object()) {
  127. if (a.is_object()) {
  128. if (a.size() != b.size())
  129. return false;
  130. auto amap = a.get<nlohmann::json::object_t>();
  131. auto bmap = b.get<nlohmann::json::object_t>();
  132. for(auto ai=amap.begin();ai!=amap.end();++ai) {
  133. if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
  134. auto bi = bmap.find(ai->first);
  135. if ((bi == bmap.end())||(bi->second != ai->second))
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. return (a == b);
  142. }
  143. return false;
  144. }
  145. struct _Network
  146. {
  147. _Network() : mostRecentDeauthTime(0) {}
  148. nlohmann::json config;
  149. std::unordered_map<uint64_t,nlohmann::json> members;
  150. std::unordered_set<uint64_t> activeBridgeMembers;
  151. std::unordered_set<uint64_t> authorizedMembers;
  152. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  153. int64_t mostRecentDeauthTime;
  154. std::mutex lock;
  155. };
  156. virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners);
  157. virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners);
  158. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  159. std::vector<DB::ChangeListener *> _changeListeners;
  160. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  161. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  162. mutable std::mutex _changeListeners_l;
  163. mutable std::mutex _networks_l;
  164. };
  165. } // namespace ZeroTier
  166. #endif