SoftwareUpdater.hpp 5.6 KB

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