freedombone-app-tahoelafs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Tahow-LAFS data storage grid implemented via Tor
  12. # https://k0rx.com/blog/2017/01/lafs.html
  13. # http://tahoe-lafs.readthedocs.io/en/latest/anonymity-configuration.html
  14. #
  15. # License
  16. # =======
  17. #
  18. # Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU Affero General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU Affero General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU Affero General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. VARIANTS='full full-vim cloud'
  33. IN_DEFAULT_INSTALL=0
  34. SHOW_ON_ABOUT=1
  35. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  36. TAHOELAFS_REPO="https://github.com/tahoe-lafs/tahoe-lafs"
  37. TAHOELAFS_COMMIT='bb782b0331a60de438136a593bba18338d8d866b'
  38. TAHOELAFS_PORT=50213
  39. TAHOELAFS_STORAGE_PORT=50214
  40. TAHOELAFS_ONION_PORT=8096
  41. TAHOELAFS_STORAGE_ONION_PORT=8097
  42. TAHOE_COMMAND="cd /home/tahoelafs/tahoelafs && venv/bin/tahoe"
  43. tahoelafs_storage_file=/home/tahoelafs/client/private/servers.yaml
  44. TAHOELAFS_SHARES_NEEDED=3
  45. TAHOELAFS_SHARES_HAPPY=7
  46. TAHOELAFS_SHARES_TOTAL=10
  47. tahoelafs_variables=(ONION_ONLY
  48. MY_USERNAME
  49. TAHOELAFS_REPO
  50. TAHOELAFS_PORT
  51. TAHOELAFS_SHARES_NEEDED
  52. TAHOELAFS_SHARES_HAPPY
  53. TAHOELAFS_SHARES_TOTAL)
  54. function add_user_tahoelafs {
  55. if [[ $(app_is_installed tahoelafs) == "0" ]]; then
  56. echo '0'
  57. return
  58. fi
  59. new_username="$1"
  60. new_user_password="$2"
  61. ${PROJECT_NAME}-pass -u $new_username -a tahoelafs -p "$new_user_password"
  62. if grep "${new_username}:" /etc/nginx/.htpasswd-tahoelafs; then
  63. sed -i '/${new_username}:/d' /etc/nginx/.htpasswd-tahoelafs
  64. fi
  65. echo "${new_user_password}" | htpasswd -i -s /etc/nginx/.htpasswd-tahoelafs ${new_username}
  66. echo '0'
  67. }
  68. function remove_user_tahoelafs {
  69. remove_username="$1"
  70. ${PROJECT_NAME}-pass -u $remove_username --rmapp tahoelafs
  71. if grep "${remove_username}:" /etc/nginx/.htpasswd-tahoelafs; then
  72. sed -i '/${remove_username}:/d' /etc/nginx/.htpasswd-tahoelafs
  73. fi
  74. }
  75. function change_password_tahoelafs {
  76. change_username="$1"
  77. change_password="$2"
  78. ${PROJECT_NAME}-pass -u $change_username -a tahoelafs -p "$change_password"
  79. if grep "${change_username}:" /etc/nginx/.htpasswd-tahoelafs; then
  80. sed -i '/tahoe-${change_username}:/d' /etc/nginx/.htpasswd-tahoelafs
  81. fi
  82. echo "${change_password}" | htpasswd -i -s /etc/nginx/.htpasswd-tahoelafs ${change_username}
  83. }
  84. function add_tahoelafs_storage_node_interactive {
  85. data=$(tempfile 2>/dev/null)
  86. trap "rm -f $data" 0 1 2 5 15
  87. dialog --backtitle $"Freedombone Configuration" \
  88. --title $"Add Tahoe-LAFS storage node" \
  89. --form $"\nEnter the storage node details which can be found on the About screen of another server" 13 75 5 \
  90. $"Hostname:" 1 1 "" 1 14 53 40 \
  91. $"Public Key:" 2 1 "" 2 14 53 255 \
  92. $"Nickname:" 3 1 "" 3 14 53 255 \
  93. $"FURL:" 4 1 "" 4 14 53 255 \
  94. 2> $data
  95. sel=$?
  96. case $sel in
  97. 1) return;;
  98. 255) return;;
  99. esac
  100. storage_hostname=$(cat $data | sed -n 1p)
  101. public_key="$(cat $data | sed -n 2p)"
  102. nick=$(cat $data | sed -n 3p)
  103. furl=$(cat $data | sed -n 4p)
  104. if [ ${#public_key} -eq 0 ]; then
  105. return
  106. fi
  107. add_tahoelafs_server "${storage_hostname}" "${public_key}" "${nick}" "${furl}"
  108. if grep -q "$public_key" ${tahoelafs_storage_file}; then
  109. dialog --title $"Add Tahoe-LAFS storage node" \
  110. --msgbox $"Storage node added" 6 40
  111. fi
  112. }
  113. function edit_tahoelafs_nodes {
  114. editor $tahoelafs_storage_file
  115. chown tahoelafs:debian-tor $tahoelafs_storage_file
  116. systemctl restart tahoelafs-client
  117. }
  118. function edit_tahoelafs_shares {
  119. read_config_param TAHOELAFS_SHARES_NEEDED
  120. read_config_param TAHOELAFS_SHARES_HAPPY
  121. read_config_param TAHOELAFS_SHARES_TOTAL
  122. data=$(tempfile 2>/dev/null)
  123. trap "rm -f $data" 0 1 2 5 15
  124. dialog --backtitle $"Freedombone Configuration" \
  125. --title $"Tahoe-LAFS shares" \
  126. --form $"\nEnter the storage node details which can be found on the About screen of another server" 13 40 3 \
  127. $"Needed:" 1 1 "${TAHOELAFS_SHARES_NEEDED}" 1 14 4 4 \
  128. $"Happy:" 2 1 "${TAHOELAFS_SHARES_HAPPY}" 2 14 4 4 \
  129. $"Total:" 3 1 "${TAHOELAFS_SHARES_TOTAL}" 3 14 4 4 \
  130. 2> $data
  131. sel=$?
  132. case $sel in
  133. 1) return;;
  134. 255) return;;
  135. esac
  136. tl_needed="$(cat $data | sed -n 1p)"
  137. tl_happy="$(cat $data | sed -n 2p)"
  138. tl_total="$(cat $data | sed -n 3p)"
  139. if [ ${#tl_needed} -gt 0 ]; then
  140. TAHOELAFS_SHARES_NEEDED=${tl_needed}
  141. fi
  142. if [ ${#tl_happy} -gt 0 ]; then
  143. TAHOELAFS_SHARES_HAPPY=${tl_happy}
  144. fi
  145. if [ ${#tl_total} -gt 0 ]; then
  146. TAHOELAFS_SHARES_TOTAL=${tl_total}
  147. fi
  148. sed -i "s|shares.needed.*|shares.needed = ${TAHOELAFS_SHARES_NEEDED}|g" /home/tahoelafs/tahoelafs/client/tahoe.cfg
  149. sed -i "s|shares.happy.*|shares.happy = ${TAHOELAFS_SHARES_HAPPY}|g" /home/tahoelafs/tahoelafs/client/tahoe.cfg
  150. sed -i "s|shares.total.*|shares.total = ${TAHOELAFS_SHARES_TOTAL}|g" /home/tahoelafs/tahoelafs/client/tahoe.cfg
  151. sed -i "s|shares.needed.*|shares.needed = ${TAHOELAFS_SHARES_NEEDED}|g" /home/tahoelafs/tahoelafs/storage/tahoe.cfg
  152. sed -i "s|shares.happy.*|shares.happy = ${TAHOELAFS_SHARES_HAPPY}|g" /home/tahoelafs/tahoelafs/storage/tahoe.cfg
  153. sed -i "s|shares.total.*|shares.total = ${TAHOELAFS_SHARES_TOTAL}|g" /home/tahoelafs/tahoelafs/storage/tahoe.cfg
  154. systemctl restart tahoelafs-storage
  155. systemctl restart tahoelafs-client
  156. dialog --title $"Tahoe-LAFS shares" \
  157. --msgbox $"Shares settings changed" 6 40
  158. }
  159. function configure_interactive_tahoelafs {
  160. data=$(tempfile 2>/dev/null)
  161. trap "rm -f $data" 0 1 2 5 15
  162. dialog --backtitle $"Freedombone Configuration" \
  163. --title $"Tahoe-LAFS" \
  164. --radiolist $"The least authority is always the best" 11 50 5 \
  165. 1 "Add a storage node" off \
  166. 2 "Manually edit storage nodes" off \
  167. 3 "Shares settings" off \
  168. 4 "Back to main menu" on 2> $data
  169. sel=$?
  170. case $sel in
  171. 1) exit 1;;
  172. 255) exit 1;;
  173. esac
  174. case $(cat $data) in
  175. 1) add_tahoelafs_storage_node_interactive;;
  176. 2) edit_tahoelafs_nodes;;
  177. 3) edit_tahoelafs_shares;;
  178. esac
  179. }
  180. function tahoelafs_setup_client_config {
  181. config_file=$1
  182. nick="$2"
  183. echo '[node]' > $config_file
  184. echo "nickname = $nick" >> $config_file
  185. echo 'reveal-IP-address = false' >> $config_file
  186. echo "web.port = tcp:${TAHOELAFS_PORT}:interface=127.0.0.1" >> $config_file
  187. echo 'web.static = public_html' >> $config_file
  188. echo 'tub.port = disabled' >> $config_file
  189. echo 'tub.location = disabled' >> $config_file
  190. echo '' >> $config_file
  191. echo '[client]' >> $config_file
  192. echo 'introducer.furl =' >> $config_file
  193. echo "shares.needed = ${TAHOELAFS_SHARES_NEEDED}" >> $config_file
  194. echo "shares.happy = ${TAHOELAFS_SHARES_HAPPY}" >> $config_file
  195. echo "shares.total = ${TAHOELAFS_SHARES_TOTAL}" >> $config_file
  196. echo '' >> $config_file
  197. echo '[storage]' >> $config_file
  198. echo 'enabled = false' >> $config_file
  199. echo 'reserved_space = 3G' >> $config_file
  200. echo '' >> $config_file
  201. echo '[helper]' >> $config_file
  202. echo 'enabled = false' >> $config_file
  203. echo '' >> $config_file
  204. echo '[connections]' >> $config_file
  205. echo 'tcp = tor' >> $config_file
  206. }
  207. function tahoelafs_setup_storage_config {
  208. config_file=$1
  209. nick="$2"
  210. echo '[node]' > $config_file
  211. echo "nickname = $nick" >> $config_file
  212. echo 'reveal-IP-address = false' >> $config_file
  213. echo 'web.port =' >> $config_file
  214. echo 'web.static = public_html' >> $config_file
  215. echo "tub.port = tcp:${TAHOELAFS_STORAGE_ONION_PORT}:interface=127.0.0.1" >> $config_file
  216. echo "tub.location = tor:${TAHOELAFS_STORAGE_ONION_HOSTNAME}:${TAHOELAFS_STORAGE_PORT}" >> $config_file
  217. echo '' >> $config_file
  218. echo '[client]' >> $config_file
  219. echo 'introducer.furl =' >> $config_file
  220. echo 'helper.furl =' >> $config_file
  221. echo '' >> $config_file
  222. echo "shares.needed = ${TAHOELAFS_SHARES_NEEDED}" >> $config_file
  223. echo "shares.happy = ${TAHOELAFS_SHARES_HAPPY}" >> $config_file
  224. echo "shares.total = ${TAHOELAFS_SHARES_TOTAL}" >> $config_file
  225. echo '' >> $config_file
  226. echo '[storage]' >> $config_file
  227. echo 'enabled = true' >> $config_file
  228. echo 'reserved_space = 3G' >> $config_file
  229. echo 'expire.enabled = true' >> $config_file
  230. echo 'expire.mode = age' >> $config_file
  231. echo 'expire.override_lease_duration = 3 months' >> $config_file
  232. echo '' >> $config_file
  233. echo '[helper]' >> $config_file
  234. echo 'enabled = false' >> $config_file
  235. echo '' >> $config_file
  236. echo '[connections]' >> $config_file
  237. echo 'tcp = tor' >> $config_file
  238. chown -R tahoelafs:debian-tor /home/tahoelafs
  239. }
  240. function install_interactive_tahoelafs {
  241. echo -n ''
  242. APP_INSTALLED=1
  243. }
  244. function upgrade_tahoelafs {
  245. systemctl stop tahoelafs
  246. function_check set_repo_commit
  247. set_repo_commit /home/tahoelafs/tahoelafs "tahoelafs commit" "$TAHOELAFS_COMMIT" $TAHOELAFS_REPO
  248. cd /home/tahoelafs/tahoelafs
  249. git submodule update --init --recursive
  250. virtualenv venv
  251. venv/bin/pip install --editable .
  252. chown -R tahoelafs:debian-tor /home/tahoelafs
  253. systemctl start tahoelafs
  254. }
  255. function backup_local_tahoelafs {
  256. source_directory=/home/tahoelafs
  257. if [ ! -d $source_directory ]; then
  258. return
  259. fi
  260. systemctl stop tahoelafs
  261. dest_directory=tahoelafs
  262. function_check backup_directory_to_usb
  263. backup_directory_to_usb $source_directory $dest_directory
  264. systemctl start tahoelafs
  265. }
  266. function restore_local_tahoelafs {
  267. echo $"Restoring Tahoe-LAFS"
  268. systemctl stop tahoelafs-storage
  269. systemctl stop tahoelafs-client
  270. temp_restore_dir=/root/temptahoelafs
  271. restore_directory_from_usb $temp_restore_dir tahoelafs
  272. mv /home/tahoelafs /home/tahoelafs-old
  273. cp -r $temp_restore_dir/home/tahoelafs /home/tahoelafs
  274. if [ ! "$?" = "0" ]; then
  275. mv /home/tahoelafs-old /home/tahoelafs
  276. exit 246833
  277. fi
  278. rm -rf /home/tahoelafs-old
  279. chown -R tahoelafs:debian-tor /home/tahoelafs
  280. systemctl start tahoelafs-client
  281. systemctl start tahoelafs-storage
  282. echo $"Restore complete"
  283. }
  284. function backup_remote_tahoelafs {
  285. source_directory=/home/tahoelafs
  286. if [ ! -d $source_directory ]; then
  287. return
  288. fi
  289. systemctl stop tahoelafs-storage
  290. systemctl stop tahoelafs-client
  291. dest_directory=tahoelafs
  292. function_check backup_directory_to_usb
  293. backup_directory_to_friend $source_directory $dest_directory
  294. systemctl start tahoelafs-client
  295. systemctl start tahoelafs-storage
  296. }
  297. function restore_remote_tahoelafs {
  298. echo $"Restoring Tahoe-LAFS"
  299. systemctl stop tahoelafs-storage
  300. systemctl stop tahoelafs-client
  301. temp_restore_dir=/root/temptahoelafs
  302. restore_directory_from_friend $temp_restore_dir tahoelafs
  303. mv /home/tahoelafs /home/tahoelafs-old
  304. cp -r $temp_restore_dir/home/tahoelafs /home/tahoelafs
  305. if [ ! "$?" = "0" ]; then
  306. mv /home/tahoelafs-old /home/tahoelafs
  307. exit 623925
  308. fi
  309. rm -rf /home/tahoelafs-old
  310. chown -R tahoelafs:debian-tor /home/tahoelafs
  311. systemctl start tahoelafs-client
  312. systemctl start tahoelafs-storage
  313. echo $"Restore complete"
  314. }
  315. function reconfigure_tahoelafs {
  316. if [ -f $tahoelafs_storage_file ]; then
  317. shred -zu $tahoelafs_storage_file
  318. fi
  319. sed -i '/HidServAuth /d' /etc/tor/torrc
  320. }
  321. function remove_tahoelafs {
  322. if [ -f /etc/nginx/sites-available/tahoelafs ]; then
  323. nginx_dissite tahoelafs
  324. rm /etc/nginx/sites-available/tahoelafs
  325. if [ -d /var/www/tahoelafs ]; then
  326. rm -rf /var/www/tahoelafs
  327. fi
  328. systemctl reload nginx
  329. fi
  330. systemctl stop tahoelafs-storage
  331. systemctl disable tahoelafs-storage
  332. rm /etc/systemd/system/tahoelafs-storage.service
  333. systemctl stop tahoelafs-client
  334. systemctl disable tahoelafs-client
  335. rm /etc/systemd/system/tahoelafs-client.service
  336. if [ -d /var/lib/tahoelafs ]; then
  337. rm -rf /var/lib/tahoelafs
  338. fi
  339. remove_completion_param install_tahoelafs
  340. function_check remove_onion_service
  341. remove_onion_service tahoelafs ${TAHOELAFS_ONION_PORT}
  342. remove_onion_service storage-tahoelafs ${TAHOELAFS_STORAGE_ONION_PORT} $(get_tahoelafs_nick)
  343. sed -i '/HidServAuth /d' /etc/tor/torrc
  344. deluser tahoelafs
  345. if [ -d /home/tahoelafs ]; then
  346. rm -rf /home/tahoelafs
  347. fi
  348. remove_app tahoelafs
  349. if [ -f /etc/nginx/.htpasswd-tahoelafs ]; then
  350. shred -zu /etc/nginx/.htpasswd-tahoelafs
  351. fi
  352. systemctl reload tor
  353. }
  354. function install_tahoelafs_to_directory {
  355. tahoe_dir=$1
  356. git_clone $TAHOELAFS_REPO $tahoe_dir
  357. cd $tahoe_dir
  358. git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
  359. git submodule update --init --recursive
  360. virtualenv venv --distribute
  361. venv/bin/pip uninstall --yes setuptools
  362. venv/bin/pip install setuptools==11.3
  363. venv/bin/pip install six==1.10.0 packaging==16.8 attrs==16.3.0 appdirs==1.4.2 pycrypto==2.1.0 cffi==1.9.1
  364. venv/bin/pip install cryptography==1.7.2 markerlib==0.6.0 distribute==0.7.3
  365. venv/bin/pip install txtorcon==0.18.0
  366. venv/bin/pip install --editable .
  367. }
  368. function create_tahoelafs_stealth_node {
  369. node_dir="$1"
  370. client_dir="$2"
  371. node_nick="$3"
  372. client_nick="$4"
  373. if [ ${#node_dir} -eq 0 ]; then
  374. echo $'No tahoe-LAFS storage node directory given'
  375. exit 783522
  376. fi
  377. if [ ${#client_dir} -eq 0 ]; then
  378. echo $'No tahoe-LAFS client directory given'
  379. exit 368935
  380. fi
  381. if [ ${#node_nick} -eq 0 ]; then
  382. echo $'No tahoe-LAFS node nick given'
  383. exit 672351
  384. fi
  385. if [ ${#client_nick} -eq 0 ]; then
  386. echo $'No tahoe-LAFS client nick given'
  387. exit 682362
  388. fi
  389. if [ ! -f ${node_dir}/tahoe.cfg ]; then
  390. su -c "mkdir ${node_dir}" - tahoelafs
  391. su -c "$TAHOE_COMMAND create-node -C ${node_dir} --hostname=fixme" - tahoelafs
  392. tahoelafs_setup_storage_config ${node_dir}/tahoe.cfg ${node_nick}
  393. fi
  394. if [ ! -f ${client_dir}/tahoe.cfg ]; then
  395. su -c "mkdir ${client_dir}" - tahoelafs
  396. su -c "$TAHOE_COMMAND create-client -C ${client_dir}" - tahoelafs
  397. tahoelafs_setup_client_config ${client_dir}/tahoe.cfg ${client_nick}
  398. fi
  399. }
  400. function create_tahoelafs_introducer {
  401. introducer_dir="$1"
  402. if [ -f ${introducer_dir}/tahoe.cfg ]; then
  403. return
  404. fi
  405. su -c "mkdir ${introducer_dir}" - tahoelafs
  406. su -c "$TAHOE_COMMAND create-introducer -C ${introducer_dir} --hide-ip --hostname=127.0.0.1" - tahoelafs
  407. }
  408. function create_tahoelafs_storage_node {
  409. # Nodes can store data
  410. node_dir="$1"
  411. furl="$2"
  412. if [ ${#furl} -eq 0 ]; then
  413. return
  414. fi
  415. if [ -f ${node_dir}/tahoe.cfg ]; then
  416. return
  417. fi
  418. su -c "mkdir ${node_dir}" - tahoelafs
  419. su -c "$TAHOE_COMMAND create-node -C ${node_dir} --introducer=\"$furl\" --listen=tor --hide-ip" - tahoelafs
  420. }
  421. function create_tahoelafs_client {
  422. # Clients have no storage
  423. client_dir="$1"
  424. furl="$2"
  425. if [ ${#furl} -eq 0 ]; then
  426. return
  427. fi
  428. if [ -f ${client_dir}/tahoe.cfg ]; then
  429. return
  430. fi
  431. su -c "mkdir ${client_dir}" - tahoelafs
  432. su -c "$TAHOE_COMMAND create-client -C ${client_dir} --introducer=\"$furl\" --listen=tor --hide-ip --hostname=127.0.0.1" - tahoelafs
  433. sed -i 's|reveal-IP-address =.*|reveal-IP-address = False|g' $client_dir/tahoe.cfg
  434. sed -i 's|tub.port =.*|tub.port = disabled|g' $client_dir/tahoe.cfg
  435. sed -i 's|tub.location =.*|tub.location = disabled|g' $client_dir/tahoe.cfg
  436. }
  437. function get_tahoelafs_furl {
  438. furl=$(cat /home/tahoelafs/storage/private/storage.furl)
  439. furl_1=$(echo "${furl}" | awk -F ' ' '{print $1}')
  440. furl_2=$(echo "${furl}" | awk -F ':' '{print $5}')
  441. echo "${furl_1}:${furl_2}"
  442. }
  443. function get_tahoelafs_nick {
  444. echo "${MY_USERNAME}-node"
  445. }
  446. function get_tahoelafs_storage_hostname {
  447. echo "$(cat /var/lib/tor/hidden_service_storage-tahoelafs/hostname)"
  448. }
  449. function get_tahoelafs_public_key {
  450. echo "$(cat /home/tahoelafs/storage/node.pubkey | grep 'v0-' | sed 's|pub-||g')"
  451. }
  452. function add_tahoelafs_server {
  453. storage_hostname="$1"
  454. public_key="$2"
  455. nick="$3"
  456. furl="$4"
  457. if [ ${#storage_hostname} -eq 0 ]; then
  458. echo $'No storage hostname'
  459. return
  460. fi
  461. if [ ${#public_key} -eq 0 ]; then
  462. echo $'No public key'
  463. return
  464. fi
  465. if [ ${#nick} -eq 0 ]; then
  466. echo $'No nick'
  467. return
  468. fi
  469. if [ ${#furl} -eq 0 ]; then
  470. echo $'No furl'
  471. return
  472. fi
  473. if [ ! -f ${tahoelafs_storage_file} ]; then
  474. echo 'storage:' > ${tahoelafs_storage_file}
  475. else
  476. if grep -q "${public_key}" ${tahoelafs_storage_file}; then
  477. echo $'Public key already exists'
  478. return
  479. fi
  480. echo '# storage' >> ${tahoelafs_storage_file}
  481. fi
  482. echo " ${public_key}:" >> ${tahoelafs_storage_file}
  483. echo " ann:" >> ${tahoelafs_storage_file}
  484. echo " nickname: ${nick}" >> ${tahoelafs_storage_file}
  485. echo " anonymous-storage-FURL: ${furl}" >> ${tahoelafs_storage_file}
  486. chown tahoelafs:debian-tor ${tahoelafs_storage_file}
  487. if ! grep -q "HidServAuth ${storage_hostname}" /etc/tor/torrc; then
  488. echo "HidServAuth ${storage_hostname}" >> /etc/tor/torrc
  489. fi
  490. }
  491. function create_tahoelafs_daemon {
  492. daemon_name=$1
  493. TAHOELAFS_DAEMON_FILE=/etc/systemd/system/tahoelafs-${daemon_name}.service
  494. echo "Creating daemon: $TAHOELAFS_DAEMON_FILE"
  495. echo '[Unit]' > $TAHOELAFS_DAEMON_FILE
  496. echo "Description=Tahoe-LAFS ${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
  497. echo 'After=syslog.target' >> $TAHOELAFS_DAEMON_FILE
  498. echo 'After=network.target' >> $TAHOELAFS_DAEMON_FILE
  499. echo '' >> $TAHOELAFS_DAEMON_FILE
  500. echo '[Service]' >> $TAHOELAFS_DAEMON_FILE
  501. echo 'Type=simple' >> $TAHOELAFS_DAEMON_FILE
  502. echo "User=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  503. echo "Group=debian-tor" >> $TAHOELAFS_DAEMON_FILE
  504. echo "WorkingDirectory=/home/tahoelafs/tahoelafs" >> $TAHOELAFS_DAEMON_FILE
  505. echo "ExecStart=/home/tahoelafs/tahoelafs/venv/bin/tahoe run /home/tahoelafs/${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
  506. echo "ExecStop=/home/tahoelafs/tahoelafs/venv/bin/tahoe stop /home/tahoelafs/${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
  507. echo 'Restart=on-failure' >> $TAHOELAFS_DAEMON_FILE
  508. echo 'RestartSec=10' >> $TAHOELAFS_DAEMON_FILE
  509. echo "Environment=\"USER=tahoelafs\" \"HOME=/home/tahoelafs\"" >> $TAHOELAFS_DAEMON_FILE
  510. echo '' >> $TAHOELAFS_DAEMON_FILE
  511. echo '[Install]' >> $TAHOELAFS_DAEMON_FILE
  512. echo 'WantedBy=multi-user.target' >> $TAHOELAFS_DAEMON_FILE
  513. systemctl enable tahoelafs-${daemon_name}
  514. systemctl daemon-reload
  515. systemctl start tahoelafs-${daemon_name}
  516. }
  517. function create_tahoelafs_web {
  518. if [ ! -d /var/www/tahoelafs/htdocs ]; then
  519. mkdir -p /var/www/tahoelafs/htdocs
  520. fi
  521. TAHOELAFS_LOGIN_TEXT=$'Tahoe-LAFS login'
  522. tahoelafs_nginx_site=/etc/nginx/sites-available/tahoelafs
  523. echo 'server {' > $tahoelafs_nginx_site
  524. echo " listen 127.0.0.1:$TAHOELAFS_ONION_PORT default_server;" >> $tahoelafs_nginx_site
  525. echo " server_name $TAHOELAFS_ONION_HOSTNAME;" >> $tahoelafs_nginx_site
  526. echo '' >> $tahoelafs_nginx_site
  527. function_check nginx_disable_sniffing
  528. nginx_disable_sniffing tahoelafs
  529. echo '' >> $tahoelafs_nginx_site
  530. echo ' # Logs' >> $tahoelafs_nginx_site
  531. echo ' access_log /dev/null;' >> $tahoelafs_nginx_site
  532. echo ' error_log /dev/null;' >> $tahoelafs_nginx_site
  533. echo '' >> $tahoelafs_nginx_site
  534. echo ' # Root' >> $tahoelafs_nginx_site
  535. echo " root /var/www/tahoelafs/htdocs;" >> $tahoelafs_nginx_site
  536. echo '' >> $tahoelafs_nginx_site
  537. echo ' location / {' >> $tahoelafs_nginx_site
  538. echo " auth_basic \"${TAHOELAFS_LOGIN_TEXT}\";" >> $tahoelafs_nginx_site
  539. echo ' auth_basic_user_file /etc/nginx/.htpasswd-tahoelafs;' >> $tahoelafs_nginx_site
  540. function_check nginx_limits
  541. nginx_limits tahoelafs '15m'
  542. echo ' rewrite /(.*) /$1 break;' >> $tahoelafs_nginx_site
  543. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $tahoelafs_nginx_site
  544. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $tahoelafs_nginx_site
  545. echo ' proxy_set_header Host $http_host;' >> $tahoelafs_nginx_site
  546. echo ' proxy_set_header X-NginX-Proxy true;' >> $tahoelafs_nginx_site
  547. echo " proxy_pass http://localhost:${TAHOELAFS_PORT};" >> $tahoelafs_nginx_site
  548. echo ' proxy_redirect off;' >> $tahoelafs_nginx_site
  549. echo ' }' >> $tahoelafs_nginx_site
  550. echo '}' >> $tahoelafs_nginx_site
  551. TAHOELAFS_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  552. ${PROJECT_NAME}-pass -u $MY_USERNAME -a tahoelafs -p "$TAHOELAFS_ADMIN_PASSWORD"
  553. if [ ! -f /etc/nginx/.htpasswd-tahoelafs ]; then
  554. touch /etc/nginx/.htpasswd-tahoelafs
  555. fi
  556. if grep "${MY_USERNAME}:" /etc/nginx/.htpasswd-tahoelafs; then
  557. sed -i '/${MY_USERNAME}:/d' /etc/nginx/.htpasswd-tahoelafs
  558. fi
  559. echo "${TAHOELAFS_ADMIN_PASSWORD}" | htpasswd -i -s /etc/nginx/.htpasswd-tahoelafs ${MY_USERNAME}
  560. function_check nginx_ensite
  561. nginx_ensite tahoelafs
  562. systemctl reload nginx
  563. }
  564. function install_tahoelafs {
  565. if [ $INSTALLING_MESH ]; then
  566. return
  567. fi
  568. apt-get -yq install build-essential python-pip python-dev libffi-dev libssl-dev
  569. apt-get -yq install libcrypto++-dev python-pycryptopp python-cffi python-virtualenv
  570. # create a user
  571. if [ ! -d /home/tahoelafs ]; then
  572. # add a gogs user account
  573. adduser --disabled-login --gecos 'tahoe-lafs' tahoelafs
  574. adduser tahoelafs debian-tor
  575. fi
  576. if [ -d /home/tahoelafs/Maildir ]; then
  577. rm -rf /home/tahoelafs/Maildir
  578. fi
  579. install_tahoelafs_to_directory /home/tahoelafs/tahoelafs
  580. # remove files we don't need
  581. rm -rf /home/tahoelafs/.mutt
  582. rm /home/tahoelafs/.emacs-mutt
  583. rm /home/tahoelafs/.muttrc
  584. rm /home/tahoelafs/.mutt-alias
  585. rm /home/tahoelafs/.procmailrc
  586. # set permissions
  587. chown -R tahoelafs:debian-tor /home/tahoelafs
  588. node_nick=$(get_tahoelafs_nick)
  589. client_nick=${MY_USERNAME}-client
  590. # create an onion address for storage node
  591. TAHOELAFS_STORAGE_ONION_HOSTNAME=$(add_onion_service storage-tahoelafs ${TAHOELAFS_STORAGE_PORT} ${TAHOELAFS_STORAGE_ONION_PORT} ${node_nick})
  592. # create an onion address for client node
  593. TAHOELAFS_ONION_HOSTNAME=$(add_onion_service tahoelafs 80 ${TAHOELAFS_ONION_PORT})
  594. create_tahoelafs_stealth_node /home/tahoelafs/storage /home/tahoelafs/client ${node_nick} ${client_nick}
  595. # start the storage node
  596. su -c '/home/tahoelafs/tahoelafs/venv/bin/python2 /home/tahoelafs/tahoelafs/venv/bin/tahoe start /home/tahoelafs/storage' - tahoelafs
  597. create_tahoelafs_daemon "storage"
  598. # start the client
  599. su -c '/home/tahoelafs/tahoelafs/venv/bin/python2 /home/tahoelafs/tahoelafs/venv/bin/tahoe start /home/tahoelafs/client' - tahoelafs
  600. add_tahoelafs_server "$(get_tahoelafs_storage_hostname)" "$(get_tahoelafs_public_key)" "${node_nick}" "$(get_tahoelafs_furl)"
  601. if ! grep -q "HidServAuth $(get_tahoelafs_storage_hostname)" /etc/tor/torrc; then
  602. echo $'Unable to create tahoelafs server'
  603. exit 738752
  604. fi
  605. if [ ! -f ${tahoelafs_storage_file} ]; then
  606. echo $'tahoelafs server file missing'
  607. exit 529362
  608. fi
  609. create_tahoelafs_daemon "client"
  610. set_completion_param "tahoelafs commit" "$TAHOELAFS_COMMIT"
  611. set_completion_param "tahoelafs onion domain" "$TAHOELAFS_ONION_HOSTNAME"
  612. create_tahoelafs_web
  613. systemctl restart tor
  614. APP_INSTALLED=1
  615. }
  616. # NOTE: deliberately no exit 0