SoftwareUpdater.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 * 60 * 1000)
  56. #define ZT_SOFTWARE_UPDATE_CHECK_PERIOD 5000
  57. /**
  58. * Default update channel
  59. */
  60. #define ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL "release"
  61. /**
  62. * Filename for latest update's meta JSON
  63. */
  64. #define ZT_SOFTWARE_UPDATE_META_FILENAME "latest-update.json"
  65. /**
  66. * Filename for latest update's binary image
  67. */
  68. #define ZT_SOFTWARE_UPDATE_BIN_FILENAME "latest-update.exe"
  69. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR "vMajor"
  70. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR "vMinor"
  71. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION "vRev"
  72. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD "vBuild"
  73. #define ZT_SOFTWARE_UPDATE_JSON_PLATFORM "platform"
  74. #define ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE "arch"
  75. #define ZT_SOFTWARE_UPDATE_JSON_VENDOR "vendor"
  76. #define ZT_SOFTWARE_UPDATE_JSON_CHANNEL "channel"
  77. #define ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY "expectedSigner"
  78. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY "signer"
  79. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNATURE "signature"
  80. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH "hash"
  81. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE "size"
  82. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_EXEC_ARGS "execArgs"
  83. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_URL "url"
  84. namespace ZeroTier {
  85. class Node;
  86. /**
  87. * This class handles retrieving and executing updates, or serving them
  88. */
  89. class SoftwareUpdater
  90. {
  91. public:
  92. /**
  93. * Each message begins with an 8-bit message verb
  94. */
  95. enum MessageVerb
  96. {
  97. /**
  98. * Payload: JSON containing current system platform, version, etc.
  99. */
  100. VERB_GET_LATEST = 1,
  101. /**
  102. * Payload: JSON describing latest update for this target. (No response is sent if there is none.)
  103. */
  104. VERB_LATEST = 2,
  105. /**
  106. * Payload:
  107. * <[16] first 128 bits of hash of data object>
  108. * <[4] 32-bit index of chunk to get>
  109. */
  110. VERB_GET_DATA = 3,
  111. /**
  112. * Payload:
  113. * <[16] first 128 bits of hash of data object>
  114. * <[4] 32-bit index of chunk>
  115. * <[...] chunk data>
  116. */
  117. VERB_DATA = 4
  118. };
  119. SoftwareUpdater(Node &node,const std::string &homePath);
  120. ~SoftwareUpdater();
  121. /**
  122. * Set whether or not we will distribute updates
  123. *
  124. * @param distribute If true, scan update-dist.d now and distribute updates found there -- if false, clear and stop distributing
  125. */
  126. void setUpdateDistribution(bool distribute);
  127. /**
  128. * Handle a software update user message
  129. *
  130. * @param origin ZeroTier address of message origin
  131. * @param data Message payload
  132. * @param len Length of message
  133. */
  134. void handleSoftwareUpdateUserMessage(uint64_t origin,const void *data,unsigned int len);
  135. /**
  136. * Check for updates and do other update-related housekeeping
  137. *
  138. * It should be called about every 10 seconds.
  139. *
  140. * @return True if we've downloaded and verified an update
  141. */
  142. bool check(const uint64_t now);
  143. /**
  144. * @return Meta-data for downloaded update or NULL if none
  145. */
  146. inline const nlohmann::json &pending() const { return _latestMeta; }
  147. /**
  148. * Apply any ready update now
  149. *
  150. * Depending on the platform this function may never return and may forcibly
  151. * exit the process. It does nothing if no update is ready.
  152. */
  153. void apply();
  154. /**
  155. * Set software update channel
  156. *
  157. * @param channel 'release', 'beta', etc.
  158. */
  159. inline void setChannel(const std::string &channel) { _channel = channel; }
  160. private:
  161. Node &_node;
  162. uint64_t _lastCheckTime;
  163. std::string _homePath;
  164. std::string _channel;
  165. FILE *_distLog;
  166. // Offered software updates if we are an update host (we have update-dist.d and update hosting is enabled)
  167. struct _D
  168. {
  169. nlohmann::json meta;
  170. std::string bin;
  171. };
  172. std::map< Array<uint8_t,16>,_D > _dist; // key is first 16 bytes of hash
  173. nlohmann::json _latestMeta;
  174. bool _latestValid;
  175. std::string _download;
  176. Array<uint8_t,16> _downloadHashPrefix;
  177. unsigned long _downloadLength;
  178. };
  179. } // namespace ZeroTier
  180. #endif