SoftwareUpdater.cpp 18 KB

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