# Load CARNet Tools
. /usr/share/carnet-tools/functions.sh
+
+function move_certs() {
+ if [ -f /etc/dovecot/private/dovecot.pem -a ! -f /etc/dovecot/private/dovecot.key ]; then
+ mv -f /etc/dovecot/private/dovecot.pem /etc/dovecot/private/dovecot.key || true
+ fi
+
+ if [ -f /etc/dovecot/dovecot.pem ]; then
+ mv -f /etc/dovecot/dovecot.pem /etc/dovecot/private/dovecot.pem || true
+ fi
+}
+
+function put_new_certs() {
+# postavlja cert i key na nove putanje iz bustera
+ cp_check_and_sed '#ssl_key = </etc/dovecot/private/dovecot.pem' \
+ 's|#ssl_key = </etc/dovecot/private/dovecot.pem|ssl_key = </etc/dovecot/private/dovecot.key|g' \
+ /etc/dovecot/conf.d/10-ssl.conf || true
+ cp_check_and_sed '#ssl_cert = </etc/dovecot/dovecot.pem' \
+ 's|#ssl_cert = </etc/dovecot/dovecot.pem|ssl_cert = </etc/dovecot/private/dovecot.pem|g' \
+ /etc/dovecot/conf.d/10-ssl.conf || true
+ cp_check_and_sed 'ssl_cert = </etc/dovecot/dovecot.pem' \
+ 's|ssl_cert = </etc/dovecot/dovecot.pem|ssl_cert = </etc/dovecot/private/dovecot.pem|g' \
+ /etc/dovecot/conf.d/10-ssl.conf || true
+ cp_check_and_sed 'ssl_key = </etc/dovecot/private/dovecot.pem' \
+ 's|ssl_key = </etc/dovecot/private/dovecot.pem|ssl_key = </etc/dovecot/private/dovecot.key|g' \
+ /etc/dovecot/conf.d/10-ssl.conf || true
+ cp_check_and_sed '#ssl_cert = </etc/dovecot/private/dovecot.pem' \
+ 's|#ssl_cert = </etc/dovecot/private/dovecot.pem|ssl_cert = </etc/dovecot/private/dovecot.pem|g' \
+ /etc/dovecot/conf.d/10-ssl.conf || true
+ cp_check_and_sed '#ssl_key = </etc/dovecot/private/dovecot.key' \
+ 's|#ssl_key = </etc/dovecot/private/dovecot.key|ssl_key = </etc/dovecot/private/dovecot.key|g' \
+ /etc/dovecot/conf.d/10-ssl.conf || true
+}
+
+
cp_check_and_sed '#disable_plaintext_auth' \
's/#disable_plaintext_auth/disable_plaintext_auth/g' \
/etc/dovecot/conf.d/10-auth.conf || true
's/disable_plaintext_auth.*$/disable_plaintext_auth = no/g' \
/etc/dovecot/conf.d/10-auth.conf || true
-if ! grep -q "mail_privileged_group.*mail$" /etc/dovecot/conf.d/10-mail.conf \
+if ! grep -q "mail_privileged_group.*mail$" /etc/dovecot/conf.d/10-mail.conf; then
cp_check_and_sed 'mail_privileged_group' \
's/mail_privileged_group.*$/mail_privileged_group = mail/g' \
/etc/dovecot/conf.d/10-mail.conf || true
's/^ssl = no/ssl = yes/g' \
/etc/dovecot/conf.d/10-ssl.conf || true
-if ! grep -q ^ssl_cert /etc/dovecot/conf.d/10-ssl.conf \
- && ! grep -q ^ssl_key /etc/dovecot/conf.d/10-ssl.conf; then
-
- if [ ! -f /etc/dovecot/dovecot.pem -a ! -f /etc/dovecot/private/dovecot.pem ]; then
- echo "CN: Generating certificate and key..."
- /usr/share/dovecot-cn/mkcert.sh || true
- fi
- cp_check_and_sed '#ssl_cert = </etc/dovecot/dovecot.pem' \
- 's|#ssl_cert = </etc/dovecot/dovecot.pem|ssl_cert = </etc/dovecot/dovecot.pem|g' \
- /etc/dovecot/conf.d/10-ssl.conf || true
- cp_check_and_sed '#ssl_key = </etc/dovecot/private/dovecot.pem' \
- 's|#ssl_key = </etc/dovecot/private/dovecot.pem|ssl_key = </etc/dovecot/private/dovecot.pem|g' \
- /etc/dovecot/conf.d/10-ssl.conf || true
- # negdje se pojavljuje dovecot.key umjesto dovecot.pem
- cp_check_and_sed 'ssl_key = </etc/dovecot/private/dovecot.key' \
- 's|ssl_key = </etc/dovecot/private/dovecot.key|ssl_key = </etc/dovecot/private/dovecot.pem|g' \
- /etc/dovecot/conf.d/10-ssl.conf || true
+dovecert="$(doveconf ssl_cert)"
+dovekey="$(doveconf ssl_key)"
+
+if [ -n "$dovecert" -a -n "$dovekey" ]; then
+ echo -n "CN: Opcije ssl_cert i ssl_key su pronađene"
+
+ cfile=$(grep -l ^ssl_cert /etc/dovecot/conf.d/*.conf | tail -1)
+ kfile=$(grep -l ^ssl_key /etc/dovecot/conf.d/*.conf | tail -1)
+
+ if grep -q ^ssl_cert $cfile && grep -q ^ssl_key $kfile; then
+ if [ "$cfile" != "/etc/dovecot/conf.d/10-ssl.conf" -o "$kfile" != "/etc/dovecot/conf.d/10-ssl.conf" ]; then
+ echo " izvan 10-ssl.conf (u $cfile), preskačem rekonfiguraciju..."
+ else
+ echo " u /etc/dovecot/conf.d/10-ssl.conf. Postavljam default vrijednosti iz Debiana 10..."
+ put_new_certs
+ move_certs
+ fi
+ fi
+else
+ echo "CN: ssl_cert i ssl_key nisu definirani, postavljam default vrijednosti iz Debiana 10!"
+ # ako postoji, pomaknut ćemo stari certifikat na novo mjesto i preimenovati kljuc
+ # ako ne postoje certifikati generiraj ih i postavi na prava mjesta
+
+ move_certs
+
+ if [ ! -f /etc/dovecot/private/dovecot.pem -a ! -f /etc/dovecot/private/dovecot.key ]; then
+ echo "CN: Pravim certifikat i kljuc i postavljam ih u /etc/dovecot/private..."
+ /usr/share/dovecot-cn/mkcert.sh || true
+ fi
+ put_new_certs
fi
+
+
### buster ima ssl_min_protocol umjesto ssl_protocols
# ne radimo ništa ako već postoji ^ssl_min_protocol = TLS*, možda je sistemac smanjivao level TLS-a
# Generates a self-signed certificate.
# Edit dovecot-openssl.cnf before running this.
+# edit by Zeljko Boros
umask 077
OPENSSL=${OPENSSL-openssl}
SSLDIR=${SSLDIR-/etc/ssl}
OPENSSLCONFIG=${OPENSSLCONFIG-/usr/share/dovecot-cn/dovecot-openssl.cnf}
-CERTDIR=/etc/dovecot
-KEYDIR=/etc/dovecot/private
+CERTKEYDIR=/etc/dovecot/private
-CERTFILE=$CERTDIR/dovecot.pem
-KEYFILE=$KEYDIR/dovecot.pem
+CERTFILE=$CERTKEYDIR/dovecot.pem
+KEYFILE=$CERTKEYDIR/dovecot.key
-if [ ! -d $CERTDIR ]; then
+if [ ! -d $CERTKEYDIR ]; then
echo "$SSLDIR/certs directory doesn't exist"
exit 1
fi
-if [ ! -d $KEYDIR ]; then
+if [ ! -d $CERTKEYDIR ]; then
echo "$SSLDIR/private directory doesn't exist"
exit 1
fi