Subversion Repositories ALCASAR

Rev

Rev 3021 | Rev 3038 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

#!/bin/bash

###########################################################################################
##                                  ALCASAR MAIL SERVICE CONFIGURATION
##
##      Script by K@M3L & T3RRY (LaPlateform), joss_p & Rexy
##  This script configure PostFix
##              0 : no email autoregistration
##              1 : PostFix is the SMTP server
##              2 : PostFix relay to an other SMTP server
##              2 : PostFix use an external email address (with SASL)
###########################################################################################

# ****** Paths *******
SED="/bin/sed -i"
CONF_FILE="/usr/local/etc/alcasar.conf"
POSTFIX_CONF_FILE="/etc/postfix/main.cf"
LOCAL_IPTABLE_FILE="/usr/local/etc/alcasar-iptables-local.sh"
SASLPATH="/etc/postfix/sasl"
smtpIP="0.0.0.0/0"
usage="Usage: alcasar-mail_install.sh -h|-0|-1|-2|-3"

nb_args=$#
if [ $nb_args -eq 0 ] # apply alcasar.conf
then
        mail=`grep ^MAIL= $CONF_FILE|cut -d"=" -f2`
        if [ "$mail" = "off" ]; then
                TYPE_MAIL=0
        else
                TYPE_MAIL=`grep ^MAIL_TYPE= $CONF_FILE|cut -d"=" -f2`
                smtpPort=`grep ^MAIL_SMTP_PORT= $CONF_FILE|cut -d"=" -f2`
                smtpIP=`grep ^MAIL_SMTP_IP= $CONF_FILE|cut -d"=" -f2`
                mailAddr=`grep ^MAIL_ADDR= $CONF_FILE|cut -d"=" -f2`
                mailMdp=`grep ^MAIL_PASSWORD= $CONF_FILE|cut -d"=" -f2`
                adminMail=`grep ^MAIL_ADMIN= $CONF_FILE|cut -d"=" -f2`
                whiteDomain=`grep ^MAIL_WHTEDOMAIN= $CONF_FILE|cut -d"=" -f2`
        fi
else # apply args
        if [ "$1" = "-h" ] || [ "$1" = "--h" ]; then
                echo $usage
                exit 0
        fi
        while getopts ":h:s:p:m:o:a:w:0123" option
        do
                case $option in
                        0)
                                TYPE_MAIL=0
                        ;;
                        1)
                                TYPE_MAIL=1
                        ;;
                        2)
                                TYPE_MAIL=2
                        ;;
                        3)
                                TYPE_MAIL=3
                        ;;
                        p)
                                smtpPort=$OPTARG
                        ;;
                        s)
                                smtpIP=$OPTARG
                        ;;
                        m)
                                mailAddr=$OPTARG
                        ;;
                        o)
                                mailMdp=$OPTARG
                        ;;
                        a)
                                adminMail=$OPTARG
                        ;;
                        w)
                                whiteDomain=$OPTARG
                        ;;
                        :)
                                echo "L'option $OPTARG requiert un argument"
                                exit 1
                        ;;
                        \?)
                                echo "$OPTARG : option invalide"
                                exit 1
                        ;;
                esac
        done
fi
if  [[ $TYPE_MAIL -eq 0 ]]; then # disable mail service
        $SED "s/^MAIL=.*/MAIL=off/" $CONF_FILE
        $SED "s/^MAIL_TYPE=.*/MAIL_TYPE=/" $CONF_FILE
        $SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
        $SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=/" $CONF_FILE
        $SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
        $SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=/" $CONF_FILE
        $SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=/" $CONF_FILE
        $SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=/" $CONF_FILE
        $SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
        $SED "/^SMTP_PORT=/ s/^/#/" $LOCAL_IPTABLE_FILE
        $SED "s/^\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT.*/#\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT -d \$SMTP_IP -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT.*/#\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT -s \$SMTP_IP -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
        $SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
        $SED "/^smtp_tls_wrappermode = yes/d" $POSTFIX_CONF_FILE
        [ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/sasl_passwd
elif [[ $TYPE_MAIL -eq 1 ]]; then # Enable mail service (act as smtp server)
        $SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
        $SED "s/^MAIL_TYPE=.*/MAIL_TYPE=1/" $CONF_FILE
        $SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
        $SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=/" $CONF_FILE
        $SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
        $SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=/" $CONF_FILE
        $SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
        $SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
        $SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
        $SED "/^SMTP_PORT=/ s/^/#/" $LOCAL_IPTABLE_FILE
        $SED "s/^\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT.*/#\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT -d \$SMTP_IP -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT.*/#\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT -s \$SMTP_IP -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
        $SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
        $SED "/^smtp_tls_wrappermode = yes/d" $POSTFIX_CONF_FILE
        [ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/sasl_passwd
elif [[ $TYPE_MAIL -eq 2 ]]; then # Enable mail service (relaying to an extern mail server)
        $SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
        $SED "s/^MAIL_TYPE=.*/MAIL_TYPE=2/" $CONF_FILE
        $SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
        $SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
        $SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
        $SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=/" $CONF_FILE
        $SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
        $SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
        $SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
        $SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT.*/\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT -d \$SMTP_IP -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT.*/\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT -s \$SMTP_IP -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost = $smtpIP:$smtpPort/g" $POSTFIX_CONF_FILE
        $SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
        $SED "/^smtp_tls_wrappermode = yes/d" $POSTFIX_CONF_FILE
        [ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/sasl_passwd
elif [[ $TYPE_MAIL -eq 3 ]]; then # Enable mail service (using an email address)
        $SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
        $SED "s/^MAIL_TYPE=.*/MAIL_TYPE=3/" $CONF_FILE
        $SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
        $SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
        $SED "s/^MAIL_ADDR=.*/MAIL_ADDR=$mailAddr/" $CONF_FILE
        $SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=$mailMdp/" $CONF_FILE
        $SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
        $SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
        $SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
        $SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT.*/\$IPTABLES -A OUTPUT -p tcp --dport \$SMTP_PORT -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT.*/\$IPTABLES -A INPUT  -p tcp --sport \$SMTP_PORT -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost = $smtpIP:$smtpPort/g" $POSTFIX_CONF_FILE
        $SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = encrypt/g" $POSTFIX_CONF_FILE
        echo "smtp_tls_wrappermode = yes" >> $POSTFIX_CONF_FILE
        [ -d ${SASLPATH} ] || mkdir ${SASLPATH}
        echo "[${smtpIP}]:${smtpPort} ${mailAddr}:${mailMdp}" > ${SASLPATH}/sasl_passwd
        postmap ${SASLPATH}/sasl_passwd
        chmod -R 644 ${SASLPATH}
        chown root:root ${SASLPATH}/sasl_passwd*
        chmod 0600 ${SASLPATH}/sasl_passwd*
else
        echo "Erreur ! Aucun type de messagerie sélectionné !"
        exit 0
fi
/usr/local/bin/alcasar-iptables.sh
systemctl restart postfix.service
exit 0