| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #!/bin/bash
- #
- # Copyright (c) 2019 Clementine Computing LLC.
- #
- # This file is part of PopuFare.
- #
- # PopuFare 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.
- #
- # PopuFare 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 PopuFare. If not, see <https://www.gnu.org/licenses/>.
- #
- export BASEDIR="/home/bus"
- . $BASEDIR/bin/common_values.sh
- usage() {
- echo -e "Usage:" 1>&2
- echo -e "\t$0 line_number" 1>&2
- exit 1
- }
- # If the supplied line number is not numeric, complain!
- #
- if echo "$1" | egrep -v '^[0-9]+$' > /dev/null; then usage; fi
- # Create a temporary file to hold the selected config line for disection
- #
- tmpfile=`mktemp /tmp/unpack_server_tmp.XXXXXX`
- # Disect the server list and get the first N lines, and then the last one line of that set will be line N
- #
- head -n "$1" "$SERVER_LIST_FILE" | tail -n 1 > $tmpfile
- # Make sure that we have the expected six fields in this line
- #
- if [ `cat $tmpfile | tr '\t' '\n' | wc -l` -ne 6 ]; then
- echo "Line $1 does not contain the required 6 tab-delimited fields. Not altering server config." 1>&2;
- rm -f $tmpfile;
- usage;
- fi
- # Extract the target username...
- #
- ssh_target_user="`cat $tmpfile | cut -f2`"
- # Extract the target hostname...
- #
- ssh_target_host="`cat $tmpfile | cut -f3`"
- # Now, take the username and hostname and make a ssh_target
- #
- ssh_target="$ssh_target_user@$ssh_target_host"
- # If they pass the "smell test", we will proceed, otherwise we bail...
- #
- if echo "$ssh_target" | egrep -qv "$SSH_TARGET_VALIDITY_CHECK"; then
- echo "Target specified on line $1 appears to be invalid \"$ssh_target\". Not altering server config." 1>&2
- rm -f $tmpfile
- usage
- fi
- # If we've gotten this far, we are going to go through with it...
- #
- # Extract and save our description...
- #
- cat $tmpfile | cut -f1 > $SYNC_DESC_FILE
- # Save our new sync target "user@host"...
- #
- echo "$ssh_target" > $SYNC_TARGET_FILE
- # Extract and save our new port number...
- #
- cat $tmpfile | cut -f4 > $SYNC_PORT_FILE
- # Here we need to first put in our target hostname, followed by a space (but no newline)
- #
- echo -n "$ssh_target_host " > $SYNC_KNOWN_HOSTS
- # then append the 5th field (target host's public key)
- #
- cat $tmpfile | cut -f5 >> $SYNC_KNOWN_HOSTS
- # and set the correct permissions
- #
- chmod $PACKAGE_SSH_MILD_FILE_PERMISSIONS $SYNC_KNOWN_HOSTS
- # Here we need to put the correct preface/header on the private key
- echo "-----BEGIN RSA PRIVATE KEY-----" > $SYNC_PRIVATE_KEY
- # next we need to take the 6th field, and reconstitute all the spaces into newlines
- #
- cat $tmpfile | cut -f6 | tr ' ' '\n' >> $SYNC_PRIVATE_KEY
- # add on the terminating line
- echo "-----END RSA PRIVATE KEY-----" >> $SYNC_PRIVATE_KEY
- # and set the correct permissions
- #
- chmod $PACKAGE_SSH_STRICT_FILE_PERMISSIONS $SYNC_PRIVATE_KEY
- # remove our temporary file
- #
- rm -f $tmpfile
- # sync the secondary storage
- #
- sync
- # and signal that we want to close any open SSH sessions and start a new one with the new server
- #
- abort_tunnel_and_update
|