Subversion Repositories ALCASAR

Rev

Rev 3020 | Rev 3022 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2993 rexy 1
#!/bin/bash
2
 
3
###########################################################################################
3018 rexy 4
##				    ALCASAR MAIL SERVICE CONFIGURATION
2993 rexy 5
##
3011 rexy 6
##	Script by K@M3L & T3RRY (LaPlateform), joss_p & Rexy
3018 rexy 7
##  This script configure PostFix
8
##		0 : no email autoregistration
9
##		1 : PostFix is the SMTP server
10
##		2 : PostFix relay to an other SMTP server
11
##		2 : PostFix use an external email address (with SASL)
2993 rexy 12
###########################################################################################
13
 
3018 rexy 14
# ****** Paths *******
2993 rexy 15
SED="/bin/sed -i"
16
CONF_FILE="/usr/local/etc/alcasar.conf"
2994 rexy 17
POSTFIX_CONF_FILE="/etc/postfix/main.cf"
2993 rexy 18
LOCAL_IPTABLE_FILE="/usr/local/etc/alcasar-iptables-local.sh"
2997 rexy 19
SASLPATH="/etc/postfix/sasl"
2993 rexy 20
smtpIP="0.0.0.0/0"
3018 rexy 21
usage="Usage: alcasar-mail_install.sh -h|-0|-1|-2|-3"
2993 rexy 22
 
23
nb_args=$#
3018 rexy 24
if [ $nb_args -eq 0 ] # apply alcasar.conf
2993 rexy 25
then
3018 rexy 26
	mail=`grep ^MAIL= $CONF_FILE|cut -d"=" -f2`
27
	if [ "$mail" = "off" ]; then
28
		TYPE_MAIL=0
29
	else
30
		TYPE_MAIL=`grep ^MAIL_TYPE= $CONF_FILE|cut -d"=" -f2`
3021 rexy 31
		smtpPort=`grep ^MAIL_SMTP_PORT= $CONF_FILE|cut -d"=" -f2`
3018 rexy 32
		smtpIP=`grep ^MAIL_SMTP_IP= $CONF_FILE|cut -d"=" -f2`
33
		mailAddr=`grep ^MAIL_ADDR= $CONF_FILE|cut -d"=" -f2`
34
		mailMdp=`grep ^MAIL_PASSWORD= $CONF_FILE|cut -d"=" -f2`
35
		adminMail=`grep ^MAIL_ADMIN= $CONF_FILE|cut -d"=" -f2`
36
		whiteDomain=`grep ^MAIL_WHTEDOMAIN= $CONF_FILE|cut -d"=" -f2`
37
	fi
38
else # apply args
39
	if [ "$1" = "-h" ] || [ "$1" = "--h" ]; then
40
		echo $usage
41
		exit 0
42
	fi
3020 rexy 43
	while getopts ":h:s:p:m:o:a:w:0123" option
2993 rexy 44
	do
45
		case $option in
46
			0)
47
				TYPE_MAIL=0
48
			;;
49
			1)
50
				TYPE_MAIL=1
51
			;;
52
			2)
53
				TYPE_MAIL=2
54
			;;
55
			3)
56
				TYPE_MAIL=3
57
			;;
58
			p)
3021 rexy 59
				smtpPort=$OPTARG
2993 rexy 60
			;;
3020 rexy 61
			s)
2993 rexy 62
				smtpIP=$OPTARG
63
			;;
64
			m)
65
				mailAddr=$OPTARG
66
			;;
67
			o)
68
				mailMdp=$OPTARG
69
			;;
70
			a)
71
				adminMail=$OPTARG
72
			;;
73
			w)
74
				whiteDomain=$OPTARG
75
			;;
76
			:)
77
				echo "L'option $OPTARG requiert un argument"
78
				exit 1
79
			;;
80
			\?)
81
				echo "$OPTARG : option invalide"
82
				exit 1
83
			;;
84
		esac
85
	done
86
fi
3020 rexy 87
echo "Type=$TYPE_MAIL - port=$port - smtpIP=$smtpIP - mailAddr=$mailAddr - mailMdp=$mailMdp - adminMail=$adminMail - whitedomain=$whiteDomain"
2994 rexy 88
if  [[ $TYPE_MAIL -eq 0 ]]; then # disable mail service
2993 rexy 89
	$SED "s/^MAIL=.*/MAIL=off/" $CONF_FILE
90
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=/" $CONF_FILE
91
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
3021 rexy 92
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=/" $CONF_FILE
2993 rexy 93
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
3018 rexy 94
	$SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=/" $CONF_FILE
2997 rexy 95
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=/" $CONF_FILE
2993 rexy 96
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=/" $CONF_FILE
2997 rexy 97
	$SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
98
	$SED "/^SMTP_PORT=/ s/^/#/" $LOCAL_IPTABLE_FILE
3018 rexy 99
	$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
100
	$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
2997 rexy 101
	$SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
3016 rexy 102
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/sasl_passwd
2997 rexy 103
elif [[ $TYPE_MAIL -eq 2 ]]; then # Enable mail service (relaying to an extern mail server)
3001 rexy 104
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
105
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=2/" $CONF_FILE
3016 rexy 106
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
3021 rexy 107
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
3016 rexy 108
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
3018 rexy 109
	$SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=/" $CONF_FILE
3016 rexy 110
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
111
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
112
	$SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
3021 rexy 113
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
3016 rexy 114
	$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
115
	$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
3020 rexy 116
	$SED "s/^relayhost =.*/relayhost = $smtpIP:$port/g" $POSTFIX_CONF_FILE
3016 rexy 117
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/sasl_passwd
2997 rexy 118
elif [[ $TYPE_MAIL -eq 3 ]]; then # Enable mail service (using an email address)
2994 rexy 119
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
120
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=3/" $CONF_FILE
3016 rexy 121
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
3021 rexy 122
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
2994 rexy 123
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=$mailAddr/" $CONF_FILE
3018 rexy 124
	$SED "s/^MAIL_PASSWORD=.*/MAIL_PASSWORD=$mailMdp/" $CONF_FILE
2997 rexy 125
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
2994 rexy 126
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
3013 rexy 127
	$SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
3021 rexy 128
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
3016 rexy 129
	$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
130
	$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
3013 rexy 131
	$SED "s/^relayhost =.*/relayhost = $smtpIP:$port/g" $POSTFIX_CONF_FILE
2997 rexy 132
	[ -d ${SASLPATH} ] || mkdir ${SASLPATH}
3013 rexy 133
	echo "[${smtpIP}]:${port} ${mailAddr}:${mailMdp}" > ${SASLPATH}/sasl_passwd
2997 rexy 134
	postmap ${SASLPATH}/sasl_passwd
3001 rexy 135
	chmod -R 644 ${SASLPATH}
136
	chown root:root ${SASLPATH}/sasl_passwd*
137
	chmod 0600 ${SASLPATH}/sasl_passwd*
2993 rexy 138
else
139
	echo "Erreur ! Aucun type de messagerie sélectionné !"
140
	exit 0
141
fi
142
/usr/local/bin/alcasar-iptables.sh
3016 rexy 143
systemctl restart postfix.service
2993 rexy 144
exit 0