2020-10-04 15:35:59 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Daemon PGP Keyring
|
|
|
|
# ------------------
|
|
|
|
#
|
|
|
|
# Initializes the PGP keyring at /home/user-data/.gnupg
|
|
|
|
# For this, we will generate a new PGP keypair (if one isn't already present)
|
|
|
|
|
|
|
|
source setup/functions.sh # load our functions
|
|
|
|
source /etc/mailinabox.conf # load global vars
|
|
|
|
export GNUPGHOME # Dump into the environment so that gpg uses it as homedir
|
|
|
|
|
|
|
|
# Install gnupg
|
|
|
|
apt_install gnupg
|
|
|
|
|
2021-03-07 18:32:21 +00:00
|
|
|
function gpg_keygen {
|
|
|
|
# Generates a private key.
|
2021-07-02 23:46:31 +00:00
|
|
|
gpg --generate-key --batch 2> /tmp/gpg_keygen_log << EOF;
|
2020-10-04 15:35:59 +00:00
|
|
|
%no-protection
|
|
|
|
Key-Type: RSA
|
|
|
|
Key-Length: 4096
|
|
|
|
Key-Usage: sign,encrypt,auth
|
2021-03-29 23:03:21 +00:00
|
|
|
Name-Real: System Management Daemon
|
2021-03-07 18:32:21 +00:00
|
|
|
Name-Email: noreply-daemon@${PRIMARY_HOSTNAME}
|
2020-10-04 15:35:59 +00:00
|
|
|
Expire-Date: 180d
|
|
|
|
%commit
|
|
|
|
EOF
|
2021-03-07 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generate a new key if:
|
|
|
|
# - There isn't a fingerprint on /etc/mailinabox.conf
|
|
|
|
# - The configured fingerprint doesn't actually exist
|
|
|
|
|
2021-07-02 23:46:31 +00:00
|
|
|
if [ "${PGPKEY-}" == "" -o "$(gpg --list-secret-keys 2> /dev/null | grep ${PGPKEY-})" = "" ]; then
|
2021-03-07 18:32:21 +00:00
|
|
|
echo "No keypair found. Generating daemon's PGP keypair..."
|
2021-07-02 23:46:31 +00:00
|
|
|
gpg_keygen
|
2021-03-07 18:32:21 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo ""
|
|
|
|
echo "Key generation failed!" 1>&2
|
|
|
|
echo "============================" 1>&2
|
2021-07-02 23:46:31 +00:00
|
|
|
cat /tmp/gpg_keygen_log 1>&2
|
2021-03-07 18:32:21 +00:00
|
|
|
echo "============================" 1>&2
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-07-02 23:46:31 +00:00
|
|
|
FPR=$(cat /tmp/gpg_keygen_log | tr -d '\n' | sed -r 's/.*([0-9A-F]{40}).*/\1/g')
|
|
|
|
echo "Generated key with fingerprint $FPR"
|
2021-03-07 18:32:21 +00:00
|
|
|
|
2020-10-04 15:35:59 +00:00
|
|
|
chown -R root:root $GNUPGHOME
|
2021-03-07 18:32:21 +00:00
|
|
|
# Remove the old key fingerprint from the configuration if it exists, and add the new one
|
2020-10-04 15:35:59 +00:00
|
|
|
echo "$(cat /etc/mailinabox.conf | grep -v "PGPKEY")" > /etc/mailinabox.conf
|
2021-03-07 18:32:21 +00:00
|
|
|
echo "PGPKEY=$FPR" >> /etc/mailinabox.conf
|
2020-10-04 15:35:59 +00:00
|
|
|
fi
|