ControlPlane.cpp 19 KB

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