SoftwareUpdater.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_SOFTWAREUPDATER_HPP
  27. #define ZT_SOFTWAREUPDATER_HPP
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <vector>
  31. #include <map>
  32. #include <string>
  33. #include <array>
  34. #include "../include/ZeroTierOne.h"
  35. #include "../node/Identity.hpp"
  36. #include "../node/Packet.hpp"
  37. #include "../ext/json/json.hpp"
  38. /**
  39. * VERB_USER_MESSAGE type ID for software update messages
  40. */
  41. #define ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE 100
  42. /**
  43. * ZeroTier address of node that provides software updates
  44. */
  45. #define ZT_SOFTWARE_UPDATE_SERVICE 0xb1d366e81fULL
  46. /**
  47. * ZeroTier identity that must be used to sign software updates
  48. *
  49. * df24360f3e - update-signing-key-0010 generated Fri Jan 13th, 2017 at 4:05pm PST
  50. */
  51. #define ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY "df24360f3e:0:06072642959c8dfb68312904d74d90197c8a7692697caa1b3fd769eca714f4370fab462fcee6ebcb5fffb63bc5af81f28a2514b2cd68daabb42f7352c06f21db"
  52. /**
  53. * Chunk size for in-band downloads (can be changed, designed to always fit in one UDP packet easily)
  54. */
  55. #define ZT_SOFTWARE_UPDATE_CHUNK_SIZE (ZT_PROTO_MAX_PACKET_LENGTH - 128)
  56. /**
  57. * Sanity limit for the size of an update binary image
  58. */
  59. #define ZT_SOFTWARE_UPDATE_MAX_SIZE (1024 * 1024 * 256)
  60. /**
  61. * How often (ms) do we check?
  62. */
  63. #define ZT_SOFTWARE_UPDATE_CHECK_PERIOD (60 * 10 * 1000)
  64. /**
  65. * Default update channel
  66. */
  67. #define ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL "release"
  68. /**
  69. * Filename for latest update's binary image
  70. */
  71. #define ZT_SOFTWARE_UPDATE_BIN_FILENAME "latest-update.exe"
  72. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR "vMajor"
  73. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR "vMinor"
  74. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION "vRev"
  75. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD "vBuild"
  76. #define ZT_SOFTWARE_UPDATE_JSON_PLATFORM "platform"
  77. #define ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE "arch"
  78. #define ZT_SOFTWARE_UPDATE_JSON_VENDOR "vendor"
  79. #define ZT_SOFTWARE_UPDATE_JSON_CHANNEL "channel"
  80. #define ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY "expectedSigner"
  81. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY "signer"
  82. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNATURE "signature"
  83. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH "hash"
  84. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE "size"
  85. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_EXEC_ARGS "execArgs"
  86. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_URL "url"
  87. namespace ZeroTier {
  88. class Node;
  89. /**
  90. * This class handles retrieving and executing updates, or serving them
  91. */
  92. class SoftwareUpdater
  93. {
  94. public:
  95. /**
  96. * Each message begins with an 8-bit message verb
  97. */
  98. enum MessageVerb
  99. {
  100. /**
  101. * Payload: JSON containing current system platform, version, etc.
  102. */
  103. VERB_GET_LATEST = 1,
  104. /**
  105. * Payload: JSON describing latest update for this target. (No response is sent if there is none.)
  106. */
  107. VERB_LATEST = 2,
  108. /**
  109. * Payload:
  110. * <[16] first 128 bits of hash of data object>
  111. * <[4] 32-bit index of chunk to get>
  112. */
  113. VERB_GET_DATA = 3,
  114. /**
  115. * Payload:
  116. * <[16] first 128 bits of hash of data object>
  117. * <[4] 32-bit index of chunk>
  118. * <[...] chunk data>
  119. */
  120. VERB_DATA = 4
  121. };
  122. SoftwareUpdater(Node &node,const std::string &homePath);
  123. ~SoftwareUpdater();
  124. /**
  125. * Set whether or not we will distribute updates
  126. *
  127. * @param distribute If true, scan update-dist.d now and distribute updates found there -- if false, clear and stop distributing
  128. */
  129. void setUpdateDistribution(bool distribute);
  130. /**
  131. * Handle a software update user message
  132. *
  133. * @param origin ZeroTier address of message origin
  134. * @param data Message payload
  135. * @param len Length of message
  136. */
  137. void handleSoftwareUpdateUserMessage(uint64_t origin,const void *data,unsigned int len);
  138. /**
  139. * Check for updates and do other update-related housekeeping
  140. *
  141. * It should be called about every 10 seconds.
  142. *
  143. * @return True if we've downloaded and verified an update
  144. */
  145. bool check(const int64_t now);
  146. /**
  147. * @return Meta-data for downloaded update or NULL if none
  148. */
  149. inline const nlohmann::json &pending() const { return _latestMeta; }
  150. /**
  151. * Apply any ready update now
  152. *
  153. * Depending on the platform this function may never return and may forcibly
  154. * exit the process. It does nothing if no update is ready.
  155. */
  156. void apply();
  157. /**
  158. * Set software update channel
  159. *
  160. * @param channel 'release', 'beta', etc.
  161. */
  162. inline void setChannel(const std::string &channel) { _channel = channel; }
  163. private:
  164. Node &_node;
  165. uint64_t _lastCheckTime;
  166. std::string _homePath;
  167. std::string _channel;
  168. FILE *_distLog;
  169. // Offered software updates if we are an update host (we have update-dist.d and update hosting is enabled)
  170. struct _D
  171. {
  172. nlohmann::json meta;
  173. std::string bin;
  174. };
  175. std::map< std::array<uint8_t,16>,_D > _dist; // key is first 16 bytes of hash
  176. nlohmann::json _latestMeta;
  177. bool _latestValid;
  178. std::string _download;
  179. std::array<uint8_t,16> _downloadHashPrefix;
  180. unsigned long _downloadLength;
  181. };
  182. } // namespace ZeroTier
  183. #endif