ControlPlane.cpp 18 KB

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