SoftwareUpdater.hpp 5.2 KB

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