freedombone-app-tahoelafs 22KB

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