#!/bin/bash # # .---. . . # | | | # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-' # ' ' --' --' -' - -' ' ' -' -' -' ' - --' # # Freedom in the Cloud # # A script which recovers a user's gpg key from a number of fragments # License # ======= # # Copyright (C) 2015 Bob Mottram # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . FRIENDS_SERVERS_LIST= MY_USERNAME= function show_help { echo '' echo 'freedombone-recoverkey -u [username] -l [friends servers list filename]' echo '' exit 0 } while [[ $# > 1 ]] do key="$1" case $key in -h|--help) show_help ;; -u|--user) shift MY_USERNAME="$1" ;; # backup list filename # typically /home/$USER/backup.list -l|--list) shift FRIENDS_SERVERS_LIST="$1" ;; *) # unknown option ;; esac shift done if [ ! $MY_USERNAME ]; then show_help fi if [ ! -d /home/$MY_USERNAME ]; then echo "User $MY_USERNAME does not exist on the system" exit 7270 fi if [ ! $MY_USERNAME ]; then echo 'No username given' exit 3578 fi if [ ! -d /home/$MY_USERNAME ]; then echo "User $MY_USERNAME does not exist on the system" exit 7270 fi FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments # find the remote backup list if [ ! $FRIENDS_SERVERS_LIST ]; then if [ -f /home/$MY_USERNAME/backup.list ]; then FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list fi fi # obtain shares/fragments from remote locations if [ $FRIENDS_SERVERS_LIST ]; then # For each remote server while read remote_server do # Get the server and its password # Format is: # username@domain:/home/username REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}') 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 if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then mkdir -p /home/$MY_USERNAME/.gnupg_fragments fi echo -n "Starting key retrieval from $REMOTE_SERVER..." /usr/bin/sshpass -p $REMOTE_PASSWORD \ scp -r -P $REMOTE_SSH_PORT $REMOTE_SERVER/.gnupg_fragments/* /home/$MY_USERNAME/.gnupg_fragments if [ ! "$?" = "0" ]; then echo 'FAILED' else echo 'Ok' fi fi done < $FRIENDS_SERVERS_LIST fi # was a directory created? if [ ! -d $FRAGMENTS_DIR ]; then echo 'No fragments have been recovered, so the key cannot be recovered' exit 7483 fi # was anything downloaded? cd $FRAGMENTS_DIR no_of_shares=$(ls -afq keyshare.asc.* | wc -l) no_of_shares=$((no_of_shares - 2)) if [[ ${no_of_shares} == 0 ]]; then echo 'No key fragments were retrieved' exit 76882 fi # set permissions on the fragments chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg_fragments # decrypt the file KEYS_FILE=$FRAGMENTS_DIR/keyshare.asc cd $FRAGMENTS_DIR gfcombine $KEYS_FILE.* if [ ! -f $KEYS_FILE ]; then echo 'Unable to decrypt key. This may mean that not enough fragments are available' exit 6283 fi echo 'Key fragments recombined' # import the gpg key su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME if [ ! "$?" = "0" ]; then echo 'Unable to import gpg key' shred -zu $KEYS_FILE exit 3682 fi shred -zu $KEYS_FILE chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg chmod -R 600 /home/$MY_USERNAME/.gnupg echo 'GPG key was recovered' exit 0