installer.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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) != decompressedLen) {
  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. sprintf(buf,"%s/uninstall.sh",zthome);
  153. if (!putBlob(uninstall_sh,buf)) {
  154. printf("! unable to write %s\n",buf);
  155. return 1;
  156. }
  157. chmod(buf,0755);
  158. chown(buf,0,0);
  159. printf("write %s\n",buf);
  160. #ifdef __APPLE__
  161. sprintf(buf,"%s/tap.kext");
  162. mkdir(buf,0755);
  163. chmod(buf,0755);
  164. chown(buf,0,0);
  165. printf("mkdir %s\n",buf);
  166. sprintf(buf,"%s/tap.kext/Contents");
  167. mkdir(buf,0755);
  168. chmod(buf,0755);
  169. chown(buf,0,0);
  170. printf("mkdir %s\n",buf);
  171. sprintf(buf,"%s/tap.kext/Contents/MacOS");
  172. mkdir(buf,0755);
  173. chmod(buf,0755);
  174. chown(buf,0,0);
  175. printf("mkdir %s\n",buf);
  176. sprintf(buf,"%s/tap.kext/Contents/Info.plist",zthome);
  177. if (!putBlob(tap_mac__Info_plist,buf)) {
  178. printf("! unable to write %s\n",buf);
  179. return 1;
  180. }
  181. chmod(buf,0644);
  182. chown(buf,0,0);
  183. printf("write %s\n",buf);
  184. sprintf(buf,"%s/tap.kext/Contents/MacOS/tap",zthome);
  185. if (!putBlob(tap_mac__tap,buf)) {
  186. printf("! unable to write %s\n",buf);
  187. return 1;
  188. }
  189. chmod(buf,0755);
  190. chown(buf,0,0);
  191. printf("write %s\n",buf);
  192. #endif
  193. #ifdef __LINUX__
  194. sprintf(buf,"/etc/init.d/zerotier-one");
  195. if (!putBlob(linux__init_d__zerotier_one,buf)) {
  196. printf("! unable to write %s\n",buf);
  197. return 1;
  198. }
  199. chown(buf,0,0);
  200. chmod(buf,0755);
  201. printf("write %s\n",buf);
  202. symlink("/etc/init.d/zerotier-one","/etc/rc0.d/K89zerotier-one");
  203. printf("link /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one\n");
  204. symlink("/etc/init.d/zerotier-one","/etc/rc2.d/S11zerotier-one");
  205. printf("link /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one\n");
  206. symlink("/etc/init.d/zerotier-one","/etc/rc3.d/S11zerotier-one");
  207. printf("link /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one\n");
  208. symlink("/etc/init.d/zerotier-one","/etc/rc4.d/S11zerotier-one");
  209. printf("link /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one\n");
  210. symlink("/etc/init.d/zerotier-one","/etc/rc5.d/S11zerotier-one");
  211. printf("link /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one\n");
  212. #endif
  213. printf("# Done!\n");
  214. #endif // __UNIX_LIKE__ -------------------------------------------------------
  215. #ifdef __WINDOWS__ // ---------------------------------------------------------
  216. #endif // __WINDOWS__ ---------------------------------------------------------
  217. return 0;
  218. }