SoftwareUpdater.cpp 17 KB

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