freedomboneeee/src/freedombone-utils-keys

287 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2016-10-27 12:14:31 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-10-27 12:14:31 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-10-27 12:14:31 +02:00
#
# Encryption key related functions
2018-04-08 14:30:21 +02:00
#
2016-10-27 12:14:31 +02:00
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2016-10-27 12:14:31 +02:00
#
# 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/>.
function interactive_gpg_from_usb {
dialog --title $"Recover Encryption Keys" \
--msgbox $'Plug in a USB keydrive containing a copy of your full key or key fragment' 6 70
HOME_DIR=/home/$MY_USERNAME
GPG_LOADING="yes"
SSH_IMPORTED="no"
GPG_CTR=0
2017-06-24 19:39:13 +02:00
2016-10-27 12:14:31 +02:00
while [[ $GPG_LOADING == "yes" ]]
do
detect_usb_drive
2018-02-26 14:50:40 +01:00
if [ ! -b "$USB_DRIVE" ]; then
2016-10-27 12:14:31 +02:00
if (( GPG_CTR > 0 )); then
2018-02-26 14:50:40 +01:00
gpg_reconstruct_key "$MY_USERNAME" interactive
gpg_update_mutt "$MY_USERNAME"
2016-10-27 12:14:31 +02:00
return 0
fi
dialog --title $"Recover Encryption Keys" --msgbox $'No USB drive found' 6 30
exit 739836
fi
2018-02-26 14:50:40 +01:00
backup_mount_drive "${USB_DRIVE}" "${MY_USERNAME}"
2016-10-27 12:14:31 +02:00
2018-02-26 14:50:40 +01:00
if [ ! -d "$USB_MOUNT" ]; then
2016-10-27 12:14:31 +02:00
if (( GPG_CTR > 0 )); then
2018-02-26 14:50:40 +01:00
backup_unmount_drive "${USB_DRIVE}"
gpg_reconstruct_key "$MY_USERNAME" interactive
2016-10-27 12:14:31 +02:00
return 0
fi
dialog --title $"Recover Encryption Keys" \
2016-10-27 16:08:28 +02:00
--msgbox $"There was a problem mounting the USB drive $USB_DRIVE to $USB_MOUNT" 6 70
2018-02-26 14:50:40 +01:00
backup_unmount_drive "${USB_DRIVE}"
2016-10-27 12:14:31 +02:00
exit 74393
fi
2018-02-26 14:50:40 +01:00
if [ ! -d "$USB_MOUNT/.gnupg" ]; then
if [ ! -d "$USB_MOUNT/.gnupg_fragments" ]; then
2016-10-27 12:14:31 +02:00
if (( GPG_CTR > 0 )); then
2018-02-26 14:50:40 +01:00
backup_unmount_drive "${USB_DRIVE}"
gpg_reconstruct_key "$MY_USERNAME" interactive
gpg_update_mutt "$MY_USERNAME"
2016-10-27 12:14:31 +02:00
return 0
fi
dialog --title $"Recover Encryption Keys" \
2016-10-27 16:08:28 +02:00
--msgbox $"The directory $USB_MOUNT/.gnupg or $USB_MOUNT/.gnupg_fragments was not found" 6 70
2018-02-26 14:50:40 +01:00
backup_unmount_drive "${USB_DRIVE}"
2016-10-27 12:14:31 +02:00
exit 723814
fi
fi
2018-02-26 14:50:40 +01:00
if [ -d "$USB_MOUNT/letsencrypt" ]; then
if [ ! -d /etc/letsencrypt ]; then
mkdir /etc/letsencrypt
fi
echo $'Recovering LetsEncrypt keys'
2018-03-03 12:49:17 +01:00
cp -r "$USB_MOUNT/letsencrypt/"* /etc/letsencrypt
2017-06-23 22:33:07 +02:00
addgroup ssl-cert
chown -R root:ssl-cert /etc/letsencrypt
fi
2018-02-26 14:50:40 +01:00
if [ -f "$USB_MOUNT/.mastergpgkey" ]; then
2017-06-23 16:17:49 +02:00
# Recovering keys from file rather than just copying the gnupg
# directory may help to avoid problems during upgrades/reinstalls
2018-02-26 14:50:40 +01:00
if [ ! -f "$USB_MOUNT/.backupgpgkey" ]; then
2017-06-23 19:39:20 +02:00
echo $'No backup key file found on USB drive'
exit 725729
fi
2017-06-24 19:39:13 +02:00
CURR_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
2018-02-26 14:50:40 +01:00
CURR_GPG_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "$CURR_EMAIL_ADDRESS" | sed -n '2p' | sed 's/^[ \t]*//')
CURR_GPG_BACKUP_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "(backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
2017-06-24 00:39:16 +02:00
# If the default key is specified within gpg.conf
2018-02-26 14:50:40 +01:00
if [ -f "$HOME_DIR/gpg.conf" ]; then
if grep -q "default-key" "$HOME_DIR/gpg.conf"; then
default_gpg_key=$(grep "default-key" "$HOME_DIR/gpg.conf")
if [[ "$default_gpg_key" != *'#'* ]]; then
2018-02-26 14:50:40 +01:00
default_gpg_key=$(grep "default-key" "$HOME_DIR/gpg.conf" | awk -F ' ' '{print $2}')
if [ ${#default_gpg_key} -gt 3 ]; then
2018-02-26 14:50:40 +01:00
CURR_GPG_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "$default_gpg_key" | sed -n '2p' | sed 's/^[ \t]*//')
fi
fi
fi
fi
2017-06-24 00:39:16 +02:00
echo $'Making backup copy of existing gpg keys'
2018-02-26 14:50:40 +01:00
if [ -d "$HOME_DIR/.gnupg" ]; then
if [ -d "$HOME_DIR/.gnupg_old" ]; then
rm -rf "$HOME_DIR/.gnupg_old"
2017-06-24 00:39:16 +02:00
fi
2018-02-26 14:50:40 +01:00
cp -r "$HOME_DIR/.gnupg $HOME_DIR/.gnupg_old"
chmod 700 "$HOME_DIR/.gnupg_old"
2018-03-03 12:49:17 +01:00
chmod -R 600 "$HOME_DIR/.gnupg_old/"*
2018-02-26 14:50:40 +01:00
chown -R "$MY_USERNAME":"$MY_USERNAME" "$HOME_DIR/.gnupg_old"
2017-06-24 00:39:16 +02:00
fi
echo $'Removing old gpg keys'
2018-02-26 14:50:40 +01:00
gpg_delete_key "$MY_USERNAME" "$CURR_GPG_BACKUP_ID"
gpg_delete_key "$MY_USERNAME" "$CURR_GPG_ID"
2017-06-24 00:39:16 +02:00
echo $'Importing master keys'
2018-02-26 14:50:40 +01:00
gpg --homedir="$HOME_DIR/.gnupg" --allow-secret-key-import --import "$USB_MOUNT/.mastergpgkey"
echo "$BACKUP_DUMMY_PASSWORD" | gpg --batch --passphrase-fd 0 --homedir="$HOME_DIR/.gnupg" --allow-secret-key-import --import "$USB_MOUNT/.backupgpgkey"
if [ -d "$HOME_DIR/.gnupg" ]; then
2017-06-24 00:39:16 +02:00
echo $'Setting permissions'
2018-02-26 14:50:40 +01:00
gpg_set_permissions "$MY_USERNAME"
echo $"Updating muttrc for $MY_USERNAME"
2018-02-26 14:50:40 +01:00
gpg_update_mutt "$MY_USERNAME"
2016-10-27 12:14:31 +02:00
fi
2017-06-23 19:42:55 +02:00
GPG_LOADING="no"
2016-10-27 12:14:31 +02:00
dialog --title $"Recover Encryption Keys" \
2017-06-23 16:17:49 +02:00
--msgbox $"GPG Keyring loaded to $HOME_DIR from master keydrive" 6 70
2016-10-27 12:14:31 +02:00
else
2018-02-26 14:50:40 +01:00
if [ -d "$USB_MOUNT/.gnupg" ]; then
if [ ! -d "$HOME_DIR/.gnupg" ]; then
mkdir "$HOME_DIR/.gnupg"
2017-06-23 16:17:49 +02:00
fi
echo $'Recovering GPG keys'
2018-03-03 12:49:17 +01:00
cp -r "$USB_MOUNT/.gnupg/"* "$HOME_DIR/.gnupg"
2017-06-23 16:17:49 +02:00
GPG_LOADING="no"
dialog --title $"Recover Encryption Keys" \
2017-06-24 13:18:48 +02:00
--msgbox $"GPG Keyring directory loaded to $HOME_DIR" 6 70
2017-06-23 16:17:49 +02:00
else
2017-06-24 13:18:48 +02:00
# Collect fragments from the USB drive
2018-02-26 14:50:40 +01:00
if [ ! -d "$HOME_DIR/.gnupg_fragments" ]; then
mkdir "$HOME_DIR/.gnupg_fragments"
2017-06-23 16:17:49 +02:00
fi
2018-03-03 12:49:17 +01:00
cp -r "$USB_MOUNT/.gnupg_fragments/"* "$HOME_DIR/.gnupg_fragments"
2016-10-27 12:14:31 +02:00
fi
fi
2018-02-26 14:50:40 +01:00
if [[ "$SSH_IMPORTED" == "no" ]]; then
if [ -d "$USB_MOUNT/.ssh" ]; then
if [ ! -d "$HOME_DIR/.ssh" ]; then
mkdir "$HOME_DIR/.ssh"
2016-10-27 12:14:31 +02:00
fi
2018-03-03 12:49:17 +01:00
cp "$USB_MOUNT/.ssh/"* "$HOME_DIR/.ssh"
2016-10-27 12:14:31 +02:00
dialog --title $"Recover Encryption Keys" \
--msgbox $"ssh keys imported" 6 70
SSH_IMPORTED="yes"
fi
fi
2018-02-26 14:50:40 +01:00
if [ -d "$USB_MOUNT" ]; then
backup_unmount_drive "${USB_DRIVE}"
2016-10-27 16:25:09 +02:00
fi
2016-10-27 12:14:31 +02:00
if [[ $GPG_LOADING == "yes" ]]; then
dialog --title $"Recover Encryption Keys" \
--msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
fi
GPG_CTR=$((GPG_CTR + 1))
done
}
2016-10-27 14:41:59 +02:00
function interactive_gpg_from_remote {
REMOTE_SERVERS_LIST=/home/$MY_USERNAME/keyshareservers.txt
# get a list of remote servers
2018-02-26 14:50:40 +01:00
"${PROJECT_NAME}-remote" -u "$MY_USERNAME" -l "$REMOTE_SERVERS_LIST" -t "Remote server"
2016-10-27 14:41:59 +02:00
2018-02-26 14:50:40 +01:00
if [ ! -f "$REMOTE_SERVERS_LIST" ]; then
dialog --title $"Encryption Keys Recovery" --msgbox $'Error obtaining server list' 6 70
2016-10-27 14:41:59 +02:00
return 1
fi
# check the number of entries in the file
2018-02-26 14:50:40 +01:00
no_of_servers=$(wc -l < "$REMOTE_SERVERS_LIST")
2016-10-27 14:41:59 +02:00
if (( no_of_servers < 3 )); then
dialog --title $"Encryption Keys Recovery" \
2016-10-27 14:41:59 +02:00
--msgbox $'There must be at least three servers to recover the key' 6 70
return 2
fi
# try to recover the key from the servers
2018-02-26 14:50:40 +01:00
if ! "${PROJECT_NAME}-recoverkey" -u "$MY_USERNAME" -l "$REMOTE_SERVERS_LIST"; then
dialog --title $"Encryption Keys Recovery" --msgbox $'Your key could not be recovered' 6 70
2016-10-27 14:41:59 +02:00
return 3
fi
dialog --title $"Encryption Keys Recovery" --msgbox $'Your key has been recovered' 6 70
2016-10-27 14:41:59 +02:00
return 0
}
function interactive_gpg {
GPG_CONFIGURED="no"
while [[ $GPG_CONFIGURED != "yes" ]]
do
GPG_CONFIGURED="yes"
2018-02-26 14:50:40 +01:00
data=$(mktemp 2>/dev/null)
2016-10-27 14:41:59 +02:00
dialog --backtitle $"Freedombone Configuration" \
--radiolist $"GPG/PGP keys for your system:" 13 70 3 \
1 $"Generate new keys (new user)" on \
2 $"Import keys from USB drive/s" off \
2018-02-26 14:50:40 +01:00
3 $"Retrieve keys from friends servers" off 2> "$data"
2016-10-27 14:41:59 +02:00
sel=$?
case $sel in
2018-02-26 14:50:40 +01:00
1) rm -f "$data"
exit 1;;
255) rm -f "$data"
exit 2;;
2016-10-27 14:41:59 +02:00
esac
2018-02-26 14:50:40 +01:00
case $(cat "$data") in
1) if [ -d "/home/${MY_USERNAME}/.gnupg" ]; then
rm -rf "/home/${MY_USERNAME}/.gnupg"
2016-10-27 14:41:59 +02:00
fi
break;;
2) interactive_gpg_from_usb
break;;
2018-02-26 14:50:40 +01:00
3) if ! interactive_gpg_from_remote; then
2016-10-27 14:41:59 +02:00
GPG_CONFIGURED="no"
fi;;
esac
2018-02-26 14:50:40 +01:00
rm -f "$data"
2016-10-27 14:41:59 +02:00
done
}
function interactive_key_recovery {
2018-02-26 14:50:40 +01:00
data=$(mktemp 2>/dev/null)
dialog --title $"Encryption Keys Recovery" \
--backtitle $"Freedombone Configuration" \
--defaultno \
--yesno $"Do you wish to recover your previous encryption keys from a USB master keydrive?" 7 60
sel=$?
case $sel in
2018-02-26 14:50:40 +01:00
1) rm -f "$data"
return;;
255) rm -f "$data"
return;;
esac
2017-06-14 22:25:25 +02:00
clear
apt-get -yq install cryptsetup
2018-02-26 14:50:40 +01:00
"${PROJECT_NAME}-recoverkey" -u "$MY_USERNAME"
if [ -d "/home/$MY_USERNAME/.gnupg" ]; then
cp -rf "/home/$MY_USERNAME/.gnupg" /root
2017-06-23 12:12:28 +02:00
chmod 700 /root/.gnupg
chmod 600 /root/.gnupg/*
printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > /root/.gnupg/S.dirmngr
if [ -d /root/.gnupg/crls.d ]; then
chmod +x /root/.gnupg/crls.d
fi
2016-10-27 16:26:56 +02:00
fi
2018-02-26 14:50:40 +01:00
rm -f "$data"
}
2016-11-21 17:00:20 +01:00
function set_password_for_all_users {
app_name="$1"
change_password="$2"
for d in /home/*/ ; do
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
2018-02-26 14:50:40 +01:00
"${PROJECT_NAME}-pass" -u "${USERNAME}" -a "${app_name}" -p "${change_password}"
2016-11-21 17:00:20 +01:00
fi
done
}
2016-10-27 12:14:31 +02:00
# NOTE: deliberately there is no "exit 0"