NetworkConfig.cpp 18 KB

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