Subversion Repositories ALCASAR

Rev

Rev 3011 | Rev 3016 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

#!/bin/bash

###########################################################################################
##                                  ALCASAR SERVICE MAIL INSTALL
##
##      Script by K@M3L & T3RRY (LaPlateform), joss_p & Rexy
##  This script configure the mail conf file and execute the configuration from the acc.
###########################################################################################

# ****** Paths - chemin des commandes *******
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 0|1|2|3"
nb_args=$#
if [ $nb_args -eq 0 ]
then
        echo "$usage"
        exit 0
fi
if [[ ${#} -ne 0 ]]
then
        while getopts ":s:p:r: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
                        ;;
                        s)
                                smtp=$OPTARG
                        ;;
                        p)
                                port=$OPTARG
                        ;;
                        r)
                                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=.*/MAIL_SMTP=/" $CONF_FILE
        $SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
        $SED "s/^MAIL_PORT=.*/MAIL_PORT=/" $CONF_FILE
        $SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $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 "/^\$IPTABLES -A OUTPUT -p tcp -d \$SMTP_IP/ s/^/#/" $LOCAL_IPTABLE_FILE
        $SED "/^\$IPTABLES -A INPUT  -p tcp -s \$SMTP_IP/ s/^/#/g" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
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/^#\$IPTABLES -A OUTPUT -p tcp -d \$SMTP_IP.*/\$IPTABLES -A OUTPUT -p tcp -d \$SMTP_IP --dport \$SMTP_PORT -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A INPUT  -p tcp -s \$SMTP_IP.*/\$IPTABLES -A INPUT  -p tcp -s \$SMTP_IP --sport \$SMTP_PORT -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost = $smtp:$port/g" $POSTFIX_CONF_FILE
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=.*/MAIL_SMTP=/" $CONF_FILE
        $SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtp/" $CONF_FILE
        $SED "s/^MAIL_PORT=.*/MAIL_PORT=$port/" $CONF_FILE
        $SED "s/^MAIL_ADDR=.*/MAIL_ADDR=$mailAddr/" $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=$port/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A OUTPUT -p tcp -d \$SMTP_IP.*/\$IPTABLES -A OUTPUT -p tcp -d \$SMTP_IP --dport \$SMTP_PORT -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^#\$IPTABLES -A INPUT  -p tcp -s \$SMTP_IP.*/\$IPTABLES -A INPUT  -p tcp -s \$SMTP_IP --sport \$SMTP_PORT -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
        $SED "s/^relayhost =.*/relayhost = $smtpIP:$port/g" $POSTFIX_CONF_FILE
        [ -d ${SASLPATH} ] || mkdir ${SASLPATH}
        echo "[${smtpIP}]:${port} ${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
exit 0