NetworkConfig.cpp 17 KB

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