SoftwareUpdater.cpp 6.4 KB

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