freedombone-app-tahoelafs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Distributed storage
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS=''
  31. TAHOELAFS_REPO="https://github.com/tahoe-lafs/tahoe-lafs"
  32. TAHOELAFS_COMMIT='30b421d48b3d1dae2ef507bf27a1f3816300cd92'
  33. TAHOELAFS_PORT=50213
  34. TAHOELAFS_WEB_PORT=3456
  35. TAHOELAFS_STORAGE_SPACE=1G
  36. TAHOELAFS_SHARED_DIR='Shared'
  37. TAHOE_COMMAND="cd /var/lib/tahoelafs && venv/bin/tahoe"
  38. tahoelafs_variables=(TAHOELAFS_REPO
  39. TAHOELAFS_COMMIT
  40. TAHOELAFS_PORT)
  41. function install_interactive_tahoelafs {
  42. echo -n ''
  43. }
  44. function change_password_tahoelafs {
  45. echo -n ''
  46. }
  47. function reconfigure_tahoelafs {
  48. for d in /home/*/ ; do
  49. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  50. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  51. if [ -d /home/$USERNAME/.tahoe ]; then
  52. su -c "$TAHOE_COMMAND stop" - $USERNAME
  53. rm -rf /home/$USERNAME/.tahoe
  54. fi
  55. if [ -d /home/$USERNAME/${TAHOELAFS_SHARED_DIR} ]; then
  56. rm -rf /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
  57. fi
  58. su -c "cd $TAHOE_COMMAND create-node" - $USERNAME
  59. if [ ! -d /home/$USERNAME/.tahoe ]; then
  60. exit 63722
  61. fi
  62. # create a shared directory
  63. if [ ! -d /home/$USERNAME/${TAHOELAFS_SHARED_DIR} ]; then
  64. mkdir /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
  65. chown -R $USERNAME:$USERNAME /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
  66. fi
  67. TAHOELAFS_CONFIG=/home/$USERNAME/.tahoe/tahoe.cfg
  68. echo '[node]' > $TAHOELAFS_CONFIG
  69. echo "nickname = $USERNAME" >> $TAHOELAFS_CONFIG
  70. echo "web.port = tcp:$TAHOELAFS_WEB_PORT:interface=127.0.0.1" >> $TAHOELAFS_CONFIG
  71. echo 'web.static = public_html' >> $TAHOELAFS_CONFIG
  72. echo "tub.port = tcp:$TAHOELAFS_PORT" >> $TAHOELAFS_CONFIG
  73. echo "tub.location = tcp:$HOSTNAME:$TAHOELAFS_PORT" >> $TAHOELAFS_CONFIG
  74. echo 'timeout.keepalive = 240' >> $TAHOELAFS_CONFIG
  75. echo 'timeout.disconnect = 1800' >> $TAHOELAFS_CONFIG
  76. echo '' >> $TAHOELAFS_CONFIG
  77. echo '[client]' >> $TAHOELAFS_CONFIG
  78. echo 'introducer.furl = None' >> $TAHOELAFS_CONFIG
  79. echo '#helper.furl =' >> $TAHOELAFS_CONFIG
  80. echo '' >> $TAHOELAFS_CONFIG
  81. echo 'shares.needed = 1' >> $TAHOELAFS_CONFIG
  82. echo 'shares.happy = 1' >> $TAHOELAFS_CONFIG
  83. echo 'shares.total = 1' >> $TAHOELAFS_CONFIG
  84. echo '' >> $TAHOELAFS_CONFIG
  85. echo '[storage]' >> $TAHOELAFS_CONFIG
  86. echo 'enabled = true' >> $TAHOELAFS_CONFIG
  87. echo "reserved_space = $TAHOELAFS_STORAGE_SPACE" >> $TAHOELAFS_CONFIG
  88. echo '#expire.enabled = true' >> $TAHOELAFS_CONFIG
  89. echo '#expire.mode =' >> $TAHOELAFS_CONFIG
  90. echo '' >> $TAHOELAFS_CONFIG
  91. echo '[helper]' >> $TAHOELAFS_CONFIG
  92. echo 'enabled = false' >> $TAHOELAFS_CONFIG
  93. echo '' >> $TAHOELAFS_CONFIG
  94. echo '[magic_folder]' >> $TAHOELAFS_CONFIG
  95. echo 'enabled = true' >> $TAHOELAFS_CONFIG
  96. echo "local.directory = /home/${USERNAME}/${TAHOELAFS_SHARED_DIR}" >> $TAHOELAFS_CONFIG
  97. chown $USERNAME:$USERNAME $TAHOELAFS_CONFIG
  98. su -c "$TAHOE_COMMAND start" - $USERNAME
  99. su -c "$TAHOE_COMMAND magic-folder create $HOSTNAME" - $USERNAME
  100. fi
  101. done
  102. }
  103. function upgrade_tahoelafs {
  104. echo -n ''
  105. # TODO
  106. }
  107. function backup_local_tahoelafs {
  108. source_directory=/home/tahoelafs/.tahoe-introducer
  109. if [ ! -d $source_directory ]; then
  110. return
  111. fi
  112. echo $"Backing up Tahoe-LAFS introducer"
  113. systemctl stop tahoelafs
  114. dest_directory=tahoelafs
  115. function_check backup_directory_to_usb
  116. backup_directory_to_usb $source_directory $dest_directory
  117. systemctl start tahoelafs
  118. echo $"Backup to $dest_directory complete"
  119. }
  120. function restore_local_tahoelafs {
  121. echo $"Restoring Tahoe-LAFS introducer"
  122. systemctl stop tahoelafs
  123. temp_restore_dir=/root/temptahoelafs
  124. restore_directory_from_usb $temp_restore_dir tahoelafs
  125. mv /home/tahoelafs/.tahoe-introducer /home/tahoelafs/.tahoe-introducer-old
  126. cp -r $temp_restore_dir/home/tahoelafs/.tahoe-introducer /home/tahoelafs/
  127. if [ ! "$?" = "0" ]; then
  128. mv /home/tahoelafs/.tahoe-introducer-old /home/tahoelafs/.tahoe-introducer
  129. exit 246833
  130. fi
  131. rm -rf /home/tahoelafs/.tahoe-introducer
  132. chown -R tahoelafs:tahoelafs /home/tahoelafs
  133. systemctl start tahoelafs
  134. echo $"Restore complete"
  135. }
  136. function backup_remote_tahoelafs {
  137. source_directory=/home/tahoelafs/.tahoe-introducer
  138. if [ ! -d $source_directory ]; then
  139. return
  140. fi
  141. echo $"Backing up Tahoe-LAFS introducer"
  142. systemctl stop tahoelafs
  143. dest_directory=tahoelafs
  144. function_check backup_directory_to_usb
  145. backup_directory_to_friend $source_directory $dest_directory
  146. systemctl start tahoelafs
  147. echo $"Backup to $dest_directory complete"
  148. }
  149. function restore_remote_tahoelafs {
  150. echo $"Restoring Tahoe-LAFS introducer"
  151. systemctl stop tahoelafs
  152. temp_restore_dir=/root/temptahoelafs
  153. restore_directory_from_friend $temp_restore_dir tahoelafs
  154. mv /home/tahoelafs/.tahoe-introducer /home/tahoelafs/.tahoe-introducer-old
  155. cp -r $temp_restore_dir/home/tahoelafs/.tahoe-introducer /home/tahoelafs/
  156. if [ ! "$?" = "0" ]; then
  157. mv /home/tahoelafs/.tahoe-introducer-old /home/tahoelafs/.tahoe-introducer
  158. exit 623925
  159. fi
  160. rm -rf /home/tahoelafs/.tahoe-introducer-old
  161. chown -R tahoelafs:tahoelafs /home/tahoelafs
  162. systemctl start tahoelafs
  163. echo $"Restore complete"
  164. }
  165. function remove_tahoelafs {
  166. if [[ $(app_is_installed tahoelafs) == "0" ]]; then
  167. return
  168. fi
  169. iptables -D INPUT -p udp --dport $TAHOELAFS_PORT -j ACCEPT
  170. iptables -D INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  171. function_check save_firewall_settings
  172. save_firewall_settings
  173. for d in /home/*/ ; do
  174. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  175. if [ -f /home/$USERNAME/.tahoe ]; then
  176. su -c "$TAHOE_COMMAND stop" - $USERNAME
  177. rm -rf /home/$USERNAME/.tahoe
  178. fi
  179. done
  180. rm -rf /var/lib/tahoelafs
  181. sed -i '/install_tahoelafs/d' $COMPLETION_FILE
  182. sed -i '/configure_firewall_for_tahoelafs/d' $COMPLETION_FILE
  183. }
  184. function configure_firewall_for_tahoelafs {
  185. if grep -Fxq "configure_firewall_for_tahoelafs" $COMPLETION_FILE; then
  186. return
  187. fi
  188. iptables -A INPUT -p udp --dport $TAHOELAFS_PORT -j ACCEPT
  189. iptables -A INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  190. function_check save_firewall_settings
  191. save_firewall_settings
  192. OPEN_PORTS+=("Tahoe-LAFS $TAHOELAFS_PORT")
  193. echo 'configure_firewall_for_tahoelafs' >> $COMPLETION_FILE
  194. }
  195. function tahoelafs_update_script {
  196. update_script_filename=$1
  197. echo '#!/bin/bash' > $update_script_filename
  198. echo 'TOX_USERNAME=$(toxid --showuser)' >> $update_script_filename
  199. echo 'PEERS_FILE=$(mktemp /tmp/tahoelafs-peers.XXXXXX)' >> $update_script_filename
  200. echo -n 'avahi-browse -atl | grep "Workstation" | ' >> $update_script_filename
  201. echo -n "awk -F ' ' '{print \$4}' " >> $update_script_filename
  202. echo '| sort -u > $PEERS_FILE' >> $update_script_filename
  203. echo 'if [ ! "$?" = "0" ]; then' >> $update_script_filename
  204. echo ' exit 1' >> $update_script_filename
  205. echo 'fi' >> $update_script_filename
  206. echo '' >> $update_script_filename
  207. echo 'if [ ! -f $PEERS_FILE ]; then' >> $update_script_filename
  208. echo ' exit 0' >> $update_script_filename
  209. echo 'fi' >> $update_script_filename
  210. echo '' >> $update_script_filename
  211. echo 'furl=""' >> $update_script_filename
  212. echo 'while IFS="" read -r line || [[ -n "$line" ]]; do' >> $update_script_filename
  213. echo ' if [[ $furl != "" ]]; then' >> $update_script_filename
  214. echo -n ' furl="$furl,$line:' >> $update_script_filename
  215. echo -n "$TAHOELAFS_PORT" >> $update_script_filename
  216. echo '"' >> $update_script_filename
  217. echo ' else' >> $update_script_filename
  218. echo -n ' furl="$line:' >> $update_script_filename
  219. echo -n "$TAHOELAFS_PORT" >> $update_script_filename
  220. echo '"' >> $update_script_filename
  221. echo ' fi' >> $update_script_filename
  222. echo 'done < "$PEERS_FILE"' >> $update_script_filename
  223. echo '' >> $update_script_filename
  224. echo 'rm $PEERS_FILE' >> $update_script_filename
  225. echo '' >> $update_script_filename
  226. echo 'for d in /home/*/ ; do' >> $update_script_filename
  227. echo -n ' USERNAME=$(echo "$d" | ' >> $update_script_filename
  228. echo "awk -F '/' '{print \$3}')'" >> $update_script_filename
  229. echo ' if [ -f /home/$USERNAME/.tahoe/tahoe.cfg ]; then' >> $update_script_filename
  230. echo ' if ! grep -q "introducer.furl = ${furl}" /home/$USERNAME/.tahoe/tahoe.cfg; then' >> $update_script_filename
  231. echo ' sed -i "s|introducer.furl =.*|introducer.furl = ${furl}|g" /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
  232. echo ' sed -i "s|nickname =.*|nickname = ${TOX_USERNAME}|g" /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
  233. echo ' chown $USERNAME:$USERNAME /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
  234. echo " su -c '$TAHOE_COMMAND restart' - \$USERNAME" >> $update_script_filename
  235. echo ' fi' >> $update_script_filename
  236. echo ' fi' >> $update_script_filename
  237. echo 'done' >> $update_script_filename
  238. echo '' >> $update_script_filename
  239. echo 'exit 0' >> $update_script_filename
  240. chmod +x $update_script_filename
  241. }
  242. function mesh_install_tahoelafs {
  243. chroot "$rootdir" apt-get -y install build-essential python-pip python-dev
  244. chroot "$rootdir" apt-get -y install libffi-dev libssl-dev python-virtualenv
  245. git clone $TAHOELAFS_REPO $rootdir/var/lib/tahoelafs
  246. cd $rootdir/var/lib/tahoelafs
  247. git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
  248. cat <<EOF > $rootdir/root/install_tahoelafs
  249. cd /var/lib/tahoelafs
  250. virtualenv venv
  251. venv/bin/pip install --editable .
  252. EOF
  253. chroot "$rootdir" chmod +x /root/install_tahoelafs
  254. chroot "$rootdir" /root/install_tahoelafs
  255. chroot "$rootdir" rm /root/install_tahoelafs
  256. tahoelafs_update_script $rootdir/usr/bin/update-tahoelafs
  257. if ! grep -q "update-tahoelafs" $rootdir/etc/crontab; then
  258. echo "*/1 * * * * root /usr/bin/update-tahoelafs 2> /dev/null" >> $rootdir/etc/crontab
  259. fi
  260. }
  261. function install_tahoelafs {
  262. if [ $INSTALLING_MESH ]; then
  263. mesh_install_tahoelafs
  264. return
  265. fi
  266. apt-get -y install build-essential python-pip python-dev libffi-dev libssl-dev python-virtualenv
  267. # create a user to run the introducer
  268. if [ ! -d /home/tahoelafs ]; then
  269. # add a gogs user account
  270. adduser --disabled-login --gecos 'tahoe-lafs' tahoelafs
  271. fi
  272. if [ -d /home/tahoelafs/Maildir ]; then
  273. rm -rf /home/tahoelafs/Maildir
  274. fi
  275. if [ ! -d /home/tahoelafs/.tahoe-introducer ]; then
  276. mkdir /home/tahoelafs/.tahoe-introducer
  277. fi
  278. git_clone $TAHOELAFS_REPO /home/tahoelafs/tahoelafs
  279. cd /home/tahoelafs/tahoelafs
  280. git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
  281. virtualenv venv
  282. venv/bin/pip install --editable .
  283. configure_firewall_for_tahoelafs
  284. su -c "cd $TAHOE_COMMAND create-introducer /home/tahoelafs/.tahoe-introducer" - tahoelafs
  285. TAHOELAFS_CONFIG=/home/tahoelafs/.tahoe-introducer/tahoe.cfg
  286. if [ ! -f $TAHOELAFS_CONFIG ]; then
  287. exit 62831
  288. fi
  289. sed -i "s|nickname =.*|nickname = $HOSTNAME|g" $TAHOELAFS_CONFIG
  290. if ! grep -q "[storage]" $TAHOELAFS_CONFIG; then
  291. echo '' >> $TAHOELAFS_CONFIG
  292. echo '[storage]' >> $TAHOELAFS_CONFIG
  293. echo 'enabled = true' >> $TAHOELAFS_CONFIG
  294. echo "reserved_space = $TAHOELAFS_STORAGE_SPACE" >> $TAHOELAFS_CONFIG
  295. fi
  296. chown -R tahoelafs:tahoelafs /home/tahoelafs
  297. TAHOELAFS_DAEMON_FILE=/etc/systemd/system/tahoelafs.service
  298. echo '[Unit]' > $TAHOELAFS_DAEMON_FILE
  299. echo 'Description=Tahoe-LAFS introducer' >> $TAHOELAFS_DAEMON_FILE
  300. echo 'After=syslog.target' >> $TAHOELAFS_DAEMON_FILE
  301. echo 'After=network.target' >> $TAHOELAFS_DAEMON_FILE
  302. echo '' >> $TAHOELAFS_DAEMON_FILE
  303. echo '[Service]' >> $TAHOELAFS_DAEMON_FILE
  304. echo 'Type=simple' >> $TAHOELAFS_DAEMON_FILE
  305. echo "User=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  306. echo "Group=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  307. echo "WorkingDirectory=/home/tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  308. echo "ExecStart=venv/bin/tahoe start /home/tahoelafs/.tahoe-introducer" >> $TAHOELAFS_DAEMON_FILE
  309. echo "ExecStop=venv/bin/tahoe stop /home/tahoelafs/.tahoe-introducer" >> $TAHOELAFS_DAEMON_FILE
  310. echo 'Restart=on-failure' >> $TAHOELAFS_DAEMON_FILE
  311. echo "Environment=\"USER=tahoelafs\" \"HOME=/home/tahoelafs\"" >> $TAHOELAFS_DAEMON_FILE
  312. echo '' >> $TAHOELAFS_DAEMON_FILE
  313. echo '[Install]' >> $TAHOELAFS_DAEMON_FILE
  314. echo 'WantedBy=multi-user.target' >> $TAHOELAFS_DAEMON_FILE
  315. systemctl enable tahoelafs
  316. systemctl daemon-reload
  317. systemctl start tahoelafs
  318. }
  319. # NOTE: deliberately no exit 0