updater.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <time.h>
  32. #include <string>
  33. #include <vector>
  34. #include <map>
  35. #include <algorithm>
  36. #include <stdexcept>
  37. #include "version.h"
  38. #include "include/ZeroTierOne.h"
  39. #include "node/Constants.hpp"
  40. #ifdef __WINDOWS__
  41. #include <WinSock2.h>
  42. #include <Windows.h>
  43. #include <tchar.h>
  44. #include <wchar.h>
  45. #include <lmcons.h>
  46. #include <newdev.h>
  47. #include <atlbase.h>
  48. #else
  49. #include <unistd.h>
  50. #include <pwd.h>
  51. #include <fcntl.h>
  52. #include <sys/types.h>
  53. #include <sys/stat.h>
  54. #include <signal.h>
  55. #endif
  56. #include "node/Utils.hpp"
  57. #include "node/Address.hpp"
  58. #include "node/Dictionary.hpp"
  59. #include "node/Identity.hpp"
  60. #include "osdep/OSUtils.hpp"
  61. #include "osdep/Http.hpp"
  62. using namespace ZeroTier;
  63. namespace {
  64. static std::map< Address,Identity > updateAuthorities()
  65. {
  66. std::map< Address,Identity > ua;
  67. { // 0001
  68. Identity id("e9bc3707b5:0:c4cef17bde99eadf9748c4fd11b9b06dc5cd8eb429227811d2c336e6b96a8d329e8abd0a4f45e47fe1bcebf878c004c822d952ff77fc2833af4c74e65985c435");
  69. ua[id.address()] = id;
  70. }
  71. { // 0002
  72. Identity id("56520eaf93:0:7d858b47988b34399a9a31136de07b46104d7edb4a98fa1d6da3e583d3a33e48be531532b886f0b12cd16794a66ab9220749ec5112cbe96296b18fe0cc79ca05");
  73. ua[id.address()] = id;
  74. }
  75. { // 0003
  76. Identity id("7c195de2e0:0:9f659071c960f9b0f0b96f9f9ecdaa27c7295feed9c79b7db6eedcc11feb705e6dd85c70fa21655204d24c897865b99eb946b753a2bbcf2be5f5e006ae618c54");
  77. ua[id.address()] = id;
  78. }
  79. { // 0004
  80. Identity id("415f4cfde7:0:54118e87777b0ea5d922c10b337c4f4bd1db7141845bd54004b3255551a6e356ba6b9e1e85357dbfafc45630b8faa2ebf992f31479e9005f0472685f2d8cbd6e");
  81. ua[id.address()] = id;
  82. }
  83. return ua;
  84. }
  85. static bool validateUpdate(
  86. const void *data,
  87. unsigned int len,
  88. const Address &signedBy,
  89. const std::string &signature)
  90. {
  91. std::map< Address,Identity > ua(updateAuthorities());
  92. std::map< Address,Identity >::const_iterator updateAuthority = ua.find(signedBy);
  93. if (updateAuthority == ua.end())
  94. return false;
  95. return updateAuthority->second.verify(data,len,signature.data(),(unsigned int)signature.length());
  96. }
  97. /*
  98. static inline const char *updateUrl()
  99. {
  100. #if defined(__LINUX__) && ( defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(__i386) )
  101. if (sizeof(void *) == 8)
  102. return "http://download.zerotier.com/ZeroTierOneInstaller-linux-x64-LATEST.nfo";
  103. else return "http://download.zerotier.com/ZeroTierOneInstaller-linux-x86-LATEST.nfo";
  104. #define GOT_UPDATE_URL
  105. #endif
  106. #ifdef __APPLE__
  107. return "http://download.zerotier.com/ZeroTierOneInstaller-mac-combined-LATEST.nfo";
  108. #define GOT_UPDATE_URL
  109. #endif
  110. #ifdef __WINDOWS__
  111. return "http://download.zerotier.com/ZeroTierOneInstaller-windows-intel-LATEST.nfo";
  112. #define GOT_UPDATE_URL
  113. #endif
  114. #ifndef GOT_UPDATE_URL
  115. return "";
  116. #endif
  117. }
  118. */
  119. static const char *parseUpdateNfo(
  120. const char *nfoText,
  121. unsigned int &vMajor,
  122. unsigned int &vMinor,
  123. unsigned int &vRevision,
  124. Address &signedBy,
  125. std::string &signature,
  126. std::string &url)
  127. {
  128. try {
  129. Dictionary nfo(nfoText);
  130. vMajor = Utils::strToUInt(nfo.get("vMajor").c_str());
  131. vMinor = Utils::strToUInt(nfo.get("vMinor").c_str());
  132. vRevision = Utils::strToUInt(nfo.get("vRevision").c_str());
  133. signedBy = nfo.get("signedBy");
  134. signature = Utils::unhex(nfo.get("ed25519"));
  135. url = nfo.get("url");
  136. if (signature.length() != ZT_C25519_SIGNATURE_LEN)
  137. return "bad ed25519 signature, invalid length";
  138. if ((url.length() <= 7)||(url.substr(0,7) != "http://"))
  139. return "invalid URL, must begin with http://";
  140. return (const char *)0;
  141. } catch ( ... ) {
  142. return "invalid NFO file format or one or more required fields missing";
  143. }
  144. }
  145. } // anonymous namespace
  146. #ifdef __WINDOWS__
  147. int _tmain(int argc, _TCHAR* argv[])
  148. #else
  149. int main(int argc,char **argv)
  150. #endif
  151. {
  152. #ifdef __WINDOWS__
  153. WSADATA wsaData;
  154. WSAStartup(MAKEWORD(2,2),&wsaData);
  155. #endif
  156. return 0;
  157. }