install.tmpl.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. export PATH=/bin:/usr/bin:/sbin:/usr/sbin
  3. shopt -s expand_aliases
  4. dryRun=0
  5. echo "*** ZeroTier One install/update ***"
  6. if [ "$UID" -ne 0 ]; then
  7. echo "Not running as root so doing dry run (no modifications to system)..."
  8. dryRun=1
  9. fi
  10. if [ $dryRun -gt 0 ]; then
  11. alias ln="echo '>> dry run: ln'"
  12. alias rm="echo '>> dry run: rm'"
  13. alias mv="echo '>> dry run: mv'"
  14. alias cp="echo '>> dry run: cp'"
  15. alias chown="echo '>> dry run: chown'"
  16. alias chgrp="echo '>> dry run: chgrp'"
  17. alias chmod="echo '>> dry run: chmod'"
  18. alias launchctl="echo '>> dry run: launchctl'"
  19. alias zerotier-cli="echo '>> dry run: zerotier-cli'"
  20. fi
  21. zthome="/Library/Application Support/ZeroTier/One"
  22. ztapp="/Applications/ZeroTier One.app"
  23. if [ ! -d "$ztapp" ]; then
  24. ztapp=`mdfind kMDItemCFBundleIdentifier == 'com.zerotier.ZeroTierOne' | grep -E '.*ZeroTier One[.]app$' | grep -v -F '/build-' | grep -v -F '/Volumes/ZeroTier' | sort | head -n 1`
  25. fi
  26. scriptPath="`dirname "$0"`/`basename "$0"`"
  27. if [ ! -r "$scriptPath" ]; then
  28. scriptPath="$0"
  29. if [ ! -r "$scriptPath" ]; then
  30. echo "Installer cannot determine its own path; $scriptPath is not readable."
  31. exit 2
  32. fi
  33. fi
  34. endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1`
  35. if [ "$endMarkerIndex" -le 100 ]; then
  36. echo 'Internal error: unable to find end of script / start of binary data marker.'
  37. exit 2
  38. fi
  39. blobStart=`expr $endMarkerIndex + 17`
  40. if [ "$blobStart" -le "$endMarkerIndex" ]; then
  41. echo 'Internal error: unable to find end of script / start of binary data marker.'
  42. exit 2
  43. fi
  44. echo 'Extracting files...'
  45. if [ $dryRun -gt 0 ]; then
  46. echo ">> dry run: tail -c +$blobStart \"$scriptPath\" | bunzip2 -c | tar -xvop -C / -f -"
  47. else
  48. rm -rf '/tmp/_zt1tmp'
  49. mkdir '/tmp/_zt1tmp'
  50. tail -c +$blobStart "$scriptPath" | bunzip2 -c | tar -xop -C '/tmp/_zt1tmp' -f -
  51. fi
  52. cd '/tmp/_zt1tmp'
  53. if [ $dryRun -eq 0 -a ! -d './Applications/ZeroTier One.app' ]; then
  54. echo 'Archive extraction failed, cannot find files in /tmp/_zt1tmp.'
  55. exit 2
  56. fi
  57. echo 'Installing zerotier-one service...'
  58. mkdir -p "$zthome"
  59. chown root:admin "$zthome"
  60. chmod 0750 "$zthome"
  61. cp -fa ./Library/Application\ Support/ZeroTier/One/* "$zthome"
  62. chown -R root:wheel "$zthome/tap.kext"
  63. chown -R root:wheel "$zthome/pre10.8/tap.kext"
  64. echo 'Installing/updating ZeroTier One.app...'
  65. if [ ! -z "$ztapp" -a -d "$ztapp" -a -f "$ztapp/Contents/Info.plist" ]; then
  66. # Preserve ownership of current app across updates... that way the admin
  67. # user who dragged it into /Applications can just trash it the way they
  68. # would any other app. This works (due to mdfind up top) even if they put
  69. # it somewhere non-standard on their system.
  70. currentAppOwner=`stat -f '%u' "$ztapp"`
  71. currentAppGroup=`stat -f '%g' "$ztapp"`
  72. rm -rf "$ztapp"
  73. mv -f './Applications/ZeroTier One.app' "$ztapp"
  74. if [ ! -z "$currentAppOwner" -a ! -z "$currentAppGroup" ]; then
  75. chown -R $currentAppOwner "$ztapp"
  76. chgrp -R $currentAppGroup "$ztapp"
  77. else
  78. chown -R root "$ztapp"
  79. chgrp -R admin "$ztapp"
  80. fi
  81. else
  82. # If there is no existing app, just drop the shipped one into place
  83. ztapp="/Applications/ZeroTier One.app"
  84. mv -f './Applications/ZeroTier One.app' "$ztapp"
  85. chown -R root "$ztapp"
  86. chgrp -R admin "$ztapp"
  87. fi
  88. # Set up symlink that watches for app deletion
  89. rm -f "$zthome/shutdownIfUnreadable"
  90. ln -sf "$ztapp/Contents/Info.plist" "$zthome/shutdownIfUnreadable"
  91. echo 'Installing zerotier-cli command line utility...'
  92. rm -f /usr/bin/zerotier-cli /usr/bin/zerotier-idtool
  93. ln -sf "/Library/Application Support/ZeroTier/One/zerotier-one" /usr/bin/zerotier-cli
  94. ln -sf "/Library/Application Support/ZeroTier/One/zerotier-one" /usr/bin/zerotier-idtool
  95. # This lets the install helper AppleScript thingy go ahead and authorize the
  96. # user after the installer is done, skiping that step for the user who did
  97. # the service install.
  98. if [ ! -f '/Library/Application Support/ZeroTier/One/authtoken.secret' ]; then
  99. echo 'Pre-creating authtoken.secret for ZeroTier service...'
  100. if [ $dryRun -eq 0 ]; then
  101. rm -f '/Library/Application Support/ZeroTier/One/authtoken.secret'
  102. head -c 1024 /dev/urandom | md5 | head -c 24 >'/Library/Application Support/ZeroTier/One/authtoken.secret'
  103. chmod 0600 '/Library/Application Support/ZeroTier/One/authtoken.secret'
  104. fi
  105. fi
  106. echo 'Installing and (re-)starting zerotier-one service via launchctl...'
  107. mv -f './Library/LaunchDaemons/com.zerotier.one.plist' '/Library/LaunchDaemons/'
  108. launchctl load /Library/LaunchDaemons/com.zerotier.one.plist
  109. # launchctl will restart us after exit if this is an online auto-update
  110. cd /tmp
  111. rm -rf _zt1tmp
  112. exit 0
  113. # Do not remove the last line or add a carriage return to it! The installer
  114. # looks for an unterminated line beginning with 16 #'s in itself to find
  115. # the binary blob data, which is appended after it.
  116. ################