Skip to content
Snippets Groups Projects
freedombone-utils-keys 10.2 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Encryption key related functions
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# 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 import_gpg_from_master_keydrive {
    USB_DRIVE=/dev/sdb1
    USB_MOUNT=/mnt/usb
    backup_device=$(detect_connected_drives)
    if [ ! "$backup_device" ]; then
    fi
    if [[ "$backup_device" != 'sd'* ]]; then
    fi
    read_config_param MY_USERNAME
    backup_mount_drive "$backup_device"
    if [ -d "$USB_MOUNT/.gnupg" ]; then
        # shellcheck disable=SC2086
        cp -rf $USB_MOUNT/.gnupg /home/$MY_USERNAME/
        chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.gnupg"
        cp -rf $USB_MOUNT/.gnupg /root/
    fi
    backup_unmount_drive $USB_DRIVE $USB_MOUNT
}

Bob Mottram's avatar
Bob Mottram committed
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
Bob Mottram's avatar
Bob Mottram committed
    while [[ $GPG_LOADING == "yes" ]]
    do
        USB_DRIVE=$(detect_connected_drives)
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -b "$USB_DRIVE" ]; then
Bob Mottram's avatar
Bob Mottram committed
            if (( GPG_CTR > 0 )); then
Bob Mottram's avatar
Bob Mottram committed
                gpg_reconstruct_key "$MY_USERNAME" interactive
                gpg_update_mutt "$MY_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
                return 0
            fi
            dialog --title $"Recover Encryption Keys" --msgbox $'No USB drive found' 6 30
Bob Mottram's avatar
Bob Mottram committed
        backup_mount_drive "${USB_DRIVE}" "${MY_USERNAME}"
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d "$USB_MOUNT" ]; then
Bob Mottram's avatar
Bob Mottram committed
            if (( GPG_CTR > 0 )); then
Bob Mottram's avatar
Bob Mottram committed
                backup_unmount_drive "${USB_DRIVE}"
                gpg_reconstruct_key "$MY_USERNAME" interactive
Bob Mottram's avatar
Bob Mottram committed
                return 0
            fi
            dialog --title $"Recover Encryption Keys" \
                   --msgbox $"There was a problem mounting the USB drive $USB_DRIVE to $USB_MOUNT" 6 70
Bob Mottram's avatar
Bob Mottram committed
            backup_unmount_drive "${USB_DRIVE}"
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d "$USB_MOUNT/.gnupg" ]; then
            if [ ! -d "$USB_MOUNT/.gnupg_fragments" ]; then
Bob Mottram's avatar
Bob Mottram committed
                if (( GPG_CTR > 0 )); then
Bob Mottram's avatar
Bob Mottram committed
                    backup_unmount_drive "${USB_DRIVE}"
                    gpg_reconstruct_key "$MY_USERNAME" interactive
                    gpg_update_mutt "$MY_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
                    return 0
                fi
                dialog --title $"Recover Encryption Keys" \
                       --msgbox $"The directory $USB_MOUNT/.gnupg or $USB_MOUNT/.gnupg_fragments was not found" 6 70
Bob Mottram's avatar
Bob Mottram committed
                backup_unmount_drive "${USB_DRIVE}"
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "$USB_MOUNT/letsencrypt" ]; then
            if [ ! -d /etc/letsencrypt ]; then
                mkdir /etc/letsencrypt
            fi
            echo $'Recovering LetsEncrypt keys'
Bob Mottram's avatar
Bob Mottram committed
            cp -r "$USB_MOUNT/letsencrypt/"* /etc/letsencrypt
Bob Mottram's avatar
Bob Mottram committed
            addgroup ssl-cert
            chown -R root:ssl-cert /etc/letsencrypt
Bob Mottram's avatar
Bob Mottram committed
        if [ -f "$USB_MOUNT/.mastergpgkey" ]; then
            # Recovering keys from file rather than just copying the gnupg
            # directory may help to avoid problems during upgrades/reinstalls
Bob Mottram's avatar
Bob Mottram committed
            if [ ! -f "$USB_MOUNT/.backupgpgkey" ]; then
Bob Mottram's avatar
Bob Mottram committed
                echo $'No backup key file found on USB drive'
            CURR_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
Bob Mottram's avatar
Bob Mottram committed
            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]*//')
            # If the default key is specified within gpg.conf
Bob Mottram's avatar
Bob Mottram committed
            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
