#! /bin/sh
#
# $Id: mkimapdcert.in,v 1.5 2005/06/29 18:01:17 mrsam Exp $
#
# Copyright 2000-2005 Double Precision, Inc.  See COPYING for
# distribution information.
#
# This is a short script to quickly generate a self-signed X.509 key for
# IMAP over SSL.  Normally this script would get called by an automatic
# package installation routine.

test -x /usr/sbin/openssl || exit 0

prefix="/usr/local"
SSLDIR="/etc/ssl/private"

if test -f ${SSLDIR}/imapd.pem
then
	echo "${SSLDIR}/imapd.pem already exists."
	exit 1
fi

umask 077
cp /dev/null ${SSLDIR}/imapd.pem
chmod 600 ${SSLDIR}/imapd.pem
chown _courier ${SSLDIR}/imapd.pem

cleanup() {
	rm -f ${SSLDIR}/imapd.pem
	rm -f ${SSLDIR}/imapd.rand
	exit 1
}

cd ${SSLDIR}
dd if=/dev/arandom of=${SSLDIR}/imapd.rand count=1 2>/dev/null
/usr/sbin/openssl req -new -x509 -days 365 -nodes \
	-config /etc/courier/imapd.cnf -out ${SSLDIR}/imapd.pem -keyout ${SSLDIR}/imapd.pem || cleanup
/usr/sbin/openssl gendh -rand ${SSLDIR}/imapd.rand 512 >>${SSLDIR}/imapd.pem || cleanup
/usr/sbin/openssl x509 -subject -dates -fingerprint -noout -in ${SSLDIR}/imapd.pem || cleanup
rm -f ${SSLDIR}/imapd.rand
