installer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <fcntl.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <signal.h>
  48. #endif
  49. #include "ext/lz4/lz4.h"
  50. #include "ext/lz4/lz4hc.h"
  51. // Include Lz4 comrpessed blobs -----------------------------------------------
  52. // zerotier-one binary (or zerotier-one.exe for Windows)
  53. #include "installer-build/zerotier_one.h"
  54. // Unix uninstall script, installed in home for user to remove
  55. #ifdef __UNIX_LIKE__
  56. #include "installer-build/uninstall_sh.h"
  57. #endif
  58. // Linux init.d script
  59. #ifdef __LINUX__
  60. #include "installer-build/linux__init_d__zerotier_one.h"
  61. #endif
  62. // Apple Tap device driver
  63. #ifdef __APPLE__
  64. #include "installer-build/tap_mac__Info_plist.h"
  65. #include "installer-build/tap_mac__tap.h"
  66. #endif
  67. // Windows Tap device drivers for x86 and x64 (installer will be x86)
  68. #ifdef __WINDOWS__
  69. #include "installer-build/tap_windows__x64__ztTap100_sys.h"
  70. #include "installer-build/tap_windows__x64__ztTap100_inf.h"
  71. #include "installer-build/tap_windows__x86__ztTap100_sys.h"
  72. #include "installer-build/tap_windows__x86__ztTap100_inf.h"
  73. #include "installer-build/tap_windows__devcon32_exe.h"
  74. #include "installer-build/tap_windows__devcon64_exe.h"
  75. #endif
  76. // ----------------------------------------------------------------------------
  77. static unsigned char *_unlz4(const void *lz4,int decompressedLen)
  78. {
  79. unsigned char *buf = new unsigned char[decompressedLen];
  80. if (LZ4_decompress_fast((const char *)lz4,(char *)buf,decompressedLen) <= 0) {
  81. delete [] buf;
  82. return (unsigned char *)0;
  83. }
  84. return buf;
  85. }
  86. static bool _putBlob(const void *lz4,int decompressedLen,const char *path,bool executable,bool protect,bool preserveOwnership)
  87. {
  88. unsigned char *data = _unlz4(lz4,decompressedLen);
  89. if (!data)
  90. return false;
  91. #ifdef __WINDOWS__
  92. DeleteFileA(path);
  93. #else
  94. struct stat oldModes;
  95. bool hasOldModes = (stat(path,&oldModes) == 0);
  96. unlink(path);
  97. #endif
  98. FILE *f = fopen(path,"wb");
  99. if (!f) {
  100. delete [] data;
  101. return false;
  102. }
  103. if (fwrite(data,decompressedLen,1,f) != 1) {
  104. fclose(f);
  105. delete [] data;
  106. #ifdef __WINDOWS__
  107. DeleteFileA(path);
  108. #else
  109. unlink(path);
  110. #endif
  111. return false;
  112. }
  113. fclose(f);
  114. #ifdef __WINDOWS__
  115. #else
  116. if (executable) {
  117. if (protect)
  118. chmod(path,0700);
  119. else chmod(path,0755);
  120. } else {
  121. if (protect)
  122. chmod(path,0600);
  123. else chmod(path,0644);
  124. }
  125. if (preserveOwnership&&hasOldModes)
  126. chown(path,oldModes.st_uid,oldModes.st_gid);
  127. else chown(path,0,0);
  128. #endif
  129. delete [] data;
  130. return true;
  131. }
  132. #define putBlob(name,path,exec,prot,pres) _putBlob((name),(name##_UNCOMPRESSED_LEN),(path),(exec),(prot),(pres))
  133. // ----------------------------------------------------------------------------
  134. #ifdef __WINDOWS__
  135. int _tmain(int argc, _TCHAR* argv[])
  136. #else
  137. int main(int argc,char **argv)
  138. #endif
  139. {
  140. #ifdef __UNIX_LIKE__ // -------------------------------------------------------
  141. char buf[4096];
  142. if (getuid() != 0) {
  143. printf("! ZeroTier One installer must be run as root.\n");
  144. return 2;
  145. }
  146. printf("# ZeroTier One installer/updater starting...\n");
  147. // Create home folder
  148. const char *zthome;
  149. #ifdef __APPLE__
  150. mkdir("/Library/Application Support/ZeroTier",0755);
  151. chmod("/Library/Application Support/ZeroTier",0755);
  152. chown("/Library/Application Support/ZeroTier",0,0);
  153. printf("mkdir /Library/Application Support/ZeroTier\n");
  154. mkdir(zthome = "/Library/Application Support/ZeroTier/One",0755);
  155. #else
  156. mkdir("/var/lib",0755);
  157. printf("mkdir /var/lib\n");
  158. mkdir(zthome = "/var/lib/zerotier-one",0755);
  159. #endif
  160. chmod(zthome,0755);
  161. chown(zthome,0,0);
  162. printf("mkdir %s\n",zthome);
  163. // Write main ZT1 binary
  164. sprintf(buf,"%s/zerotier-one",zthome);
  165. if (!putBlob(zerotier_one,buf,true,false,false)) {
  166. printf("! unable to write %s\n",buf);
  167. return 1;
  168. }
  169. printf("write %s\n",buf);
  170. // Create command line interface symlink
  171. unlink("/usr/bin/zerotier-cli");
  172. symlink(buf,"/usr/bin/zerotier-cli");
  173. printf("link %s /usr/bin/zerotier-cli\n",buf);
  174. // Write uninstall script into home folder
  175. sprintf(buf,"%s/uninstall.sh",zthome);
  176. if (!putBlob(uninstall_sh,buf,true,false,false)) {
  177. printf("! unable to write %s\n",buf);
  178. return 1;
  179. }
  180. printf("write %s\n",buf);
  181. #ifdef __APPLE__
  182. // Write tap.kext into home folder
  183. sprintf(buf,"%s/tap.kext",zthome);
  184. mkdir(buf,0755);
  185. chmod(buf,0755);
  186. chown(buf,0,0);
  187. printf("mkdir %s\n",buf);
  188. sprintf(buf,"%s/tap.kext/Contents",zthome);
  189. mkdir(buf,0755);
  190. chmod(buf,0755);
  191. chown(buf,0,0);
  192. printf("mkdir %s\n",buf);
  193. sprintf(buf,"%s/tap.kext/Contents/MacOS",zthome);
  194. mkdir(buf,0755);
  195. chmod(buf,0755);
  196. chown(buf,0,0);
  197. printf("mkdir %s\n",buf);
  198. sprintf(buf,"%s/tap.kext/Contents/Info.plist",zthome);
  199. if (!putBlob(tap_mac__Info_plist,buf,false,false,false)) {
  200. printf("! unable to write %s\n",buf);
  201. return 1;
  202. }
  203. printf("write %s\n",buf);
  204. sprintf(buf,"%s/tap.kext/Contents/MacOS/tap",zthome);
  205. if (!putBlob(tap_mac__tap,buf,true,false,false)) {
  206. printf("! unable to write %s\n",buf);
  207. return 1;
  208. }
  209. printf("write %s\n",buf);
  210. // Write or update GUI application into /Applications
  211. #endif
  212. #ifdef __LINUX__
  213. // Write Linux init script
  214. sprintf(buf,"/etc/init.d/zerotier-one");
  215. if (!putBlob(linux__init_d__zerotier_one,buf,true,false,false)) {
  216. printf("! unable to write %s\n",buf);
  217. return 1;
  218. }
  219. printf("write %s\n",buf);
  220. symlink("/etc/init.d/zerotier-one","/etc/rc0.d/K89zerotier-one");
  221. printf("link /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one\n");
  222. symlink("/etc/init.d/zerotier-one","/etc/rc2.d/S11zerotier-one");
  223. printf("link /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one\n");
  224. symlink("/etc/init.d/zerotier-one","/etc/rc3.d/S11zerotier-one");
  225. printf("link /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one\n");
  226. symlink("/etc/init.d/zerotier-one","/etc/rc4.d/S11zerotier-one");
  227. printf("link /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one\n");
  228. symlink("/etc/init.d/zerotier-one","/etc/rc5.d/S11zerotier-one");
  229. printf("link /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one\n");
  230. symlink("/etc/init.d/zerotier-one","/etc/rc6.d/S11zerotier-one");
  231. printf("link /etc/init.d/zerotier-one /etc/rc6.d/S11zerotier-one\n");
  232. #endif
  233. printf("# Done!\n");
  234. #endif // __UNIX_LIKE__ -------------------------------------------------------
  235. #ifdef __WINDOWS__ // ---------------------------------------------------------
  236. #endif // __WINDOWS__ ---------------------------------------------------------
  237. return 0;
  238. }