#!/bin/sh
#
# Renew a Let's Encrypt certificate, with some special handling for the
# jabber.eyrie.org certificate.  Meant to be run from cron for automatic,
# ongoing certificate renewal.

set -e

# Ensure a sane PATH.
PATH=/bin:/sbin:/usr/bin:/usr/sbin

# Map the various certificate names to the right set of -w and -d flags for
# all of the various alternate names.
case $1 in
    archives.eyrie.org)
        flags="-w /srv/archives/pub -d archives.eyrie.org -d ftp.eyrie.org"
        ;;
    calvin.eyrie.org)
        flags="-w /srv/archives/pub -d calvin.eyrie.org"
        ;;
    git.eyrie.org)
        flags="-w /srv/git -d git.eyrie.org"
        ;;
    haven.eyrie.org)
        flags="-w /srv/www/pages -d haven.eyrie.org"
        ;;
    hope.eyrie.org)
        flags="-w /srv/archives/pub -d hope.eyrie.org -d inn.eyrie.org"
        ;;
    jabber.eyrie.org)
        flags="-w /srv/archives/pub -d jabber.eyrie.org -d eyrie.org"
        ;;
    lists.eyrie.org)
        flags="-w /var/lib/mailman -d lists.eyrie.org"
        ;;
    lothlorien.eyrie.org)
        flags="-w /srv/www/lothlorien -d lothlorien.eyrie.org"
        ;;
    news.eyrie.org)
        flags="-w /srv/archives/pub -d news.eyrie.org"
        ;;
    www.eyrie.org)
        flags="-w /srv/www/pages -d www.eyrie.org -d journals.eyrie.org"
        ;;
    *)
        echo "Unknown domain $1" >&2
        exit 1
        ;;
esac

# Do the renewal.
certbot -n certonly --non-interactive --renew-by-default \
    -m eagle@eyrie.org --webroot $flags

# Restart Apache.
apache2ctl graceful

# If renewing one of the Postfix certificates, also restart postfix.
if [ "$1" = 'haven.eyrie.org' ] || [ "$1" = 'hope.eyrie.org' ]; then
    service postfix restart
fi

# If renewing the certificate for hope.eyrie.org, also copy it for PostgreSQL
# and restart the database server.
if [ "$1" = 'hope.eyrie.org' ]; then
    for dir in /etc/postgresql/*/main; do
        cp /etc/letsencrypt/live/hope.eyrie.org/privkey.pem "$dir"/ssl-key.pem
        chown postgres:postgres "$dir"/ssl-key.pem
        chmod 600 "$dir"/ssl-key.pem
        cp /etc/letsencrypt/live/hope.eyrie.org/fullchain.pem \
            "$dir"/ssl-cert.pem
        chown postgres:postgres "$dir"/ssl-cert.pem
        chmod 644 "$dir"/ssl-cert.pem
    done
    systemctl postgresql reload
fi

# If renewing calvin.eyrie.org, also copy it to each chatserver.
if [ "$1" = 'calvin.eyrie.org' ]; then
    umask 077
    for dir in /etc/calvin/*; do
        cp /etc/letsencrypt/live/calvin.eyrie.org/privkey.pem \
            "$dir"/key.pem
        chown root:calvin "$dir"/key.pem
        chmod 640 "$dir"/key.pem
        cp /etc/letsencrypt/live/calvin.eyrie.org/fullchain.pem \
            "$dir"/cert.pem
        chown root:root "$dir"/cert.pem
        chmod 644 "$dir"/cert.pem
    done
    umask 022
fi

# If renewing news.eyrie.org, copy it to where nnrpd can find it.
if [ "$1" = 'news.eyrie.org' ]; then
    umask 077
    cp /etc/letsencrypt/live/news.eyrie.org/privkey.pem \
        /etc/news/tls/key.pem
    chown root:news /etc/news/tls/key.pem
    chmod 640 /etc/news/tls/key.pem
    cp /etc/letsencrypt/live/news.eyrie.org/cert.pem \
        /etc/news/tls/cert.pem
    chown root:root /etc/news/tls/cert.pem
    chmod 644 /etc/news/tls/cert.pem
    cp /etc/letsencrypt/live/news.eyrie.org/chain.pem \
        /etc/news/tls/ca.pem
    chown root:root /etc/news/tls/ca.pem
    chmod 644 /etc/news/tls/ca.pem
    umask 022
fi

# If renewing the jabber.eyrie.org certificate, regenerate the assembled
# certificate file and then restart ejabberd.
if [ "$1" = 'jabber.eyrie.org' ]; then
    umask 077
    cat /etc/letsencrypt/live/jabber.eyrie.org/privkey.pem      \
        /etc/letsencrypt/live/jabber.eyrie.org/fullchain.pem    \
        > /etc/ejabberd/ejabberd.pem
    chown ejabberd:ejabberd /etc/ejabberd/ejabberd.pem
    chmod 600 /etc/ejabberd/ejabberd.pem
    umask 022
    service ejabberd restart
fi
exit 0

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=for stopwords
Allbery Postfix certbot eyrie.org

=head1 NAME

renew-certificate - Wrapper around certbot to renew an eyrie.org certificate

=head1 SYNOPSIS

B<renew-certificate> I<hostname>

=head1 DESCRIPTION

A wrapper around B<certbot> that understands all the flags used for renewing
eyrie.org certificates and has logic to take additional actions after
certificate renewal (restarting Apache, restarting Postfix, or copying the
certificate and key into some other location as required for different
programs).

Normally, this script is run from cron, staggered across the three month
lifetime of Let's Encrypt certificates.

Ideally, this wrapper will eventually be replaced with direct B<certbot>
configuration of post-rotate commands.

=head1 AUTHOR

Russ Allbery <eagle@eyrie.org>

=head1 SEE ALSO

certbot(1)

=cut
__END_OF_DOCS__
