freedombone-app-tahoelafs 24KB

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