DB.hpp 5.7 KB

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