NetworkConfig.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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: 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 "NetworkConfig.hpp"
  14. #include "DNS.hpp"
  15. #include <algorithm>
  16. #include <stdint.h>
  17. namespace ZeroTier {
  18. NetworkConfig::NetworkConfig()
  19. : networkId(0)
  20. , timestamp(0)
  21. , credentialTimeMaxDelta(0)
  22. , revision(0)
  23. , issuedTo()
  24. , remoteTraceTarget()
  25. , flags(0)
  26. , remoteTraceLevel(Trace::LEVEL_NORMAL)
  27. , mtu(0)
  28. , multicastLimit(0)
  29. , specialistCount(0)
  30. , routeCount(0)
  31. , staticIpCount(0)
  32. , ruleCount(0)
  33. , capabilityCount(0)
  34. , tagCount(0)
  35. , certificateOfOwnershipCount(0)
  36. , capabilities()
  37. , tags()
  38. , certificatesOfOwnership()
  39. , type(ZT_NETWORK_TYPE_PRIVATE)
  40. , dnsCount(0)
  41. , ssoEnabled(false)
  42. , authenticationURL()
  43. , authenticationExpiryTime(0)
  44. , issuerURL()
  45. , centralAuthURL()
  46. , ssoNonce()
  47. , ssoState()
  48. , ssoClientID()
  49. {
  50. name[0] = 0;
  51. memset(specialists, 0, sizeof(uint64_t) * ZT_MAX_NETWORK_SPECIALISTS);
  52. memset(routes, 0, sizeof(ZT_VirtualNetworkRoute) * ZT_MAX_NETWORK_ROUTES);
  53. memset(staticIps, 0, sizeof(InetAddress) * ZT_MAX_ZT_ASSIGNED_ADDRESSES);
  54. memset(rules, 0, sizeof(ZT_VirtualNetworkRule) * ZT_MAX_NETWORK_RULES);
  55. memset(&dns, 0, sizeof(ZT_VirtualNetworkDNS));
  56. memset(authenticationURL, 0, sizeof(authenticationURL));
  57. memset(issuerURL, 0, sizeof(issuerURL));
  58. memset(centralAuthURL, 0, sizeof(centralAuthURL));
  59. memset(ssoNonce, 0, sizeof(ssoNonce));
  60. memset(ssoState, 0, sizeof(ssoState));
  61. memset(ssoClientID, 0, sizeof(ssoClientID));
  62. strncpy(ssoProvider, "default", sizeof(ssoProvider));
  63. ssoProvider[sizeof(ssoProvider) - 1] = 0;
  64. }
  65. bool NetworkConfig::toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>& d, bool includeLegacy) const
  66. {
  67. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>* tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  68. char tmp2[128] = { 0 };
  69. try {
  70. d.clear();
  71. // Try to put the more human-readable fields first
  72. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_VERSION, (uint64_t)ZT_NETWORKCONFIG_VERSION)) {
  73. delete tmp;
  74. return false;
  75. }
  76. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID, this->networkId)) {
  77. delete tmp;
  78. return false;
  79. }
  80. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP, this->timestamp)) {
  81. delete tmp;
  82. return false;
  83. }
  84. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA, this->credentialTimeMaxDelta)) {
  85. delete tmp;
  86. return false;
  87. }
  88. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION, this->revision)) {
  89. delete tmp;
  90. return false;
  91. }
  92. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO, this->issuedTo.toString(tmp2))) {
  93. delete tmp;
  94. return false;
  95. }
  96. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET, this->remoteTraceTarget.toString(tmp2))) {
  97. delete tmp;
  98. return false;
  99. }
  100. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL, (uint64_t)this->remoteTraceLevel)) {
  101. delete tmp;
  102. return false;
  103. }
  104. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS, this->flags)) {
  105. delete tmp;
  106. return false;
  107. }
  108. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT, (uint64_t)this->multicastLimit)) {
  109. delete tmp;
  110. return false;
  111. }
  112. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE, (uint64_t)this->type)) {
  113. delete tmp;
  114. return false;
  115. }
  116. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME, this->name)) {
  117. delete tmp;
  118. return false;
  119. }
  120. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_MTU, (uint64_t)this->mtu)) {
  121. delete tmp;
  122. return false;
  123. }
  124. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  125. if (includeLegacy) {
  126. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD, this->enableBroadcast())) {
  127. return false;
  128. }
  129. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD, this->isPrivate())) {
  130. return false;
  131. }
  132. std::string v4s;
  133. for (unsigned int i = 0; i < staticIpCount; ++i) {
  134. if (this->staticIps[i].ss_family == AF_INET) {
  135. if (v4s.length() > 0) {
  136. v4s.push_back(',');
  137. }
  138. char buf[64];
  139. v4s.append(this->staticIps[i].toString(buf));
  140. }
  141. }
  142. if (v4s.length() > 0) {
  143. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD, v4s.c_str())) {
  144. return false;
  145. }
  146. }
  147. std::string v6s;
  148. for (unsigned int i = 0; i < staticIpCount; ++i) {
  149. if (this->staticIps[i].ss_family == AF_INET6) {
  150. if (v6s.length() > 0) {
  151. v6s.push_back(',');
  152. }
  153. char buf[64];
  154. v6s.append(this->staticIps[i].toString(buf));
  155. }
  156. }
  157. if (v6s.length() > 0) {
  158. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD, v6s.c_str())) {
  159. return false;
  160. }
  161. }
  162. std::string ets;
  163. unsigned int et = 0;
  164. ZT_VirtualNetworkRuleType lastrt = ZT_NETWORK_RULE_ACTION_ACCEPT;
  165. for (unsigned int i = 0; i < ruleCount; ++i) {
  166. ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f);
  167. if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) {
  168. et = rules[i].v.etherType;
  169. }
  170. else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) {
  171. if (((int)lastrt < 32) || (lastrt == ZT_NETWORK_RULE_MATCH_ETHERTYPE)) {
  172. if (ets.length() > 0) {
  173. ets.push_back(',');
  174. }
  175. char tmp2[16] = { 0 };
  176. ets.append(Utils::hex((uint16_t)et, tmp2));
  177. }
  178. et = 0;
  179. }
  180. lastrt = rt;
  181. }
  182. if (ets.length() > 0) {
  183. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD, ets.c_str())) {
  184. return false;
  185. }
  186. }
  187. if (this->com) {
  188. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD, this->com.toString().c_str())) {
  189. return false;
  190. }
  191. }
  192. std::string ab;
  193. for (unsigned int i = 0; i < this->specialistCount; ++i) {
  194. if ((this->specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) {
  195. if (ab.length() > 0) {
  196. ab.push_back(',');
  197. }
  198. char tmp2[16] = { 0 };
  199. ab.append(Address(this->specialists[i]).toString(tmp2));
  200. }
  201. }
  202. if (ab.length() > 0) {
  203. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD, ab.c_str())) {
  204. return false;
  205. }
  206. }
  207. }
  208. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  209. // Then add binary blobs
  210. if (this->com) {
  211. tmp->clear();
  212. this->com.serialize(*tmp);
  213. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_COM, *tmp)) {
  214. return false;
  215. }
  216. }
  217. tmp->clear();
  218. for (unsigned int i = 0; i < this->capabilityCount; ++i) {
  219. this->capabilities[i].serialize(*tmp);
  220. }
  221. if (tmp->size()) {
  222. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES, *tmp)) {
  223. return false;
  224. }
  225. }
  226. tmp->clear();
  227. for (unsigned int i = 0; i < this->tagCount; ++i) {
  228. this->tags[i].serialize(*tmp);
  229. }
  230. if (tmp->size()) {
  231. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_TAGS, *tmp)) {
  232. return false;
  233. }
  234. }
  235. tmp->clear();
  236. for (unsigned int i = 0; i < this->certificateOfOwnershipCount; ++i) {
  237. this->certificatesOfOwnership[i].serialize(*tmp);
  238. }
  239. if (tmp->size()) {
  240. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP, *tmp)) {
  241. return false;
  242. }
  243. }
  244. tmp->clear();
  245. for (unsigned int i = 0; i < this->specialistCount; ++i) {
  246. tmp->append((uint64_t)this->specialists[i]);
  247. }
  248. if (tmp->size()) {
  249. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS, *tmp)) {
  250. return false;
  251. }
  252. }
  253. tmp->clear();
  254. for (unsigned int i = 0; i < this->routeCount; ++i) {
  255. reinterpret_cast<const InetAddress*>(&(this->routes[i].target))->serialize(*tmp);
  256. reinterpret_cast<const InetAddress*>(&(this->routes[i].via))->serialize(*tmp);
  257. tmp->append((uint16_t)this->routes[i].flags);
  258. tmp->append((uint16_t)this->routes[i].metric);
  259. }
  260. if (tmp->size()) {
  261. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_ROUTES, *tmp)) {
  262. return false;
  263. }
  264. }
  265. tmp->clear();
  266. for (unsigned int i = 0; i < this->staticIpCount; ++i) {
  267. this->staticIps[i].serialize(*tmp);
  268. }
  269. if (tmp->size()) {
  270. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS, *tmp)) {
  271. return false;
  272. }
  273. }
  274. if (this->ruleCount) {
  275. tmp->clear();
  276. Capability::serializeRules(*tmp, rules, ruleCount);
  277. if (tmp->size()) {
  278. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES, *tmp)) {
  279. return false;
  280. }
  281. }
  282. }
  283. tmp->clear();
  284. DNS::serializeDNS(*tmp, &dns);
  285. if (tmp->size()) {
  286. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_DNS, *tmp)) {
  287. return false;
  288. }
  289. }
  290. if (this->ssoVersion == 0) {
  291. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION, this->ssoVersion)) {
  292. return false;
  293. }
  294. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED, this->ssoEnabled)) {
  295. return false;
  296. }
  297. if (this->ssoEnabled) {
  298. if (this->authenticationURL[0]) {
  299. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL)) {
  300. return false;
  301. }
  302. }
  303. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_EXPIRY_TIME, this->authenticationExpiryTime)) {
  304. return false;
  305. }
  306. }
  307. }
  308. else if (this->ssoVersion == 1) {
  309. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION, this->ssoVersion)) {
  310. return false;
  311. }
  312. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED, this->ssoEnabled)) {
  313. return false;
  314. }
  315. // if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL)) return false;
  316. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUER_URL, this->issuerURL)) {
  317. return false;
  318. }
  319. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_CENTRAL_ENDPOINT_URL, this->centralAuthURL)) {
  320. return false;
  321. }
  322. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_NONCE, this->ssoNonce)) {
  323. return false;
  324. }
  325. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_STATE, this->ssoState)) {
  326. return false;
  327. }
  328. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_CLIENT_ID, this->ssoClientID)) {
  329. return false;
  330. }
  331. if (! d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_PROVIDER, this->ssoProvider)) {
  332. return false;
  333. }
  334. }
  335. delete tmp;
  336. }
  337. catch (...) {
  338. delete tmp;
  339. throw;
  340. }
  341. return true;
  342. }
  343. bool NetworkConfig::fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>& d)
  344. {
  345. static const NetworkConfig NIL_NC;
  346. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>* tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  347. try {
  348. *this = NIL_NC;
  349. // Fields that are always present, new or old
  350. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID, 0);
  351. if (! this->networkId) {
  352. delete tmp;
  353. return false;
  354. }
  355. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP, 0);
  356. this->credentialTimeMaxDelta = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA, 0);
  357. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION, 0);
  358. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO, 0);
  359. if (! this->issuedTo) {
  360. delete tmp;
  361. return false;
  362. }
  363. this->remoteTraceTarget = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET);
  364. this->remoteTraceLevel = (Trace::Level)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL);
  365. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT, 0);
  366. d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME, this->name, sizeof(this->name));
  367. this->mtu = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MTU, ZT_DEFAULT_MTU);
  368. if (this->mtu < 1280) {
  369. this->mtu = 1280; // minimum MTU allowed by IPv6 standard and others
  370. }
  371. else if (this->mtu > ZT_MAX_MTU) {
  372. this->mtu = ZT_MAX_MTU;
  373. }
  374. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION, 0) < 6) {
  375. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  376. char tmp2[1024] = { 0 };
  377. // Decode legacy fields if version is old
  378. if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD)) {
  379. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
  380. }
  381. this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION; // always enable for old-style netconf
  382. this->type = (d.getB(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD, true)) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
  383. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD, tmp2, sizeof(tmp2)) > 0) {
  384. char* saveptr = (char*)0;
  385. for (char* f = Utils::stok(tmp2, ",", &saveptr); (f); f = Utils::stok((char*)0, ",", &saveptr)) {
  386. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) {
  387. break;
  388. }
  389. InetAddress ip(f);
  390. if (! ip.isNetwork()) {
  391. this->staticIps[this->staticIpCount++] = ip;
  392. }
  393. }
  394. }
  395. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD, tmp2, sizeof(tmp2)) > 0) {
  396. char* saveptr = (char*)0;
  397. for (char* f = Utils::stok(tmp2, ",", &saveptr); (f); f = Utils::stok((char*)0, ",", &saveptr)) {
  398. if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) {
  399. break;
  400. }
  401. InetAddress ip(f);
  402. if (! ip.isNetwork()) {
  403. this->staticIps[this->staticIpCount++] = ip;
  404. }
  405. }
  406. }
  407. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD, tmp2, sizeof(tmp2)) > 0) {
  408. this->com.fromString(tmp2);
  409. }
  410. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD, tmp2, sizeof(tmp2)) > 0) {
  411. char* saveptr = (char*)0;
  412. for (char* f = Utils::stok(tmp2, ",", &saveptr); (f); f = Utils::stok((char*)0, ",", &saveptr)) {
  413. unsigned int et = Utils::hexStrToUInt(f) & 0xffff;
  414. if ((this->ruleCount + 2) > ZT_MAX_NETWORK_RULES) {
  415. break;
  416. }
  417. if (et > 0) {
  418. this->rules[this->ruleCount].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE;
  419. this->rules[this->ruleCount].v.etherType = (uint16_t)et;
  420. ++this->ruleCount;
  421. }
  422. this->rules[this->ruleCount++].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
  423. }
  424. }
  425. else {
  426. this->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
  427. this->ruleCount = 1;
  428. }
  429. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD, tmp2, sizeof(tmp2)) > 0) {
  430. char* saveptr = (char*)0;
  431. for (char* f = Utils::stok(tmp2, ",", &saveptr); (f); f = Utils::stok((char*)0, ",", &saveptr)) {
  432. this->addSpecialist(Address(Utils::hexStrToU64(f)), ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE);
  433. }
  434. }
  435. #else
  436. delete tmp;
  437. return false;
  438. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  439. }
  440. else {
  441. // Otherwise we can use the new fields
  442. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS, 0);
  443. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE, (uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  444. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM, *tmp)) {
  445. this->com.deserialize(*tmp, 0);
  446. }
  447. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES, *tmp)) {
  448. try {
  449. unsigned int p = 0;
  450. while (p < tmp->size()) {
  451. Capability cap;
  452. p += cap.deserialize(*tmp, p);
  453. this->capabilities[this->capabilityCount++] = cap;
  454. }
  455. }
  456. catch (...) {
  457. }
  458. std::sort(&(this->capabilities[0]), &(this->capabilities[this->capabilityCount]));
  459. }
  460. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_TAGS, *tmp)) {
  461. try {
  462. unsigned int p = 0;
  463. while (p < tmp->size()) {
  464. Tag tag;
  465. p += tag.deserialize(*tmp, p);
  466. this->tags[this->tagCount++] = tag;
  467. }
  468. }
  469. catch (...) {
  470. }
  471. std::sort(&(this->tags[0]), &(this->tags[this->tagCount]));
  472. }
  473. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP, *tmp)) {
  474. unsigned int p = 0;
  475. while (p < tmp->size()) {
  476. if (certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP) {
  477. p += certificatesOfOwnership[certificateOfOwnershipCount++].deserialize(*tmp, p);
  478. }
  479. else {
  480. CertificateOfOwnership foo;
  481. p += foo.deserialize(*tmp, p);
  482. }
  483. }
  484. }
  485. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS, *tmp)) {
  486. unsigned int p = 0;
  487. while ((p + 8) <= tmp->size()) {
  488. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
  489. this->specialists[this->specialistCount++] = tmp->at<uint64_t>(p);
  490. }
  491. p += 8;
  492. }
  493. }
  494. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES, *tmp)) {
  495. unsigned int p = 0;
  496. while ((p < tmp->size()) && (routeCount < ZT_MAX_NETWORK_ROUTES)) {
  497. p += reinterpret_cast<InetAddress*>(&(this->routes[this->routeCount].target))->deserialize(*tmp, p);
  498. p += reinterpret_cast<InetAddress*>(&(this->routes[this->routeCount].via))->deserialize(*tmp, p);
  499. this->routes[this->routeCount].flags = tmp->at<uint16_t>(p);
  500. p += 2;
  501. this->routes[this->routeCount].metric = tmp->at<uint16_t>(p);
  502. p += 2;
  503. ++this->routeCount;
  504. }
  505. }
  506. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS, *tmp)) {
  507. unsigned int p = 0;
  508. while ((p < tmp->size()) && (staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  509. p += this->staticIps[this->staticIpCount++].deserialize(*tmp, p);
  510. }
  511. }
  512. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES, *tmp)) {
  513. this->ruleCount = 0;
  514. unsigned int p = 0;
  515. Capability::deserializeRules(*tmp, p, this->rules, this->ruleCount, ZT_MAX_NETWORK_RULES);
  516. }
  517. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_DNS, *tmp)) {
  518. unsigned int p = 0;
  519. DNS::deserializeDNS(*tmp, p, &dns);
  520. }
  521. this->ssoVersion = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION, 0ULL);
  522. this->ssoEnabled = d.getB(ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED, false);
  523. if (this->ssoVersion == 0) {
  524. // implicit flow
  525. if (this->ssoEnabled) {
  526. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL, (unsigned int)sizeof(this->authenticationURL)) > 0) {
  527. this->authenticationURL[sizeof(this->authenticationURL) - 1] = 0; // ensure null terminated
  528. }
  529. else {
  530. this->authenticationURL[0] = 0;
  531. }
  532. this->authenticationExpiryTime = d.getI(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_EXPIRY_TIME, 0);
  533. }
  534. else {
  535. this->authenticationURL[0] = 0;
  536. this->authenticationExpiryTime = 0;
  537. }
  538. }
  539. else if (this->ssoVersion == 1) {
  540. // full flow
  541. if (this->ssoEnabled) {
  542. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL, (unsigned int)sizeof(this->authenticationURL)) > 0) {
  543. this->authenticationURL[sizeof(this->authenticationURL) - 1] = 0;
  544. }
  545. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUER_URL, this->issuerURL, (unsigned int)sizeof(this->issuerURL)) > 0) {
  546. this->issuerURL[sizeof(this->issuerURL) - 1] = 0;
  547. }
  548. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CENTRAL_ENDPOINT_URL, this->centralAuthURL, (unsigned int)sizeof(this->centralAuthURL)) > 0) {
  549. this->centralAuthURL[sizeof(this->centralAuthURL) - 1] = 0;
  550. }
  551. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_NONCE, this->ssoNonce, (unsigned int)sizeof(this->ssoNonce)) > 0) {
  552. this->ssoNonce[sizeof(this->ssoNonce) - 1] = 0;
  553. }
  554. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATE, this->ssoState, (unsigned int)sizeof(this->ssoState)) > 0) {
  555. this->ssoState[sizeof(this->ssoState) - 1] = 0;
  556. }
  557. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CLIENT_ID, this->ssoClientID, (unsigned int)sizeof(this->ssoClientID)) > 0) {
  558. this->ssoClientID[sizeof(this->ssoClientID) - 1] = 0;
  559. }
  560. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SSO_PROVIDER, this->ssoProvider, (unsigned int)(sizeof(this->ssoProvider))) > 0) {
  561. this->ssoProvider[sizeof(this->ssoProvider) - 1] = 0;
  562. }
  563. else {
  564. strncpy(this->ssoProvider, "default", sizeof(this->ssoProvider));
  565. this->ssoProvider[sizeof(this->ssoProvider) - 1] = 0;
  566. }
  567. }
  568. else {
  569. this->authenticationURL[0] = 0;
  570. this->authenticationExpiryTime = 0;
  571. this->centralAuthURL[0] = 0;
  572. this->ssoNonce[0] = 0;
  573. this->ssoState[0] = 0;
  574. this->ssoClientID[0] = 0;
  575. this->issuerURL[0] = 0;
  576. this->ssoProvider[0] = 0;
  577. }
  578. }
  579. }
  580. // printf("~~~\n%s\n~~~\n",d.data());
  581. // dump();
  582. // printf("~~~\n");
  583. delete tmp;
  584. return true;
  585. }
  586. catch (...) {
  587. delete tmp;
  588. return false;
  589. }
  590. }
  591. } // namespace ZeroTier