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

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

prefix="/usr"

if test -f /usr/share/pop3d.pem
then
	echo "/usr/share/pop3d.pem already exists."
	exit 1
fi

cp /dev/null /usr/share/pop3d.pem
chmod 600 /usr/share/pop3d.pem
chown root /usr/share/pop3d.pem

cleanup() {
	rm -f /usr/share/pop3d.pem
	rm -f /usr/share/pop3d.rand
	exit 1
}

dd if=/dev/urandom of=/usr/share/pop3d.rand count=1 2>/dev/null
/usr/bin/openssl req -new -x509 -days 365 -nodes \
	-config /etc/courier-imap/pop3d.cnf -out /usr/share/pop3d.pem -keyout /usr/share/pop3d.pem || cleanup
/usr/bin/openssl gendh -rand /usr/share/pop3d.rand 512 >>/usr/share/pop3d.pem || cleanup
/usr/bin/openssl x509 -subject -dates -fingerprint -noout -in /usr/share/pop3d.pem || cleanup
rm -f /usr/share/pop3d.rand
