#!/bin/bash # _____ _ _ # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___ # | __| _| -_| -_| . | . | | . | . | | -_| # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___| # # Freedom in the Cloud # # scuttlebot pub application. Enables nat traversal for SSB. # https://scuttlebot.io # # This is deprecated for now because it appears that Patchwork # Is moving to DHT invites, which presumably eliminates the # need for separate pubs. Invites generated by scuttlebot # are currently incompatible with the new DHT format. # # License # ======= # # Copyright (C) 2017-2019 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='' APP_CATEGORY=chat IN_DEFAULT_INSTALL=0 SHOW_ON_ABOUT=0 SHOW_CLEARNET_ADDRESS_ON_ABOUT=0 SHOW_DOMAIN_IN_WEBADMIN=0 REQUIRES_APP= SCUTTLEBOT_VERSION='13.0.0' SCUTTLEBOT_REPLICATE_NODES=10 SCUTTLEBOT_PORT=8008 SCUTTLEBOT_ONION_PORT=8623 SCUTTLEBOT_SHORT_DESCRIPTION=$'Scuttlebot Pub' SCUTTLEBOT_DESCRIPTION=$'Scuttlebot Pub' SCUTTLEBOT_MOBILE_APP_URL='https://f-droid.org/en/packages/se.manyver/' scuttlebot_variables=(MY_USERNAME DEFAULT_DOMAIN_NAME SYSTEM_TYPE) function change_default_domain_name_scuttlebot { new_default_domain_name="$1" { echo '{'; echo " \"host\": \"${new_default_domain_name}\","; echo ' "tor-only": false,'; } > /etc/scuttlebot/.ssb/config systemctl restart scuttlebot } function logging_on_scuttlebot { echo -n '' } function logging_off_scuttlebot { echo -n '' } function scuttlebot_create_single_invite { invite_string=$(su -c "/etc/scuttlebot/node_modules/.bin/sbot invite.create 1" - scuttlebot | sed 's/"//g') if [ ! "$invite_string" ]; then echo $'Unable to create scuttlebot invite' exit 36 fi write_config_param SCUTTLEBOT_INVITE "$invite_string" } function scuttlebot_create_invite { scuttlebot_create_single_invite clear echo -e "\\n\\nYour Scuttlebot invite code is:\\n\\n${SCUTTLEBOT_INVITE}\\n\\n" # shellcheck disable=SC2034 read -n1 -r -p $"Press any key to continue..." key } function configure_interactive_scuttlebot { W=(1 $"Create an invite") while true do # shellcheck disable=SC2068 selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Scuttlebot" --menu $"Choose an operation, or ESC to exit:" 10 60 2 "${W[@]}" 3>&2 2>&1 1>&3) if [ ! "$selection" ]; then break fi case $selection in 1) scuttlebot_create_invite;; esac done } function remove_user_scuttlebot { echo -n '' # remove_username="$1" } function add_user_scuttlebot { # new_username="$1" # new_user_password="$2" # This doesn't have users echo '0' } function install_interactive_scuttlebot { if [[ $ONION_ONLY != "no" ]]; then SCUTTLEBOT_DOMAIN_NAME='scuttlebot.local' write_config_param "SCUTTLEBOT_DOMAIN_NAME" "$SCUTTLEBOT_DOMAIN_NAME" else function_check interactive_site_details interactive_site_details scuttlebot fi APP_INSTALLED=1 } function change_password_scuttlebot { # new_username="$1" # new_user_password="$2" echo '0' } function reconfigure_scuttlebot { if [ -d /etc/scuttlebot/.ssb ]; then systemctl stop scuttlebot rm -rf /etc/scuttlebot/.ssb systemctl start scuttlebot fi } function upgrade_scuttlebot { if ! grep -q 'scuttlebot version:' $"COMPLETION_FILE"; then return fi CURR_SCUTTLEBOT_VERSION=$(get_completion_param "scuttlebot version") echo "scuttlebot current version: ${CURR_SCUTTLEBOT_VERSION}" echo "scuttlebot app version: ${SCUTTLEBOT_VERSION}" if [[ "${CURR_SCUTTLEBOT_VERSION}" == "${SCUTTLEBOT_VERSION}" ]]; then return fi cat <<EOF > /usr/bin/upgrade_scuttlebot #!/bin/bash cd /etc/scuttlebot || exit 1 if ! npm upgrade -g scuttlebot@${SCUTTLEBOT_VERSION} --save; then exit 2 fi exit 0 EOF chmod +x /usr/bin/upgrade_scuttlebot su -c '/usr/bin/upgrade_scuttlebot' - scuttlebot rm /usr/bin/upgrade_scuttlebot sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" "${COMPLETION_FILE}" } function backup_local_scuttlebot { if [ -d /etc/scuttlebot/.ssb ]; then systemctl stop scuttlebot function_check backup_directory_to_usb backup_directory_to_usb /etc/scuttlebot/.ssb scuttlebot systemctl start scuttlebot fi } function restore_local_scuttlebot { if [ -d /etc/scuttlebot ]; then systemctl stop scuttlebot temp_restore_dir=/root/tempscuttlebot function_check restore_directory_from_usb restore_directory_from_usb $temp_restore_dir scuttlebot if [ -d $temp_restore_dir/etc/scuttlebot/.ssb ]; then cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/ else cp -r $temp_restore_dir/* /etc/scuttlebot/.ssb/* fi systemctl start scuttlebot rm -rf $temp_restore_dir fi } function backup_remote_scuttlebot { echo -n '' } function restore_remote_scuttlebot { echo -n '' } function remove_scuttlebot { firewall_remove ${SCUTTLEBOT_PORT} systemctl stop scuttlebot systemctl disable scuttlebot rm /etc/systemd/system/scuttlebot.service systemctl daemon-reload userdel -r scuttlebot if [ -d /etc/scuttlebot ]; then rm -rf /etc/scuttlebot fi remove_completion_param install_scuttlebot sed -i '/scuttlebot /d' "$COMPLETION_FILE" } function mesh_install_scuttlebot { #shellcheck disable=SC2153 if [[ "$VARIANT" != "meshclient" && "$VARIANT" != "meshusb" ]]; then return fi SCUTTLEBOT_ONION_HOSTNAME= # shellcheck disable=SC2086 chroot "$rootdir" $INSTALL_PACKAGES libsodium23 chroot "$rootdir" $INSTALL_PACKAGES autotools-dev automake get_npm_arch mesh_setup_npm if [ ! -d "$rootdir/etc/scuttlebot" ]; then mkdir -p "$rootdir/etc/scuttlebot" fi # an unprivileged user to install and run as chroot "$rootdir" useradd -d /etc/scuttlebot/ scuttlebot chroot "$rootdir" chown -R scuttlebot:scuttlebot /etc/scuttlebot cat <<EOF > "$rootdir/usr/bin/install_scuttlebot" #!/bin/bash cd /etc/scuttlebot || exit 1 if ! npm install --arch=$NPM_ARCH scuttlebot@${SCUTTLEBOT_VERSION}; then exit 2 fi exit 0 EOF chroot "$rootdir" /bin/chmod +x /usr/bin/install_scuttlebot chroot "$rootdir" sudo -u scuttlebot /usr/bin/install_scuttlebot rm "$rootdir/usr/bin/install_scuttlebot" if [ ! -f "$rootdir/etc/scuttlebot/node_modules/.bin/sbot" ]; then echo $'Scuttlebot was not installed' exit 528253 fi # daemon { echo '[Unit]'; echo 'Description=Scuttlebot (messaging system)'; echo 'After=syslog.target'; echo 'After=network.target'; echo ''; echo '[Service]'; echo 'Type=simple'; echo 'User=scuttlebot'; echo 'Group=scuttlebot'; echo "WorkingDirectory=/etc/scuttlebot"; echo 'ExecStart=/etc/scuttlebot/node_modules/scuttlebot/bin.js server'; echo 'Restart=always'; echo 'Environment="USER=scuttlebot"'; echo 'PrivateTmp=true'; echo 'PrivateDevices=false'; echo 'NoNewPrivileges=true'; echo 'CapabilityBoundingSet=~CAP_SYS_ADMIN'; echo ''; echo '[Install]'; echo 'WantedBy=multi-user.target'; } > "$rootdir/etc/systemd/system/scuttlebot.service" } function install_scuttlebutt-notification { cat <<EOF > /usr/bin/install_scuttlebutt_notification #!/bin/bash cd /etc/scuttlebot || exit 1 if ! git clone https://github.com/ssbc/ssb-example-pm notification; then echo $'ssb-example-pm was not cloned' exit 2 fi if [ ! -d /etc/scuttlebot/notification ]; then echo $'/etc/scuttlebot/notification not found' exit 3 fi cd /etc/scuttlebot/notification || exit 4 npm install exit 0 EOF chmod +x /usr/bin/install_scuttlebutt_notification su -c '/usr/bin/install_scuttlebutt_notification' - scuttlebot rm /usr/bin/install_scuttlebutt_notification if [ ! -f /etc/scuttlebot/notification/ssb-example-pm.js ]; then echo $'ssb-example-pm.js was not cloned' exit 24 fi } function install_scuttlebot { function_check install_nodejs install_nodejs scuttlebot $INSTALL_PACKAGES libsodium23 $INSTALL_PACKAGES autotools-dev automake if [ -d /etc/scuttlebot ]; then rm -rf /etc/scuttlebot fi mkdir -p /etc/scuttlebot # an unprivileged user to install and run as useradd -d /etc/scuttlebot/ scuttlebot chown -R scuttlebot:scuttlebot /etc/scuttlebot cat <<EOF > /usr/bin/install_scuttlebot #!/bin/bash cd /etc/scuttlebot || exit 1 if ! npm install scuttlebot@${SCUTTLEBOT_VERSION}; then exit 2 fi if ! npm install ssb-dht-invite@0.9.0; then exit 3 fi exit 0 EOF chmod +x /usr/bin/install_scuttlebot su -c '/usr/bin/install_scuttlebot' - scuttlebot rm /usr/bin/install_scuttlebot if [ ! -f /etc/scuttlebot/node_modules/.bin/sbot ]; then echo $'Scuttlebot was not installed' exit 52 fi # daemon { echo '[Unit]'; echo 'Description=Scuttlebot (messaging system)'; echo 'After=syslog.target'; echo 'After=network.target'; echo ''; echo '[Service]'; echo 'Type=simple'; echo 'User=scuttlebot'; echo 'Group=scuttlebot'; echo "WorkingDirectory=/etc/scuttlebot"; echo 'ExecStart=/etc/scuttlebot/node_modules/scuttlebot/bin.js server'; echo 'Restart=always'; echo 'Environment="USER=scuttlebot"'; echo ''; echo '[Install]'; echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/scuttlebot.service enable_mdns if [ ! -d /etc/scuttlebot/.ssb ]; then mkdir /etc/scuttlebot/.ssb fi SCUTTLEBOT_ONION_HOSTNAME=$(add_onion_service scuttlebot 80 ${SCUTTLEBOT_ONION_PORT}) if [[ "$ONION_ONLY" == 'no' ]]; then { echo '{'; echo " \"host\": \"${DEFAULT_DOMAIN_NAME}\","; echo ' "tor-only": false,'; } > /etc/scuttlebot/.ssb/config else { echo '{'; echo " \"host\": \"${SCUTTLEBOT_ONION_HOSTNAME}\","; echo ' "tor-only": true,'; } > /etc/scuttlebot/.ssb/config fi { echo " \"port\": ${SCUTTLEBOT_PORT},"; echo ' "timeout": 30000,'; echo ' "pub": true,'; echo ' "local": true,'; echo ' "friends": {'; echo " \"dunbar\": ${SCUTTLEBOT_REPLICATE_NODES},"; echo ' "hops": 1'; echo ' },'; echo ' "gossip": {'; echo ' "connections": 2'; echo ' },'; echo ' "master": [],'; echo ' "logging": {'; echo ' "level": "error"'; echo ' }'; echo '}'; } >> /etc/scuttlebot/.ssb/config chown -R scuttlebot:scuttlebot /etc/scuttlebot # start the daemon systemctl enable scuttlebot.service systemctl daemon-reload systemctl restart scuttlebot.service firewall_add scuttlebot ${SCUTTLEBOT_PORT} # wait for keys to be generated ctr=0 while [ $ctr -lt 6 ]; do if [ -f /etc/scuttlebot/.ssb/manifest.json ]; then break fi ctr=$((ctr+1)) sleep 3 done if [ ! -f /etc/scuttlebot/.ssb/manifest.json ]; then echo $'scuttlebot manifest was not generated' exit 36 fi scuttlebot_create_single_invite create_default_web_site install_scuttlebutt-notification if ! grep -q "scuttlebot version:" "${COMPLETION_FILE}"; then echo "scuttlebot version:${SCUTTLEBOT_VERSION}" >> "${COMPLETION_FILE}" else sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" "${COMPLETION_FILE}" fi APP_INSTALLED=1 } # NOTE: deliberately no exit 0