ControlPlane.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. #include "../ext/json/json.hpp"
  28. #include "../controller/EmbeddedNetworkController.hpp"
  29. #include "../node/InetAddress.hpp"
  30. #include "../node/Node.hpp"
  31. #include "../node/Utils.hpp"
  32. #include "../node/World.hpp"
  33. #include "../osdep/OSUtils.hpp"
  34. namespace ZeroTier {
  35. namespace {
  36. static void _networkToJson(nlohmann::json &nj,const ZT_VirtualNetworkConfig *nc,const std::string &portDeviceName,const OneService::NetworkSettings &localSettings)
  37. {
  38. char tmp[256];
  39. const char *nstatus = "",*ntype = "";
  40. switch(nc->status) {
  41. case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  42. case ZT_NETWORK_STATUS_OK: nstatus = "OK"; break;
  43. case ZT_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  44. case ZT_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  45. case ZT_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  46. case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  47. }
  48. switch(nc->type) {
  49. case ZT_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  50. case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  51. }
  52. Utils::snprintf(tmp,sizeof(tmp),"%.16llx",nc->nwid);
  53. nj["id"] = tmp;
  54. nj["nwid"] = tmp;
  55. Utils::snprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(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));
  56. nj["mac"] = tmp;
  57. nj["name"] = nc->name;
  58. nj["status"] = nstatus;
  59. nj["type"] = ntype;
  60. nj["mtu"] = nc->mtu;
  61. nj["dhcp"] = (bool)(nc->dhcp != 0);
  62. nj["bridge"] = (bool)(nc->bridge != 0);
  63. nj["broadcastEnabled"] = (bool)(nc->broadcastEnabled != 0);
  64. nj["portError"] = nc->portError;
  65. nj["netconfRevision"] = nc->netconfRevision;
  66. nj["portDeviceName"] = portDeviceName;
  67. nj["allowManaged"] = localSettings.allowManaged;
  68. nj["allowGlobal"] = localSettings.allowGlobal;
  69. nj["allowDefault"] = localSettings.allowDefault;
  70. nlohmann::json aa = nlohmann::json::array();
  71. for(unsigned int i=0;i<nc->assignedAddressCount;++i) {
  72. aa.push_back(reinterpret_cast<const InetAddress *>(&(nc->assignedAddresses[i]))->toString());
  73. }
  74. nj["assignedAddresses"] = aa;
  75. nlohmann::json ra = nlohmann::json::array();
  76. for(unsigned int i=0;i<nc->routeCount;++i) {
  77. nlohmann::json rj;
  78. rj["target"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].target))->toString();
  79. if (nc->routes[i].via.ss_family == nc->routes[i].target.ss_family)
  80. rj["via"] = reinterpret_cast<const InetAddress *>(&(nc->routes[i].via))->toIpString();
  81. else rj["via"] = nlohmann::json();
  82. rj["flags"] = (int)nc->routes[i].flags;
  83. rj["metric"] = (int)nc->routes[i].metric;
  84. ra.push_back(rj);
  85. }
  86. nj["routes"] = ra;
  87. }
  88. static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer)
  89. {
  90. char tmp[256];
  91. const char *prole = "";
  92. switch(peer->role) {
  93. case ZT_PEER_ROLE_LEAF: prole = "LEAF"; break;
  94. case ZT_PEER_ROLE_MOON: prole = "MOON"; break;
  95. case ZT_PEER_ROLE_PLANET: prole = "PLANET"; break;
  96. }
  97. Utils::snprintf(tmp,sizeof(tmp),"%.10llx",peer->address);
  98. pj["address"] = tmp;
  99. pj["versionMajor"] = peer->versionMajor;
  100. pj["versionMinor"] = peer->versionMinor;
  101. pj["versionRev"] = peer->versionRev;
  102. Utils::snprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev);
  103. pj["version"] = tmp;
  104. pj["latency"] = peer->latency;
  105. pj["role"] = prole;
  106. nlohmann::json pa = nlohmann::json::array();
  107. for(unsigned int i=0;i<peer->pathCount;++i) {
  108. nlohmann::json j;
  109. j["address"] = reinterpret_cast<const InetAddress *>(&(peer->paths[i].address))->toString();
  110. j["lastSend"] = peer->paths[i].lastSend;
  111. j["lastReceive"] = peer->paths[i].lastReceive;
  112. j["trustedPathId"] = peer->paths[i].trustedPathId;
  113. j["linkQuality"] = (double)peer->paths[i].linkQuality / (double)ZT_PATH_LINK_QUALITY_MAX;
  114. j["active"] = (bool)(peer->paths[i].expired == 0);
  115. j["expired"] = (bool)(peer->paths[i].expired != 0);
  116. j["preferred"] = (bool)(peer->paths[i].preferred != 0);
  117. pa.push_back(j);
  118. }
  119. pj["paths"] = pa;
  120. }
  121. static void _moonToJson(nlohmann::json &mj,const World &world)
  122. {
  123. char tmp[64];
  124. Utils::snprintf(tmp,sizeof(tmp),"%.16llx",world.id());
  125. mj["id"] = tmp;
  126. mj["timestamp"] = world.timestamp();
  127. mj["signature"] = Utils::hex(world.signature().data,(unsigned int)world.signature().size());
  128. mj["updatesMustBeSignedBy"] = Utils::hex(world.updatesMustBeSignedBy().data,(unsigned int)world.updatesMustBeSignedBy().size());
  129. nlohmann::json ra = nlohmann::json::array();
  130. for(std::vector<World::Root>::const_iterator r(world.roots().begin());r!=world.roots().end();++r) {
  131. nlohmann::json rj;
  132. rj["identity"] = r->identity.toString(false);
  133. nlohmann::json eps = nlohmann::json::array();
  134. for(std::vector<InetAddress>::const_iterator a(r->stableEndpoints.begin());a!=r->stableEndpoints.end();++a)
  135. eps.push_back(a->toString());
  136. rj["stableEndpoints"] = eps;
  137. ra.push_back(rj);
  138. }
  139. mj["roots"] = ra;
  140. mj["waiting"] = false;
  141. }
  142. } // anonymous namespace
  143. ControlPlane::ControlPlane(OneService *svc,Node *n) :
  144. _svc(svc),
  145. _node(n),
  146. _controller((EmbeddedNetworkController *)0)
  147. {
  148. }
  149. unsigned int ControlPlane::handleRequest(
  150. const InetAddress &fromAddress,
  151. unsigned int httpMethod,
  152. const std::string &path,
  153. const std::map<std::string,std::string> &headers,
  154. const std::string &body,
  155. std::string &responseBody,
  156. std::string &responseContentType)
  157. {
  158. char tmp[256];
  159. unsigned int scode = 404;
  160. nlohmann::json res;
  161. std::vector<std::string> ps(OSUtils::split(path.c_str(),"/","",""));
  162. std::map<std::string,std::string> urlArgs;
  163. Mutex::Lock _l(_lock);
  164. /* Note: this is kind of restricted in what it'll take. It does not support
  165. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  166. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  167. * paths to simply-named resources. */
  168. if (ps.size() > 0) {
  169. std::size_t qpos = ps[ps.size() - 1].find('?');
  170. if (qpos != std::string::npos) {
  171. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  172. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  173. std::vector<std::string> asplit(OSUtils::split(args.c_str(),"&","",""));
  174. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  175. std::size_t eqpos = a->find('=');
  176. if (eqpos == std::string::npos)
  177. urlArgs[*a] = "";
  178. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  179. }
  180. }
  181. }
  182. bool isAuth = false;
  183. {
  184. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  185. if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) {
  186. isAuth = true;
  187. } else {
  188. ah = urlArgs.find("auth");
  189. if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0))
  190. isAuth = true;
  191. }
  192. }
  193. #ifdef __SYNOLOGY__
  194. #include <stdlib.h>
  195. // Authenticate via Synology's built-in cgi script
  196. if (!isAuth) {
  197. /*
  198. fprintf(stderr, "path = %s\n", path.c_str());
  199. fprintf(stderr, "headers.size=%d\n", headers.size());
  200. std::map<std::string, std::string>::const_iterator it(headers.begin());
  201. while(it != headers.end()) {
  202. fprintf(stderr,"header[%s] = %s\n", (it->first).c_str(), (it->second).c_str());
  203. it++;
  204. }
  205. */
  206. // parse out url args
  207. int synotoken_pos = path.find("SynoToken");
  208. int argpos = path.find("?");
  209. if(synotoken_pos != std::string::npos && argpos != std::string::npos) {
  210. std::string cookie = path.substr(argpos+1, synotoken_pos-(argpos+1));
  211. std::string synotoken = path.substr(synotoken_pos);
  212. std::string cookie_val = cookie.substr(cookie.find("=")+1);
  213. std::string synotoken_val = synotoken.substr(synotoken.find("=")+1);
  214. // Set necessary env for auth script
  215. std::map<std::string,std::string>::const_iterator ah2(headers.find("x-forwarded-for"));
  216. setenv("HTTP_COOKIE", cookie_val.c_str(), true);
  217. setenv("HTTP_X_SYNO_TOKEN", synotoken_val.c_str(), true);
  218. setenv("REMOTE_ADDR", ah2->second.c_str(),true);
  219. //fprintf(stderr, "HTTP_COOKIE: %s\n",std::getenv ("HTTP_COOKIE"));
  220. //fprintf(stderr, "HTTP_X_SYNO_TOKEN: %s\n",std::getenv ("HTTP_X_SYNO_TOKEN"));
  221. //fprintf(stderr, "REMOTE_ADDR: %s\n",std::getenv ("REMOTE_ADDR"));
  222. // check synology web auth
  223. char user[256], buf[1024];
  224. FILE *fp = NULL;
  225. bzero(user, 256);
  226. fp = popen("/usr/syno/synoman/webman/modules/authenticate.cgi", "r");
  227. if(!fp)
  228. isAuth = false;
  229. else {
  230. bzero(buf, sizeof(buf));
  231. fread(buf, 1024, 1, fp);
  232. if(strlen(buf) > 0) {
  233. snprintf(user, 256, "%s", buf);
  234. isAuth = true;
  235. }
  236. }
  237. pclose(fp);
  238. }
  239. }
  240. #endif
  241. if (httpMethod == HTTP_GET) {
  242. if (isAuth) {
  243. if (ps[0] == "status") {
  244. ZT_NodeStatus status;
  245. _node->status(&status);
  246. Utils::snprintf(tmp,sizeof(tmp),"%.10llx",status.address);
  247. res["address"] = tmp;
  248. res["publicIdentity"] = status.publicIdentity;
  249. res["online"] = (bool)(status.online != 0);
  250. res["tcpFallbackActive"] = _svc->tcpFallbackActive();
  251. res["versionMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  252. res["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  253. res["versionRev"] = ZEROTIER_ONE_VERSION_REVISION;
  254. res["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  255. Utils::snprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  256. res["version"] = tmp;
  257. res["clock"] = OSUtils::now();
  258. World planet(_node->planet());
  259. res["planetWorldId"] = planet.id();
  260. res["planetWorldTimestamp"] = planet.timestamp();
  261. #ifdef ZT_ENABLE_CLUSTER
  262. nlohmann::json cj;
  263. ZT_ClusterStatus cs;
  264. _node->clusterStatus(&cs);
  265. if (cs.clusterSize >= 1) {
  266. nlohmann::json cja = nlohmann::json::array();
  267. for(unsigned int i=0;i<cs.clusterSize;++i) {
  268. nlohmann::json cjm;
  269. cjm["id"] = (int)cs.members[i].id;
  270. cjm["msSinceLastHeartbeat"] = cs.members[i].msSinceLastHeartbeat;
  271. cjm["alive"] = (bool)(cs.members[i].alive != 0);
  272. cjm["x"] = cs.members[i].x;
  273. cjm["y"] = cs.members[i].y;
  274. cjm["z"] = cs.members[i].z;
  275. cjm["load"] = cs.members[i].load;
  276. cjm["peers"] = cs.members[i].peers;
  277. cja.push_back(cjm);
  278. }
  279. cj["members"] = cja;
  280. cj["myId"] = (int)cs.myId;
  281. cj["clusterSize"] = cs.clusterSize;
  282. }
  283. res["cluster"] = cj;
  284. #else
  285. res["cluster"] = nlohmann::json();
  286. #endif
  287. scode = 200;
  288. } else if (ps[0] == "moon") {
  289. std::vector<World> moons(_node->moons());
  290. if (ps.size() == 1) {
  291. // Return [array] of all moons
  292. res = nlohmann::json::array();
  293. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  294. nlohmann::json mj;
  295. _moonToJson(mj,*m);
  296. res.push_back(mj);
  297. }
  298. scode = 200;
  299. } else {
  300. // Return a single moon by ID
  301. const uint64_t id = Utils::hexStrToU64(ps[1].c_str());
  302. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  303. if (m->id() == id) {
  304. _moonToJson(res,*m);
  305. scode = 200;
  306. break;
  307. }
  308. }
  309. }
  310. } else if (ps[0] == "network") {
  311. ZT_VirtualNetworkList *nws = _node->networks();
  312. if (nws) {
  313. if (ps.size() == 1) {
  314. // Return [array] of all networks
  315. res = nlohmann::json::array();
  316. for(unsigned long i=0;i<nws->networkCount;++i) {
  317. OneService::NetworkSettings localSettings;
  318. _svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
  319. nlohmann::json nj;
  320. _networkToJson(nj,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
  321. res.push_back(nj);
  322. }
  323. scode = 200;
  324. } else if (ps.size() == 2) {
  325. // Return a single network by ID or 404 if not found
  326. const uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  327. for(unsigned long i=0;i<nws->networkCount;++i) {
  328. if (nws->networks[i].nwid == wantnw) {
  329. OneService::NetworkSettings localSettings;
  330. _svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
  331. _networkToJson(res,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
  332. scode = 200;
  333. break;
  334. }
  335. }
  336. } else scode = 404;
  337. _node->freeQueryResult((void *)nws);
  338. } else scode = 500;
  339. } else if (ps[0] == "peer") {
  340. ZT_PeerList *pl = _node->peers();
  341. if (pl) {
  342. if (ps.size() == 1) {
  343. // Return [array] of all peers
  344. res = nlohmann::json::array();
  345. for(unsigned long i=0;i<pl->peerCount;++i) {
  346. nlohmann::json pj;
  347. _peerToJson(pj,&(pl->peers[i]));
  348. res.push_back(pj);
  349. }
  350. scode = 200;
  351. } else if (ps.size() == 2) {
  352. // Return a single peer by ID or 404 if not found
  353. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  354. for(unsigned long i=0;i<pl->peerCount;++i) {
  355. if (pl->peers[i].address == wantp) {
  356. _peerToJson(res,&(pl->peers[i]));
  357. scode = 200;
  358. break;
  359. }
  360. }
  361. } else scode = 404;
  362. _node->freeQueryResult((void *)pl);
  363. } else scode = 500;
  364. } else {
  365. if (_controller) {
  366. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  367. } else scode = 404;
  368. }
  369. } else scode = 401; // isAuth == false
  370. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  371. if (isAuth) {
  372. if (ps[0] == "moon") {
  373. if (ps.size() == 2) {
  374. uint64_t seed = 0;
  375. try {
  376. nlohmann::json j(OSUtils::jsonParse(body));
  377. if (j.is_object()) {
  378. seed = Utils::hexStrToU64(OSUtils::jsonString(j["seed"],"0").c_str());
  379. }
  380. } catch ( ... ) {
  381. // discard invalid JSON
  382. }
  383. std::vector<World> moons(_node->moons());
  384. const uint64_t id = Utils::hexStrToU64(ps[1].c_str());
  385. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  386. if (m->id() == id) {
  387. _moonToJson(res,*m);
  388. scode = 200;
  389. break;
  390. }
  391. }
  392. if ((scode != 200)&&(seed != 0)) {
  393. char tmp[64];
  394. Utils::snprintf(tmp,sizeof(tmp),"%.16llx",id);
  395. res["id"] = tmp;
  396. res["roots"] = nlohmann::json::array();
  397. res["timestamp"] = 0;
  398. res["signature"] = nlohmann::json();
  399. res["updatesMustBeSignedBy"] = nlohmann::json();
  400. res["waiting"] = true;
  401. _node->orbit(id,seed);
  402. }
  403. } else scode = 404;
  404. } else if (ps[0] == "network") {
  405. if (ps.size() == 2) {
  406. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  407. _node->join(wantnw,(void *)0); // does nothing if we are a member
  408. ZT_VirtualNetworkList *nws = _node->networks();
  409. if (nws) {
  410. for(unsigned long i=0;i<nws->networkCount;++i) {
  411. if (nws->networks[i].nwid == wantnw) {
  412. OneService::NetworkSettings localSettings;
  413. _svc->getNetworkSettings(nws->networks[i].nwid,localSettings);
  414. try {
  415. nlohmann::json j(OSUtils::jsonParse(body));
  416. if (j.is_object()) {
  417. nlohmann::json &allowManaged = j["allowManaged"];
  418. if (allowManaged.is_boolean()) localSettings.allowManaged = (bool)allowManaged;
  419. nlohmann::json &allowGlobal = j["allowGlobal"];
  420. if (allowGlobal.is_boolean()) localSettings.allowGlobal = (bool)allowGlobal;
  421. nlohmann::json &allowDefault = j["allowDefault"];
  422. if (allowDefault.is_boolean()) localSettings.allowDefault = (bool)allowDefault;
  423. }
  424. } catch ( ... ) {
  425. // discard invalid JSON
  426. }
  427. _svc->setNetworkSettings(nws->networks[i].nwid,localSettings);
  428. _networkToJson(res,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid),localSettings);
  429. scode = 200;
  430. break;
  431. }
  432. }
  433. _node->freeQueryResult((void *)nws);
  434. } else scode = 500;
  435. } else scode = 404;
  436. } else {
  437. if (_controller)
  438. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  439. else scode = 404;
  440. }
  441. } else scode = 401; // isAuth == false
  442. } else if (httpMethod == HTTP_DELETE) {
  443. if (isAuth) {
  444. if (ps[0] == "moon") {
  445. if (ps.size() == 2) {
  446. _node->deorbit(Utils::hexStrToU64(ps[1].c_str()));
  447. res["result"] = true;
  448. scode = 200;
  449. } // else 404
  450. } else if (ps[0] == "network") {
  451. ZT_VirtualNetworkList *nws = _node->networks();
  452. if (nws) {
  453. if (ps.size() == 2) {
  454. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  455. for(unsigned long i=0;i<nws->networkCount;++i) {
  456. if (nws->networks[i].nwid == wantnw) {
  457. _node->leave(wantnw,(void **)0);
  458. res["result"] = true;
  459. scode = 200;
  460. break;
  461. }
  462. }
  463. } // else 404
  464. _node->freeQueryResult((void *)nws);
  465. } else scode = 500;
  466. } else {
  467. if (_controller)
  468. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  469. else scode = 404;
  470. }
  471. } else scode = 401; // isAuth = false
  472. } else {
  473. scode = 400;
  474. }
  475. if (responseBody.length() == 0) {
  476. if ((res.is_object())||(res.is_array()))
  477. responseBody = OSUtils::jsonDump(res);
  478. else responseBody = "{}";
  479. responseContentType = "application/json";
  480. }
  481. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  482. // Also double-check isAuth since forbidding this without auth feels safer.
  483. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  484. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  485. if (responseBody.length() > 0)
  486. responseBody = jsonp->second + "(" + responseBody + ");";
  487. else responseBody = jsonp->second + "(null);";
  488. responseContentType = "application/javascript";
  489. }
  490. return scode;
  491. }
  492. } // namespace ZeroTier