installer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. /*
  28. * This can be run to install ZT1 for the first time or to update it. It
  29. * carries all payloads internally as LZ4 compressed blobs.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <stdint.h>
  35. #include "node/Constants.hpp"
  36. #include "version.h"
  37. #ifdef __WINDOWS__
  38. #include <Windows.h>
  39. #include <tchar.h>
  40. #include <wchar.h>
  41. #else
  42. #include <unistd.h>
  43. #include <pwd.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <signal.h>
  47. #endif
  48. #include "ext/lz4/lz4.h"
  49. #include "ext/lz4/lz4hc.h"
  50. // Include Lz4 comrpessed blobs -----------------------------------------------
  51. // zerotier-one binary (or zerotier-one.exe for Windows)
  52. #include "installer-build/zerotier_one.h"
  53. // Unix uninstall script, installed in home for user to remove
  54. #ifdef __UNIX_LIKE__
  55. #include "installer-build/uninstall_sh.h"
  56. #endif
  57. // Linux init.d script
  58. #ifdef __LINUX__
  59. #include "installer-build/linux__init_d__zerotier_one.h"
  60. #endif
  61. // Apple Tap device driver
  62. #ifdef __APPLE__
  63. #include "installer-build/tap_mac__Info_plist.h"
  64. #include "installer-build/tap_mac__tap.h"
  65. #endif
  66. // Windows Tap device drivers for x86 and x64 (installer will be x86)
  67. #ifdef __WINDOWS__
  68. #include "installer-build/tap_windows__x64__ztTap100_sys.h"
  69. #include "installer-build/tap_windows__x64__ztTap100_inf.h"
  70. #include "installer-build/tap_windows__x86__ztTap100_sys.h"
  71. #include "installer-build/tap_windows__x86__ztTap100_inf.h"
  72. #include "installer-build/tap_windows__devcon32_exe.h"
  73. #include "installer-build/tap_windows__devcon64_exe.h"
  74. #endif
  75. // ----------------------------------------------------------------------------
  76. static unsigned char *_unlz4(const void *lz4,int decompressedLen)
  77. {
  78. unsigned char *buf = new unsigned char[decompressedLen];
  79. if (LZ4_decompress_fast((const char *)lz4,(char *)buf,decompressedLen) <= 0) {
  80. delete [] buf;
  81. return (unsigned char *)0;
  82. }
  83. return buf;
  84. }
  85. static bool _putBlob(const void *lz4,int decompressedLen,const char *path)
  86. {
  87. unsigned char *data = _unlz4(lz4,decompressedLen);
  88. if (!data)
  89. return false;
  90. #ifdef __WINDOWS__
  91. DeleteFileA(path);
  92. #else
  93. unlink(path);
  94. #endif
  95. FILE *f = fopen(path,"w");
  96. if (!f) {
  97. delete [] data;
  98. return false;
  99. }
  100. if (fwrite(data,decompressedLen,1,f) != 1) {
  101. fclose(f);
  102. delete [] data;
  103. #ifdef __WINDOWS__
  104. DeleteFileA(path);
  105. #else
  106. unlink(path);
  107. #endif
  108. return false;
  109. }
  110. fclose(f);
  111. delete [] data;
  112. return true;
  113. }
  114. #define putBlob(name,path) _putBlob(name,name##_UNCOMPRESSED_LEN,path)
  115. // ----------------------------------------------------------------------------
  116. #ifdef __WINDOWS__
  117. int _tmain(int argc, _TCHAR* argv[])
  118. #else
  119. int main(int argc,char **argv)
  120. #endif
  121. {
  122. #ifdef __UNIX_LIKE__ // -------------------------------------------------------
  123. char buf[4096];
  124. if (getuid() != 0) {
  125. printf("! ZeroTier One installer must be run as root.\n");
  126. return 2;
  127. }
  128. printf("# ZeroTier One installer/updater starting...\n");
  129. const char *zthome;
  130. #ifdef __APPLE__
  131. mkdir("/Library/Application Support/ZeroTier",0755);
  132. chmod("/Library/Application Support/ZeroTier",0755);
  133. chown("/Library/Application Support/ZeroTier",0,0);
  134. printf("mkdir /Library/Application Support/ZeroTier\n");
  135. mkdir(zthome = "/Library/Application Support/ZeroTier/One",0755);
  136. #else
  137. mkdir("/var/lib",0755);
  138. printf("mkdir /var/lib\n");
  139. mkdir(zthome = "/var/lib/zerotier-one",0755);
  140. #endif
  141. chmod(zthome,0755);
  142. chown(zthome,0,0);
  143. printf("mkdir %s\n",zthome);
  144. sprintf(buf,"%s/zerotier-one",zthome);
  145. if (!putBlob(zerotier_one,buf)) {
  146. printf("! unable to write %s\n",buf);
  147. return 1;
  148. }
  149. chmod(buf,0755);
  150. chown(buf,0,0);
  151. printf("write %s\n",buf);
  152. unlink("/usr/bin/zerotier-cli");
  153. symlink(buf,"/usr/bin/zerotier-cli");
  154. printf("link %s /usr/bin/zerotier-cli\n",buf);
  155. sprintf(buf,"%s/uninstall.sh",zthome);
  156. if (!putBlob(uninstall_sh,buf)) {
  157. printf("! unable to write %s\n",buf);
  158. return 1;
  159. }
  160. chmod(buf,0755);
  161. chown(buf,0,0);
  162. printf("write %s\n",buf);
  163. #ifdef __APPLE__
  164. sprintf(buf,"%s/tap.kext");
  165. mkdir(buf,0755);
  166. chmod(buf,0755);
  167. chown(buf,0,0);
  168. printf("mkdir %s\n",buf);
  169. sprintf(buf,"%s/tap.kext/Contents");
  170. mkdir(buf,0755);
  171. chmod(buf,0755);
  172. chown(buf,0,0);
  173. printf("mkdir %s\n",buf);
  174. sprintf(buf,"%s/tap.kext/Contents/MacOS");
  175. mkdir(buf,0755);
  176. chmod(buf,0755);
  177. chown(buf,0,0);
  178. printf("mkdir %s\n",buf);
  179. sprintf(buf,"%s/tap.kext/Contents/Info.plist",zthome);
  180. if (!putBlob(tap_mac__Info_plist,buf)) {
  181. printf("! unable to write %s\n",buf);
  182. return 1;
  183. }
  184. chmod(buf,0644);
  185. chown(buf,0,0);
  186. printf("write %s\n",buf);
  187. sprintf(buf,"%s/tap.kext/Contents/MacOS/tap",zthome);
  188. if (!putBlob(tap_mac__tap,buf)) {
  189. printf("! unable to write %s\n",buf);
  190. return 1;
  191. }
  192. chmod(buf,0755);
  193. chown(buf,0,0);
  194. printf("write %s\n",buf);
  195. #endif
  196. #ifdef __LINUX__
  197. sprintf(buf,"/etc/init.d/zerotier-one");
  198. if (!putBlob(linux__init_d__zerotier_one,buf)) {
  199. printf("! unable to write %s\n",buf);
  200. return 1;
  201. }
  202. chown(buf,0,0);
  203. chmod(buf,0755);
  204. printf("write %s\n",buf);
  205. symlink("/etc/init.d/zerotier-one","/etc/rc0.d/K89zerotier-one");
  206. printf("link /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one\n");
  207. symlink("/etc/init.d/zerotier-one","/etc/rc2.d/S11zerotier-one");
  208. printf("link /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one\n");
  209. symlink("/etc/init.d/zerotier-one","/etc/rc3.d/S11zerotier-one");
  210. printf("link /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one\n");
  211. symlink("/etc/init.d/zerotier-one","/etc/rc4.d/S11zerotier-one");
  212. printf("link /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one\n");
  213. symlink("/etc/init.d/zerotier-one","/etc/rc5.d/S11zerotier-one");
  214. printf("link /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one\n");
  215. symlink("/etc/init.d/zerotier-one","/etc/rc6.d/S11zerotier-one");
  216. printf("link /etc/init.d/zerotier-one /etc/rc6.d/S11zerotier-one\n");
  217. #endif
  218. printf("# Done!\n");
  219. #endif // __UNIX_LIKE__ -------------------------------------------------------
  220. #ifdef __WINDOWS__ // ---------------------------------------------------------
  221. #endif // __WINDOWS__ ---------------------------------------------------------
  222. return 0;
  223. }