Updater.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "Updater.hpp"
  28. #include "RuntimeEnvironment.hpp"
  29. #include "Logger.hpp"
  30. #include "Defaults.hpp"
  31. #include "Utils.hpp"
  32. #include "Topology.hpp"
  33. #include "../version.h"
  34. namespace ZeroTier {
  35. Updater::Updater(const RuntimeEnvironment *renv) :
  36. _r(renv),
  37. _download((_Download *)0)
  38. {
  39. refreshShared();
  40. }
  41. Updater::~Updater()
  42. {
  43. Mutex::Lock _l(_lock);
  44. delete _download;
  45. }
  46. void Updater::refreshShared()
  47. {
  48. std::string updatesPath(_r->homePath + ZT_PATH_SEPARATOR_S + "updates.d");
  49. std::map<std::string,bool> ud(Utils::listDirectory(updatesPath.c_str()));
  50. Mutex::Lock _l(_lock);
  51. _sharedUpdates.clear();
  52. for(std::map<std::string,bool>::iterator u(ud.begin());u!=ud.end();++u) {
  53. if (u->second)
  54. continue; // skip directories
  55. if ((u->first.length() >= 4)&&(!strcasecmp(u->first.substr(u->first.length() - 4).c_str(),".nfo")))
  56. continue; // skip .nfo companion files
  57. std::string fullPath(updatesPath + ZT_PATH_SEPARATOR_S + u->first);
  58. std::string nfoPath(fullPath + ".nfo");
  59. std::string buf;
  60. if (Utils::readFile(nfoPath.c_str(),buf)) {
  61. Dictionary nfo(buf);
  62. _Shared shared;
  63. shared.filename = fullPath;
  64. std::string sha512(Utils::unhex(nfo.get("sha512",std::string())));
  65. if (sha512.length() < sizeof(shared.sha512)) {
  66. TRACE("skipped shareable update due to missing fields in companion .nfo: %s",fullPath.c_str());
  67. continue;
  68. }
  69. memcpy(shared.sha512,sha512.data(),sizeof(shared.sha512));
  70. std::string sig(Utils::unhex(nfo.get("sha512sig_ed25519",std::string())));
  71. if (sig.length() < shared.sig.size()) {
  72. TRACE("skipped shareable update due to missing fields in companion .nfo: %s",fullPath.c_str());
  73. continue;
  74. }
  75. memcpy(shared.sig.data,sig.data(),shared.sig.size());
  76. // Check signature to guard against updates.d being used as a data
  77. // exfiltration mechanism. We will only share properly signed updates,
  78. // nothing else.
  79. Address signedBy(nfo.get("signedBy",std::string()));
  80. std::map< Address,Identity >::const_iterator authority(ZT_DEFAULTS.updateAuthorities.find(signedBy));
  81. if ((authority == ZT_DEFAULTS.updateAuthorities.end())||(!authority->second.verify(shared.sha512,64,shared.sig))) {
  82. TRACE("skipped shareable update: not signed by valid authority or signature invalid: %s",fullPath.c_str());
  83. continue;
  84. }
  85. shared.signedBy = signedBy;
  86. int64_t fs = Utils::getFileSize(fullPath.c_str());
  87. if (fs <= 0) {
  88. TRACE("skipped shareable update due to unreadable, invalid, or 0-byte file: %s",fullPath.c_str());
  89. continue;
  90. }
  91. shared.size = (unsigned long)fs;
  92. Array<unsigned char,16> first16Bytes;
  93. memcpy(first16Bytes.data,sha512.data(),16);
  94. _sharedUpdates[first16Bytes] = shared;
  95. } else {
  96. TRACE("skipped shareable update due to missing companion .nfo: %s",fullPath.c_str());
  97. continue;
  98. }
  99. }
  100. }
  101. void Updater::getUpdateIfThisIsNewer(unsigned int vMajor,unsigned int vMinor,unsigned int revision)
  102. {
  103. if (vMajor < ZEROTIER_ONE_VERSION_MAJOR)
  104. return;
  105. else if (vMajor == ZEROTIER_ONE_VERSION_MAJOR) {
  106. if (vMinor < ZEROTIER_ONE_VERSION_MINOR)
  107. return;
  108. else if (vMinor == ZEROTIER_ONE_VERSION_MINOR) {
  109. if (revision <= ZEROTIER_ONE_VERSION_REVISION)
  110. return;
  111. }
  112. }
  113. std::string updateFilename(generateUpdateFilename());
  114. if (!updateFilename.length()) {
  115. TRACE("a new update to %u.%u.%u is available, but this platform doesn't support auto updates",vMajor,vMinor,revision);
  116. return;
  117. }
  118. std::vector< SharedPtr<Peer> > peers;
  119. _r->topology->eachPeer(Topology::CollectPeersWithActiveDirectPath(peers,Utils::now()));
  120. TRACE("new update available to %u.%u.%u, looking for %s from %u peers",vMajor,vMinor,revision,updateFilename.c_str(),(unsigned int)peers.size());
  121. if (!peers.size())
  122. return;
  123. for(std::vector< SharedPtr<Peer> >::iterator p(peers.begin());p!=peers.end();++p) {
  124. Packet outp(p->address(),_r->identity.address(),Packet::VERB_FILE_INFO_REQUEST);
  125. outp.append((unsigned char)0);
  126. outp.append((uint16_t)updateFilename.length());
  127. outp.append(updateFilename.data(),updateFilename.length());
  128. _r->sw->send(outp,true);
  129. }
  130. }
  131. void Updater::retryIfNeeded()
  132. {
  133. }
  134. void Updater::handleChunk(const void *sha512First16,unsigned long at,const void *chunk,unsigned long len)
  135. {
  136. }
  137. std::string Updater::generateUpdateFilename(unsigned int vMajor,unsigned int vMinor,unsigned int revision)
  138. {
  139. // Not supported... yet? Get it first cause it might identify as Linux too.
  140. #ifdef __ANDROID__
  141. #define _updSupported 1
  142. return std::string();
  143. #endif
  144. // Linux on x86 and possibly in the future ARM
  145. #ifdef __LINUX__
  146. #if defined(__i386) || defined(__i486) || defined(__i586) || defined(__i686) || defined(__amd64) || defined(__x86_64) || defined(i386)
  147. #define _updSupported 1
  148. char buf[128];
  149. Utils::snprintf(buf,sizeof(buf),"zt1-%u_%u_%u-linux-%s-update",vMajor,vMinor,revision,(sizeof(void *) == 8) ? "x64" : "x86");
  150. return std::string(buf);
  151. #endif
  152. /*
  153. #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
  154. #define _updSupported 1
  155. char buf[128];
  156. Utils::snprintf(buf,sizeof(buf),"zt1-%u_%u_%u-linux-%s-update",vMajor,vMinor,revision,(sizeof(void *) == 8) ? "arm64" : "arm32");
  157. return std::string(buf);
  158. #endif
  159. */
  160. #endif
  161. // Apple stuff... only Macs so far...
  162. #ifdef __APPLE__
  163. #define _updSupported 1
  164. #if defined(__powerpc) || defined(__powerpc__) || defined(__ppc__) || defined(__ppc64) || defined(__ppc64__) || defined(__powerpc64__)
  165. return std::string();
  166. #endif
  167. #if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)
  168. return std::string();
  169. #endif
  170. char buf[128];
  171. Utils::snprintf(buf,sizeof(buf),"zt1-%u_%u_%u-mac-x86combined-update",vMajor,vMinor,revision);
  172. return std::string(buf);
  173. #endif
  174. // ???
  175. #ifndef _updSupported
  176. return std::string();
  177. #endif
  178. }
  179. bool Updater::parseUpdateFilename(const char *filename,unsigned int &vMajor,unsigned int &vMinor,unsigned int &revision)
  180. {
  181. std::vector<std::string> byDash(Utils::split(filename,"-","",""));
  182. if (byDash.size() < 2)
  183. return false;
  184. std::vector<std::string> byUnderscore(Utils::split(byDash[1].c_str(),"_","",""));
  185. if (byUnderscore.size() < 3)
  186. return false;
  187. vMajor = Utils::strToUInt(byUnderscore[0].c_str());
  188. vMinor = Utils::strToUInt(byUnderscore[1].c_str());
  189. revision = Utils::strToUInt(byUnderscore[2].c_str());
  190. return true;
  191. }
  192. } // namespace ZeroTier