SoftwareUpdater.hpp 5.1 KB

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