ControlPlane.cpp 20 KB

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