850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen#!/bin/sh
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen# Generates a self-signed certificate.
e7b2b71d91da7f07439247fe0df02921ab13f61eTimo Sirainen# Edit dovecot-openssl.cnf before running this.
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
e8e00a579e12dcb57b0c4077fed2df3f4106ae5aTimo Sirainenumask 077
850f5063cc1228fa19e91323b51e1f997577f3f8Timo SirainenOPENSSL=${OPENSSL-openssl}
850f5063cc1228fa19e91323b51e1f997577f3f8Timo SirainenSSLDIR=${SSLDIR-/etc/ssl}
850f5063cc1228fa19e91323b51e1f997577f3f8Timo SirainenOPENSSLCONFIG=${OPENSSLCONFIG-dovecot-openssl.cnf}
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
47229f40be81e07162694e2993ded30818a6708aTimo SirainenCERTDIR=$SSLDIR/certs
47229f40be81e07162694e2993ded30818a6708aTimo SirainenKEYDIR=$SSLDIR/private
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
47229f40be81e07162694e2993ded30818a6708aTimo SirainenCERTFILE=$CERTDIR/dovecot.pem
47229f40be81e07162694e2993ded30818a6708aTimo SirainenKEYFILE=$KEYDIR/dovecot.pem
47229f40be81e07162694e2993ded30818a6708aTimo Sirainen
47229f40be81e07162694e2993ded30818a6708aTimo Sirainenif [ ! -d $CERTDIR ]; then
4827dd103cc836a57aae3ca1aa9409ce45f648c3Timo Sirainen echo "$SSLDIR/certs directory doesn't exist"
47229f40be81e07162694e2993ded30818a6708aTimo Sirainen exit 1
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenfi
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
47229f40be81e07162694e2993ded30818a6708aTimo Sirainenif [ ! -d $KEYDIR ]; then
4827dd103cc836a57aae3ca1aa9409ce45f648c3Timo Sirainen echo "$SSLDIR/private directory doesn't exist"
47229f40be81e07162694e2993ded30818a6708aTimo Sirainen exit 1
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenfi
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenif [ -f $CERTFILE ]; then
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen echo "$CERTFILE already exists, won't overwrite"
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen exit 1
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenfi
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenif [ -f $KEYFILE ]; then
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen echo "$KEYFILE already exists, won't overwrite"
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen exit 1
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenfi
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainen
46307f44857cc2a72f4b79538cb5fa0c6e8eb32bTimo Sirainen$OPENSSL req -new -x509 -nodes -config $OPENSSLCONFIG -out $CERTFILE -keyout $KEYFILE -days 365 || exit 2
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenchmod 0600 $KEYFILE
850f5063cc1228fa19e91323b51e1f997577f3f8Timo Sirainenecho
0f0dfe241add9fc4d37df7a9862e40d7de8555b5Timo Sirainen$OPENSSL x509 -subject -fingerprint -noout -in $CERTFILE || exit 2