NetworkConfig.cpp 19 KB

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