Skip to content
Snippets Groups Projects
freedombone-clientcert 4.98 KiB
Newer Older
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
#
# Generates an email client cert for use with IMAP clients

# See:
#   http://strange.systems/certificate-based-auth-with-dovecot-sendmail
#   http://help.fabasoftfolio.com/index.php?topic=doc/Installation-and-Configuration-of-Fabasoft-Folio-IMAP-Service/client-certificate-authentication.htm

# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# 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
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
# 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/>.
Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAIN=${PROJECT_NAME}-clientcert
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

COUNTRY_CODE="US"
AREA="Free Speech Zone"
LOCATION="Freedomville"
ORGANISATION="Freedombone"
UNIT="Freedombone Unit"
EXTENSIONS=""

function show_help {
    echo ''
Bob Mottram's avatar
Bob Mottram committed
    echo $"${PROJECT_NAME}-clientcert -u [username]"
    echo $'Creates email certificates for use with IMAP clients'
    echo $'     --help                  Show help'
    echo $'  -u --username [name]       Username'
Bob Mottram's avatar
Bob Mottram committed
while [ $# -gt 1 ]
Bob Mottram's avatar
Bob Mottram committed
    key="$1"

    case $key in
        --help)
            show_help
            ;;
        -u|--username)
            shift
            USERNAME="$1"
            ;;
        *)
            # unknown option
            ;;
    esac
Bob Mottram's avatar
Bob Mottram committed
if [ ! "$USERNAME" ]; then
    echo $'No username specified'
Bob Mottram's avatar
Bob Mottram committed
if [ ! -d "/home/$USERNAME" ]; then
    echo $"User $USERNAME not found"
Bob Mottram's avatar
Bob Mottram committed
if [ -d "/home/$USERNAME/emailcert" ]; then
    echo $'Client certs were already for created'
fi

if [ ! -f /etc/dovecot/passwd-file ]; then
    touch /etc/dovecot/passwd-file
fi

# Add a user password
Bob Mottram's avatar
Bob Mottram committed
if ! grep -q "$USERNAME:{plain}" /etc/dovecot/passwd-file; then
Bob Mottram's avatar
Bob Mottram committed
    echo "$USERNAME:{plain}::::::nopassword" >> /etc/dovecot/passwd-file
fi

chmod 600 /etc/dovecot/passwd-file

# create a user cert
Bob Mottram's avatar
Bob Mottram committed
"${PROJECT_NAME}-addcert" -h "$USERNAME" --nodh ""
Bob Mottram's avatar
Bob Mottram committed
if [ ! -f "/etc/ssl/private/$USERNAME.key" ]; then
    echo $'User certificates were not created'
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "/home/$USERNAME/emailcert"
# create a certificate request
Bob Mottram's avatar
Bob Mottram committed
openssl req -new -sha256 -subj \
        "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$USERNAME" \
Bob Mottram's avatar
Bob Mottram committed
        -key "/etc/ssl/private/$USERNAME.key" \
        -out "/etc/ssl/requests/$USERNAME.csr"
Bob Mottram's avatar
Bob Mottram committed
if [ ! -f "/etc/ssl/requests/$USERNAME.csr" ]; then
    echo $'Certificate request was not created'
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "/home/$USERNAME/emailcert"
# sign the certificate request
cd /etc/ssl || exit 74
Bob Mottram's avatar
Bob Mottram committed
openssl ca -config /etc/ssl/dovecot-ca.cnf \
Bob Mottram's avatar
Bob Mottram committed
        -in "/etc/ssl/requests/$USERNAME.csr" \
        -out "/etc/ssl/certs/$USERNAME.cer"
Bob Mottram's avatar
Bob Mottram committed
if [ ! -f "/etc/ssl/certs/$USERNAME.cer" ]; then
    echo $'Authentication certificate was not created'
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "/home/$USERNAME/emailcert"
# move the cert to the user's home
Bob Mottram's avatar
Bob Mottram committed
mkdir "/home/$USERNAME/emailcert"
Bob Mottram's avatar
Bob Mottram committed
# shellcheck disable=SC2086
mv /etc/ssl/certs/$USERNAME.cer /home/$USERNAME/emailcert
Bob Mottram's avatar
Bob Mottram committed
cp "/etc/ssl/certs/dovecot.crt" "/home/$USERNAME/emailcert"
cp "/etc/ssl/certs/ca-$HOSTNAME.crt" "/home/$USERNAME/emailcert"
Bob Mottram's avatar
Bob Mottram committed
# shellcheck disable=SC2086
mv /etc/ssl/private/$USERNAME.key /home/$USERNAME/emailcert
# shellcheck disable=SC2086
mv /etc/ssl/certs/$USERNAME.crt /home/$USERNAME/emailcert
Bob Mottram's avatar
Bob Mottram committed
openssl pkcs12 -export -in "/home/$USERNAME/emailcert/$USERNAME.cer" \
        -out "/home/$USERNAME/emailcert/$USERNAME.p12" \
        -inkey "/home/$USERNAME/emailcert/$USERNAME.key" \
        -certfile "/home/$USERNAME/emailcert/ca-$HOSTNAME.crt" \
Bob Mottram's avatar
Bob Mottram committed
        -password pass:""
Bob Mottram's avatar
Bob Mottram committed
# make an install script
Bob Mottram's avatar
Bob Mottram committed
{ echo '#!/bin/bash';
  echo "sudo mv ca-$HOSTNAME.crt /etc/ssl/certs";
  echo "sudo mv $USERNAME.crt /etc/ssl/certs";
  echo "sudo mv dovecot.crt /etc/ssl/certs";
  echo "sudo mv $USERNAME.key /etc/ssl/private";
  echo 'exit 0'; } > "/home/$USERNAME/emailcert/install.sh"
# set permissions for the user
Bob Mottram's avatar
Bob Mottram committed
chmod -R 755 "/home/$USERNAME/emailcert"
chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/emailcert"
chmod +x "/home/$USERNAME/emailcert/install.sh"
Bob Mottram's avatar
Bob Mottram committed
rm "/etc/ssl/requests/$USERNAME.csr"
echo $'Email authentication certificate created. You can obtain it on the client with:'
Bob Mottram's avatar
Bob Mottram committed
echo "      scp -P 2222 -r $USERNAME@$HOSTNAME:/home/$USERNAME/emailcert ~/"