#!/bin/sh # based on doc/mkcert.sh # Generates a self-signed certificate. OPENSSL=${OPENSSL-openssl} SSLDIR=${SSLDIR-/etc/ssl/dovecot} OPENSSLCONFIG=${OPENSSLCONFIG-/etc/dovecot/dovecot-openssl.cnf} CERTDIR=$SSLDIR KEYDIR=$SSLDIR # parse cert and key file from dovecot.conf dovecot_conf=/etc/dovecot/dovecot.conf ssl_cert_file= ssl_key_file= if [ -r "$dovecot_conf" ]; then ssl_cert_file=$(awk -F'[[:space:]]*=[[:space:]]*' '/^ssl_cert_file/ { print $2}' $dovecot_conf) ssl_cert_file=$(awk -F'[[:space:]]*=[[:space:]]*' '/^ssl_key_file/ { print $2}' $dovecot_conf) fi CERTFILE=${ssl_cert_file:-$CERTDIR/server.pem} KEYFILE=${ssl_key_file:-$KEYDIR/server.key} if [ -e "$CERTFILE" ]; then echo "Keeiping existing $CERTFILE" exit 0 fi if [ -e "$KEYFILE" ]; then echo "Keeiping existing $KEYFILE" exit 0 fi if [ ! -c /dev/urandom ] && [ ! -c /dev/random ]; then echo "No /dev/urandom or /dev/random so ssl cert not created" exit 1 fi $OPENSSL req -new -x509 -nodes -config $OPENSSLCONFIG -out $CERTFILE -keyout $KEYFILE -days 365 || exit 2 chmod 0600 $KEYFILE echo $OPENSSL x509 -subject -fingerprint -noout -in $CERTFILE || exit 2