ControlPlane.cpp 18 KB

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