freedomboneeee/src/freedombone-recoverkey

178 lines
4.9 KiB
Plaintext
Raw Normal View History

2015-06-30 22:45:18 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2015-06-30 22:45:18 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2015-06-30 22:45:18 +02:00
#
# A script which recovers a user's gpg key from a number of fragments
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2015-06-30 22:45:18 +02:00
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
2015-06-30 22:45:18 +02:00
# 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
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2015-06-30 22:45:18 +02:00
#
2016-02-13 23:09:27 +01:00
# 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/>.
2015-06-30 22:45:18 +02:00
2015-11-27 12:42:16 +01:00
PROJECT_NAME='freedombone'
2015-11-27 17:52:23 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-recoverkey
2015-11-27 12:42:16 +01:00
export TEXTDOMAINDIR="/usr/share/locale"
2018-03-02 23:56:38 +01:00
source "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars"
2016-10-20 16:33:54 +02:00
# include utils which allow function_check, go and drive mount
2018-03-02 20:17:02 +01:00
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
2016-10-20 16:33:54 +02:00
for f in $UTILS_FILES
do
2018-03-02 20:17:02 +01:00
source "$f"
2016-10-20 16:33:54 +02:00
done
FRIENDS_SERVERS_LIST=
MY_USERNAME=
2015-06-30 22:45:18 +02:00
function show_help {
echo ''
2017-06-23 23:14:24 +02:00
echo $"${PROJECT_NAME}-recoverkey -u [username]"
echo $' -l [friends servers list filename]'
2015-06-30 22:45:18 +02:00
echo ''
exit 0
}
2018-02-25 12:04:13 +01:00
while [ $# -gt 1 ]
2015-06-30 22:45:18 +02:00
do
key="$1"
2015-06-30 22:45:18 +02:00
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
2015-06-30 22:45:18 +02:00
done
2018-03-02 20:17:02 +01:00
if [ ! "$MY_USERNAME" ]; then
2015-06-30 22:45:18 +02:00
show_help
fi
2018-03-02 20:17:02 +01:00
if [ ! -d "/home/$MY_USERNAME" ]; then
2015-11-27 16:29:43 +01:00
echo $"User $MY_USERNAME does not exist on the system"
2015-06-30 22:45:18 +02:00
exit 7270
fi
2018-03-02 20:17:02 +01:00
if [ ! "$MY_USERNAME" ]; then
2015-11-27 16:29:43 +01:00
echo $'No username given'
2015-06-30 22:45:18 +02:00
exit 3578
fi
2018-03-02 20:17:02 +01:00
if [ ! -d "/home/$MY_USERNAME" ]; then
2015-11-27 16:29:43 +01:00
echo $"User $MY_USERNAME does not exist on the system"
2015-06-30 22:45:18 +02:00
exit 7270
fi
2018-03-02 20:17:02 +01:00
FRAGMENTS_DIR="/home/$MY_USERNAME/.gnupg_fragments"
# if no remote backup list was given then assume recover from USB
2018-03-02 20:17:02 +01:00
if [ ! "$FRIENDS_SERVERS_LIST" ]; then
interactive_gpg_from_usb
exit 0
fi
# obtain shares/fragments from remote locations
2018-03-02 20:17:02 +01:00
if [ "$FRIENDS_SERVERS_LIST" ]; then
# For each remote server
2018-03-02 20:17:02 +01:00
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}')
2018-03-02 20:17:02 +01:00
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
2018-03-02 20:17:02 +01:00
if [ ! -d "/home/$MY_USERNAME/.gnupg_fragments" ]; then
mkdir -p "/home/$MY_USERNAME/.gnupg_fragments"
fi
2015-11-27 16:29:43 +01:00
echo -n $"Starting key retrieval from $REMOTE_SERVER..."
2018-03-02 20:17:02 +01:00
/usr/bin/sshpass -p "$REMOTE_PASSWORD" \
2018-03-03 12:49:17 +01:00
scp -r -P "$REMOTE_SSH_PORT" "$REMOTE_SERVER/.gnupg_fragments/"* "/home/$MY_USERNAME/.gnupg_fragments"
2018-03-02 20:17:02 +01:00
# shellcheck disable=SC2181
if [ ! "$?" = "0" ]; then
2015-11-27 16:29:43 +01:00
echo $'FAILED'
else
2015-11-27 16:29:43 +01:00
echo $'Ok'
fi
fi
2018-03-02 20:17:02 +01:00
done < "$FRIENDS_SERVERS_LIST"
fi
# was a directory created?
2018-03-02 20:17:02 +01:00
if [ ! -d "$FRAGMENTS_DIR" ]; then
2015-11-27 16:29:43 +01:00
echo $'No fragments have been recovered, so the key cannot be recovered'
2015-06-30 22:45:18 +02:00
exit 7483
fi
# was anything downloaded?
2018-03-02 20:17:02 +01:00
cd "$FRAGMENTS_DIR" || exit 24682468
# shellcheck disable=SC2012
2015-07-04 00:35:57 +02:00
no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
2015-07-04 18:32:25 +02:00
if (( no_of_shares == 0 )); then
2015-11-27 16:29:43 +01:00
echo $'No key fragments were retrieved'
exit 76882
fi
# set permissions on the fragments
2018-03-02 20:17:02 +01:00
chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg_fragments"
2015-06-30 22:45:18 +02:00
# decrypt the file
2015-07-02 22:43:17 +02:00
KEYS_FILE=$FRAGMENTS_DIR/keyshare.asc
2018-03-02 20:17:02 +01:00
cd "$FRAGMENTS_DIR" || exit 482746874624
gfcombine "$KEYS_FILE.*"
2015-07-02 22:43:17 +02:00
2018-03-02 20:17:02 +01:00
if [ ! -f "$KEYS_FILE" ]; then
2015-11-27 16:29:43 +01:00
echo $'Unable to decrypt key. This may mean that not enough fragments are available'
2015-07-02 22:43:17 +02:00
exit 6283
2015-06-30 22:45:18 +02:00
fi
2015-07-02 22:43:17 +02:00
2015-11-27 16:29:43 +01:00
echo $'Key fragments recombined'
2015-06-30 22:45:18 +02:00
# import the gpg key
2018-03-02 20:17:02 +01:00
if ! gpg --homedir="/home/$MY_USERNAME/.gnupg" --allow-secret-key-import --import "$KEYS_FILE"; then
2015-11-27 16:29:43 +01:00
echo $'Unable to import gpg key'
rm "$KEYS_FILE"
2015-06-30 22:45:18 +02:00
exit 3682
fi
rm "$KEYS_FILE"
2018-03-02 20:17:02 +01:00
chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg"
chmod -R 600 "/home/$MY_USERNAME/.gnupg"
2015-06-30 22:45:18 +02:00
2015-11-27 16:29:43 +01:00
echo $'GPG key was recovered'
2015-06-30 22:45:18 +02:00
exit 0