SoftwareUpdater.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdint.h>
  30. #include "../node/Constants.hpp"
  31. #include "../version.h"
  32. #ifdef __WINDOWS__
  33. #include <WinSock2.h>
  34. #include <Windows.h>
  35. #include <ShlObj.h>
  36. #include <netioapi.h>
  37. #include <iphlpapi.h>
  38. #else
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/wait.h>
  42. #include <unistd.h>
  43. #include <ifaddrs.h>
  44. #endif
  45. #include "SoftwareUpdater.hpp"
  46. #include "../node/Utils.hpp"
  47. #include "../node/SHA512.hpp"
  48. #include "../node/Buffer.hpp"
  49. #include "../node/Node.hpp"
  50. #include "../osdep/OSUtils.hpp"
  51. namespace ZeroTier {
  52. SoftwareUpdater::SoftwareUpdater(Node &node,const std::string &homePath) :
  53. _node(node),
  54. _lastCheckTime(0),
  55. _homePath(homePath),
  56. _channel(ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL),
  57. _distLog((FILE *)0),
  58. _latestValid(false),
  59. _downloadLength(0)
  60. {
  61. OSUtils::rm((_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME).c_str());
  62. }
  63. SoftwareUpdater::~SoftwareUpdater()
  64. {
  65. if (_distLog)
  66. fclose(_distLog);
  67. }
  68. void SoftwareUpdater::setUpdateDistribution(bool distribute)
  69. {
  70. _dist.clear();
  71. if (distribute) {
  72. _distLog = fopen((_homePath + ZT_PATH_SEPARATOR_S "update-dist.log").c_str(),"a");
  73. const std::string udd(_homePath + ZT_PATH_SEPARATOR_S "update-dist.d");
  74. const std::vector<std::string> ud(OSUtils::listDirectory(udd.c_str()));
  75. for(std::vector<std::string>::const_iterator u(ud.begin());u!=ud.end();++u) {
  76. // Each update has a companion .json file describing it. Other files are ignored.
  77. if ((u->length() > 5)&&(u->substr(u->length() - 5,5) == ".json")) {
  78. std::string buf;
  79. if (OSUtils::readFile((udd + ZT_PATH_SEPARATOR_S + *u).c_str(),buf)) {
  80. try {
  81. _D d;
  82. d.meta = OSUtils::jsonParse(buf); // throws on invalid JSON
  83. // If update meta is called e.g. foo.exe.json, then foo.exe is the update itself
  84. const std::string binPath(udd + ZT_PATH_SEPARATOR_S + u->substr(0,u->length() - 5));
  85. const std::string metaHash(OSUtils::jsonBinFromHex(d.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]));
  86. if ((metaHash.length() == ZT_SHA512_DIGEST_LEN)&&(OSUtils::readFile(binPath.c_str(),d.bin))) {
  87. uint8_t sha512[ZT_SHA512_DIGEST_LEN];
  88. SHA512::hash(sha512,d.bin.data(),(unsigned int)d.bin.length());
  89. if (!memcmp(sha512,metaHash.data(),ZT_SHA512_DIGEST_LEN)) { // double check that hash in JSON is correct
  90. d.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE] = d.bin.length(); // override with correct value -- setting this in meta json is optional
  91. _dist[Array<uint8_t,16>(sha512)] = d;
  92. if (_distLog) {
  93. fprintf(_distLog,".......... INIT: DISTRIBUTING %s (%u bytes)" ZT_EOL_S,binPath.c_str(),(unsigned int)d.bin.length());
  94. fflush(_distLog);
  95. }
  96. }
  97. }
  98. } catch ( ... ) {} // ignore bad meta JSON, etc.
  99. }
  100. }
  101. }
  102. } else {
  103. if (_distLog) {
  104. fclose(_distLog);
  105. _distLog = (FILE *)0;
  106. }
  107. }
  108. }
  109. void SoftwareUpdater::handleSoftwareUpdateUserMessage(uint64_t origin,const void *data,unsigned int len)
  110. {
  111. if (!len) return;
  112. const MessageVerb v = (MessageVerb)reinterpret_cast<const uint8_t *>(data)[0];
  113. try {
  114. switch(v) {
  115. case VERB_GET_LATEST:
  116. case VERB_LATEST: {
  117. nlohmann::json req = OSUtils::jsonParse(std::string(reinterpret_cast<const char *>(data) + 1,len - 1)); // throws on invalid JSON
  118. if (req.is_object()) {
  119. const unsigned int rvMaj = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR],0);
  120. const unsigned int rvMin = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
  121. const unsigned int rvRev = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
  122. const unsigned int rvBld = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD],0);
  123. const unsigned int rvPlatform = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_PLATFORM],0);
  124. const unsigned int rvArch = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE],0);
  125. const unsigned int rvVendor = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VENDOR],0);
  126. const std::string rvChannel(OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_CHANNEL],""));
  127. if (v == VERB_GET_LATEST) {
  128. if (_dist.size() > 0) {
  129. const nlohmann::json *latest = (const nlohmann::json *)0;
  130. const std::string expectedSigner = OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY],"");
  131. unsigned int bestVMaj = rvMaj;
  132. unsigned int bestVMin = rvMin;
  133. unsigned int bestVRev = rvRev;
  134. unsigned int bestVBld = rvBld;
  135. for(std::map< Array<uint8_t,16>,_D >::const_iterator d(_dist.begin());d!=_dist.end();++d) {
  136. // The arch field in update description .json files can be an array for e.g. multi-arch update files
  137. const nlohmann::json &dvArch2 = d->second.meta[ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE];
  138. std::vector<unsigned int> dvArch;
  139. if (dvArch2.is_array()) {
  140. for(unsigned long i=0;i<dvArch2.size();++i)
  141. dvArch.push_back((unsigned int)OSUtils::jsonInt(dvArch2[i],0));
  142. } else {
  143. dvArch.push_back((unsigned int)OSUtils::jsonInt(dvArch2,0));
  144. }
  145. if ((OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_PLATFORM],0) == rvPlatform)&&
  146. (std::find(dvArch.begin(),dvArch.end(),rvArch) != dvArch.end())&&
  147. (OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VENDOR],0) == rvVendor)&&
  148. (OSUtils::jsonString(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_CHANNEL],"") == rvChannel)&&
  149. (OSUtils::jsonString(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == expectedSigner)) {
  150. const unsigned int dvMaj = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR],0);
  151. const unsigned int dvMin = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
  152. const unsigned int dvRev = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
  153. const unsigned int dvBld = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD],0);
  154. if (Utils::compareVersion(dvMaj,dvMin,dvRev,dvBld,bestVMaj,bestVMin,bestVRev,bestVBld) > 0) {
  155. latest = &(d->second.meta);
  156. bestVMaj = dvMaj;
  157. bestVMin = dvMin;
  158. bestVRev = dvRev;
  159. bestVBld = dvBld;
  160. }
  161. }
  162. }
  163. if (latest) {
  164. std::string lj;
  165. lj.push_back((char)VERB_LATEST);
  166. lj.append(OSUtils::jsonDump(*latest));
  167. _node.sendUserMessage((void *)0,origin,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,lj.data(),(unsigned int)lj.length());
  168. if (_distLog) {
  169. fprintf(_distLog,"%.10llx GET_LATEST %u.%u.%u_%u platform %u arch %u vendor %u channel %s -> LATEST %u.%u.%u_%u" ZT_EOL_S,(unsigned long long)origin,rvMaj,rvMin,rvRev,rvBld,rvPlatform,rvArch,rvVendor,rvChannel.c_str(),bestVMaj,bestVMin,bestVRev,bestVBld);
  170. fflush(_distLog);
  171. }
  172. }
  173. } // else no reply, since we have nothing to distribute
  174. } else { // VERB_LATEST
  175. if ((origin == ZT_SOFTWARE_UPDATE_SERVICE)&&
  176. (Utils::compareVersion(rvMaj,rvMin,rvRev,rvBld,ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,ZEROTIER_ONE_VERSION_BUILD) > 0)&&
  177. (OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY)) {
  178. const unsigned long len = (unsigned long)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE],0);
  179. const std::string hash = OSUtils::jsonBinFromHex(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]);
  180. if ((len <= ZT_SOFTWARE_UPDATE_MAX_SIZE)&&(hash.length() >= 16)) {
  181. if (_latestMeta != req) {
  182. _latestMeta = req;
  183. _latestValid = false;
  184. OSUtils::rm((_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME).c_str());
  185. _download = std::string();
  186. memcpy(_downloadHashPrefix.data,hash.data(),16);
  187. _downloadLength = len;
  188. }
  189. if ((_downloadLength > 0)&&(_download.length() < _downloadLength)) {
  190. Buffer<128> gd;
  191. gd.append((uint8_t)VERB_GET_DATA);
  192. gd.append(_downloadHashPrefix.data,16);
  193. gd.append((uint32_t)_download.length());
  194. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  195. //printf(">> GET_DATA @%u\n",(unsigned int)_download.length());
  196. }
  197. }
  198. }
  199. }
  200. }
  201. } break;
  202. case VERB_GET_DATA:
  203. if ((len >= 21)&&(_dist.size() > 0)) {
  204. unsigned long idx = (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 17) << 24;
  205. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 18) << 16;
  206. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 19) << 8;
  207. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 20);
  208. //printf("<< GET_DATA @%u from %.10llx for %s\n",(unsigned int)idx,origin,Utils::hex(reinterpret_cast<const uint8_t *>(data) + 1,16).c_str());
  209. std::map< Array<uint8_t,16>,_D >::iterator d(_dist.find(Array<uint8_t,16>(reinterpret_cast<const uint8_t *>(data) + 1)));
  210. if ((d != _dist.end())&&(idx < (unsigned long)d->second.bin.length())) {
  211. Buffer<ZT_SOFTWARE_UPDATE_CHUNK_SIZE + 128> buf;
  212. buf.append((uint8_t)VERB_DATA);
  213. buf.append(reinterpret_cast<const uint8_t *>(data) + 1,16);
  214. buf.append((uint32_t)idx);
  215. buf.append(d->second.bin.data() + idx,std::min((unsigned long)ZT_SOFTWARE_UPDATE_CHUNK_SIZE,(unsigned long)(d->second.bin.length() - idx)));
  216. _node.sendUserMessage((void *)0,origin,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,buf.data(),buf.size());
  217. //printf(">> DATA @%u\n",(unsigned int)idx);
  218. }
  219. }
  220. break;
  221. case VERB_DATA:
  222. if ((len >= 21)&&(_downloadLength > 0)&&(!memcmp(_downloadHashPrefix.data,reinterpret_cast<const uint8_t *>(data) + 1,16))) {
  223. unsigned long idx = (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 17) << 24;
  224. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 18) << 16;
  225. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 19) << 8;
  226. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 20);
  227. //printf("<< DATA @%u / %u bytes (we now have %u bytes)\n",(unsigned int)idx,(unsigned int)(len - 21),(unsigned int)_download.length());
  228. if (idx == (unsigned long)_download.length()) {
  229. _download.append(reinterpret_cast<const char *>(data) + 21,len - 21);
  230. if (_download.length() < _downloadLength) {
  231. Buffer<128> gd;
  232. gd.append((uint8_t)VERB_GET_DATA);
  233. gd.append(_downloadHashPrefix.data,16);
  234. gd.append((uint32_t)_download.length());
  235. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  236. //printf(">> GET_DATA @%u\n",(unsigned int)_download.length());
  237. }
  238. }
  239. }
  240. break;
  241. default:
  242. if (_distLog) {
  243. fprintf(_distLog,"%.10llx WARNING: bad update message verb==%u length==%u (unrecognized verb)" ZT_EOL_S,(unsigned long long)origin,(unsigned int)v,len);
  244. fflush(_distLog);
  245. }
  246. break;
  247. }
  248. } catch ( ... ) {
  249. if (_distLog) {
  250. fprintf(_distLog,"%.10llx WARNING: bad update message verb==%u length==%u (unexpected exception, likely invalid JSON)" ZT_EOL_S,(unsigned long long)origin,(unsigned int)v,len);
  251. fflush(_distLog);
  252. }
  253. }
  254. }
  255. bool SoftwareUpdater::check(const uint64_t now)
  256. {
  257. if ((now - _lastCheckTime) >= ZT_SOFTWARE_UPDATE_CHECK_PERIOD) {
  258. _lastCheckTime = now;
  259. char tmp[512];
  260. const unsigned int len = OSUtils::ztsnprintf(tmp,sizeof(tmp),
  261. "%c{\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR "\":%d,"
  262. "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR "\":%d,"
  263. "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION "\":%d,"
  264. "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD "\":%d,"
  265. "\"" ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY "\":\"%s\","
  266. "\"" ZT_SOFTWARE_UPDATE_JSON_PLATFORM "\":%d,"
  267. "\"" ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE "\":%d,"
  268. "\"" ZT_SOFTWARE_UPDATE_JSON_VENDOR "\":%d,"
  269. "\"" ZT_SOFTWARE_UPDATE_JSON_CHANNEL "\":\"%s\"}",
  270. (char)VERB_GET_LATEST,
  271. ZEROTIER_ONE_VERSION_MAJOR,
  272. ZEROTIER_ONE_VERSION_MINOR,
  273. ZEROTIER_ONE_VERSION_REVISION,
  274. ZEROTIER_ONE_VERSION_BUILD,
  275. ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY,
  276. ZT_BUILD_PLATFORM,
  277. ZT_BUILD_ARCHITECTURE,
  278. (int)ZT_VENDOR_ZEROTIER,
  279. _channel.c_str());
  280. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,tmp,len);
  281. //printf(">> GET_LATEST\n");
  282. }
  283. if (_latestValid)
  284. return true;
  285. if (_downloadLength > 0) {
  286. if (_download.length() >= _downloadLength) {
  287. // This is the very important security validation part that makes sure
  288. // this software update doesn't have cooties.
  289. const std::string binPath(_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME);
  290. try {
  291. // (1) Check the hash itself to make sure the image is basically okay
  292. uint8_t sha512[ZT_SHA512_DIGEST_LEN];
  293. SHA512::hash(sha512,_download.data(),(unsigned int)_download.length());
  294. char hexbuf[(ZT_SHA512_DIGEST_LEN * 2) + 2];
  295. if (OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH],"") == Utils::hex(sha512,ZT_SHA512_DIGEST_LEN,hexbuf)) {
  296. // (2) Check signature by signing authority
  297. const std::string sig(OSUtils::jsonBinFromHex(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNATURE]));
  298. if (Identity(ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY).verify(_download.data(),(unsigned int)_download.length(),sig.data(),(unsigned int)sig.length())) {
  299. // (3) Try to save file, and if so we are good.
  300. OSUtils::rm(binPath.c_str());
  301. if (OSUtils::writeFile(binPath.c_str(),_download)) {
  302. OSUtils::lockDownFile(binPath.c_str(),false);
  303. _latestValid = true;
  304. //printf("VALID UPDATE\n%s\n",OSUtils::jsonDump(_latestMeta).c_str());
  305. _download = std::string();
  306. _downloadLength = 0;
  307. return true;
  308. }
  309. }
  310. }
  311. } catch ( ... ) {} // any exception equals verification failure
  312. // If we get here, checks failed.
  313. //printf("INVALID UPDATE (!!!)\n%s\n",OSUtils::jsonDump(_latestMeta).c_str());
  314. OSUtils::rm(binPath.c_str());
  315. _latestMeta = nlohmann::json();
  316. _latestValid = false;
  317. _download = std::string();
  318. _downloadLength = 0;
  319. } else {
  320. Buffer<128> gd;
  321. gd.append((uint8_t)VERB_GET_DATA);
  322. gd.append(_downloadHashPrefix.data,16);
  323. gd.append((uint32_t)_download.length());
  324. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  325. //printf(">> GET_DATA @%u\n",(unsigned int)_download.length());
  326. }
  327. }
  328. return false;
  329. }
  330. void SoftwareUpdater::apply()
  331. {
  332. std::string updatePath(_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME);
  333. if ((_latestMeta.is_object())&&(_latestValid)&&(OSUtils::fileExists(updatePath.c_str(),false))) {
  334. #ifdef __WINDOWS__
  335. std::string cmdArgs(OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_EXEC_ARGS],""));
  336. if (cmdArgs.length() > 0) {
  337. updatePath.push_back(' ');
  338. updatePath.append(cmdArgs);
  339. }
  340. STARTUPINFOA si;
  341. PROCESS_INFORMATION pi;
  342. memset(&si,0,sizeof(si));
  343. memset(&pi,0,sizeof(pi));
  344. CreateProcessA(NULL,const_cast<LPSTR>(updatePath.c_str()),NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi);
  345. // Windows doesn't exit here -- updater will stop the service during update, etc. -- but we do want to stop multiple runs from happening
  346. _latestMeta = nlohmann::json();
  347. _latestValid = false;
  348. #else
  349. char *argv[256];
  350. unsigned long ac = 0;
  351. argv[ac++] = const_cast<char *>(updatePath.c_str());
  352. const std::vector<std::string> argsSplit(OSUtils::split(OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_EXEC_ARGS],"").c_str()," ","\\","\""));
  353. for(std::vector<std::string>::const_iterator a(argsSplit.begin());a!=argsSplit.end();++a) {
  354. argv[ac] = const_cast<char *>(a->c_str());
  355. if (++ac == 255) break;
  356. }
  357. argv[ac] = (char *)0;
  358. chmod(updatePath.c_str(),0700);
  359. // Close all open file descriptors except stdout/stderr/etc.
  360. int minMyFd = STDIN_FILENO;
  361. if (STDOUT_FILENO > minMyFd) minMyFd = STDOUT_FILENO;
  362. if (STDERR_FILENO > minMyFd) minMyFd = STDERR_FILENO;
  363. ++minMyFd;
  364. #ifdef _SC_OPEN_MAX
  365. int maxMyFd = (int)sysconf(_SC_OPEN_MAX);
  366. if (maxMyFd <= minMyFd)
  367. maxMyFd = 65536;
  368. #else
  369. int maxMyFd = 65536;
  370. #endif
  371. while (minMyFd < maxMyFd)
  372. close(minMyFd++);
  373. execv(updatePath.c_str(),argv);
  374. fprintf(stderr,"FATAL: unable to execute software update binary at %s\n",updatePath.c_str());
  375. exit(1);
  376. #endif
  377. }
  378. }
  379. } // namespace ZeroTier