installer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/redhat__init_d__zerotier_one.c"
  57. #include "installer-build/debian__init_d__zerotier_one.c"
  58. #endif
  59. // Apple Tap device driver
  60. #ifdef __APPLE__
  61. #include "installer-build/tap_mac__Info_plist.c"
  62. #include "installer-build/tap_mac__tap.c"
  63. #endif
  64. // Windows Tap device drivers
  65. #ifdef __WINDOWS__
  66. #include "installer-build/tap_windows__x64__ztTap100_sys.c"
  67. #include "installer-build/tap_windows__x64__ztTap100_inf.c"
  68. #include "installer-build/tap_windows__x86__ztTap100_sys.c"
  69. #include "installer-build/tap_windows__x86__ztTap100_inf.c"
  70. #include "installer-build/tap_windows__devcon32_exe.c"
  71. #include "installer-build/tap_windows__devcon64_exe.c"
  72. #endif
  73. // ----------------------------------------------------------------------------
  74. static unsigned char *unlz4(const void *lz4,int decompressedLen)
  75. {
  76. unsigned char *buf = new unsigned char[decompressedLen];
  77. if (LZ4_decompress_fast((const char *)lz4,(char *)buf,decompressedLen) != decompressedLen) {
  78. delete [] buf;
  79. return (unsigned char *)0;
  80. }
  81. return buf;
  82. }
  83. static bool _instFile(const void *lz4,int decompressedLen,const char *path)
  84. {
  85. unsigned char *data = unlzr(lz4,decompressedLen);
  86. if (!data)
  87. return false;
  88. FILE *f = fopen(path,"w");
  89. if (!f) {
  90. delete [] data;
  91. return false;
  92. }
  93. if (fwrite(data,decompressedLen,1,f) != 1) {
  94. fclose(f);
  95. delete [] data;
  96. Utils::rm(path);
  97. return false;
  98. }
  99. fclose(f);
  100. delete [] data;
  101. return true;
  102. }
  103. #define instFile(name,path) _instFile(name,name##_UNCOMPRESSED_LEN,path)
  104. #ifdef __WINDOWS__
  105. int _tmain(int argc, _TCHAR* argv[])
  106. #else
  107. int main(int argc,char **argv)
  108. #endif
  109. {
  110. char buf[4096];
  111. #ifdef __UNIX_LIKE__
  112. if (getuid() != 0) {
  113. fprintf(stderr,"ZeroTier One installer must be run as root.\n");
  114. return 2;
  115. }
  116. const char *zthome;
  117. #ifdef __APPLE__
  118. mkdir("/Library/Application Support/ZeroTier",0755);
  119. mkdir(zthome = "/Library/Application Support/ZeroTier/One",0755);
  120. #else
  121. mkdir("/var/lib",0755);
  122. mkdir(zthome = "/var/lib/zerotier-one",0755);
  123. #endif
  124. chown(zthome,0,0);
  125. sprintf(buf,"%s/zerotier-one",zthome);
  126. if (!instFile(zerotier_one,buf)) {
  127. fprintf(stderr,"Unable to write %s\n",buf);
  128. return 1;
  129. }
  130. chmod(buf,0700);
  131. chown(buf,0,0);
  132. fprintf(stdout,"%s\n",buf);
  133. sprintf(buf,"%s/uninstall.sh",zthome);
  134. if (!instFile(uninstall_sh,buf)) {
  135. fprintf(stderr,"Unable to write %s\n",buf);
  136. return 1;
  137. }
  138. chmod(buf,0755);
  139. chown(buf,0,0);
  140. fprintf(stdout,"%s\n",buf);
  141. #ifdef __APPLE__
  142. sprintf(buf,"%s/tap.kext");
  143. mkdir(buf,0755);
  144. chmod(buf,0755);
  145. chown(buf,0,0);
  146. sprintf(buf,"%s/tap.kext/Contents");
  147. mkdir(buf,0755);
  148. chmod(buf,0755);
  149. chown(buf,0,0);
  150. sprintf(buf,"%s/tap.kext/Contents/MacOS");
  151. mkdir(buf,0755);
  152. chmod(buf,0755);
  153. chown(buf,0,0);
  154. sprintf(buf,"%s/tap.kext/Contents/Info.plist",zthome);
  155. if (!instFile(tap_mac__Info_plist,buf)) {
  156. fprintf(stderr,"Unable to write %s\n",buf);
  157. return 1;
  158. }
  159. chmod(buf,0644);
  160. chown(buf,0,0);
  161. fprintf(stdout,"%s\n",buf);
  162. sprintf(buf,"%s/tap.kext/Contents/MacOS/tap",zthome);
  163. if (!instFile(tap_mac__tap,buf)) {
  164. fprintf(stderr,"Unable to write %s\n",buf);
  165. return 1;
  166. }
  167. chmod(buf,0755);
  168. chown(buf,0,0);
  169. fprintf(stdout,"%s\n",buf);
  170. #endif
  171. #ifdef __LINUX__
  172. struct stat st;
  173. if (stat("/etc/redhat-release",&st) == 0) {
  174. // Redhat-derived distribution
  175. sprintf(buf,"/etc/init.d/zerotier-one");
  176. if (!instFile(redhat__init_d__zerotier_one,buf)) {
  177. fprintf(stderr,"Unable to write %s\n",buf);
  178. return 1;
  179. }
  180. chmod(buf,0755);
  181. fprintf(stdout,"%s (version for RedHat-derived distros)\n",buf);
  182. }
  183. if (stat("/etc/debian_version",&st) == 0) {
  184. // Debian-derived distribution
  185. sprintf(buf,"/etc/init.d/zerotier-one");
  186. if (!instFile(debian__init_d__zerotier_one,buf)) {
  187. fprintf(stderr,"Unable to write %s\n",buf);
  188. return 1;
  189. }
  190. chmod(buf,0755);
  191. fprintf(stdout,"%s (version for Debian-derived distros)\n",buf);
  192. }
  193. #endif
  194. #endif // __UNIX_LIKE__
  195. #ifdef __WINDOWS__
  196. #endif // __WINDOWS__
  197. return 0;
  198. }