#!/bin/bash
### MODUL-NR. 007 ###
# (c) August 2008 Stefan Schäfer / FSP Computer & Netzwerke
# (c) 2009-2017 Stefan Schäfer / invis-server.org / stefan@invis-server.org
# (c) 2013,2014 Dimitri Asarowski / invis-server.org / dimitri@invis-server.org
# (c) 2013-2017 Ingo Göppert / invis-server.org / ingo@invis-server.org

# License: GPLv3
# Questions: info@invis-server.org

# Das Modul installiert alle Komponenten der Mailserverfunktionen und richtet diese ein.

# This module installs all components of the mailserver functionality

# Dieses Programm ist freie Software. Sie können es unter den Bedingungen der 
# GNU General Public License, wie von der Free Software Foundation veröffentlicht,
# weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder
# (nach Ihrer Option) jeder späteren Version.

# Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen
# von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite 
# Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 
# Details finden Sie in der GNU General Public License.

# Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem 
# Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>. 

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# include functions
source $SINEINCLUDEDIR/functions

modulename=`basename $0`

clear
windowtitle="Mailserver Setup"
pgsubtitle="Installation und Einrichtung aller Mailserverkomponenten"


zypper -n install -n `cat $PACKAGELISTDIR/mailserver_additional` 2>&1| tee -a $LOGFILE | pgbox
zyppercheck ${PIPESTATUS[0]}

# amavisd-new und clamav einrichten
av=`getconfdata "AV" "2"`
if [[ $av == "clamav" ]]; then
        cp $TEMPLATEDIR/$modulename/amavisd/amavisd_clamav.conf /etc/amavisd.conf 2>&1| tee -a $LOGFILE | pgbox
else
	cp $TEMPLATEDIR/$modulename/amavisd/amavisd.conf /etc/amavisd.conf 2>&1| tee -a $LOGFILE | pgbox
fi

# Der Dienst freshclam wird auf jeden Fall gestartet, da Clamav zur Standart Installation gehoert
# und er von amavis immer als secondary Virenscanner genutzt wird.
chkservice "freshclam"

# Rechte am Virusmails-Quarantaene-Verzeichnis anpassen
chown -R vscan:vscan /var/spool/amavis 2>&1| tee -a $LOGFILE | pgbox
chmod g+rwx /var/spool/amavis/virusmails 2>&1| tee -a $LOGFILE | pgbox

# Spam-Assassin aktualisieren
sa-update

# setting up amavisd
path="/etc"
file="amavisd.conf"
fqdn=`getconfdata "FQDN" "2"`
string="invis.invis-net.loc%$fqdn"
changevalues $path $file "$string"
domain=`getconfdata "Domain" "2"`
string="invis-net.loc%$domain"
changevalues $path $file "$string"
chkservice "amavis"

