installer.cpp 5.7 KB

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