ControlPlane.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "ControlPlane.hpp"
  19. #include "OneService.hpp"
  20. #include "../version.h"
  21. #include "../include/ZeroTierOne.h"
  22. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  23. #include <http_parser.h>
  24. #else
  25. #include "../ext/http-parser/http_parser.h"
  26. #endif
  27. #ifdef ZT_USE_SYSTEM_JSON_PARSER
  28. #include <json-parser/json.h>
  29. #else
  30. #include "../ext/json-parser/json.h"
  31. #endif
  32. #include "../controller/EmbeddedNetworkController.hpp"
  33. #include "../node/InetAddress.hpp"
  34. #include "../node/Node.hpp"
  35. #include "../node/Utils.hpp"
  36. #include "../osdep/OSUtils.hpp"
  37. namespace ZeroTier {
  38. static std::string _jsonEscape(const char *s)
  39. {
  40. std::string buf;
  41. for(const char *p=s;(*p);++p) {
  42. switch(*p) {
  43. case '\t': buf.append("\\t"); break;
  44. case '\b': buf.append("\\b"); break;
  45. case '\r': buf.append("\\r"); break;
  46. case '\n': buf.append("\\n"); break;
  47. case '\f': buf.append("\\f"); break;
  48. case '"': buf.append("\\\""); break;
  49. case '\\': buf.append("\\\\"); break;
  50. case '/': buf.append("\\/"); break;
  51. default: buf.push_back(*p); break;
  52. }
  53. }
  54. return buf;
  55. }
  56. static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); }
  57. static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int count)
  58. {
  59. std::string buf;
  60. buf.push_back('[');
  61. for(unsigned int i=0;i<count;++i) {
  62. if (i > 0)
  63. buf.push_back(',');
  64. buf.push_back('"');
  65. buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(ss[i]))->toString()));
  66. buf.push_back('"');
  67. }
  68. buf.push_back(']');
  69. return buf;
  70. }
  71. static std::string _jsonEnumerate(const ZT_VirtualNetworkRoute *routes,unsigned int count)
  72. {
  73. std::string buf;
  74. buf.push_back('[');
  75. for(unsigned int i=0;i<count;++i) {
  76. if (i > 0)
  77. buf.push_back(',');
  78. buf.append("{\"target\":\"");
  79. buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(routes[i].target))->toString()));
  80. buf.append("\",\"via\":");
  81. if (routes[i].via.ss_family == routes[i].target.ss_family) {
  82. buf.push_back('"');
  83. buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(routes[i].via))->toIpString()));
  84. buf.append("\",");
  85. } else buf.append("null,");
  86. char tmp[1024];
  87. Utils::snprintf(tmp,sizeof(tmp),"\"flags\":%u,\"metric\":%u}",(unsigned int)routes[i].flags,(unsigned int)routes[i].metric);
  88. buf.append(tmp);
  89. }
  90. buf.push_back(']');
  91. return buf;
  92. }
  93. static void _jsonAppend(unsigned int depth,std::string &buf,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName,const OneService::NetworkSettings &localSettings)
  94. {
  95. char json[4096];
  96. char prefix[32];
  97. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  98. return;
  99. for(unsigned int i=0;i<depth;++i)
  100. prefix[i] = '\t';
  101. prefix[depth] = '\0';
  102. const char *nstatus = "",*ntype = "";
  103. switch(nc->status) {
  104. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  105. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  106. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  107. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  108. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  109. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  110. }
  111. switch(nc->type) {
  112. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  113. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  114. }
  115. Utils::snprintf(json,sizeof(json),
  116. "%s{\n"
  117. "%s\t\"nwid\": \"%.16llx\",\n"
  118. "%s\t\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\",\n"
  119. "%s\t\"name\": \"%s\",\n"
  120. "%s\t\"status\": \"%s\",\n"
  121. "%s\t\"type\": \"%s\",\n"
  122. "%s\t\"mtu\": %u,\n"
  123. "%s\t\"dhcp\": %s,\n"
  124. "%s\t\"bridge\": %s,\n"
  125. "%s\t\"broadcastEnabled\": %s,\n"
  126. "%s\t\"portError\": %d,\n"
  127. "%s\t\"netconfRevision\": %lu,\n"
  128. "%s\t\"assignedAddresses\": %s,\n"
  129. "%s\t\"routes\": %s,\n"
  130. "%s\t\"portDeviceName\": \"%s\",\n"
  131. "%s\t\"allowManaged\": %s,\n"
  132. "%s\t\"allowGlobal\": %s,\n"
  133. "%s\t\"allowDefault\": %s\n"
  134. "%s}",
  135. prefix,
  136. prefix,nc->nwid,
  137. prefix,(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff),
  138. prefix,_jsonEscape(nc->name).c_str(),
  139. prefix,nstatus,
  140. prefix,ntype,
  141. prefix,nc->mtu,
  142. prefix,(nc->dhcp == 0) ? "false" : "true",
  143. prefix,(nc->bridge == 0) ? "false" : "true",
  144. prefix,(nc->broadcastEnabled == 0) ? "false" : "true",
  145. prefix,nc->portError,
  146. prefix,nc->netconfRevision,
  147. prefix,_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(),
  148. prefix,_jsonEnumerate(nc->routes,nc->routeCount).c_str(),
  149. prefix,_jsonEscape(portDeviceName).c_str(),
  150. prefix,(localSettings.allowManaged) ? "true" : "false",
  151. prefix,(localSettings.allowGlobal) ? "true" : "false",
  152. prefix,(localSettings.allowDefault) ? "true" : "false",
  153. prefix);
  154. buf.append(json);
  155. }
  156. static std::string _jsonEnumerate(unsigned int depth,const ZT_PeerPhysicalPath *pp,unsigned int count)
  157. {
  158. char json[1024];
  159. char prefix[32];
  160. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  161. return std::string();
  162. for(unsigned int i=0;i<depth;++i)
  163. prefix[i] = '\t';
  164. prefix[depth] = '\0';
  165. std::string buf;
  166. for(unsigned int i=0;i<count;++i) {
  167. if (i > 0)
  168. buf.push_back(',');
  169. Utils::snprintf(json,sizeof(json),
  170. "{\n"
  171. "%s\t\"address\": \"%s\",\n"
  172. "%s\t\"lastSend\": %llu,\n"
  173. "%s\t\"lastReceive\": %llu,\n"
  174. "%s\t\"active\": %s,\n"
  175. "%s\t\"preferred\": %s,\n"
  176. "%s\t\"trustedPathId\": %llu\n"
  177. "%s}",
  178. prefix,_jsonEscape(reinterpret_cast<const InetAddress *>(&(pp[i].address))->toString()).c_str(),
  179. prefix,pp[i].lastSend,
  180. prefix,pp[i].lastReceive,
  181. prefix,(pp[i].active == 0) ? "false" : "true",
  182. prefix,(pp[i].preferred == 0) ? "false" : "true",
  183. prefix,pp[i].trustedPathId,
  184. prefix);
  185. buf.append(json);
  186. }
  187. return buf;
  188. }
  189. static void _jsonAppend(unsigned int depth,std::string &buf,const ZT_Peer *peer)
  190. {
  191. char json[1024];
  192. char prefix[32];
  193. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  194. return;
  195. for(unsigned int i=0;i<depth;++i)
  196. prefix[i] = '\t';
  197. prefix[depth] = '\0';
  198. const char *prole = "";
  199. switch(peer->role) {
  200. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  201. case ZT_PEER_ROLE_RELAY: prole = "RELAY"; break;
  202. case ZT_PEER_ROLE_ROOT: prole = "ROOT"; break;
  203. }
  204. Utils::snprintf(json,sizeof(json),
  205. "%s{\n"
  206. "%s\t\"address\": \"%.10llx\",\n"
  207. "%s\t\"lastUnicastFrame\": %llu,\n"
  208. "%s\t\"lastMulticastFrame\": %llu,\n"
  209. "%s\t\"versionMajor\": %d,\n"
  210. "%s\t\"versionMinor\": %d,\n"
  211. "%s\t\"versionRev\": %d,\n"
  212. "%s\t\"version\": \"%d.%d.%d\",\n"
  213. "%s\t\"latency\": %u,\n"
  214. "%s\t\"role\": \"%s\",\n"
  215. "%s\t\"paths\": [%s]\n"
  216. "%s}",
  217. prefix,
  218. prefix,peer->address,
  219. prefix,peer->lastUnicastFrame,
  220. prefix,peer->lastMulticastFrame,
  221. prefix,peer->versionMajor,
  222. prefix,peer->versionMinor,
  223. prefix,peer->versionRev,
  224. prefix,peer->versionMajor,peer->versionMinor,peer->versionRev,
  225. prefix,peer->latency,
  226. prefix,prole,
  227. prefix,_jsonEnumerate(depth+1,peer->paths,peer->pathCount).c_str(),
  228. prefix);
  229. buf.append(json);
  230. }
  231. ControlPlane::ControlPlane(OneService *svc,Node *n,const char *uiStaticPath) :
  232. _svc(svc),
  233. _node(n),
  234. _controller((EmbeddedNetworkController *)0),
  235. _uiStaticPath((uiStaticPath) ? uiStaticPath : "")
  236. {
  237. }
  238. ControlPlane::~ControlPlane()
  239. {
  240. }
  241. unsigned int ControlPlane::handleRequest(
  242. const InetAddress &fromAddress,
  243. unsigned int httpMethod,
  244. const std::string &path,
  245. const std::map<std::string,std::string> &headers,
  246. const std::string &body,
  247. std::string &responseBody,
  248. std::string &responseContentType)
  249. {
  250. char json[8194];
  251. unsigned int scode = 404;
  252. std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
  253. std::map<std::string,std::string> urlArgs;
  254. Mutex::Lock _l(_lock);
  255. if (!((fromAddress.ipsEqual(InetAddress::LO4))||(fromAddress.ipsEqual(InetAddress::LO6))))
  256. return 403; // Forbidden: we only allow access from localhost right now
  257. /* Note: this is kind of restricted in what it'll take. It does not support
  258. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  259. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  260. * paths to simply-named resources. */
  261. if (ps.size() > 0) {
  262. std::size_t qpos = ps[ps.size() - 1].find('?');
  263. if (qpos != std::string::npos) {
  264. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  265. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  266. std::vector<std::string> asplit(Utils::split(args.c_str(),"&","",""));
  267. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  268. std::size_t eqpos = a->find('=');
  269. if (eqpos == std::string::npos)
  270. urlArgs[*a] = "";
  271. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  272. }
  273. }
  274. } else {
  275. ps.push_back(std::string("index.html"));
  276. }
  277. bool isAuth = false;
  278. {
  279. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  280. if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) {
  281. isAuth = true;
  282. } else {
  283. ah = urlArgs.find("auth");
  284. if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0))
  285. isAuth = true;
  286. }
  287. }
  288. if (httpMethod == HTTP_GET) {
  289. std::string ext;
  290. std::size_t dotIdx = ps[0].find_last_of('.');
  291. if (dotIdx != std::string::npos)
  292. ext = ps[0].substr(dotIdx);
  293. if ((ps.size() == 1)&&(ext.length() >= 2)&&(ext[0] == '.')) {
  294. /* Static web pages can be served without authentication to enable a simple web
  295. * UI. This is still only allowed from approved IP addresses. Anything with a
  296. * dot in the first path element (e.g. foo.html) is considered a static page,
  297. * as nothing in the API is so named. */
  298. if (_uiStaticPath.length() > 0) {
  299. if (ext == ".html")
  300. responseContentType = "text/html";
  301. else if (ext == ".js")
  302. responseContentType = "application/javascript";
  303. else if (ext == ".jsx")
  304. responseContentType = "text/jsx";
  305. else if (ext == ".json")
  306. responseContentType = "application/json";
  307. else if (ext == ".css")
  308. responseContentType = "text/css";
  309. else if (ext == ".png")
  310. responseContentType = "image/png";
  311. else if (ext == ".jpg")
  312. responseContentType = "image/jpeg";
  313. else if (ext == ".gif")
  314. responseContentType = "image/gif";
  315. else if (ext == ".txt")
  316. responseContentType = "text/plain";
  317. else if (ext == ".xml")
  318. responseContentType = "text/xml";
  319. else if (ext == ".svg")
  320. responseContentType = "image/svg+xml";
  321. else responseContentType = "application/octet-stream";
  322. scode = OSUtils::readFile((_uiStaticPath + ZT_PATH_SEPARATOR_S + ps[0]).c_str(),responseBody) ? 200 : 404;
  323. } else {
  324. scode = 404;
  325. }
  326. } else if (isAuth) {
  327. /* Things that require authentication -- a.k.a. everything but static web app pages. */
  328. if (ps[0] == "status") {
  329. responseContentType = "application/json";
  330. ZT_NodeStatus status;
  331. _node->status(&status);
  332. std::string clusterJson;
  333. #ifdef ZT_ENABLE_CLUSTER
  334. {
  335. ZT_ClusterStatus cs;
  336. _node->clusterStatus(&cs);
  337. if (cs.clusterSize >= 1) {
  338. char t[1024];
  339. Utils::snprintf(t,sizeof(t),"{\n\t\t\"myId\": %u,\n\t\t\"clusterSize\": %u,\n\t\t\"members\": [",cs.myId,cs.clusterSize);
  340. clusterJson.append(t);
  341. for(unsigned int i=0;i<cs.clusterSize;++i) {
  342. Utils::snprintf(t,sizeof(t),"%s\t\t\t{\n\t\t\t\t\"id\": %u,\n\t\t\t\t\"msSinceLastHeartbeat\": %u,\n\t\t\t\t\"alive\": %s,\n\t\t\t\t\"x\": %d,\n\t\t\t\t\"y\": %d,\n\t\t\t\t\"z\": %d,\n\t\t\t\t\"load\": %llu,\n\t\t\t\t\"peers\": %llu\n\t\t\t}",
  343. ((i == 0) ? "\n" : ",\n"),
  344. cs.members[i].id,
  345. cs.members[i].msSinceLastHeartbeat,
  346. (cs.members[i].alive != 0) ? "true" : "false",
  347. cs.members[i].x,
  348. cs.members[i].y,
  349. cs.members[i].z,
  350. cs.members[i].load,
  351. cs.members[i].peers);
  352. clusterJson.append(t);
  353. }
  354. clusterJson.append(" ]\n\t\t}");
  355. }
  356. }
  357. #endif
  358. Utils::snprintf(json,sizeof(json),
  359. "{\n"
  360. "\t\"address\": \"%.10llx\",\n"
  361. "\t\"publicIdentity\": \"%s\",\n"
  362. "\t\"worldId\": %llu,\n"
  363. "\t\"worldTimestamp\": %llu,\n"
  364. "\t\"online\": %s,\n"
  365. "\t\"tcpFallbackActive\": %s,\n"
  366. "\t\"versionMajor\": %d,\n"
  367. "\t\"versionMinor\": %d,\n"
  368. "\t\"versionRev\": %d,\n"
  369. "\t\"version\": \"%d.%d.%d\",\n"
  370. "\t\"clock\": %llu,\n"
  371. "\t\"cluster\": %s\n"
  372. "}\n",
  373. status.address,
  374. status.publicIdentity,
  375. status.worldId,
  376. status.worldTimestamp,
  377. (status.online) ? "true" : "false",
  378. (_svc->tcpFallbackActive()) ? "true" : "false",
  379. ZEROTIER_ONE_VERSION_MAJOR,
  380. ZEROTIER_ONE_VERSION_MINOR,
  381. ZEROTIER_ONE_VERSION_REVISION,
  382. ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,
  383. (unsigned long long)OSUtils::now(),
  384. ((clusterJson.length() > 0) ? clusterJson.c_str() : "null"));
  385. responseBody = json;
  386. scode = 200;
  387. } else if (ps[0] == "config") {
  388. responseContentType = "application/json";
  389. responseBody = "{}"; // TODO
  390. scode = 200;
  391. } else if (ps[0] == "network") {
  392. ZT_VirtualNetworkList *nws = _node->networks();
  393. if (nws) {
  394. if (ps.size() == 1) {
  395. // Return [array] of all networks
  396. responseContentType = "application/json";
  397. responseBody = "[\n";
  398. for(unsigned long i=0;i<nws->networkCount;++i) {
  399. if (i > 0)
  400. responseBody.append(",");
  401. OneService::NetworkSettings localSettings;
  402. _svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
  403. _jsonAppend(1,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
  404. }
  405. responseBody.append("\n]\n");
  406. scode = 200;
  407. } else if (ps.size() == 2) {
  408. // Return a single network by ID or 404 if not found
  409. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  410. for(unsigned long i=0;i<nws->networkCount;++i) {
  411. if (nws->networks[i].nwid == wantnw) {
  412. responseContentType = "application/json";
  413. OneService::NetworkSettings localSettings;
  414. _svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
  415. _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
  416. responseBody.push_back('\n');
  417. scode = 200;
  418. break;
  419. }
  420. }
  421. } // else 404
  422. _node->freeQueryResult((void *)nws);
  423. } else scode = 500;
  424. } else if (ps[0] == "peer") {
  425. ZT_PeerList *pl = _node->peers();
  426. if (pl) {
  427. if (ps.size() == 1) {
  428. // Return [array] of all peers
  429. responseContentType = "application/json";
  430. responseBody = "[\n";
  431. for(unsigned long i=0;i<pl->peerCount;++i) {
  432. if (i > 0)
  433. responseBody.append(",\n");
  434. _jsonAppend(1,responseBody,&(pl->peers[i]));
  435. }
  436. responseBody.append("\n]\n");
  437. scode = 200;
  438. } else if (ps.size() == 2) {
  439. // Return a single peer by ID or 404 if not found
  440. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  441. for(unsigned long i=0;i<pl->peerCount;++i) {
  442. if (pl->peers[i].address == wantp) {
  443. responseContentType = "application/json";
  444. _jsonAppend(0,responseBody,&(pl->peers[i]));
  445. responseBody.push_back('\n');
  446. scode = 200;
  447. break;
  448. }
  449. }
  450. } // else 404
  451. _node->freeQueryResult((void *)pl);
  452. } else scode = 500;
  453. } else if (ps[0] == "newIdentity") {
  454. // Return a newly generated ZeroTier identity -- this is primarily for debugging
  455. // and testing to make it easy for automated test scripts to generate test IDs.
  456. Identity newid;
  457. newid.generate();
  458. responseBody = newid.toString(true);
  459. responseContentType = "text/plain";
  460. scode = 200;
  461. } else {
  462. if (_controller)
  463. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  464. else scode = 404;
  465. }
  466. } else scode = 401; // isAuth == false
  467. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  468. if (isAuth) {
  469. if (ps[0] == "config") {
  470. // TODO
  471. } else if (ps[0] == "network") {
  472. if (ps.size() == 2) {
  473. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  474. _node->join(wantnw,(void *)0); // does nothing if we are a member
  475. ZT_VirtualNetworkList *nws = _node->networks();
  476. if (nws) {
  477. for(unsigned long i=0;i<nws->networkCount;++i) {
  478. if (nws->networks[i].nwid == wantnw) {
  479. OneService::NetworkSettings localSettings;
  480. _svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
  481. json_value *j = json_parse(body.c_str(),body.length());
  482. if (j) {
  483. if (j->type == json_object) {
  484. for(unsigned int k=0;k<j->u.object.length;++k) {
  485. if (!strcmp(j->u.object.values[k].name,"allowManaged")) {
  486. if (j->u.object.values[k].value->type == json_boolean)
  487. localSettings.allowManaged = (j->u.object.values[k].value->u.boolean != 0);
  488. } else if (!strcmp(j->u.object.values[k].name,"allowGlobal")) {
  489. if (j->u.object.values[k].value->type == json_boolean)
  490. localSettings.allowGlobal = (j->u.object.values[k].value->u.boolean != 0);
  491. } else if (!strcmp(j->u.object.values[k].name,"allowDefault")) {
  492. if (j->u.object.values[k].value->type == json_boolean)
  493. localSettings.allowDefault = (j->u.object.values[k].value->u.boolean != 0);
  494. }
  495. }
  496. }
  497. json_value_free(j);
  498. }
  499. _svc->setNetworkSettings(nws->networks[i].nwid,localSettings);
  500. responseContentType = "application/json";
  501. _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
  502. responseBody.push_back('\n');
  503. scode = 200;
  504. break;
  505. }
  506. }
  507. _node->freeQueryResult((void *)nws);
  508. } else scode = 500;
  509. }
  510. } else {
  511. if (_controller)
  512. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  513. else scode = 404;
  514. }
  515. } else scode = 401; // isAuth == false
  516. } else if (httpMethod == HTTP_DELETE) {
  517. if (isAuth) {
  518. if (ps[0] == "config") {
  519. // TODO
  520. } else if (ps[0] == "network") {
  521. ZT_VirtualNetworkList *nws = _node->networks();
  522. if (nws) {
  523. if (ps.size() == 2) {
  524. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  525. for(unsigned long i=0;i<nws->networkCount;++i) {
  526. if (nws->networks[i].nwid == wantnw) {
  527. _node->leave(wantnw,(void **)0);
  528. responseBody = "true";
  529. responseContentType = "application/json";
  530. scode = 200;
  531. break;
  532. }
  533. }
  534. } // else 404
  535. _node->freeQueryResult((void *)nws);
  536. } else scode = 500;
  537. } else {
  538. if (_controller)
  539. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  540. else scode = 404;
  541. }
  542. } else {
  543. scode = 401; // isAuth = false
  544. }
  545. } else {
  546. scode = 400;
  547. responseBody = "Method not supported.";
  548. }
  549. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  550. // Also double-check isAuth since forbidding this without auth feels safer.
  551. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  552. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  553. if (responseBody.length() > 0)
  554. responseBody = jsonp->second + "(" + responseBody + ");";
  555. else responseBody = jsonp->second + "(null);";
  556. responseContentType = "application/javascript";
  557. }
  558. return scode;
  559. }
  560. } // namespace ZeroTier