freedombone-app-tahoelafs 24KB

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