installer.c 8.0 KB

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