Skip to content
Snippets Groups Projects
freedombone-recoverkey 5.08 KiB
Newer Older
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
#
# A script which recovers a user's gpg key from a number of fragments

# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# 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
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
# 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/>.
Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-recoverkey
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

source "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars"
Bob Mottram's avatar
Bob Mottram committed

# include utils which allow function_check, go and drive mount
Bob Mottram's avatar
Bob Mottram committed
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
Bob Mottram's avatar
Bob Mottram committed
for f in $UTILS_FILES
do
Bob Mottram's avatar
Bob Mottram committed
    source "$f"
Bob Mottram's avatar
Bob Mottram committed
done

FRIENDS_SERVERS_LIST=
MY_USERNAME=
function show_help {
    echo ''
Bob Mottram's avatar
Bob Mottram committed
    echo $"${PROJECT_NAME}-recoverkey -u [username]"
    echo $'                           -l [friends servers list filename]'
    echo ''
    exit 0
}

Bob Mottram's avatar
Bob Mottram committed
while [ $# -gt 1 ]
    case $key in
        -h|--help)
            show_help
            ;;
        -u|--user)
            shift
            MY_USERNAME="$1"
            ;;
        -d|--dev)
            shift
            if [[ "$1" == 'simple' ]]; then
                import_gpg_from_master_keydrive
                exit 0
            fi
            ;;
        # backup list filename
        # typically /home/$USER/backup.list
        -l|--list)
            shift
            FRIENDS_SERVERS_LIST="$1"
            ;;
        *)
            # unknown option
            ;;
    esac
Bob Mottram's avatar
Bob Mottram committed
if [ ! "$MY_USERNAME" ]; then
    show_help
fi
Bob Mottram's avatar
Bob Mottram committed
if [ ! -d "/home/$MY_USERNAME" ]; then
    echo $"User $MY_USERNAME does not exist on the system"
Bob Mottram's avatar
Bob Mottram committed
if [ ! "$MY_USERNAME" ]; then
    echo $'No username given'
    exit 3578
fi
Bob Mottram's avatar
Bob Mottram committed
if [ ! -d "/home/$MY_USERNAME" ]; then
    echo $"User $MY_USERNAME does not exist on the system"
    exit 7270
fi
Bob Mottram's avatar
Bob Mottram committed
FRAGMENTS_DIR="/home/$MY_USERNAME/.gnupg_fragments"
# if no remote backup list was given then assume recover from USB
Bob Mottram's avatar
Bob Mottram committed
if [ ! "$FRIENDS_SERVERS_LIST" ]; then
    interactive_gpg_from_usb
    exit 0
fi

# obtain shares/fragments from remote locations
Bob Mottram's avatar
Bob Mottram committed
if [ "$FRIENDS_SERVERS_LIST" ]; then
    # For each remote server
Bob Mottram's avatar
Bob Mottram committed
    while read -r remote_server
    do
        # Get the server and its password
        # Format is:
        #   username@domain:/home/username <port number> <ssh password>
        REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
Bob Mottram's avatar
Bob Mottram committed
        if [ "$REMOTE_SERVER" ]; then
            REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
            REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $3}')

            # create a directory if it doesn't exist
Bob Mottram's avatar
Bob Mottram committed
            if [ ! -d "/home/$MY_USERNAME/.gnupg_fragments" ]; then
                mkdir -p "/home/$MY_USERNAME/.gnupg_fragments"
            echo -n $"Starting key retrieval from $REMOTE_SERVER..."
Bob Mottram's avatar
Bob Mottram committed
            /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
Bob Mottram's avatar
Bob Mottram committed
                             scp -r -P "$REMOTE_SSH_PORT" "$REMOTE_SERVER/.gnupg_fragments/"* "/home/$MY_USERNAME/.gnupg_fragments"
Bob Mottram's avatar
Bob Mottram committed
            # shellcheck disable=SC2181
            if [ ! "$?" = "0" ]; then
                echo $'FAILED'
                echo $'Ok'
Bob Mottram's avatar
Bob Mottram committed
    done < "$FRIENDS_SERVERS_LIST"

# was a directory created?
Bob Mottram's avatar
Bob Mottram committed
if [ ! -d "$FRAGMENTS_DIR" ]; then
    echo $'No fragments have been recovered, so the key cannot be recovered'
# was anything downloaded?
cd "$FRAGMENTS_DIR" || exit 24
Bob Mottram's avatar
Bob Mottram committed
# shellcheck disable=SC2012
no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
Bob Mottram's avatar
Bob Mottram committed
if (( no_of_shares == 0 )); then
    echo $'No key fragments were retrieved'
fi

# set permissions on the fragments
Bob Mottram's avatar
Bob Mottram committed
chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg_fragments"
# decrypt the file
KEYS_FILE=$FRAGMENTS_DIR/keyshare.asc
cd "$FRAGMENTS_DIR" || exit 48
Bob Mottram's avatar
Bob Mottram committed
gfcombine "$KEYS_FILE.*"
Bob Mottram's avatar
Bob Mottram committed
if [ ! -f "$KEYS_FILE" ]; then
    echo $'Unable to decrypt key. This may mean that not enough fragments are available'
echo $'Key fragments recombined'

# import the gpg key
Bob Mottram's avatar
Bob Mottram committed
if ! gpg --homedir="/home/$MY_USERNAME/.gnupg" --allow-secret-key-import --import "$KEYS_FILE"; then
    echo $'Unable to import gpg key'
Bob Mottram's avatar
Bob Mottram committed
    rm "$KEYS_FILE"
Bob Mottram's avatar
Bob Mottram committed
rm "$KEYS_FILE"
Bob Mottram's avatar
Bob Mottram committed
chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg"
chmod -R 600 "/home/$MY_USERNAME/.gnupg"
echo $'GPG key was recovered'