| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702 | 
							- #!/bin/bash
 - #
 - # .---.                  .              .
 - # |                      |              |
 - # |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
 - # |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
 - # '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
 - #
 - #                    Freedom in the Cloud
 - #
 - # Tahow-LAFS data storage grid implemented via Tor
 - # https://k0rx.com/blog/2017/01/lafs.html
 - # http://tahoe-lafs.readthedocs.io/en/latest/anonymity-configuration.html
 - #
 - # License
 - # =======
 - #
 - # Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
 - #
 - # This program is free software: you can redistribute it and/or modify
 - # it under the terms of the GNU Affero General Public License as published by
 - # the Free Software Foundation, either version 3 of the License, or
 - # (at your option) any later version.
 - #
 - # This program is distributed in the hope that it will be useful,
 - # but WITHOUT ANY WARRANTY; without even the implied warranty of
 - # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 - # GNU Affero General Public License for more details.
 - #
 - # You should have received a copy of the GNU Affero General Public License
 - # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 - 
 - VARIANTS='full full-vim cloud'
 - 
 - IN_DEFAULT_INSTALL=0
 - SHOW_ON_ABOUT=1
 - 
 - TAHOELAFS_REPO="https://github.com/tahoe-lafs/tahoe-lafs"
 - TAHOELAFS_COMMIT='bb782b0331a60de438136a593bba18338d8d866b'
 - 
 - TAHOELAFS_PORT=50213
 - TAHOELAFS_STORAGE_PORT=50214
 - TAHOELAFS_ONION_PORT=8096
 - TAHOELAFS_STORAGE_ONION_PORT=8097
 - 
 - TAHOE_COMMAND="cd /home/tahoelafs/tahoelafs && venv/bin/tahoe"
 - tahoelafs_storage_file=/home/tahoelafs/client/private/servers.yaml
 - 
 - TAHOELAFS_SHARES_NEEDED=3
 - TAHOELAFS_SHARES_HAPPY=7
 - TAHOELAFS_SHARES_TOTAL=10
 - 
 - tahoelafs_variables=(ONION_ONLY
 -                      MY_USERNAME
 -                      TAHOELAFS_REPO
 -                      TAHOELAFS_PORT
 -                      TAHOELAFS_SHARES_NEEDED
 -                      TAHOELAFS_SHARES_HAPPY
 -                      TAHOELAFS_SHARES_TOTAL)
 - 
 - function add_user_tahoelafs {
 -     if [[ $(app_is_installed tahoelafs) == "0" ]]; then
 -         echo '0'
 -         return
 -     fi
 - 
 -     new_username="$1"
 -     new_user_password="$2"
 -     ${PROJECT_NAME}-pass -u $new_username -a tahoelafs -p "$new_user_password"
 -     if grep "${new_username}:" /etc/nginx/.htpasswd-tahoelafs; then
 -         sed -i '/${new_username}:/d' /etc/nginx/.htpasswd-tahoelafs
 -     fi
 -     echo "${new_user_password}" | htpasswd -i -s /etc/nginx/.htpasswd-tahoelafs ${new_username}
 -     echo '0'
 - }
 - 
 - function remove_user_tahoelafs {
 -     remove_username="$1"
 -     ${PROJECT_NAME}-pass -u $remove_username --rmapp tahoelafs
 -     if grep "${remove_username}:" /etc/nginx/.htpasswd-tahoelafs; then
 -         sed -i '/${remove_username}:/d' /etc/nginx/.htpasswd-tahoelafs
 -     fi
 - }
 - 
 - function change_password_tahoelafs {
 -     change_username="$1"
 -     change_password="$2"
 -     ${PROJECT_NAME}-pass -u $change_username -a tahoelafs -p "$change_password"
 -     if grep "${change_username}:" /etc/nginx/.htpasswd-tahoelafs; then
 -         sed -i '/tahoe-${change_username}:/d' /etc/nginx/.htpasswd-tahoelafs
 -     fi
 -     echo "${change_password}" | htpasswd -i -s /etc/nginx/.htpasswd-tahoelafs ${change_username}
 - }
 - 
 - function add_tahoelafs_storage_node_interactive {
 -     data=$(tempfile 2>/dev/null)
 -     trap "rm -f $data" 0 1 2 5 15
 -     dialog --backtitle $"Freedombone Configuration" \
 -            --title $"Add Tahoe-LAFS storage node" \
 -            --form $"\nEnter the storage node details which can be found on the About screen of another server" 13 75 5 \
 -            $"Hostname:" 1 1 "" 1 14 53 40 \
 -            $"Public Key:" 2 1 "" 2 14 53 255 \
 -            $"Nickname:" 3 1 "" 3 14 53 255 \
 -            $"FURL:" 4 1 "" 4 14 53 255 \
 -            2> $data
 -     sel=$?
 -     case $sel in
 -         1) return;;
 -         255) return;;
 -     esac
 -     storage_hostname=$(cat $data | sed -n 1p)
 -     public_key="$(cat $data | sed -n 2p)"
 -     nick=$(cat $data | sed -n 3p)
 -     furl=$(cat $data | sed -n 4p)
 - 
 -     if [ ${#public_key} -eq 0 ]; then
 -         return
 -     fi
 - 
 -     add_tahoelafs_server "${storage_hostname}" "${public_key}" "${nick}" "${furl}"
 - 
 -     if grep -q "$public_key" ${tahoelafs_storage_file}; then
 -         dialog --title $"Add Tahoe-LAFS storage node" \
 -                --msgbox $"Storage node added" 6 40
 -     fi
 - }
 - 
 - function edit_tahoelafs_nodes {
 -     editor $tahoelafs_storage_file
 -     chown tahoelafs:debian-tor $tahoelafs_storage_file
 -     systemctl restart tahoelafs-client
 - }
 - 
 - function edit_tahoelafs_shares {
 -     read_config_param TAHOELAFS_SHARES_NEEDED
 -     read_config_param TAHOELAFS_SHARES_HAPPY
 -     read_config_param TAHOELAFS_SHARES_TOTAL
 - 
 -     data=$(tempfile 2>/dev/null)
 -     trap "rm -f $data" 0 1 2 5 15
 -     dialog --backtitle $"Freedombone Configuration" \
 -            --title $"Tahoe-LAFS shares" \
 -            --form $"\nEnter the storage node details which can be found on the About screen of another server" 13 40 3 \
 -            $"Needed:" 1 1 "${TAHOELAFS_SHARES_NEEDED}" 1 14 4 4 \
 -            $"Happy:" 2 1 "${TAHOELAFS_SHARES_HAPPY}" 2 14 4 4 \
 -            $"Total:" 3 1 "${TAHOELAFS_SHARES_TOTAL}" 3 14 4 4 \
 -            2> $data
 -     sel=$?
 -     case $sel in
 -         1) return;;
 -         255) return;;
 -     esac
 -     tl_needed="$(cat $data | sed -n 1p)"
 -     tl_happy="$(cat $data | sed -n 2p)"
 -     tl_total="$(cat $data | sed -n 3p)"
 -     if [ ${#tl_needed} -gt 0 ]; then
 -         TAHOELAFS_SHARES_NEEDED=${tl_needed}
 -     fi
 -     if [ ${#tl_happy} -gt 0 ]; then
 -         TAHOELAFS_SHARES_HAPPY=${tl_happy}
 -     fi
 -     if [ ${#tl_total} -gt 0 ]; then
 -         TAHOELAFS_SHARES_TOTAL=${tl_total}
 -     fi
 - 
 -     sed -i "s|shares.needed.*|shares.needed = ${TAHOELAFS_SHARES_NEEDED}|g" /home/tahoelafs/tahoelafs/client/tahoe.cfg
 -     sed -i "s|shares.happy.*|shares.happy = ${TAHOELAFS_SHARES_HAPPY}|g" /home/tahoelafs/tahoelafs/client/tahoe.cfg
 -     sed -i "s|shares.total.*|shares.total = ${TAHOELAFS_SHARES_TOTAL}|g" /home/tahoelafs/tahoelafs/client/tahoe.cfg
 - 
 -     sed -i "s|shares.needed.*|shares.needed = ${TAHOELAFS_SHARES_NEEDED}|g" /home/tahoelafs/tahoelafs/storage/tahoe.cfg
 -     sed -i "s|shares.happy.*|shares.happy = ${TAHOELAFS_SHARES_HAPPY}|g" /home/tahoelafs/tahoelafs/storage/tahoe.cfg
 -     sed -i "s|shares.total.*|shares.total = ${TAHOELAFS_SHARES_TOTAL}|g" /home/tahoelafs/tahoelafs/storage/tahoe.cfg
 - 
 -     systemctl restart tahoelafs-storage
 -     systemctl restart tahoelafs-client
 - 
 -     dialog --title $"Tahoe-LAFS shares" \
 -            --msgbox $"Shares settings changed" 6 40
 - }
 - 
 - function configure_interactive_tahoelafs {
 -     data=$(tempfile 2>/dev/null)
 -     trap "rm -f $data" 0 1 2 5 15
 -     dialog --backtitle $"Freedombone Configuration" \
 -            --title $"Tahoe-LAFS" \
 -            --radiolist $"The least authority is always the best" 11 50 5 \
 -            1 "Add a storage node" off \
 -            2 "Manually edit storage nodes" off \
 -            3 "Shares settings" off \
 -            4 "Back to main menu" on 2> $data
 -     sel=$?
 -     case $sel in
 -         1) exit 1;;
 -         255) exit 1;;
 -     esac
 -     case $(cat $data) in
 -         1) add_tahoelafs_storage_node_interactive;;
 -         2) edit_tahoelafs_nodes;;
 -         3) edit_tahoelafs_shares;;
 -     esac
 - }
 - 
 - function tahoelafs_setup_client_config {
 -     config_file=$1
 -     nick="$2"
 - 
 -     echo '[node]' > $config_file
 -     echo "nickname = $nick" >> $config_file
 -     echo 'reveal-IP-address = false' >> $config_file
 -     echo "web.port = tcp:${TAHOELAFS_PORT}:interface=127.0.0.1" >> $config_file
 -     echo 'web.static = public_html' >> $config_file
 -     echo 'tub.port = disabled' >> $config_file
 -     echo 'tub.location = disabled' >> $config_file
 -     echo '' >> $config_file
 -     echo '[client]' >> $config_file
 -     echo 'introducer.furl =' >> $config_file
 -     echo "shares.needed = ${TAHOELAFS_SHARES_NEEDED}" >> $config_file
 -     echo "shares.happy = ${TAHOELAFS_SHARES_HAPPY}" >> $config_file
 -     echo "shares.total = ${TAHOELAFS_SHARES_TOTAL}" >> $config_file
 -     echo '' >> $config_file
 -     echo '[storage]' >> $config_file
 -     echo 'enabled = false' >> $config_file
 -     echo 'reserved_space = 3G' >> $config_file
 -     echo '' >> $config_file
 -     echo '[helper]' >> $config_file
 -     echo 'enabled = false' >> $config_file
 -     echo '' >> $config_file
 -     echo '[connections]' >> $config_file
 -     echo 'tcp = tor' >> $config_file
 - }
 - 
 - function tahoelafs_setup_storage_config {
 -     config_file=$1
 -     nick="$2"
 - 
 -     echo '[node]' > $config_file
 -     echo "nickname = $nick" >> $config_file
 -     echo 'reveal-IP-address = false' >> $config_file
 -     echo 'web.port =' >> $config_file
 -     echo 'web.static = public_html' >> $config_file
 -     echo "tub.port = tcp:${TAHOELAFS_STORAGE_ONION_PORT}:interface=127.0.0.1" >> $config_file
 -     echo "tub.location = tor:${TAHOELAFS_STORAGE_ONION_HOSTNAME}:${TAHOELAFS_STORAGE_PORT}" >> $config_file
 -     echo '' >> $config_file
 -     echo '[client]' >> $config_file
 -     echo 'introducer.furl =' >> $config_file
 -     echo 'helper.furl =' >> $config_file
 -     echo '' >> $config_file
 -     echo "shares.needed = ${TAHOELAFS_SHARES_NEEDED}" >> $config_file
 -     echo "shares.happy = ${TAHOELAFS_SHARES_HAPPY}" >> $config_file
 -     echo "shares.total = ${TAHOELAFS_SHARES_TOTAL}" >> $config_file
 -     echo '' >> $config_file
 -     echo '[storage]' >> $config_file
 -     echo 'enabled = true' >> $config_file
 -     echo 'reserved_space = 3G' >> $config_file
 -     echo 'expire.enabled = true' >> $config_file
 -     echo 'expire.mode = age' >> $config_file
 -     echo 'expire.override_lease_duration = 3 months' >> $config_file
 -     echo '' >> $config_file
 -     echo '[helper]' >> $config_file
 -     echo 'enabled = false' >> $config_file
 -     echo '' >> $config_file
 -     echo '[connections]' >> $config_file
 -     echo 'tcp = tor' >> $config_file
 - 
 -     chown -R tahoelafs:debian-tor /home/tahoelafs
 - }
 - 
 - function install_interactive_tahoelafs {
 -     echo -n ''
 -     APP_INSTALLED=1
 - }
 - 
 - function upgrade_tahoelafs {
 -     systemctl stop tahoelafs
 -     function_check set_repo_commit
 -     set_repo_commit /home/tahoelafs/tahoelafs "tahoelafs commit" "$TAHOELAFS_COMMIT" $TAHOELAFS_REPO
 -     cd /home/tahoelafs/tahoelafs
 -     git submodule update --init --recursive
 -     virtualenv venv
 -     venv/bin/pip install --editable .
 -     chown -R tahoelafs:debian-tor /home/tahoelafs
 -     systemctl start tahoelafs
 - }
 - 
 - function backup_local_tahoelafs {
 -     source_directory=/home/tahoelafs
 -     if [ ! -d $source_directory ]; then
 -         return
 -     fi
 -     systemctl stop tahoelafs
 -     dest_directory=tahoelafs
 -     function_check backup_directory_to_usb
 -     backup_directory_to_usb $source_directory $dest_directory
 -     systemctl start tahoelafs
 - }
 - 
 - function restore_local_tahoelafs {
 -     echo $"Restoring Tahoe-LAFS"
 -     systemctl stop tahoelafs-storage
 -     systemctl stop tahoelafs-client
 -     temp_restore_dir=/root/temptahoelafs
 -     restore_directory_from_usb $temp_restore_dir tahoelafs
 -     mv /home/tahoelafs /home/tahoelafs-old
 -     cp -r $temp_restore_dir/home/tahoelafs /home/tahoelafs
 -     if [ ! "$?" = "0" ]; then
 -         mv /home/tahoelafs-old /home/tahoelafs
 -         exit 246833
 -     fi
 -     rm -rf /home/tahoelafs-old
 -     chown -R tahoelafs:debian-tor /home/tahoelafs
 -     systemctl start tahoelafs-client
 -     systemctl start tahoelafs-storage
 -     echo $"Restore complete"
 - }
 - 
 - function backup_remote_tahoelafs {
 -     source_directory=/home/tahoelafs
 -     if [ ! -d $source_directory ]; then
 -         return
 -     fi
 -     systemctl stop tahoelafs-storage
 -     systemctl stop tahoelafs-client
 -     dest_directory=tahoelafs
 -     function_check backup_directory_to_usb
 -     backup_directory_to_friend $source_directory $dest_directory
 -     systemctl start tahoelafs-client
 -     systemctl start tahoelafs-storage
 - }
 - 
 - function restore_remote_tahoelafs {
 -     echo $"Restoring Tahoe-LAFS"
 -     systemctl stop tahoelafs-storage
 -     systemctl stop tahoelafs-client
 -     temp_restore_dir=/root/temptahoelafs
 -     restore_directory_from_friend $temp_restore_dir tahoelafs
 -     mv /home/tahoelafs /home/tahoelafs-old
 -     cp -r $temp_restore_dir/home/tahoelafs /home/tahoelafs
 -     if [ ! "$?" = "0" ]; then
 -         mv /home/tahoelafs-old /home/tahoelafs
 -         exit 623925
 -     fi
 -     rm -rf /home/tahoelafs-old
 -     chown -R tahoelafs:debian-tor /home/tahoelafs
 -     systemctl start tahoelafs-client
 -     systemctl start tahoelafs-storage
 -     echo $"Restore complete"
 - }
 - 
 - function reconfigure_tahoelafs {
 -     if [ -f $tahoelafs_storage_file ]; then
 -         shred -zu $tahoelafs_storage_file
 -     fi
 -     sed -i '/HidServAuth /d' /etc/tor/torrc
 - }
 - 
 - function remove_tahoelafs {
 -     if [ -f /etc/nginx/sites-available/tahoelafs ]; then
 -         nginx_dissite tahoelafs
 -         rm /etc/nginx/sites-available/tahoelafs
 -         if [ -d /var/www/tahoelafs ]; then
 -             rm -rf /var/www/tahoelafs
 -         fi
 -         systemctl reload nginx
 -     fi
 - 
 -     systemctl stop tahoelafs-storage
 -     systemctl disable tahoelafs-storage
 -     rm /etc/systemd/system/tahoelafs-storage.service
 - 
 -     systemctl stop tahoelafs-client
 -     systemctl disable tahoelafs-client
 -     rm /etc/systemd/system/tahoelafs-client.service
 - 
 -     if [ -d /var/lib/tahoelafs ]; then
 -         rm -rf /var/lib/tahoelafs
 -     fi
 -     remove_completion_param install_tahoelafs
 -     function_check remove_onion_service
 -     remove_onion_service tahoelafs ${TAHOELAFS_ONION_PORT}
 -     remove_onion_service storage-tahoelafs ${TAHOELAFS_STORAGE_ONION_PORT} $(get_tahoelafs_nick)
 -     sed -i '/HidServAuth /d' /etc/tor/torrc
 -     deluser tahoelafs
 -     if [ -d /home/tahoelafs ]; then
 -         rm -rf /home/tahoelafs
 -     fi
 -     remove_app tahoelafs
 -     if [ -f /etc/nginx/.htpasswd-tahoelafs ]; then
 -         shred -zu /etc/nginx/.htpasswd-tahoelafs
 -     fi
 -     systemctl reload tor
 - }
 - 
 - function install_tahoelafs_to_directory {
 -     tahoe_dir=$1
 - 
 -     git_clone $TAHOELAFS_REPO $tahoe_dir
 -     cd $tahoe_dir
 -     git checkout $TAHOELAFS_COMMIT -b $TAHOELAFS_COMMIT
 -     git submodule update --init --recursive
 -     virtualenv venv --distribute
 -     venv/bin/pip uninstall --yes setuptools
 -     venv/bin/pip install setuptools==11.3
 -     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
 -     venv/bin/pip install cryptography==1.7.2 markerlib==0.6.0 distribute==0.7.3
 -     venv/bin/pip install txtorcon==0.18.0
 -     venv/bin/pip install --editable .
 - }
 - 
 - function create_tahoelafs_stealth_node {
 -     node_dir="$1"
 -     client_dir="$2"
 -     node_nick="$3"
 -     client_nick="$4"
 - 
 -     if [ ${#node_dir} -eq 0 ]; then
 -         echo $'No tahoe-LAFS storage node directory given'
 -         exit 783522
 -     fi
 -     if [ ${#client_dir} -eq 0 ]; then
 -         echo $'No tahoe-LAFS client directory given'
 -         exit 368935
 -     fi
 -     if [ ${#node_nick} -eq 0 ]; then
 -         echo $'No tahoe-LAFS node nick given'
 -         exit 672351
 -     fi
 -     if [ ${#client_nick} -eq 0 ]; then
 -         echo $'No tahoe-LAFS client nick given'
 -         exit 682362
 -     fi
 - 
 -     if [ ! -f ${node_dir}/tahoe.cfg ]; then
 -         su -c "mkdir ${node_dir}" - tahoelafs
 -         su -c "$TAHOE_COMMAND create-node -C ${node_dir} --hostname=fixme" - tahoelafs
 -         tahoelafs_setup_storage_config ${node_dir}/tahoe.cfg ${node_nick}
 -     fi
 - 
 -     if [ ! -f ${client_dir}/tahoe.cfg ]; then
 -         su -c "mkdir ${client_dir}" - tahoelafs
 -         su -c "$TAHOE_COMMAND create-client -C ${client_dir}" - tahoelafs
 -         tahoelafs_setup_client_config ${client_dir}/tahoe.cfg ${client_nick}
 -     fi
 - }
 - 
 - function create_tahoelafs_introducer {
 -     introducer_dir="$1"
 - 
 -     if [ -f ${introducer_dir}/tahoe.cfg ]; then
 -         return
 -     fi
 - 
 -     su -c "mkdir ${introducer_dir}" - tahoelafs
 -     su -c "$TAHOE_COMMAND create-introducer -C ${introducer_dir} --hide-ip --hostname=127.0.0.1" - tahoelafs
 - }
 - 
 - function create_tahoelafs_storage_node {
 -     # Nodes can store data
 -     node_dir="$1"
 -     furl="$2"
 - 
 -     if [ ${#furl} -eq 0 ]; then
 -         return
 -     fi
 - 
 -     if [ -f ${node_dir}/tahoe.cfg ]; then
 -         return
 -     fi
 - 
 -     su -c "mkdir ${node_dir}" - tahoelafs
 -     su -c "$TAHOE_COMMAND create-node -C ${node_dir} --introducer=\"$furl\" --listen=tor --hide-ip" - tahoelafs
 - }
 - 
 - function create_tahoelafs_client {
 -     # Clients have no storage
 -     client_dir="$1"
 -     furl="$2"
 - 
 -     if [ ${#furl} -eq 0 ]; then
 -         return
 -     fi
 - 
 -     if [ -f ${client_dir}/tahoe.cfg ]; then
 -         return
 -     fi
 - 
 -     su -c "mkdir ${client_dir}" - tahoelafs
 -     su -c "$TAHOE_COMMAND create-client -C ${client_dir} --introducer=\"$furl\" --listen=tor --hide-ip --hostname=127.0.0.1" - tahoelafs
 -     sed -i 's|reveal-IP-address =.*|reveal-IP-address = False|g' $client_dir/tahoe.cfg
 -     sed -i 's|tub.port =.*|tub.port = disabled|g' $client_dir/tahoe.cfg
 -     sed -i 's|tub.location =.*|tub.location = disabled|g' $client_dir/tahoe.cfg
 - }
 - 
 - function get_tahoelafs_furl {
 -     furl=$(cat /home/tahoelafs/storage/private/storage.furl)
 -     furl_1=$(echo "${furl}" | awk -F ' ' '{print $1}')
 -     furl_2=$(echo "${furl}" | awk -F ':' '{print $5}')
 -     echo "${furl_1}:${furl_2}"
 - }
 - 
 - function get_tahoelafs_nick {
 -     echo "${MY_USERNAME}-node"
 - }
 - 
 - function get_tahoelafs_storage_hostname {
 -     echo "$(cat /var/lib/tor/hidden_service_storage-tahoelafs/hostname)"
 - }
 - 
 - function get_tahoelafs_public_key {
 -     echo "$(cat /home/tahoelafs/storage/node.pubkey | grep 'v0-' | sed 's|pub-||g')"
 - }
 - 
 - function add_tahoelafs_server {
 -     storage_hostname="$1"
 -     public_key="$2"
 -     nick="$3"
 -     furl="$4"
 - 
 -     if [ ${#storage_hostname} -eq 0 ]; then
 -         echo $'No storage hostname'
 -         return
 -     fi
 -     if [ ${#public_key} -eq 0 ]; then
 -         echo $'No public key'
 -         return
 -     fi
 -     if [ ${#nick} -eq 0 ]; then
 -         echo $'No nick'
 -         return
 -     fi
 -     if [ ${#furl} -eq 0 ]; then
 -         echo $'No furl'
 -         return
 -     fi
 - 
 -     if [ ! -f ${tahoelafs_storage_file} ]; then
 -         echo 'storage:' > ${tahoelafs_storage_file}
 -     else
 -         if grep -q "${public_key}" ${tahoelafs_storage_file}; then
 -             echo $'Public key already exists'
 -             return
 -         fi
 -         echo '# storage' >> ${tahoelafs_storage_file}
 -     fi
 -     echo "  ${public_key}:" >> ${tahoelafs_storage_file}
 -     echo "    ann:" >> ${tahoelafs_storage_file}
 -     echo "      nickname: ${nick}" >> ${tahoelafs_storage_file}
 -     echo "      anonymous-storage-FURL: ${furl}" >> ${tahoelafs_storage_file}
 -     chown tahoelafs:debian-tor ${tahoelafs_storage_file}
 - 
 -     if ! grep -q "HidServAuth ${storage_hostname}" /etc/tor/torrc; then
 -         echo "HidServAuth ${storage_hostname}" >> /etc/tor/torrc
 -     fi
 - }
 - 
 - function create_tahoelafs_daemon {
 -     daemon_name=$1
 - 
 -     TAHOELAFS_DAEMON_FILE=/etc/systemd/system/tahoelafs-${daemon_name}.service
 -     echo "Creating daemon: $TAHOELAFS_DAEMON_FILE"
 - 
 -     echo '[Unit]' > $TAHOELAFS_DAEMON_FILE
 -     echo "Description=Tahoe-LAFS ${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
 -     echo 'After=syslog.target' >> $TAHOELAFS_DAEMON_FILE
 -     echo 'After=network.target' >> $TAHOELAFS_DAEMON_FILE
 -     echo '' >> $TAHOELAFS_DAEMON_FILE
 -     echo '[Service]' >> $TAHOELAFS_DAEMON_FILE
 -     echo 'Type=simple' >> $TAHOELAFS_DAEMON_FILE
 -     echo "User=tahoelafs" >> $TAHOELAFS_DAEMON_FILE
 -     echo "Group=debian-tor" >> $TAHOELAFS_DAEMON_FILE
 -     echo "WorkingDirectory=/home/tahoelafs/tahoelafs" >> $TAHOELAFS_DAEMON_FILE
 -     echo "ExecStart=/home/tahoelafs/tahoelafs/venv/bin/tahoe run /home/tahoelafs/${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
 -     echo "ExecStop=/home/tahoelafs/tahoelafs/venv/bin/tahoe stop /home/tahoelafs/${daemon_name}" >> $TAHOELAFS_DAEMON_FILE
 -     echo 'Restart=on-failure' >> $TAHOELAFS_DAEMON_FILE
 -     echo 'RestartSec=10' >> $TAHOELAFS_DAEMON_FILE
 -     echo "Environment=\"USER=tahoelafs\" \"HOME=/home/tahoelafs\"" >> $TAHOELAFS_DAEMON_FILE
 -     echo '' >> $TAHOELAFS_DAEMON_FILE
 -     echo '[Install]' >> $TAHOELAFS_DAEMON_FILE
 -     echo 'WantedBy=multi-user.target' >> $TAHOELAFS_DAEMON_FILE
 -     systemctl enable tahoelafs-${daemon_name}
 -     systemctl daemon-reload
 -     systemctl start tahoelafs-${daemon_name}
 - }
 - 
 - function create_tahoelafs_web {
 -     if [ ! -d /var/www/tahoelafs/htdocs ]; then
 -         mkdir -p /var/www/tahoelafs/htdocs
 -     fi
 -     TAHOELAFS_LOGIN_TEXT=$'Tahoe-LAFS login'
 - 
 -     tahoelafs_nginx_site=/etc/nginx/sites-available/tahoelafs
 -     echo 'server {' > $tahoelafs_nginx_site
 -     echo "    listen 127.0.0.1:$TAHOELAFS_ONION_PORT default_server;" >> $tahoelafs_nginx_site
 -     echo "    server_name $TAHOELAFS_ONION_HOSTNAME;" >> $tahoelafs_nginx_site
 -     echo '' >> $tahoelafs_nginx_site
 -     function_check nginx_disable_sniffing
 -     nginx_disable_sniffing tahoelafs
 -     echo '' >> $tahoelafs_nginx_site
 -     echo '  # Logs' >> $tahoelafs_nginx_site
 -     echo '  access_log /dev/null;' >> $tahoelafs_nginx_site
 -     echo '  error_log /dev/null;' >> $tahoelafs_nginx_site
 -     echo '' >> $tahoelafs_nginx_site
 -     echo '  # Root' >> $tahoelafs_nginx_site
 -     echo "  root /var/www/tahoelafs/htdocs;" >> $tahoelafs_nginx_site
 -     echo '' >> $tahoelafs_nginx_site
 -     echo '  location / {' >> $tahoelafs_nginx_site
 -     echo "    auth_basic \"${TAHOELAFS_LOGIN_TEXT}\";" >> $tahoelafs_nginx_site
 -     echo '    auth_basic_user_file /etc/nginx/.htpasswd-tahoelafs;' >> $tahoelafs_nginx_site
 -     function_check nginx_limits
 -     nginx_limits tahoelafs '15m'
 -     echo '    rewrite /(.*) /$1 break;' >> $tahoelafs_nginx_site
 -     echo '    proxy_set_header X-Real-IP $remote_addr;' >> $tahoelafs_nginx_site
 -     echo '    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $tahoelafs_nginx_site
 -     echo '    proxy_set_header Host $http_host;' >> $tahoelafs_nginx_site
 -     echo '    proxy_set_header X-NginX-Proxy true;' >> $tahoelafs_nginx_site
 -     echo "    proxy_pass http://localhost:${TAHOELAFS_PORT};" >> $tahoelafs_nginx_site
 -     echo '    proxy_redirect off;' >> $tahoelafs_nginx_site
 -     echo '  }' >> $tahoelafs_nginx_site
 -     echo '}' >> $tahoelafs_nginx_site
 - 
 -     TAHOELAFS_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
 -     ${PROJECT_NAME}-pass -u $MY_USERNAME -a tahoelafs -p "$TAHOELAFS_ADMIN_PASSWORD"
 -     if [ ! -f /etc/nginx/.htpasswd-tahoelafs ]; then
 -         touch /etc/nginx/.htpasswd-tahoelafs
 -     fi
 -     if grep "${MY_USERNAME}:" /etc/nginx/.htpasswd-tahoelafs; then
 -         sed -i '/${MY_USERNAME}:/d' /etc/nginx/.htpasswd-tahoelafs
 -     fi
 -     echo "${TAHOELAFS_ADMIN_PASSWORD}" | htpasswd -i -s /etc/nginx/.htpasswd-tahoelafs ${MY_USERNAME}
 - 
 -     function_check nginx_ensite
 -     nginx_ensite tahoelafs
 -     systemctl reload nginx
 - }
 - 
 - function install_tahoelafs {
 -     if [ $INSTALLING_MESH ]; then
 -         return
 -     fi
 - 
 -     apt-get -yq install build-essential python-pip python-dev libffi-dev libssl-dev
 -     apt-get -yq install libcrypto++-dev python-pycryptopp python-cffi python-virtualenv
 - 
 -     # create a user
 -     if [ ! -d /home/tahoelafs ]; then
 -         # add a gogs user account
 -         adduser --disabled-login --gecos 'tahoe-lafs' tahoelafs
 -         adduser tahoelafs debian-tor
 -     fi
 - 
 -     if [ -d /home/tahoelafs/Maildir ]; then
 -         rm -rf /home/tahoelafs/Maildir
 -     fi
 - 
 -     install_tahoelafs_to_directory /home/tahoelafs/tahoelafs
 - 
 -     # remove files we don't need
 -     rm -rf /home/tahoelafs/.mutt
 -     rm /home/tahoelafs/.emacs-mutt
 -     rm /home/tahoelafs/.muttrc
 -     rm /home/tahoelafs/.mutt-alias
 -     rm /home/tahoelafs/.procmailrc
 - 
 -     # set permissions
 -     chown -R tahoelafs:debian-tor /home/tahoelafs
 - 
 -     node_nick=$(get_tahoelafs_nick)
 -     client_nick=${MY_USERNAME}-client
 - 
 -     # create an onion address for storage node
 -     TAHOELAFS_STORAGE_ONION_HOSTNAME=$(add_onion_service storage-tahoelafs ${TAHOELAFS_STORAGE_PORT} ${TAHOELAFS_STORAGE_ONION_PORT} ${node_nick})
 - 
 -     # create an onion address for client node
 -     TAHOELAFS_ONION_HOSTNAME=$(add_onion_service tahoelafs 80 ${TAHOELAFS_ONION_PORT})
 - 
 -     create_tahoelafs_stealth_node /home/tahoelafs/storage /home/tahoelafs/client ${node_nick} ${client_nick}
 - 
 -     # start the storage node
 -     su -c '/home/tahoelafs/tahoelafs/venv/bin/python2 /home/tahoelafs/tahoelafs/venv/bin/tahoe start /home/tahoelafs/storage' - tahoelafs
 -     create_tahoelafs_daemon "storage"
 - 
 -     # start the client
 -     su -c '/home/tahoelafs/tahoelafs/venv/bin/python2 /home/tahoelafs/tahoelafs/venv/bin/tahoe start /home/tahoelafs/client' - tahoelafs
 -     add_tahoelafs_server "$(get_tahoelafs_storage_hostname)" "$(get_tahoelafs_public_key)" "${node_nick}" "$(get_tahoelafs_furl)"
 -     if ! grep -q "HidServAuth $(get_tahoelafs_storage_hostname)" /etc/tor/torrc; then
 -         echo $'Unable to create tahoelafs server'
 -         exit 738752
 -     fi
 -     if [ ! -f ${tahoelafs_storage_file} ]; then
 -         echo $'tahoelafs server file missing'
 -         exit 529362
 -     fi
 -     create_tahoelafs_daemon "client"
 - 
 -     set_completion_param "tahoelafs commit" "$TAHOELAFS_COMMIT"
 -     set_completion_param "tahoelafs onion domain" "$TAHOELAFS_ONION_HOSTNAME"
 - 
 -     create_tahoelafs_web
 -     systemctl restart tor
 -     APP_INSTALLED=1
 - }
 - 
 - # NOTE: deliberately no exit 0
 
 
  |