#!/bin/bash
#
#  dovecot-2.2.sh -- configure dovecot 2.2 in pda/SE
#  2013-08-02 stvo
#  $Id: dovecot-2.2.sh,v 1.17 2024/08/26 11:14:51 testx Exp testx $
#
set -e
#set -x
INSTDIR=/usr/local/pd-admin2
INSTDIR22=${INSTDIR}/dovecot-2.2
DATE=`date +%Y-%m-%d-%H:%M:%S`

if ${INSTDIR22}/bin/doveconf namespace/inbox/prefix 2>/dev/null \
   | grep -q 'prefix = INBOX\.'; then
   touch ${INSTDIR}/CONFIGURE/.migrated-from-ci
fi

default_process_limit=$(cat ${INSTDIR}/CONFIGURE/.dovecot_default_process_limit 2>/dev/null) || default_process_limit=1000

echo "default_process_limit = <$default_process_limit>"

mail_max_userip_connections=$(cat ${INSTDIR}/CONFIGURE/.dovecot_mail_max_userip_connections 2>/dev/null) || mail_max_userip_connections=50

echo "mail_max_userip_connections = <$mail_max_userip_connections>"

function remove_all_but_the_three_latest_dovecot_conf_backups ()
{
   (
      cd ${INSTDIR22}/etc
      dirs=($(printf "%s\n" dovecot.20??-??-??-??:??:?? | sort -r))
      unset dirs[0]
      unset dirs[1]
      unset dirs[2]
      set +u # prevent error due to unbound variable
      for d in "${dirs[@]}"
      do
         printf "deleting configuration backup <%s>\n" "$d"
         rm -fr "$d"
      done
   )
}

if [ -d ${INSTDIR22}/etc/dovecot ]; then
   echo "Bestehende Konfiguration wird nach dovecot.${DATE} verschoben ..."
   mv ${INSTDIR22}/etc/dovecot ${INSTDIR22}/etc/dovecot.${DATE}
   echo "OK."
   remove_all_but_the_three_latest_dovecot_conf_backups
fi

echo "Konfigurations-Template wird kopiert ..."
cp -ax ${INSTDIR22}/etc/dovecot.tmpl ${INSTDIR22}/etc/dovecot
echo "OK."

echo "Datenbankpassword wird ermittelt ..."

VADMINPASSWD=`${INSTDIR}/bin/perl - <<_
if (-f '/opt/pdadmin/etc/pdadmin.conf') {
   do '/opt/pdadmin/etc/pdadmin.conf';
}
elsif (-x '/etc/admin/config.pl') {
   do '/etc/admin/config.pl';
}
else {
   die "neither pdadmin.conf nor config.pl found\n";
}
print "\\$password\n";
_`
#echo ${VADMINPASSWD}

POPUSER_UID=`${INSTDIR}/bin/perl - <<_
   my \\$p = (getpwnam('popuser'))[2];
   die "getpwnam failed for user popuser\n" if not defined \\$p;
   print "\\$p\n";
_`
#echo $POPUSER_UID

POPUSER_GID=`${INSTDIR}/bin/perl - <<_
   my \\$p = (getpwnam('popuser'))[3];
   die "getpwnam failed for user popuser\n" if not defined \\$p;
   print "\\$p\n";
_`
#echo $POPUSER_GID

echo "OK."

echo "Datenbankzugriff wird eingerichtet ..."
cat ${INSTDIR22}/etc/dovecot/dovecot-sql.conf.ext.tmpl \
   | sed -e "s/%VADMINPASSWD%/${VADMINPASSWD}/" \
      -e "s/%POPUSER_UID%/${POPUSER_UID}/" \
      -e "s/%POPUSER_GID%/${POPUSER_GID}/" \
   > ${INSTDIR22}/etc/dovecot/dovecot-sql.conf.ext
chmod go= ${INSTDIR22}/etc/dovecot/dovecot-sql.conf.ext
echo "OK."

