#!/bin/bash

H=$1

if [ "$H" == "" ]; then
   H=`/opt/pdadmin/bin/hostname.pl`
fi

test -e /opt/pdadmin/sslcerts/$H-key || {
    echo "/opt/pdadmin/sslcerts/$H-key not found"
    echo "usage: $0 <hostname>"
    exit 1
}

#qmail StartTTLS
touch /var/qmail/control/servercert.pem
chown qmaild:nofiles /var/qmail/control/servercert.pem
chmod 440 /var/qmail/control/servercert.pem
cat /opt/pdadmin/sslcerts/$H-key /opt/pdadmin/sslcerts/$H-cert /opt/pdadmin/sslcerts/$H-cacert > /var/qmail/control/servercert.pem

#CourierIMAP
sed 's,TLS_TRUSTCERTS.*,TLS_TRUSTCERTS=/usr/local/pd-admin2/share/imapd.cacert,' /usr/local/pd-admin2/etc/imapd-ssl > /usr/local/pd-admin2/etc/imapd-ssl~
sed 's,TLS_TRUSTCERTS.*,TLS_TRUSTCERTS=/usr/local/pd-admin2/share/imapd.cacert,' /usr/local/pd-admin2/etc/pop3d-ssl > /usr/local/pd-admin2/etc/pop3d-ssl~
mv /usr/local/pd-admin2/etc/imapd-ssl~ /usr/local/pd-admin2/etc/imapd-ssl
mv /usr/local/pd-admin2/etc/pop3d-ssl~ /usr/local/pd-admin2/etc/pop3d-ssl

touch /usr/local/pd-admin2/share/imapd.pem
chown qmaild:nofiles /usr/local/pd-admin2/share/imapd.pem
chmod 440 /usr/local/pd-admin2/share/imapd.pem
cat /opt/pdadmin/sslcerts/$H-key /opt/pdadmin/sslcerts/$H-cert > /usr/local/pd-admin2/share/imapd.pem
ln -s /usr/local/pd-admin2/share/imapd.pem /usr/local/pd-admin2/share/pop3d.pem
cat /opt/pdadmin/sslcerts/$H-cacert > /usr/local/pd-admin2/share/imapd.cacert

mkdir -p /etc/ssl/
cat /opt/pdadmin/sslcerts/$H-cacert > /etc/ssl/cert.pem

#runfiles installieren
cd /usr/src
wget http://download.pd-admin.de/runfiles-ssl.tar.gz
tar xzf runfiles-ssl.tar.gz
mv courier-imap-ssl courier-pop3-ssl qmail-smtpSd  /service

grep TLSRSACertificateFile /usr/local/pd-admin2/etc/proftpd.conf > /dev/null || {
cat <<__ >> /usr/local/pd-admin2/etc/proftpd.conf
<IfModule mod_tls.c>
TLSEngine                  on
TLSProtocol SSLv3 TLSv1
TLSRSACertificateFile      /opt/pdadmin/sslcerts/$H-cert
TLSRSACertificateKeyFile   /opt/pdadmin/sslcerts/$H-key
TLSCACertificateFile       /opt/pdadmin/sslcerts/$H-cacert
TLSVerifyClient            off
TLSRequired                off
TLSRenegotiate none
</IfModule>
__
killall proftpd
}
cd /usr/src
wget download.pd-admin.de/.cron_tmprsadh.pl
perl .cron_tmprsadh.pl
/var/qmail/bin/update_tmprsadh
