DB.hpp 5.6 KB

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