Bob Mottram's avatar
Bob Mottram committed
                        default_gpg_key=$(grep "default-key" "$HOME_DIR/gpg.conf" | awk -F ' ' '{print $2}')
                        if [ ${#default_gpg_key} -gt 3 ]; then
Bob Mottram's avatar
Bob Mottram committed
                            CURR_GPG_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "$default_gpg_key" | sed -n '2p' | sed 's/^[ \t]*//')
            echo $'Making backup copy of existing gpg keys'
Bob Mottram's avatar
Bob Mottram committed
            if [ -d "$HOME_DIR/.gnupg" ]; then
                if [ -d "$HOME_DIR/.gnupg_old" ]; then
                    rm -rf "$HOME_DIR/.gnupg_old"
                cp -r "$HOME_DIR/.gnupg" "$HOME_DIR/.gnupg_old"
Bob Mottram's avatar
Bob Mottram committed
                chmod 700 "$HOME_DIR/.gnupg_old"
Bob Mottram's avatar
Bob Mottram committed
                chmod -R 600 "$HOME_DIR/.gnupg_old/"*
Bob Mottram's avatar
Bob Mottram committed
                chown -R "$MY_USERNAME":"$MY_USERNAME" "$HOME_DIR/.gnupg_old"
            fi
            echo $'Removing old gpg keys'
Bob Mottram's avatar
Bob Mottram committed
            gpg_delete_key "$MY_USERNAME" "$CURR_GPG_BACKUP_ID"
            gpg_delete_key "$MY_USERNAME" "$CURR_GPG_ID"
            echo $'Importing master keys'
Bob Mottram's avatar
Bob Mottram committed
            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
                echo $'Setting permissions'
Bob Mottram's avatar
Bob Mottram committed
                gpg_set_permissions "$MY_USERNAME"
                echo $"Updating muttrc for $MY_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
                gpg_update_mutt "$MY_USERNAME"
Bob Mottram's avatar
Bob Mottram committed
            fi
Bob Mottram's avatar
Bob Mottram committed
            GPG_LOADING="no"
Bob Mottram's avatar
Bob Mottram committed
            dialog --title $"Recover Encryption Keys" \
                   --msgbox $"GPG Keyring loaded to $HOME_DIR from master keydrive" 6 70
Bob Mottram's avatar
Bob Mottram committed
        else
Bob Mottram's avatar
Bob Mottram committed
            if [ -d "$USB_MOUNT/.gnupg" ]; then
                if [ ! -d "$HOME_DIR/.gnupg" ]; then
                    mkdir "$HOME_DIR/.gnupg"
                fi
                echo $'Recovering GPG keys'
Bob Mottram's avatar
Bob Mottram committed
                cp -r "$USB_MOUNT/.gnupg/"* "$HOME_DIR/.gnupg"
                GPG_LOADING="no"
                dialog --title $"Recover Encryption Keys" \
Bob Mottram's avatar
Bob Mottram committed
                       --msgbox $"GPG Keyring directory loaded to $HOME_DIR" 6 70
Bob Mottram's avatar
Bob Mottram committed
                # Collect fragments from the USB drive
Bob Mottram's avatar
Bob Mottram committed
                if [ ! -d "$HOME_DIR/.gnupg_fragments" ]; then
                    mkdir "$HOME_DIR/.gnupg_fragments"
Bob Mottram's avatar
Bob Mottram committed
                cp -r "$USB_MOUNT/.gnupg_fragments/"* "$HOME_DIR/.gnupg_fragments"
Bob Mottram's avatar
Bob Mottram committed
        if [[ "$SSH_IMPORTED" == "no" ]]; then
            if [ -d "$USB_MOUNT/.ssh" ]; then
                if [ ! -d "$HOME_DIR/.ssh" ]; then
                    mkdir "$HOME_DIR/.ssh"
Bob Mottram's avatar
Bob Mottram committed
                fi
Bob Mottram's avatar
Bob Mottram committed
                cp "$USB_MOUNT/.ssh/"* "$HOME_DIR/.ssh"
Bob Mottram's avatar
Bob Mottram committed
                dialog --title $"Recover Encryption Keys" \
                       --msgbox $"ssh keys imported" 6 70
                SSH_IMPORTED="yes"
            fi
        fi

Bob Mottram's avatar
Bob Mottram committed
        if [ -d "$USB_MOUNT" ]; then
            backup_unmount_drive "${USB_DRIVE}"
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
        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
}

Bob Mottram's avatar
Bob Mottram committed
function interactive_gpg {
    GPG_CONFIGURED="no"
    while [[ $GPG_CONFIGURED != "yes" ]]
    do
        GPG_CONFIGURED="yes"
Bob Mottram's avatar
Bob Mottram committed
        data=$(mktemp 2>/dev/null)
Bob Mottram's avatar
Bob Mottram committed
        dialog --backtitle $"Freedombone Configuration" \
Bob Mottram's avatar
Bob Mottram committed
               --radiolist $"GPG/PGP keys for your system:" 13 70 2 \
Bob Mottram's avatar
Bob Mottram committed
               1 $"Generate new keys (new user)" on \
Bob Mottram's avatar
Bob Mottram committed
               2 $"Import keys from USB drive/s" off 2> "$data"
Bob Mottram's avatar
Bob Mottram committed
        sel=$?
        case $sel in
Bob Mottram's avatar
Bob Mottram committed
            1) rm -f "$data"
               exit 1;;
            255) rm -f "$data"
                 exit 2;;
Bob Mottram's avatar
Bob Mottram committed
        esac
Bob Mottram's avatar
Bob Mottram committed
        case $(cat "$data") in
            1) if [ -d "/home/${MY_USERNAME}/.gnupg" ]; then
                   rm -rf "/home/${MY_USERNAME}/.gnupg"
Bob Mottram's avatar
Bob Mottram committed
               fi
               break;;
            2) interactive_gpg_from_usb
               break;;
        esac
Bob Mottram's avatar
Bob Mottram committed
        rm -f "$data"
function interactive_key_recovery {
Bob Mottram's avatar
Bob Mottram committed
    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
Bob Mottram's avatar
Bob Mottram committed
        1) rm -f "$data"
           return;;
        255) rm -f "$data"
             return;;
    clear
    $INSTALL_PACKAGES cryptsetup
Bob Mottram's avatar
Bob Mottram committed
    "${PROJECT_NAME}-recoverkey" -u "$MY_USERNAME"
    if [ -d "/home/$MY_USERNAME/.gnupg" ]; then
        cp -rf "/home/$MY_USERNAME/.gnupg" /root
        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
Bob Mottram's avatar
Bob Mottram committed
    rm -f "$data"
Bob Mottram's avatar
Bob Mottram committed
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
Bob Mottram's avatar
Bob Mottram committed
            "${PROJECT_NAME}-pass" -u "${USERNAME}" -a "${app_name}" -p "${change_password}"
Bob Mottram's avatar
Bob Mottram committed
        fi
    done
}

Bob Mottram's avatar
Bob Mottram committed
# NOTE: deliberately there is no "exit 0"