NetworkConfig.cpp 18 KB

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