ControlPlane.cpp 18 KB

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