2
0

SoftwareUpdater.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdexcept>
  31. #include "../version.h"
  32. #include "SoftwareUpdater.hpp"
  33. #include "Dictionary.hpp"
  34. #include "C25519.hpp"
  35. #include "Identity.hpp"
  36. #include "Logger.hpp"
  37. #include "RuntimeEnvironment.hpp"
  38. #include "Thread.hpp"
  39. #include "Node.hpp"
  40. #include "Utils.hpp"
  41. #ifdef __UNIX_LIKE__
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #endif
  47. namespace ZeroTier {
  48. SoftwareUpdater::SoftwareUpdater(const RuntimeEnvironment *renv) :
  49. _r(renv),
  50. _myVersion(packVersion(ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION)),
  51. _lastUpdateAttempt(0),
  52. _status(UPDATE_STATUS_IDLE),
  53. _die(false),
  54. _lock()
  55. {
  56. }
  57. SoftwareUpdater::~SoftwareUpdater()
  58. {
  59. _die = true;
  60. for(;;) {
  61. _lock.lock();
  62. bool ip = (_status != UPDATE_STATUS_IDLE);
  63. _lock.unlock();
  64. if (ip)
  65. Thread::sleep(500);
  66. else break;
  67. }
  68. }
  69. void SoftwareUpdater::cleanOldUpdates()
  70. {
  71. std::string updatesDir(_r->homePath + ZT_PATH_SEPARATOR_S + "updates.d");
  72. std::map<std::string,bool> dl(Utils::listDirectory(updatesDir.c_str()));
  73. for(std::map<std::string,bool>::iterator i(dl.begin());i!=dl.end();++i) {
  74. if (!i->second)
  75. Utils::rm((updatesDir + ZT_PATH_SEPARATOR_S + i->first).c_str());
  76. }
  77. }
  78. const char *SoftwareUpdater::parseNfo(
  79. const char *nfoText,
  80. unsigned int &vMajor,
  81. unsigned int &vMinor,
  82. unsigned int &vRevision,
  83. Address &signedBy,
  84. std::string &signature,
  85. std::string &url)
  86. {
  87. try {
  88. Dictionary nfo(nfoText);
  89. vMajor = Utils::strToUInt(nfo.get("vMajor").c_str());
  90. vMinor = Utils::strToUInt(nfo.get("vMinor").c_str());
  91. vRevision = Utils::strToUInt(nfo.get("vRevision").c_str());
  92. signedBy = nfo.get("signedBy");
  93. signature = Utils::unhex(nfo.get("ed25519"));
  94. url = nfo.get("url");
  95. if (signature.length() != ZT_C25519_SIGNATURE_LEN)
  96. return "bad ed25519 signature, invalid length";
  97. if ((url.length() <= 7)||(url.substr(0,7) != "http://"))
  98. return "invalid URL, must begin with http://";
  99. return (const char *)0;
  100. } catch ( ... ) {
  101. return "invalid NFO file format or one or more required fields missing";
  102. }
  103. }
  104. bool SoftwareUpdater::validateUpdate(
  105. const void *data,
  106. unsigned int len,
  107. const Address &signedBy,
  108. const std::string &signature)
  109. {
  110. std::map< Address,Identity >::const_iterator updateAuthority = ZT_DEFAULTS.updateAuthorities.find(signedBy);
  111. if (updateAuthority == ZT_DEFAULTS.updateAuthorities.end())
  112. return false;
  113. return updateAuthority->second.verify(data,len,signature.data(),(unsigned int)signature.length());
  114. }
  115. void SoftwareUpdater::_cbHandleGetLatestVersionInfo(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
  116. {
  117. SoftwareUpdater *upd = (SoftwareUpdater *)arg;
  118. const RuntimeEnvironment *_r = (const RuntimeEnvironment *)upd->_r;
  119. Mutex::Lock _l(upd->_lock);
  120. if ((upd->_die)||(upd->_status != UPDATE_STATUS_GETTING_NFO)) {
  121. upd->_status = UPDATE_STATUS_IDLE;
  122. return;
  123. }
  124. if (code != 200) {
  125. LOG("software update check failed: server responded with code %d",code);
  126. upd->_status = UPDATE_STATUS_IDLE;
  127. return;
  128. }
  129. try {
  130. unsigned int vMajor = 0,vMinor = 0,vRevision = 0;
  131. Address signedBy;
  132. std::string signature,url;
  133. const char *err = parseNfo(body.c_str(),vMajor,vMinor,vRevision,signedBy,signature,url);
  134. if (err) {
  135. LOG("software update check aborted: .nfo file parse error: %s",err);
  136. upd->_status = UPDATE_STATUS_IDLE;
  137. return;
  138. }
  139. if (!ZT_DEFAULTS.updateAuthorities.count(signedBy)) {
  140. LOG("software update check aborted: .nfo file specifies unknown signing authority");
  141. upd->_status = UPDATE_STATUS_IDLE;
  142. return;
  143. }
  144. #ifndef ZT_ALWAYS_UPDATE /* for testing */
  145. if (packVersion(vMajor,vMinor,vRevision) <= upd->_myVersion) {
  146. TRACE("software update check complete: version on update site is not newer than my version, no update necessary");
  147. upd->_status = UPDATE_STATUS_IDLE;
  148. return;
  149. }
  150. #endif
  151. upd->_status = UPDATE_STATUS_GETTING_FILE;
  152. upd->_signedBy = signedBy;
  153. upd->_signature = signature;
  154. HttpClient::GET(url,HttpClient::NO_HEADERS,ZT_UPDATE_HTTP_TIMEOUT,&_cbHandleGetLatestVersionBinary,arg);
  155. } catch ( ... ) {
  156. LOG("software update check failed: .nfo file invalid or missing field(s)");
  157. upd->_status = UPDATE_STATUS_IDLE;
  158. }
  159. }
  160. void SoftwareUpdater::_cbHandleGetLatestVersionBinary(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
  161. {
  162. SoftwareUpdater *upd = (SoftwareUpdater *)arg;
  163. const RuntimeEnvironment *_r = (const RuntimeEnvironment *)upd->_r;
  164. Mutex::Lock _l(upd->_lock);
  165. if (!validateUpdate(body.data(),(unsigned int)body.length(),upd->_signedBy,upd->_signature)) {
  166. LOG("software update failed: update fetched from '%s' failed signature check (image size: %u)",url.c_str(),(unsigned int)body.length());
  167. upd->_status = UPDATE_STATUS_IDLE;
  168. return;
  169. }
  170. size_t lastSlash = url.rfind('/');
  171. if (lastSlash == std::string::npos) { // sanity check, shouldn't happen
  172. LOG("software update failed: invalid URL");
  173. upd->_status = UPDATE_STATUS_IDLE;
  174. return;
  175. }
  176. std::string updatesDir(_r->homePath + ZT_PATH_SEPARATOR_S + "updates.d");
  177. std::string updateFilename(url.substr(lastSlash + 1));
  178. if ((updateFilename.length() < 3)||(updateFilename.find("..") != std::string::npos)) {
  179. LOG("software update failed: invalid URL: filename contains invalid characters");
  180. upd->_status = UPDATE_STATUS_IDLE;
  181. return;
  182. }
  183. for(std::string::iterator c(updateFilename.begin());c!=updateFilename.end();++c) {
  184. // Only allow a list of whitelisted characters to make up the filename to prevent any
  185. // path shenanigans, esp on Windows where / is not the path separator.
  186. if (!strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.0123456789",*c)) {
  187. LOG("software update failed: invalid URL: filename contains invalid characters");
  188. upd->_status = UPDATE_STATUS_IDLE;
  189. return;
  190. }
  191. }
  192. std::string updatePath(updatesDir + ZT_PATH_SEPARATOR_S + updateFilename);
  193. #ifdef __WINDOWS__
  194. CreateDirectoryA(updatesDir.c_str(),NULL);
  195. #else
  196. mkdir(updatesDir.c_str(),0755);
  197. #endif
  198. FILE *upf = fopen(updatePath.c_str(),"wb");
  199. if (!upf) {
  200. LOG("software update failed: unable to open %s for writing",updatePath.c_str());
  201. upd->_status = UPDATE_STATUS_IDLE;
  202. return;
  203. }
  204. if (fwrite(body.data(),body.length(),1,upf) != 1) {
  205. LOG("software update failed: unable to write to %s",updatePath.c_str());
  206. upd->_status = UPDATE_STATUS_IDLE;
  207. fclose(upf);
  208. Utils::rm(updatePath);
  209. return;
  210. }
  211. fclose(upf);
  212. #ifdef __UNIX_LIKE__
  213. ::chmod(updatePath.c_str(),0755);
  214. #endif
  215. // We exit with this reason code and the path as the text. It is the
  216. // caller's responsibility (main.c) to pick this up and do the right
  217. // thing.
  218. upd->_status = UPDATE_STATUS_IDLE;
  219. _r->node->terminate(Node::NODE_RESTART_FOR_UPGRADE,updatePath.c_str());
  220. }
  221. } // namespace ZeroTier