# Kopieren der Postfix-Konfigurationsdateien
cp $TEMPLATEDIR/$modulename/postfix/* /etc/postfix 2>&1| tee -a $LOGFILE
    
## SOGo, Group-e, Zarafa oder Roundcube
whichgroupware=`getconfdata "Groupware" "2"`
case "$whichgroupware" in

    "sogo"|"roundcube"|"tine20" )
	# Dovecot einrichten 
	pgsubtitle="Dovecot IMAP wird installiert und eingerichtet"
	zypper -n install -n dovecot22 2>&1| tee -a $LOGFILE | pgbox
	zyppercheck ${PIPESTATUS[0]}
	# copy own apparmo profiles
	cp -r $TEMPLATEDIR/$modulename/dovecot/apparmor/* /etc/apparmor.d/ 2>&1| tee -a $LOGFILE
	systemctl restart apparmor.service 2>&1| tee -a $LOGFILE
	# Konfiguration anpassen
	cp -r $TEMPLATEDIR/$modulename/dovecot/ad/* /etc/dovecot/ 2>&1| tee -a $LOGFILE
	path="/etc/dovecot/conf.d"
	file="15-lda.conf"
	domain=`getconfdata "Domain" "2"`
	string="invis-net.loc%$domain"
	changevalues $path $file "$string"
	#Dovecot-LDAP Anbindung konfigurieren
	path="/etc/dovecot/"
	file="dovecot-ldap.conf.ext"
	basedn=`getconfdata "baseDN" "2"`
	string="dc=invis-net,dc=loc%$basedn"
	changevalues $path $file "$string"

	string="DC=invis-net,DC=loc%$basedn"
	changevalues $path $file "$string"

	ldapadminpw=`getconfdata "LDAPAdminPW" "2"`
	string="admin-secret%$ldapadminpw"
	changevalues $path $file "$string"

	ldaphost=`hostname -f`
	string="ldaphostname%$ldaphost"
	changevalues $path $file "$string"

	# Benutzerkonto fuer das Mailhandling anlegen
	groupadd -r -g 399 vmail| tee -a $LOGFILE | pgbox
	useradd -r -u 399 -d /var/spool/mail -c "Benutzerkonto fuer Dovecot Mailhandling" -s /bin/false -g vmail vmail| tee -a $LOGFILE | pgbox
	
	# Rechte an Verzeichnissen so anpassen, dass dovecot darin schreiben darf
	chown vmail:mail /var/lib/dovecot 2>&1| tee -a $LOGFILE | pgbox
	chmod 0775 /var/lib/dovecot 2>&1| tee -a $LOGFILE | pgbox
	chown -R :vmail /var/spool/mail 2>&1| tee -a $LOGFILE | pgbox

	# Dictionary Verzeichnis fuer IMAP ACLs anlegen.
	mkdir /var/lib/dovecot/db
	chown vmail.vmail /var/lib/dovecot/db
	
	# Dovecots Modulpfad an Systemarchitektur anpassen
	if [[ $SYSLIBDIR == "/usr/lib64" ]]; then 
	    string="/usr/lib/dovecot/modules%$SYSLIBDIR/dovecot/modules"
	    changevalues $path $file "$string"
	fi
	usermod -a -G pkeys dovecot 2>&1| tee -a $LOGFILE | pgbox
	chkservice "dovecot"

	# Postfix konfiguration kopieren
	rm -f `ls /etc/postfix/ |grep "main.zarafa"`
	mv /etc/postfix/main.dovecot /etc/postfix/main.cf 2>&1| tee -a $LOGFILE | pgbox
	
	# Transportweg fuer lokale emails festlegen
	echo "$domain	dovecot:" >> /etc/postfix/relay 2>&1| tee -a $LOGFILE | pgbox
	postmap /etc/postfix/relay 2> /dev/null
	;;
    "kopano" )
	pgsubtitle="Kopano Groupware und Mailserver wird installiert und eingerichtet"
	# Festlegen, welche Pakete installiert werden sollen.
	kopanorepo=`getconfdata "KopanoRepo" "2"`
	if [[ $kopanorepo == "limited" ]]; then
	    zypper ar -f $REPODIR/kopano-limited.repo 2>&1| tee -a $LOGFILE | pgbox
	    zypper ar -f $REPODIR/kopano-webapp-limited.repo 2>&1| tee -a $LOGFILE | pgbox
	    msgbox "Die Kopano Limited Repositories benötigen Zugangsdaten, diese werden nachfolgend abgefragt." "Kopano Repository"
	    zypper --gpg-auto-import-keys ref --repo Kopano-openSUSE_limited
	    zypper --gpg-auto-import-keys ref --repo Kopano-Webapp-Limited
	    if [[ $? != 0 ]]; then
		msgbox "Ohne gültige Zugangsdaten kann das Kopano Limited Repository nicht hinzugefügt werden. Die weitere Installation des Servers wird abgebrochen." "Ungültige Zugangsdaten"
		exit 1
	    fi
	    zypper -n install -n `cat $PACKAGELISTDIR/kopanolimited` 2>&1| tee -a $LOGFILE | pgbox
	    zyppercheck ${PIPESTATUS[0]}
	else
	    zypper -n install -n `cat $PACKAGELISTDIR/kopanoobs` 2>&1| tee -a $LOGFILE | pgbox
	    zyppercheck ${PIPESTATUS[0]}
	    # Propmap file is in /usr/share/kopano
	    ln -s /usr/share/kopano/ldap.propmap.cfg /etc/kopano/
	fi

	# Kopano nur installieren, Konfigurieren und Starten der Dienste erfolgt im Groupware Abschnitt
	# Symbolischen Link auf das z-push2 Verzeichnis anlegen
	ln -s /srv/www/htdocs/z-push2 /srv/www/htdocs/z-push
	
	rm -f `ls /etc/postfix/ |grep "main.[dovecot|cyrus]"` 2>&1| tee -a $LOGFILE | pgbox
	mv /etc/postfix/main.zarafa /etc/postfix/main.cf 2>&1| tee -a $LOGFILE | pgbox
	
	;;
esac
pgsubtitle="Weitere Konfigurationsanpassungen am Mailserver-Setup werden durchgeführt"
    
# Postfix Konfiguration anpassen
path="/etc/postfix"
file="main.cf"
fqdn=`getconfdata "FQDN" "2"`
string="invis.invis-net.loc%$fqdn"
changevalues $path $file "$string"

domain=`getconfdata "Domain" "2"`
string="invis-net.loc%$domain"
changevalues $path $file "$string"

nw=`getconfdata "Networkbase" "2"`
shortnmask=`getconfdata "NMask-short" "2"`
string="192.168.220.0/24%$nw/$shortnmask"
changevalues $path $file $string
    
# Wenn Zugangsdaten für mailrelay bekannt sasl_passwd erzeugen
mailrelay=`getconfdata "SMTPRELAY" "2"`
smtpport=`getconfdata "SMTPRELAY" "3"`
if [[ $mailrelay != "none" ]]; then
    string="\[mail.example.org\]%$mailrelay:$smtpport"
    changevalues $path $file "$string"
    smtpun=`getconfdata "SMTPRUN" "2"`
    smtppw=`getconfdata "SMTPRPW" "2"`
    echo "$mailrelay:$smtpport		$smtpun:$smtppw" >> /etc/postfix/sasl_passwd
    /usr/sbin/postmap /etc/postfix/sasl_passwd 2> /dev/null
fi
    
# canonical-files anpassen
file="canonical"
fqdn=`getconfdata "FQDN" "2"`
string="invis.invis-net.loc%$fqdn"
changevalues $path $file "$string"

domain=`getconfdata "Domain" "2"`
string="invis-net.loc%$domain"
changevalues $path $file "$string"
postmap $path/$file
files=(s-canonical.cf r-canonical.cf ldap-users.cf ldap-users2.cf ldap-groups.cf)

for datei in ${files[*]}; do
    #echo $datei
    basedn=`getconfdata "baseDN" "2"`
    string="dc=invis-net,dc=loc%$basedn"
    changevalues $path $datei "$string"
    string="DC=invis-net,DC=loc%$basedn"
    changevalues $path $datei "$string"

    adminpw=`getconfdata "LDAPAdminPW" "2"`
    string="admin-secret%$adminpw"
    changevalues $path $datei "$string"

    string="invis-net.loc%$domain"
    changevalues $path $datei "$string"
done

# Postfix und saslauthd neustarten und in Runlevel aufnehmen
chkservice "postfix"
# saslauthd only if kopano is installed
if [[ $whichgroupware == "kopano" ]]; then
    chkservice "saslauthd"
fi

# Fetchmail einrichten
# Sysconfig Datei bearbeiten
cp $TEMPLATEDIR/$modulename/fetchmail/fetchmail.service /etc/systemd/system/
systemctl daemon-reload
path="/etc/systemd/system"
file="fetchmail.service"
string="localdomain%$domain"
changevalues $path $file "$string"

# Dummy fetchmailrc anlegen.
fetchmailrc="/var/lib/cornaz/build/.fetchmailrc"
echo 'poll pop.example.com protocol pop3 user dummy pass . is junk here' > $fetchmailrc
chown wwwrun.www $fetchmailrc
chmod 0660 $fetchmailrc
    
# fetchcopy ausfuehren
/var/lib/cornaz/bin/fetchcopy				2>&1|tee -a $LOGFILE |pgbox
chmod 0700 /var/lib/cornaz/inuse/.fetchmailrc		2>&1|tee -a $LOGFILE |pgbox
chown fetchmail /var/lib/cornaz/inuse/.fetchmailrc	2>&1|tee -a $LOGFILE |pgbox

# fetchmail starten und in Autostart aufnehmen
chkservice "fetchmail"

