CV2.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Copyright (c)2025 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: 2026-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. #include "CV2.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #include "../node/Constants.hpp"
  16. #include "../node/SHA512.hpp"
  17. #include "EmbeddedNetworkController.hpp"
  18. #include "../version.h"
  19. #include "CtlUtil.hpp"
  20. #include <libpq-fe.h>
  21. #include <sstream>
  22. #include <iomanip>
  23. #include <climits>
  24. #include <chrono>
  25. using json = nlohmann::json;
  26. namespace {
  27. }
  28. using namespace ZeroTier;
  29. CV2::CV2(const Identity &myId, const char *path, int listenPort)
  30. : DB()
  31. , _pool()
  32. , _myId(myId)
  33. , _myAddress(myId.address())
  34. , _ready(0)
  35. , _connected(1)
  36. , _run(1)
  37. , _waitNoticePrinted(false)
  38. , _listenPort(listenPort)
  39. {
  40. fprintf(stderr, "CV2::CV2\n");
  41. char myAddress[64];
  42. _myAddressStr = myId.address().toString(myAddress);
  43. _connString = std::string(path);
  44. auto f = std::make_shared<PostgresConnFactory>(_connString);
  45. _pool = std::make_shared<ConnectionPool<PostgresConnection> >(
  46. 15, 5, std::static_pointer_cast<ConnectionFactory>(f));
  47. memset(_ssoPsk, 0, sizeof(_ssoPsk));
  48. char *const ssoPskHex = getenv("ZT_SSO_PSK");
  49. #ifdef ZT_TRACE
  50. fprintf(stderr, "ZT_SSO_PSK: %s\n", ssoPskHex);
  51. #endif
  52. if (ssoPskHex) {
  53. // SECURITY: note that ssoPskHex will always be null-terminated if libc actually
  54. // returns something non-NULL. If the hex encodes something shorter than 48 bytes,
  55. // it will be padded at the end with zeroes. If longer, it'll be truncated.
  56. Utils::unhex(ssoPskHex, _ssoPsk, sizeof(_ssoPsk));
  57. }
  58. _readyLock.lock();
  59. fprintf(stderr, "[%s] NOTICE: %.10llx controller PostgreSQL waiting for initial data download..." ZT_EOL_S, ::_timestr(), (unsigned long long)_myAddress.toInt());
  60. _waitNoticePrinted = true;
  61. initializeNetworks();
  62. initializeMembers();
  63. _heartbeatThread = std::thread(&CV2::heartbeat, this);
  64. _membersDbWatcher = std::thread(&CV2::membersDbWatcher, this);
  65. _networksDbWatcher = std::thread(&CV2::networksDbWatcher, this);
  66. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  67. _commitThread[i] = std::thread(&CV2::commitThread, this);
  68. }
  69. _onlineNotificationThread = std::thread(&CV2::onlineNotificationThread, this);
  70. }
  71. CV2::~CV2()
  72. {
  73. _run = 0;
  74. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  75. _heartbeatThread.join();
  76. _membersDbWatcher.join();
  77. _networksDbWatcher.join();
  78. _commitQueue.stop();
  79. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  80. _commitThread[i].join();
  81. }
  82. _onlineNotificationThread.join();
  83. }
  84. bool CV2::waitForReady()
  85. {
  86. while (_ready < 2) {
  87. _readyLock.lock();
  88. _readyLock.unlock();
  89. }
  90. return true;
  91. }
  92. bool CV2::isReady()
  93. {
  94. return (_ready == 2) && _connected;
  95. }
  96. bool CV2::save(nlohmann::json &record,bool notifyListeners)
  97. {
  98. bool modified = false;
  99. try {
  100. if (!record.is_object()) {
  101. fprintf(stderr, "record is not an object?!?\n");
  102. return false;
  103. }
  104. const std::string objtype = record["objtype"];
  105. if (objtype == "network") {
  106. //fprintf(stderr, "network save\n");
  107. const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
  108. if (nwid) {
  109. nlohmann::json old;
  110. get(nwid,old);
  111. if ((!old.is_object())||(!_compareRecords(old,record))) {
  112. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
  113. _commitQueue.post(std::pair<nlohmann::json,bool>(record,notifyListeners));
  114. modified = true;
  115. }
  116. }
  117. } else if (objtype == "member") {
  118. std::string networkId = record["nwid"];
  119. std::string memberId = record["id"];
  120. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
  121. const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
  122. //fprintf(stderr, "member save %s-%s\n", networkId.c_str(), memberId.c_str());
  123. if ((id)&&(nwid)) {
  124. nlohmann::json network,old;
  125. get(nwid,network,id,old);
  126. if ((!old.is_object())||(!_compareRecords(old,record))) {
  127. //fprintf(stderr, "commit queue post\n");
  128. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
  129. _commitQueue.post(std::pair<nlohmann::json,bool>(record,notifyListeners));
  130. modified = true;
  131. } else {
  132. //fprintf(stderr, "no change\n");
  133. }
  134. }
  135. } else {
  136. fprintf(stderr, "uhh waaat\n");
  137. }
  138. } catch (std::exception &e) {
  139. fprintf(stderr, "Error on PostgreSQL::save: %s\n", e.what());
  140. } catch (...) {
  141. fprintf(stderr, "Unknown error on PostgreSQL::save\n");
  142. }
  143. return modified;
  144. }
  145. void CV2::eraseNetwork(const uint64_t networkId)
  146. {
  147. fprintf(stderr, "PostgreSQL::eraseNetwork\n");
  148. char tmp2[24];
  149. waitForReady();
  150. Utils::hex(networkId, tmp2);
  151. std::pair<nlohmann::json,bool> tmp;
  152. tmp.first["id"] = tmp2;
  153. tmp.first["objtype"] = "_delete_network";
  154. tmp.second = true;
  155. _commitQueue.post(tmp);
  156. nlohmann::json nullJson;
  157. _networkChanged(tmp.first, nullJson, true);
  158. }
  159. void CV2::eraseMember(const uint64_t networkId, const uint64_t memberId)
  160. {
  161. fprintf(stderr, "PostgreSQL::eraseMember\n");
  162. char tmp2[24];
  163. waitForReady();
  164. std::pair<nlohmann::json,bool> tmp, nw;
  165. Utils::hex(networkId, tmp2);
  166. tmp.first["nwid"] = tmp2;
  167. Utils::hex(memberId, tmp2);
  168. tmp.first["id"] = tmp2;
  169. tmp.first["objtype"] = "_delete_member";
  170. tmp.second = true;
  171. _commitQueue.post(tmp);
  172. nlohmann::json nullJson;
  173. _memberChanged(tmp.first, nullJson, true);
  174. }
  175. void CV2::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress, const char *osArch)
  176. {
  177. std::lock_guard<std::mutex> l(_lastOnline_l);
  178. NodeOnlineRecord &i = _lastOnline[std::pair<uint64_t,uint64_t>(networkId, memberId)];
  179. i.lastSeen = OSUtils::now();
  180. if (physicalAddress) {
  181. i.physicalAddress = physicalAddress;
  182. }
  183. i.osArch = std::string(osArch);
  184. }
  185. void CV2::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress)
  186. {
  187. this->nodeIsOnline(networkId, memberId, physicalAddress, "unknown/unknown");
  188. }
  189. AuthInfo CV2::getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL)
  190. {
  191. // TODO: Redo this for CV2
  192. Metrics::db_get_sso_info++;
  193. // NONCE is just a random character string. no semantic meaning
  194. // state = HMAC SHA384 of Nonce based on shared sso key
  195. //
  196. // need nonce timeout in database? make sure it's used within X time
  197. // X is 5 minutes for now. Make configurable later?
  198. //
  199. // how do we tell when a nonce is used? if auth_expiration_time is set
  200. std::string networkId = member["nwid"];
  201. std::string memberId = member["id"];
  202. char authenticationURL[4096] = {0};
  203. AuthInfo info;
  204. info.enabled = true;
  205. //if (memberId == "a10dccea52" && networkId == "8056c2e21c24673d") {
  206. // fprintf(stderr, "invalid authinfo for grant's machine\n");
  207. // info.version=1;
  208. // return info;
  209. //}
  210. // fprintf(stderr, "PostgreSQL::updateMemberOnLoad: %s-%s\n", networkId.c_str(), memberId.c_str());
  211. std::shared_ptr<PostgresConnection> c;
  212. try {
  213. // c = _pool->borrow();
  214. // pqxx::work w(*c->c);
  215. // char nonceBytes[16] = {0};
  216. // std::string nonce = "";
  217. // // check if the member exists first.
  218. // pqxx::row count = w.exec_params1("SELECT count(id) FROM ztc_member WHERE id = $1 AND network_id = $2 AND deleted = false", memberId, networkId);
  219. // if (count[0].as<int>() == 1) {
  220. // // get active nonce, if exists.
  221. // pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  222. // "WHERE network_id = $1 AND member_id = $2 "
  223. // "AND ((NOW() AT TIME ZONE 'UTC') <= authentication_expiry_time) AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  224. // networkId, memberId);
  225. // if (r.size() == 0) {
  226. // // no active nonce.
  227. // // find an unused nonce, if one exists.
  228. // pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  229. // "WHERE network_id = $1 AND member_id = $2 "
  230. // "AND authentication_expiry_time IS NULL AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  231. // networkId, memberId);
  232. // if (r.size() == 1) {
  233. // // we have an existing nonce. Use it
  234. // nonce = r.at(0)[0].as<std::string>();
  235. // Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  236. // } else if (r.empty()) {
  237. // // create a nonce
  238. // Utils::getSecureRandom(nonceBytes, 16);
  239. // char nonceBuf[64] = {0};
  240. // Utils::hex(nonceBytes, sizeof(nonceBytes), nonceBuf);
  241. // nonce = std::string(nonceBuf);
  242. // pqxx::result ir = w.exec_params0("INSERT INTO ztc_sso_expiry "
  243. // "(nonce, nonce_expiration, network_id, member_id) VALUES "
  244. // "($1, TO_TIMESTAMP($2::double precision/1000), $3, $4)",
  245. // nonce, OSUtils::now() + 300000, networkId, memberId);
  246. // w.commit();
  247. // } else {
  248. // // > 1 ?!? Thats an error!
  249. // fprintf(stderr, "> 1 unused nonce!\n");
  250. // exit(6);
  251. // }
  252. // } else if (r.size() == 1) {
  253. // nonce = r.at(0)[0].as<std::string>();
  254. // Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  255. // } else {
  256. // // more than 1 nonce in use? Uhhh...
  257. // fprintf(stderr, "> 1 nonce in use for network member?!?\n");
  258. // exit(7);
  259. // }
  260. // r = w.exec_params(
  261. // "SELECT oc.client_id, oc.authorization_endpoint, oc.issuer, oc.provider, oc.sso_impl_version "
  262. // "FROM ztc_network AS n "
  263. // "INNER JOIN ztc_org o "
  264. // " ON o.owner_id = n.owner_id "
  265. // "LEFT OUTER JOIN ztc_network_oidc_config noc "
  266. // " ON noc.network_id = n.id "
  267. // "LEFT OUTER JOIN ztc_oidc_config oc "
  268. // " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  269. // "WHERE n.id = $1 AND n.sso_enabled = true", networkId);
  270. // std::string client_id = "";
  271. // std::string authorization_endpoint = "";
  272. // std::string issuer = "";
  273. // std::string provider = "";
  274. // uint64_t sso_version = 0;
  275. // if (r.size() == 1) {
  276. // client_id = r.at(0)[0].as<std::optional<std::string>>().value_or("");
  277. // authorization_endpoint = r.at(0)[1].as<std::optional<std::string>>().value_or("");
  278. // issuer = r.at(0)[2].as<std::optional<std::string>>().value_or("");
  279. // provider = r.at(0)[3].as<std::optional<std::string>>().value_or("");
  280. // sso_version = r.at(0)[4].as<std::optional<uint64_t>>().value_or(1);
  281. // } else if (r.size() > 1) {
  282. // fprintf(stderr, "ERROR: More than one auth endpoint for an organization?!?!? NetworkID: %s\n", networkId.c_str());
  283. // } else {
  284. // fprintf(stderr, "No client or auth endpoint?!?\n");
  285. // }
  286. // info.version = sso_version;
  287. // // no catch all else because we don't actually care if no records exist here. just continue as normal.
  288. // if ((!client_id.empty())&&(!authorization_endpoint.empty())) {
  289. // uint8_t state[48];
  290. // HMACSHA384(_ssoPsk, nonceBytes, sizeof(nonceBytes), state);
  291. // char state_hex[256];
  292. // Utils::hex(state, 48, state_hex);
  293. // if (info.version == 0) {
  294. // char url[2048] = {0};
  295. // OSUtils::ztsnprintf(url, sizeof(authenticationURL),
  296. // "%s?response_type=id_token&response_mode=form_post&scope=openid+email+profile&redirect_uri=%s&nonce=%s&state=%s&client_id=%s",
  297. // authorization_endpoint.c_str(),
  298. // url_encode(redirectURL).c_str(),
  299. // nonce.c_str(),
  300. // state_hex,
  301. // client_id.c_str());
  302. // info.authenticationURL = std::string(url);
  303. // } else if (info.version == 1) {
  304. // info.ssoClientID = client_id;
  305. // info.issuerURL = issuer;
  306. // info.ssoProvider = provider;
  307. // info.ssoNonce = nonce;
  308. // info.ssoState = std::string(state_hex) + "_" +networkId;
  309. // info.centralAuthURL = redirectURL;
  310. // #ifdef ZT_DEBUG
  311. // fprintf(
  312. // stderr,
  313. // "ssoClientID: %s\nissuerURL: %s\nssoNonce: %s\nssoState: %s\ncentralAuthURL: %s\nprovider: %s\n",
  314. // info.ssoClientID.c_str(),
  315. // info.issuerURL.c_str(),
  316. // info.ssoNonce.c_str(),
  317. // info.ssoState.c_str(),
  318. // info.centralAuthURL.c_str(),
  319. // provider.c_str());
  320. // #endif
  321. // }
  322. // } else {
  323. // fprintf(stderr, "client_id: %s\nauthorization_endpoint: %s\n", client_id.c_str(), authorization_endpoint.c_str());
  324. // }
  325. // }
  326. // _pool->unborrow(c);
  327. } catch (std::exception &e) {
  328. fprintf(stderr, "ERROR: Error updating member on load for network %s: %s\n", networkId.c_str(), e.what());
  329. }
  330. return info; //std::string(authenticationURL);
  331. }
  332. void CV2::initializeNetworks()
  333. { fprintf(stderr, "Initializing networks...\n");
  334. try {
  335. char qbuf[2048];
  336. sprintf(qbuf, "SELECT id, name, configuration , (EXTRACT(EPOCH FROM creation_time AT TIME ZONE 'UTC')*1000)::bigint, "
  337. "(EXTRACT(EPOCH FROM last_modified AT TIME ZONE 'UTC')*1000)::bigint, revision "
  338. "FROM networks_ctl WHERE controller_id = '%s'", _myAddressStr.c_str());
  339. auto c = _pool->borrow();
  340. pqxx::work w(*c->c);
  341. fprintf(stderr, "Load networks from psql...\n");
  342. auto stream = pqxx::stream_from::query(w, qbuf);
  343. std::tuple<
  344. std::string // network ID
  345. , std::optional<std::string> // name
  346. , std::string // configuration
  347. , std::optional<uint64_t> // creation_time
  348. , std::optional<uint64_t> // last_modified
  349. , std::optional<uint64_t> // revision
  350. > row;
  351. uint64_t count = 0;
  352. uint64_t total = 0;
  353. while (stream >> row) {
  354. auto start = std::chrono::high_resolution_clock::now();
  355. json empty;
  356. json config;
  357. initNetwork(config);
  358. std::string nwid = std::get<0>(row);
  359. std::string name = std::get<1>(row).value_or("");
  360. json cfgtmp = json::parse(std::get<2>(row));
  361. std::optional<uint64_t> created_at = std::get<3>(row);
  362. std::optional<uint64_t> last_modified = std::get<4>(row);
  363. std::optional<uint64_t> revision = std::get<5>(row);
  364. config["id"] = nwid;
  365. config["name"] = name;
  366. config["creationTime"] = created_at.value_or(0);
  367. config["lastModified"] = last_modified.value_or(0);
  368. config["revision"] = revision.value_or(0);
  369. config["capabilities"] = cfgtmp["capabilities"].is_array() ? cfgtmp["capabilities"] : json::array();
  370. config["enableBroadcast"] = cfgtmp["enableBroadcast"].is_boolean() ? cfgtmp["enableBroadcast"].get<bool>() : false;
  371. config["mtu"] = cfgtmp["mtu"].is_number() ? cfgtmp["mtu"].get<int32_t>() : 2800;
  372. config["multicastLimit"] = cfgtmp["multicastLimit"].is_number() ? cfgtmp["multicastLimit"].get<int32_t>() : 64;
  373. config["private"] = cfgtmp["private"].is_boolean() ? cfgtmp["private"].get<bool>() : true;
  374. config["remoteTraceLevel"] = cfgtmp["remoteTraceLevel"].is_number() ? cfgtmp["remoteTraceLevel"].get<int32_t>() : 0;
  375. config["remoteTraceTarget"] = cfgtmp["remoteTraceTarget"].is_string() ? cfgtmp["remoteTraceTarget"].get<std::string>() : "";
  376. config["revision"] = revision.value_or(0);
  377. config["rules"] = cfgtmp["rules"].is_array() ? cfgtmp["rules"] : json::array();
  378. config["tags"] = cfgtmp["tags"].is_array() ? cfgtmp["tags"] : json::array();
  379. if (cfgtmp["v4AssignMode"].is_object()) {
  380. config["v4AssignMode"] = cfgtmp["v4AssignMode"];
  381. } else {
  382. config["v4AssignMode"] = json::object();
  383. config["v4AssignMode"]["zt"] = true;
  384. }
  385. if (cfgtmp["v6AssignMode"].is_object()) {
  386. config["v6AssignMode"] = cfgtmp["v6AssignMode"];
  387. } else {
  388. config["v6AssignMode"] = json::object();
  389. config["v6AssignMode"]["zt"] = true;
  390. config["v6AssignMode"]["6plane"] = true;
  391. config["v6AssignMode"]["rfc4193"] = false;
  392. }
  393. config["ssoEnabled"] = cfgtmp["ssoEnabled"].is_boolean() ? cfgtmp["ssoEnabled"].get<bool>() : false;
  394. config["objtype"] = "network";
  395. config["routes"] = cfgtmp["routes"].is_array() ? cfgtmp["routes"] : json::array();
  396. config["clientId"] = cfgtmp["clientId"].is_string() ? cfgtmp["clientId"].get<std::string>() : "";
  397. config["authorizationEndpoint"] = cfgtmp["authorizationEndpoint"].is_string() ? cfgtmp["authorizationEndpoint"].get<std::string>() : nullptr;
  398. config["provider"] = cfgtmp["ssoProvider"].is_string() ? cfgtmp["ssoProvider"].get<std::string>() : "";
  399. if (!cfgtmp["dns"].is_object()) {
  400. cfgtmp["dns"] = json::object();
  401. cfgtmp["dns"]["domain"] = "";
  402. cfgtmp["dns"]["servers"] = json::array();
  403. } else {
  404. config["dns"] = cfgtmp["dns"];
  405. }
  406. config["ipAssignmentPools"] = cfgtmp["ipAssignmentPools"].is_array() ? cfgtmp["ipAssignmentPools"] : json::array();
  407. Metrics::network_count++;
  408. _networkChanged(empty, config, false);
  409. auto end = std::chrono::high_resolution_clock::now();
  410. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);;
  411. total += dur.count();
  412. ++count;
  413. if (count > 0 && count % 10000 == 0) {
  414. fprintf(stderr, "Averaging %lu us per network\n", (total/count));
  415. }
  416. }
  417. w.commit();
  418. _pool->unborrow(c);
  419. fprintf(stderr, "done.\n");
  420. if (++this->_ready == 2) {
  421. if (_waitNoticePrinted) {
  422. fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
  423. }
  424. _readyLock.unlock();
  425. }
  426. fprintf(stderr, "network init done\n");
  427. } catch (std::exception &e) {
  428. fprintf(stderr, "ERROR: Error initializing networks: %s\n", e.what());
  429. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  430. exit(-1);
  431. }
  432. }
  433. void CV2::initializeMembers()
  434. {
  435. std::string memberId;
  436. std::string networkId;
  437. try {
  438. char qbuf[2048];
  439. sprintf(qbuf,
  440. "SELECT nm.device_id, nm.network_id, nm.authorized, nm.active_bridge, nm.ip_assignments, nm.no_auto_assign_ips, "
  441. "nm.sso_exempt, (EXTRACT(EPOCH FROM nm.authentication_expiry_time AT TIME ZONE 'UTC')*1000)::bigint, "
  442. "(EXTRACT(EPOCH FROM nm.creation_time AT TIME ZONE 'UTC')*1000)::bigint, nm.identity, "
  443. "(EXTRACT(EPOCH FROM nm.last_authorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  444. "(EXTRACT(EPOCH FROM nm.last_deauthorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  445. "nm.remote_trace_level, nm.remote_trace_target, nm.revision, nm.capabilities, nm.tags "
  446. "FROM network_memberships_ctl nm "
  447. "INNER JOIN networks_ctl n "
  448. " ON nm.network_id = n.id "
  449. "WHERE n.controller_id = '%s'", _myAddressStr.c_str());
  450. auto c = _pool->borrow();
  451. pqxx::work w(*c->c);
  452. fprintf(stderr, "Load members from psql...\n");
  453. auto stream = pqxx::stream_from::query(w, qbuf);
  454. std::tuple<
  455. std::string // device ID
  456. , std::string // network ID
  457. , bool // authorized
  458. , std::optional<bool> // active_bridge
  459. , std::optional<std::string> // ip_assignments
  460. , std::optional<bool> // no_auto_assign_ips
  461. , std::optional<bool> // sso_exempt
  462. , std::optional<uint64_t> // authentication_expiry_time
  463. , std::optional<uint64_t> // creation_time
  464. , std::optional<std::string> // identity
  465. , std::optional<uint64_t> // last_authorized_time
  466. , std::optional<uint64_t> // last_deauthorized_time
  467. , std::optional<int32_t> // remote_trace_level
  468. , std::optional<std::string> // remote_trace_target
  469. , std::optional<uint64_t> // revision
  470. , std::optional<std::string> // capabilities
  471. , std::optional<std::string> // tags
  472. > row;
  473. uint64_t count = 0;
  474. uint64_t total = 0;
  475. while (stream >> row) {
  476. auto start = std::chrono::high_resolution_clock::now();
  477. json empty;
  478. json config;
  479. initMember(config);
  480. memberId = std::get<0>(row);
  481. networkId = std::get<1>(row);
  482. bool authorized = std::get<2>(row);
  483. std::optional<bool> active_bridge = std::get<3>(row);
  484. std::string ip_assignments = std::get<4>(row).value_or("");
  485. std::optional<bool> no_auto_assign_ips = std::get<5>(row);
  486. std::optional<bool> sso_exempt = std::get<6>(row);
  487. std::optional<uint64_t> authentication_expiry_time = std::get<7>(row);
  488. std::optional<uint64_t> creation_time = std::get<8>(row);
  489. std::optional<std::string> identity = std::get<9>(row);
  490. std::optional<uint64_t> last_authorized_time = std::get<10>(row);
  491. std::optional<uint64_t> last_deauthorized_time = std::get<11>(row);
  492. std::optional<int32_t> remote_trace_level = std::get<12>(row);
  493. std::optional<std::string> remote_trace_target = std::get<13>(row);
  494. std::optional<uint64_t> revision = std::get<14>(row);
  495. std::optional<std::string> capabilities = std::get<15>(row);
  496. std::optional<std::string> tags = std::get<16>(row);
  497. config["objtype"] = "member";
  498. config["id"] = memberId;
  499. config["address"] = identity.value_or("");
  500. config["nwid"] = networkId;
  501. config["authorized"] = authorized;
  502. config["activeBridge"] = active_bridge.value_or(false);
  503. config["ipAssignments"] = json::array();
  504. if (ip_assignments != "{}") {
  505. std::string tmp = ip_assignments.substr(1, ip_assignments.length() - 2);
  506. std::vector<std::string> addrs = split(tmp, ',');
  507. for (auto it = addrs.begin(); it != addrs.end(); ++it) {
  508. config["ipAssignments"].push_back(*it);
  509. }
  510. }
  511. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  512. config["creationTime"] = creation_time.value_or(0);
  513. config["lastAuthorizedTime"] = last_authorized_time.value_or(0);
  514. config["lastDeauthorizedTime"] = last_deauthorized_time.value_or(0);
  515. config["noAutoAssignIPs"] = no_auto_assign_ips.value_or(false);
  516. config["remoteTraceLevel"] = remote_trace_level.value_or(0);
  517. config["remoteTraceTarget"] = remote_trace_target.value_or(nullptr);
  518. config["revision"] = revision.value_or(0);
  519. config["ssoExempt"] = sso_exempt.value_or(false);
  520. config["authenticationExpiryTime"] = authentication_expiry_time.value_or(0);
  521. config["tags"] = json::parse(tags.value_or("[]"));
  522. Metrics::member_count++;
  523. _memberChanged(empty, config, false);
  524. memberId = "";
  525. networkId = "";
  526. auto end = std::chrono::high_resolution_clock::now();
  527. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  528. total += dur.count();
  529. ++count;
  530. if (count > 0 && count % 10000 == 0) {
  531. fprintf(stderr, "Averaging %lu us per member\n", (total/count));
  532. }
  533. }
  534. if (count > 0) {
  535. fprintf(stderr, "Took %lu us per member to load\n", (total/count));
  536. }
  537. stream.complete();
  538. w.commit();
  539. _pool->unborrow(c);
  540. fprintf(stderr, "done.\n");
  541. if (++this->_ready == 2) {
  542. if (_waitNoticePrinted) {
  543. fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
  544. }
  545. _readyLock.unlock();
  546. }
  547. fprintf(stderr, "member init done\n");
  548. } catch (std::exception &e) {
  549. fprintf(stderr, "ERROR: Error initializing member: %s-%s %s\n", networkId.c_str(), memberId.c_str(), e.what());
  550. exit(-1);
  551. }
  552. }
  553. void CV2::heartbeat()
  554. {
  555. char publicId[1024];
  556. char hostnameTmp[1024];
  557. _myId.toString(false,publicId);
  558. if (gethostname(hostnameTmp, sizeof(hostnameTmp))!= 0) {
  559. hostnameTmp[0] = (char)0;
  560. } else {
  561. for (int i = 0; i < (int)sizeof(hostnameTmp); ++i) {
  562. if ((hostnameTmp[i] == '.')||(hostnameTmp[i] == 0)) {
  563. hostnameTmp[i] = (char)0;
  564. break;
  565. }
  566. }
  567. }
  568. const char *controllerId = _myAddressStr.c_str();
  569. const char *publicIdentity = publicId;
  570. const char *hostname = hostnameTmp;
  571. while (_run == 1) {
  572. auto c = _pool->borrow();
  573. int64_t ts = OSUtils::now();
  574. if (c->c) {
  575. std::string major = std::to_string(ZEROTIER_ONE_VERSION_MAJOR);
  576. std::string minor = std::to_string(ZEROTIER_ONE_VERSION_MINOR);
  577. std::string rev = std::to_string(ZEROTIER_ONE_VERSION_REVISION);
  578. std::string version = major + "." + minor + "." + rev;
  579. std::string versionStr = "v" + version;
  580. try {
  581. pqxx::work w{*c->c};
  582. w.exec_params0("INSERT INTO controllers_ctl (id, hostname, last_heartbeat, public_identity, version) VALUES "
  583. "($1, $2, TO_TIMESTAMP($3::double precision/1000), $4, $5) "
  584. "ON CONFLICT (id) DO UPDATE SET hostname = EXCLUDED.hostname, last_heartbeat = EXCLUDED.last_heartbeat, "
  585. "public_identity = EXCLUDED.public_identity, version = EXCLUDED.version",
  586. controllerId, hostname, ts, publicIdentity, versionStr);
  587. w.commit();
  588. } catch (std::exception &e) {
  589. fprintf(stderr, "ERROR: Error in heartbeat: %s\n", e.what());
  590. continue;
  591. } catch (...) {
  592. fprintf(stderr, "ERROR: Unknown error in heartbeat\n");
  593. continue;
  594. }
  595. }
  596. _pool->unborrow(c);
  597. std::this_thread::sleep_for(std::chrono::seconds(1));
  598. }
  599. fprintf(stderr, "Exited heartbeat thread\n");
  600. }
  601. void CV2::membersDbWatcher() {
  602. auto c = _pool->borrow();
  603. std::string stream = "member_" + _myAddressStr;
  604. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  605. MemberNotificationReceiver m(this, *c->c, stream);
  606. while(_run == 1) {
  607. c->c->await_notification(5, 0);
  608. }
  609. _pool->unborrow(c);
  610. fprintf(stderr, "Exited membersDbWatcher\n");
  611. }
  612. void CV2::networksDbWatcher()
  613. {
  614. std::string stream = "network_" + _myAddressStr;
  615. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  616. auto c = _pool->borrow();
  617. NetworkNotificationReceiver n(this, *c->c, stream);
  618. while(_run == 1) {
  619. c->c->await_notification(5,0);
  620. }
  621. _pool->unborrow(c);
  622. fprintf(stderr, "Exited networksDbWatcher\n");
  623. }
  624. void CV2::commitThread()
  625. {
  626. fprintf(stderr, "%s: commitThread start\n", _myAddressStr.c_str());
  627. std::pair<nlohmann::json,bool> qitem;
  628. while(_commitQueue.get(qitem)&&(_run == 1)) {
  629. //fprintf(stderr, "commitThread tick\n");
  630. if (!qitem.first.is_object()) {
  631. fprintf(stderr, "not an object\n");
  632. continue;
  633. }
  634. std::shared_ptr<PostgresConnection> c;
  635. try {
  636. c = _pool->borrow();
  637. } catch (std::exception &e) {
  638. fprintf(stderr, "ERROR: %s\n", e.what());
  639. continue;
  640. }
  641. if (!c) {
  642. fprintf(stderr, "Error getting database connection\n");
  643. continue;
  644. }
  645. Metrics::pgsql_commit_ticks++;
  646. try {
  647. nlohmann::json &config = (qitem.first);
  648. const std::string objtype = config["objtype"];
  649. if (objtype == "member") {
  650. // fprintf(stderr, "%s: commitThread: member\n", _myAddressStr.c_str());
  651. std::string memberId;
  652. std::string networkId;
  653. try {
  654. pqxx::work w(*c->c);
  655. memberId = config["id"];
  656. networkId = config["nwid"];
  657. std::string target = "NULL";
  658. if (!config["remoteTraceTarget"].is_null()) {
  659. target = config["remoteTraceTarget"];
  660. }
  661. pqxx::row nwrow = w.exec_params1("SELECT COUNT(id) FROM networks WHERE id = $1", networkId);
  662. int nwcount = nwrow[0].as<int>();
  663. if (nwcount != 1) {
  664. fprintf(stderr, "network %s does not exist. skipping member upsert\n", networkId.c_str());
  665. w.abort();
  666. _pool->unborrow(c);
  667. continue;
  668. }
  669. // only needed for hooks, and no hooks for now
  670. // pqxx::row mrow = w.exec_params1("SELECT COUNT(id) FROM device_networks WHERE device_id = $1 AND network_id = $2", memberId, networkId);
  671. // int membercount = mrow[0].as<int>();
  672. // bool isNewMember = (membercount == 0);
  673. pqxx::result res = w.exec_params0(
  674. "INSERT INTO network_memberships_ctl (device_id, network_id, authorized, active_bridge, ip_assignments, "
  675. "no_auto_assign_ips, sso_exempt, authentication_expiry_time, capabilities, creation_time, "
  676. "identity, last_authorized_time, last_deauthorized_time, "
  677. "remote_trace_level, remote_trace_target, revision, tags, version_major, version_minor, "
  678. "version_revision, version_protocol) "
  679. "VALUES ($1, $2, $3, $4, $5, $6, $7, TO_TIMESTAMP($8::double precision/1000), $9, "
  680. "TO_TIMESTAMP($10::double precision/1000), $11, TO_TIMESTAMP($12::double precision/1000), "
  681. "TO_TIMESTAMP($13::double precision/1000), $14, $15, $16, $17, $18, $19, $20, $21) "
  682. "ON CONFLICT (device_id, network_id) DO UPDATE SET "
  683. "authorized = EXCLUDED.authorized, active_bridge = EXCLUDED.active_bridge, "
  684. "ip_assignments = EXCLUDED.ip_assignments, no_auto_assign_ips = EXCLUDED.no_auto_assign_ips, "
  685. "sso_exempt = EXCLUDED.sso_exempt, authentication_expiry_time = EXCLUDED.authentication_expiry_time, "
  686. "capabilities = EXCLUDED.capabilities, creation_time = EXCLUDED.creation_time, "
  687. "identity = EXCLUDED.identity, last_authorized_time = EXCLUDED.last_authorized_time, "
  688. "last_deauthorized_time = EXCLUDED.last_deauthorized_time, "
  689. "remote_trace_level = EXCLUDED.remote_trace_level, remote_trace_target = EXCLUDED.remote_trace_target, "
  690. "revision = EXCLUDED.revision, tags = EXCLUDED.tags, version_major = EXCLUDED.version_major, "
  691. "version_minor = EXCLUDED.version_minor, version_revision = EXCLUDED.version_revision, "
  692. "version_protocol = EXCLUDED.version_protocol",
  693. memberId,
  694. networkId,
  695. (bool)config["authorized"],
  696. (bool)config["activeBridge"],
  697. config["ipAssignments"].get<std::vector<std::string>>(),
  698. (bool)config["noAutoAssignIps"],
  699. (bool)config["ssoExempt"],
  700. (uint64_t)config["authenticationExpiryTime"],
  701. OSUtils::jsonDump(config["capabilities"], -1),
  702. (uint64_t)config["creationTime"],
  703. OSUtils::jsonString(config["identity"], ""),
  704. (uint64_t)config["lastAuthorizedTime"],
  705. (uint64_t)config["lastDeauthorizedTime"],
  706. (int)config["remoteTraceLevel"],
  707. target,
  708. (uint64_t)config["revision"],
  709. OSUtils::jsonDump(config["tags"], -1),
  710. (int)config["vMajor"],
  711. (int)config["vMinor"],
  712. (int)config["vRev"],
  713. (int)config["vProto"]);
  714. w.commit();
  715. // No hooks for now
  716. // if (_smee != NULL && isNewMember) {
  717. // pqxx::row row = w.exec_params1(
  718. // "SELECT "
  719. // " count(h.hook_id) "
  720. // "FROM "
  721. // " ztc_hook h "
  722. // " INNER JOIN ztc_org o ON o.org_id = h.org_id "
  723. // " INNER JOIN ztc_network n ON n.owner_id = o.owner_id "
  724. // " WHERE "
  725. // "n.id = $1 ",
  726. // networkId
  727. // );
  728. // int64_t hookCount = row[0].as<int64_t>();
  729. // if (hookCount > 0) {
  730. // notifyNewMember(networkId, memberId);
  731. // }
  732. // }
  733. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  734. const uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
  735. if (nwidInt && memberidInt) {
  736. nlohmann::json nwOrig;
  737. nlohmann::json memOrig;
  738. nlohmann::json memNew(config);
  739. get(nwidInt, nwOrig, memberidInt, memOrig);
  740. _memberChanged(memOrig, memNew, qitem.second);
  741. } else {
  742. fprintf(stderr, "%s: Can't notify of change. Error parsing nwid or memberid: %llu-%llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt, (unsigned long long)memberidInt);
  743. }
  744. } catch (std::exception &e) {
  745. fprintf(stderr, "%s ERROR: Error updating member %s-%s: %s\n", _myAddressStr.c_str(), networkId.c_str(), memberId.c_str(), e.what());
  746. }
  747. } else if (objtype == "network") {
  748. try {
  749. // fprintf(stderr, "%s: commitThread: network\n", _myAddressStr.c_str());
  750. pqxx::work w(*c->c);
  751. std::string id = config["id"];
  752. // network must already exist
  753. pqxx::result res = w.exec_params0(
  754. "INSERT INTO networks_ctl (id, name, configuration, controller_id, revision) "
  755. "VALUES ($1, $2, $3, $4, $5) "
  756. "ON CONFLICT (id) DO UPDATE SET "
  757. "name = EXCLUDED.name, configuration = EXCLUDED.configuration, revision = EXCLUDED.revision+1",
  758. id,
  759. OSUtils::jsonString(config["name"], ""),
  760. OSUtils::jsonDump(config, -1),
  761. _myAddressStr,
  762. ((uint64_t)config["revision"])
  763. );
  764. w.commit();
  765. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  766. if (nwidInt) {
  767. nlohmann::json nwOrig;
  768. nlohmann::json nwNew(config);
  769. get(nwidInt, nwOrig);
  770. _networkChanged(nwOrig, nwNew, qitem.second);
  771. } else {
  772. fprintf(stderr, "%s: Can't notify network changed: %llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt);
  773. }
  774. } catch (std::exception &e) {
  775. fprintf(stderr, "%s ERROR: Error updating network: %s\n", _myAddressStr.c_str(), e.what());
  776. }
  777. } else if (objtype == "_delete_network") {
  778. // fprintf(stderr, "%s: commitThread: delete network\n", _myAddressStr.c_str());
  779. try {
  780. // don't think we need this. Deletion handled by CV2 API
  781. pqxx::work w(*c->c);
  782. std::string networkId = config["id"];
  783. w.exec_params0("DELETE FROM network_memberships_ctl WHERE network_id = $1", networkId);
  784. w.exec_params0("DELETE FROM networks_ctl WHERE id = $1", networkId);
  785. w.commit();
  786. } catch (std::exception &e) {
  787. fprintf(stderr, "%s ERROR: Error deleting network: %s\n", _myAddressStr.c_str(), e.what());
  788. }
  789. } else if (objtype == "_delete_member") {
  790. // fprintf(stderr, "%s commitThread: delete member\n", _myAddressStr.c_str());
  791. try {
  792. pqxx::work w(*c->c);
  793. std::string memberId = config["id"];
  794. std::string networkId = config["nwid"];
  795. pqxx::result res = w.exec_params0(
  796. "DELETE FROM network_memberships_ctl WHERE device_id = $1 AND network_id = $2",
  797. memberId, networkId);
  798. w.commit();
  799. } catch (std::exception &e) {
  800. fprintf(stderr, "%s ERROR: Error deleting member: %s\n", _myAddressStr.c_str(), e.what());
  801. }
  802. } else {
  803. fprintf(stderr, "%s ERROR: unknown objtype\n", _myAddressStr.c_str());
  804. }
  805. } catch (std::exception &e) {
  806. fprintf(stderr, "%s ERROR: Error getting objtype: %s\n", _myAddressStr.c_str(), e.what());
  807. }
  808. _pool->unborrow(c);
  809. c.reset();
  810. }
  811. fprintf(stderr, "%s commitThread finished\n", _myAddressStr.c_str());
  812. }
  813. void CV2::onlineNotificationThread() {
  814. waitForReady();
  815. _connected = 1;
  816. nlohmann::json jtmp1, jtmp2;
  817. while (_run == 1) {
  818. auto c = _pool->borrow();
  819. auto c2 = _pool->borrow();
  820. try {
  821. fprintf(stderr, "%s onlineNotificationThread\n", _myAddressStr.c_str());
  822. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord,_PairHasher> lastOnline;
  823. {
  824. std::lock_guard<std::mutex> l(_lastOnline_l);
  825. lastOnline.swap(_lastOnline);
  826. }
  827. pqxx::work w(*c->c);
  828. pqxx::work w2(*c2->c);
  829. bool firstRun = true;
  830. bool memberAdded = false;
  831. uint64_t updateCount = 0;
  832. pqxx::pipeline pipe(w);
  833. for (auto i = lastOnline.begin(); i != lastOnline.end(); ++i) {
  834. updateCount++;
  835. uint64_t nwid_i = i->first.first;
  836. char nwidTmp[64];
  837. char memTmp[64];
  838. char ipTmp[64];
  839. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  840. OSUtils::ztsnprintf(memTmp,sizeof(memTmp), "%.10llx", i->first.second);
  841. if(!get(nwid_i, jtmp1, i->first.second, jtmp2)) {
  842. continue; // skip non existent networks/members
  843. }
  844. std::string networkId(nwidTmp);
  845. std::string memberId(memTmp);
  846. try {
  847. pqxx::row r = w2.exec_params1("SELECT device_id, network_id FROM network_memberships_ctl WHERE network_id = $1 AND device_id = $2",
  848. networkId, memberId);
  849. } catch (pqxx::unexpected_rows &e) {
  850. continue;
  851. }
  852. int64_t ts = i->second.lastSeen;
  853. std::string ipAddr = i->second.physicalAddress.toIpString(ipTmp);
  854. std::string timestamp = std::to_string(ts);
  855. std::string osArch = i->second.osArch;
  856. std::vector<std::string> osArchSplit = split(osArch, '/');
  857. std::string os = osArchSplit[0];
  858. std::string arch = osArchSplit[1];
  859. if (ipAddr.empty()) {
  860. ipAddr = "relayed";
  861. }
  862. json record = {
  863. {ipAddr, ts},
  864. };
  865. std::string device_network_insert = "INSERT INTO network_memberships_ctl (device_id, network_id, last_seen, os, arch) "
  866. "VALUES ('"+w2.esc(memberId)+"', '"+w2.esc(networkId)+"', '"+w2.esc(record.dump())+"'::JSONB, "
  867. "'"+w2.esc(os)+"', '"+w2.esc(arch)+"') "
  868. "ON CONFLICT (device_id, network_id) DO UPDATE SET os = EXCLUDED.os, arch = EXCLUDED.arch, "
  869. "last_seen = network_memberships_ctl.last_seen || EXCLUDED.last_seen";
  870. pipe.insert(device_network_insert);
  871. Metrics::pgsql_node_checkin++;
  872. }
  873. pipe.complete();;
  874. w2.commit();
  875. w.commit();
  876. fprintf(stderr, "%s: Updated online status of %lu members\n", _myAddressStr.c_str(), updateCount);
  877. } catch (std::exception &e) {
  878. fprintf(stderr, "%s ERROR: Error in onlineNotificationThread: %s\n", _myAddressStr.c_str(), e.what());
  879. } catch (...) {
  880. fprintf(stderr, "%s ERROR: Unknown error in onlineNotificationThread\n", _myAddressStr.c_str());
  881. }
  882. _pool->unborrow(c2);
  883. _pool->unborrow(c);
  884. std::this_thread::sleep_for(std::chrono::seconds(10));
  885. }
  886. fprintf(stderr, "%s: Fell out of run loop in onlineNotificationThread\n", _myAddressStr.c_str());
  887. if (_run == 1) {
  888. fprintf(stderr, "ERROR: %s onlineNotificationThread should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  889. exit(6);
  890. }
  891. }
  892. #endif // ZT_CONTROLLER_USE_LIBPQ