echo "andere Variablen werden gesetzt ..."
cat ${INSTDIR22}/etc/dovecot/conf.d/10-master.conf \
   | sed -e "s/%default_process_limit%/${default_process_limit}/" \
   | sed -e "s/%mail_max_userip_connections%/${mail_max_userip_connections}/" \
   > ${INSTDIR22}/etc/dovecot/conf.d/10-master.conf_
mv ${INSTDIR22}/etc/dovecot/conf.d/10-master.conf{_,}
echo "OK."

echo "Berechtigungen werden angepasst ..."
chown -hR root:popuser ${INSTDIR22}/etc/dovecot
chmod -R g+r,o= ${INSTDIR22}/etc/dovecot
chmod 750 ${INSTDIR22}/etc/dovecot ${INSTDIR22}/etc/dovecot/conf.d

echo "OK."

#
#  enable ssl only if certificate is found
#
DCBASE=${INSTDIR22}
#
#  enable SSL/TLS for POP3 & IMAP
#
if [ -f /usr/local/pd-admin2/share/imapd.pem ]; then
   echo "certificate found, enable ssl/tls"
   cat >> $DCBASE/etc/dovecot/conf.d/10-ssl.conf <<_
ssl = yes
ssl_key = </usr/local/pd-admin2/share/imapd.pem
ssl_cert = </usr/local/pd-admin2/share/imapd.pem
_

   min_tls_version=$($INSTDIR/bin/mysql -u vadmin -p$VADMINPASSWD vadmin -NBe "select varvalue from config where varname = 'min_tls_version';")
   echo "min_tls_version = <$min_tls_version>"
   if [[ ! -z $min_tls_version && ! $min_tls_version = none ]]; then
      cat >> $DCBASE/etc/dovecot/conf.d/10-ssl.conf <<_
ssl_min_protocol = $min_tls_version
_
   fi

   if [ ! -e ${INSTDIR22}/etc/dovecot/dh.pem-factory ]
   then
      cat > ${INSTDIR22}/etc/dovecot/dh.pem-factory <<_
-----BEGIN DH PARAMETERS-----
[payload removed: 700 characters in 11 lines]
-----END DH PARAMETERS-----
_
   fi

   mv ${INSTDIR22}/etc/dovecot/dh.pem{-factory,}
   cat >> $DCBASE/etc/dovecot/conf.d/10-ssl.conf <<_
ssl_dh = <${INSTDIR22}/etc/dovecot/dh.pem
_

   if [ -f /usr/local/pd-admin2/share/imapd.cacert ]; then
      cat >> $DCBASE/etc/dovecot/conf.d/10-ssl.conf <<_
ssl_ca = </usr/local/pd-admin2/share/imapd.cacert
_
   fi
else
   echo "no certificate found, ssl/tls disabled"
   cat >> $DCBASE/etc/dovecot/conf.d/10-ssl.conf <<_
ssl = no
_
fi

#
#  append the INBOX-Prefix if there is a migration from
#  Courier-IMAP
#

if [ -f ${INSTDIR}/CONFIGURE/.migrated-from-ci ]; then
cat >> $DCBASE/etc/dovecot/conf.d/10-mail.conf <<_
#
#  keep INBOX-prefix for compat. with Courier-IMAP
#
namespace inbox {
   prefix = INBOX.
}
_

fi

#
#  BNK/SE#162
#
cat >> $DCBASE/etc/dovecot/conf.d/15-lda.conf <<_
submission_host = 127.0.0.1
_

#
#  BNK/SE#160
#
cat >> $DCBASE/etc/dovecot/dovecot.conf <<_
service stats {
   unix_listener stats-writer {
      group = popuser
      mode = 0660
   }
}
_

${INSTDIR22}/bin/sievec ${DCBASE}/etc/dovecot/sieve-global.conf
chgrp popuser ${DCBASE}/etc/dovecot/sieve-global.conf.svbin

echo "Dovecot-Konfiguration abgeschlossen."
