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 "../include/ZeroTierOne.h"
  16. #include "../node/Identity.hpp"
  17. #include "../node/Packet.hpp"
  18. #include <array>
  19. #include <map>
  20. #include <nlohmann/json.hpp>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <string>
  24. #include <vector>
  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. public:
  81. /**
  82. * Each message begins with an 8-bit message verb
  83. */
  84. enum MessageVerb {
  85. /**
  86. * Payload: JSON containing current system platform, version, etc.
  87. */
  88. VERB_GET_LATEST = 1,
  89. /**
  90. * Payload: JSON describing latest update for this target. (No response is sent if there is none.)
  91. */
  92. VERB_LATEST = 2,
  93. /**
  94. * Payload:
  95. * <[16] first 128 bits of hash of data object>
  96. * <[4] 32-bit index of chunk to get>
  97. */
  98. VERB_GET_DATA = 3,
  99. /**
  100. * Payload:
  101. * <[16] first 128 bits of hash of data object>
  102. * <[4] 32-bit index of chunk>
  103. * <[...] chunk data>
  104. */
  105. VERB_DATA = 4
  106. };
  107. SoftwareUpdater(Node& node, const std::string& homePath);
  108. ~SoftwareUpdater();
  109. /**
  110. * Set whether or not we will distribute updates
  111. *
  112. * @param distribute If true, scan update-dist.d now and distribute updates found there -- if false, clear and stop distributing
  113. */
  114. void setUpdateDistribution(bool distribute);
  115. /**
  116. * Handle a software update user message
  117. *
  118. * @param origin ZeroTier address of message origin
  119. * @param data Message payload
  120. * @param len Length of message
  121. */
  122. void handleSoftwareUpdateUserMessage(uint64_t origin, const void* data, unsigned int len);
  123. /**
  124. * Check for updates and do other update-related housekeeping
  125. *
  126. * It should be called about every 10 seconds.
  127. *
  128. * @return True if we've downloaded and verified an update
  129. */
  130. bool check(const int64_t now);
  131. /**
  132. * @return Meta-data for downloaded update or NULL if none
  133. */
  134. inline const nlohmann::json& pending() const
  135. {
  136. return _latestMeta;
  137. }
  138. /**
  139. * Apply any ready update now
  140. *
  141. * Depending on the platform this function may never return and may forcibly
  142. * exit the process. It does nothing if no update is ready.
  143. */
  144. void apply();
  145. /**
  146. * Set software update channel
  147. *
  148. * @param channel 'release', 'beta', etc.
  149. */
  150. inline void setChannel(const std::string& channel)
  151. {
  152. _channel = channel;
  153. }
  154. private:
  155. Node& _node;
  156. uint64_t _lastCheckTime;
  157. std::string _homePath;
  158. std::string _channel;
  159. FILE* _distLog;
  160. // Offered software updates if we are an update host (we have update-dist.d and update hosting is enabled)
  161. struct _D {
  162. nlohmann::json meta;
  163. std::string bin;
  164. };
  165. std::map<std::array<uint8_t, 16>, _D> _dist; // key is first 16 bytes of hash
  166. nlohmann::json _latestMeta;
  167. bool _latestValid;
  168. std::string _download;
  169. std::array<uint8_t, 16> _downloadHashPrefix;
  170. unsigned long _downloadLength;
  171. };
  172. } // namespace ZeroTier
  173. #endif