SoftwareUpdater.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "../version.h"
  31. #include "SoftwareUpdater.hpp"
  32. #include "Dictionary.hpp"
  33. #include "C25519.hpp"
  34. #include "Identity.hpp"
  35. #include "Logger.hpp"
  36. #include "RuntimeEnvironment.hpp"
  37. #include "Thread.hpp"
  38. #include "Node.hpp"
  39. #ifdef __UNIX_LIKE__
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #endif
  45. namespace ZeroTier {
  46. SoftwareUpdater::SoftwareUpdater(const RuntimeEnvironment *renv) :
  47. _r(renv),
  48. _myVersion(packVersion(ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION)),
  49. _lastUpdateAttempt(0),
  50. _status(UPDATE_STATUS_IDLE),
  51. _die(false),
  52. _lock()
  53. {
  54. }
  55. SoftwareUpdater::~SoftwareUpdater()
  56. {
  57. _die = true;
  58. for(;;) {
  59. _lock.lock();
  60. bool ip = (_status != UPDATE_STATUS_IDLE);
  61. _lock.unlock();
  62. if (ip)
  63. Thread::sleep(500);
  64. else break;
  65. }
  66. }
  67. void SoftwareUpdater::_cbHandleGetLatestVersionInfo(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
  68. {
  69. SoftwareUpdater *upd = (SoftwareUpdater *)arg;
  70. const RuntimeEnvironment *_r = (const RuntimeEnvironment *)upd->_r;
  71. Mutex::Lock _l(upd->_lock);
  72. if ((upd->_die)||(upd->_status != UPDATE_STATUS_GETTING_NFO)) {
  73. upd->_status = UPDATE_STATUS_IDLE;
  74. return;
  75. }
  76. if (code != 200) {
  77. LOG("unable to check for software updates, response code %d (%s)",code,body.c_str());
  78. upd->_status = UPDATE_STATUS_IDLE;
  79. return;
  80. }
  81. try {
  82. Dictionary nfo(body);
  83. const unsigned int vMajor = Utils::strToUInt(nfo.get("vMajor").c_str());
  84. const unsigned int vMinor = Utils::strToUInt(nfo.get("vMinor").c_str());
  85. const unsigned int vRevision = Utils::strToUInt(nfo.get("vRevision").c_str());
  86. const Address signedBy(nfo.get("signedBy"));
  87. const std::string signature(Utils::unhex(nfo.get("ed25519")));
  88. const std::string &url = nfo.get("url");
  89. if (signature.length() != ZT_C25519_SIGNATURE_LEN) {
  90. LOG("software update aborted: .nfo file invalid: bad Ed25519 signature");
  91. upd->_status = UPDATE_STATUS_IDLE;
  92. return;
  93. }
  94. if ((url.length() <= 7)||(url.substr(0,7) != "http://")) {
  95. LOG("software update aborted: .nfo file invalid: update URL must begin with http://");
  96. upd->_status = UPDATE_STATUS_IDLE;
  97. return;
  98. }
  99. if (packVersion(vMajor,vMinor,vRevision) <= upd->_myVersion) {
  100. LOG("software update aborted: .nfo file invalid: version on web site <= my version");
  101. upd->_status = UPDATE_STATUS_IDLE;
  102. return;
  103. }
  104. if (!ZT_DEFAULTS.updateAuthorities.count(signedBy)) {
  105. LOG("software update aborted: .nfo file specifies unknown signing authority");
  106. upd->_status = UPDATE_STATUS_IDLE;
  107. return;
  108. }
  109. upd->_status = UPDATE_STATUS_GETTING_FILE;
  110. upd->_signedBy = signedBy;
  111. upd->_signature = signature;
  112. HttpClient::GET(url,HttpClient::NO_HEADERS,ZT_UPDATE_HTTP_TIMEOUT,&_cbHandleGetLatestVersionBinary,arg);
  113. } catch ( ... ) {
  114. LOG("software update check failed: .nfo file invalid: fields missing or invalid dictionary format");
  115. upd->_status = UPDATE_STATUS_IDLE;
  116. }
  117. }
  118. void SoftwareUpdater::_cbHandleGetLatestVersionBinary(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
  119. {
  120. SoftwareUpdater *upd = (SoftwareUpdater *)arg;
  121. const RuntimeEnvironment *_r = (const RuntimeEnvironment *)upd->_r;
  122. Mutex::Lock _l(upd->_lock);
  123. std::map< Address,Identity >::const_iterator updateAuthority = ZT_DEFAULTS.updateAuthorities.find(upd->_signedBy);
  124. if (updateAuthority == ZT_DEFAULTS.updateAuthorities.end()) { // sanity check, shouldn't happen
  125. LOG("software update aborted: .nfo file specifies unknown signing authority");
  126. upd->_status = UPDATE_STATUS_IDLE;
  127. return;
  128. }
  129. // The all-important authenticity check... :)
  130. if (!updateAuthority->second.verify(body.data(),body.length(),upd->_signature.data(),upd->_signature.length())) {
  131. LOG("software update aborted: update fetched from '%s' failed certificate check against signer %s",url.c_str(),updateAuthority->first.toString().c_str());
  132. upd->_status = UPDATE_STATUS_IDLE;
  133. return;
  134. }
  135. #ifdef __UNIX_LIKE__
  136. size_t lastSlash = url.rfind('/');
  137. if (lastSlash == std::string::npos) { // sanity check, shouldn't happen
  138. LOG("software update aborted: invalid URL");
  139. upd->_status = UPDATE_STATUS_IDLE;
  140. return;
  141. }
  142. std::string updatesDir(_r->homePath + ZT_PATH_SEPARATOR_S + "updates.d");
  143. std::string updatePath(updatesDir + ZT_PATH_SEPARATOR_S + url.substr(lastSlash + 1));
  144. mkdir(updatesDir.c_str(),0755);
  145. int fd = ::open(updatePath.c_str(),O_WRONLY|O_CREAT|O_TRUNC,0755);
  146. if (fd <= 0) {
  147. LOG("software update aborted: unable to open %s for writing",updatePath.c_str());
  148. upd->_status = UPDATE_STATUS_IDLE;
  149. return;
  150. }
  151. if ((long)::write(fd,body.data(),body.length()) != (long)body.length()) {
  152. LOG("software update aborted: unable to write to %s",updatePath.c_str());
  153. upd->_status = UPDATE_STATUS_IDLE;
  154. return;
  155. }
  156. ::close(fd);
  157. ::chmod(updatePath.c_str(),0755);
  158. _r->node->terminate(Node::NODE_RESTART_FOR_UPGRADE,updatePath.c_str());
  159. #endif
  160. #ifdef __WINDOWS__
  161. todo;
  162. #endif
  163. }
  164. } // namespace ZeroTier