Subversion Repositories ALCASAR

Rev

Rev 3061 | Rev 3064 | 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
3061 rexy 11
##		3 : PostFix use an external email address (with Cyrus-SASL)
2993 rexy 12
###########################################################################################
13
 
3061 rexy 14
######################################################
15
##			Email configuration examples (mode = 3)
16
## common parameters : smtp_use_tls = yes, smtp_tls_security_level = encrypt, smtp_sasl_auth_enable = yes
17
## common rules : 'myhostname' parameter should be the domain name of the sasl_email account
3062 rexy 18
########## smtp.free.fr:465 (expose mechanisms : PLAIN LOGIN CRAM-MD5 DIGEST-MD5)
3061 rexy 19
## smtp_sasl_security_option = noanonymous, relayhost = [smtp.free.fr]:465, smtp_tls_wrappermode = yes
3062 rexy 20
########## smtp.free.fr:587 (expose mechanismes 
3061 rexy 21
## smtp_sasl_security_option = noanonymous, relayhost = [smtp.free.fr]:587, smtp_tls_wrappermode = no
3062 rexy 22
########## smtp.orange.fr:465 (expose mechanisms : LOGIN PLAIN)
23
## smtp_sasl_security_option = noanonymous, relayhost = [smtp.orange.fr]:465, smtp_tls_wrappermode = yes
24
########## smtp.sfr.fr:465 (expose mechanisms : LOGIN PLAIN)
25
## smtp_sasl_security_option = noanonymous, relayhost = [smtp.sfr.fr]:465, smtp_tls_wrappermode = yes
26
########## smtp.laposte.net:465 (expose mechanisms : LOGIN PLAIN)
27
## smtp_sasl_security_option = noanonymous, relayhost = [laposte.net]:465, smtp_tls_wrappermode = yes
28
########## smtp.bbox.net:465 (expose mechanisms : LOGIN PLAIN)
29
## smtp_sasl_security_option = noanonymous, relayhost = [laposte.net]:465, smtp_tls_wrappermode = yes
30
########## smtp.gmail.com:587 (expose mechanisms : LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH)
31
## smtp_sasl_security_option = noanonymous, relayhost = [gmail.com]:587, smtp_tls_wrappermode = no
3061 rexy 32
 
3018 rexy 33
# ****** Paths *******
2993 rexy 34
SED="/bin/sed -i"
35
CONF_FILE="/usr/local/etc/alcasar.conf"
2994 rexy 36
POSTFIX_CONF_FILE="/etc/postfix/main.cf"
2993 rexy 37
LOCAL_IPTABLE_FILE="/usr/local/etc/alcasar-iptables-local.sh"
2997 rexy 38
SASLPATH="/etc/postfix/sasl"
2993 rexy 39
smtpIP="0.0.0.0/0"
3039 rexy 40
hostName=`grep ^HOSTNAME= $CONF_FILE|cut -d"=" -f2`
41
domainName=`grep ^DOMAIN= $CONF_FILE|cut -d"=" -f2`
3018 rexy 42
usage="Usage: alcasar-mail_install.sh -h|-0|-1|-2|-3"
2993 rexy 43
 
44
nb_args=$#
3039 rexy 45
if [ $nb_args -eq 0 ]; then  # apply alcasar.conf
3018 rexy 46
	mail=`grep ^MAIL= $CONF_FILE|cut -d"=" -f2`
47
	if [ "$mail" = "off" ]; then
48
		TYPE_MAIL=0
49
	else
50
		TYPE_MAIL=`grep ^MAIL_TYPE= $CONF_FILE|cut -d"=" -f2`
3021 rexy 51
		smtpPort=`grep ^MAIL_SMTP_PORT= $CONF_FILE|cut -d"=" -f2`
3018 rexy 52
		smtpIP=`grep ^MAIL_SMTP_IP= $CONF_FILE|cut -d"=" -f2`
53
		mailAddr=`grep ^MAIL_ADDR= $CONF_FILE|cut -d"=" -f2`
3039 rexy 54
		[ -e ${SASLPATH}/sasl_passwd ] && mailMdp=`cat $SASLPATH/sasl_passwd|cut -d":" -f3`
3018 rexy 55
		adminMail=`grep ^MAIL_ADMIN= $CONF_FILE|cut -d"=" -f2`
3039 rexy 56
		whiteDomain=`grep ^MAIL_WHITEDOMAIN= $CONF_FILE|cut -d"=" -f2`
3018 rexy 57
	fi
58
else # apply args
59
	if [ "$1" = "-h" ] || [ "$1" = "--h" ]; then
60
		echo $usage
61
		exit 0
62
	fi
3020 rexy 63
	while getopts ":h:s:p:m:o:a:w:0123" option
2993 rexy 64
	do
65
		case $option in
66
			0)
67
				TYPE_MAIL=0
68
			;;
69
			1)
70
				TYPE_MAIL=1
71
			;;
72
			2)
73
				TYPE_MAIL=2
74
			;;
75
			3)
76
				TYPE_MAIL=3
77
			;;
78
			p)
3021 rexy 79
				smtpPort=$OPTARG
2993 rexy 80
			;;
3020 rexy 81
			s)
2993 rexy 82
				smtpIP=$OPTARG
83
			;;
84
			m)
85
				mailAddr=$OPTARG
86
			;;
87
			o)
88
				mailMdp=$OPTARG
89
			;;
90
			a)
91
				adminMail=$OPTARG
92
			;;
93
			w)
94
				whiteDomain=$OPTARG
95
			;;
96
			:)
97
				echo "L'option $OPTARG requiert un argument"
98
				exit 1
99
			;;
100
			\?)
101
				echo "$OPTARG : option invalide"
102
				exit 1
103
			;;
104
		esac
105
	done
106
fi
2994 rexy 107
if  [[ $TYPE_MAIL -eq 0 ]]; then # disable mail service
2993 rexy 108
	$SED "s/^MAIL=.*/MAIL=off/" $CONF_FILE
109
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=/" $CONF_FILE
110
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
3021 rexy 111
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=/" $CONF_FILE
2993 rexy 112
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
2997 rexy 113
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=/" $CONF_FILE
2993 rexy 114
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=/" $CONF_FILE
2997 rexy 115
	$SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
116
	$SED "/^SMTP_PORT=/ s/^/#/" $LOCAL_IPTABLE_FILE
3018 rexy 117
	$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
118
	$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 119
	$SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
3022 rexy 120
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
3038 rexy 121
	$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = no/g" $POSTFIX_CONF_FILE
122
	$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
3039 rexy 123
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/*
3022 rexy 124
elif [[ $TYPE_MAIL -eq 1 ]]; then # Enable mail service (act as smtp server)
125
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
126
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=1/" $CONF_FILE
127
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
3038 rexy 128
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
3022 rexy 129
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
130
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
131
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
132
	$SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
3039 rexy 133
	$SED "s/^SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
134
	$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
135
	$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
3038 rexy 136
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
137
	$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
138
	$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
3022 rexy 139
	$SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
140
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
3038 rexy 141
	$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = no/g" $POSTFIX_CONF_FILE
142
	$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
3039 rexy 143
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/*
2997 rexy 144
elif [[ $TYPE_MAIL -eq 2 ]]; then # Enable mail service (relaying to an extern mail server)
3001 rexy 145
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
146
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=2/" $CONF_FILE
3016 rexy 147
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
3021 rexy 148
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
3016 rexy 149
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
150
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
151
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
3039 rexy 152
	$SED "s/^SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
153
	$SED "s/^SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
154
	$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
155
	$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
3016 rexy 156
	$SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
3021 rexy 157
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
3016 rexy 158
	$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
159
	$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
3052 rexy 160
	$SED "s/^relayhost =.*/relayhost = [$smtpIP]:$smtpPort/g" $POSTFIX_CONF_FILE
3022 rexy 161
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
3038 rexy 162
	$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = no/g" $POSTFIX_CONF_FILE
163
	$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
3039 rexy 164
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/*
2997 rexy 165
elif [[ $TYPE_MAIL -eq 3 ]]; then # Enable mail service (using an email address)
2994 rexy 166
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
167
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=3/" $CONF_FILE
3016 rexy 168
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
3021 rexy 169
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
2994 rexy 170
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=$mailAddr/" $CONF_FILE
2997 rexy 171
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
2994 rexy 172
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
3039 rexy 173
	$SED "s/^SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
174
	$SED "s/^SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
175
	$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
176
	$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 177
	$SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
3021 rexy 178
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
3016 rexy 179
	$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
180
	$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
3052 rexy 181
	$SED "s/^relayhost =.*/relayhost = [$smtpIP]:$smtpPort/g" $POSTFIX_CONF_FILE
3022 rexy 182
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = encrypt/g" $POSTFIX_CONF_FILE
3060 rexy 183
	if [ "$smtpPort" = "465" ]; then # wrappermode is madatory only if port = 465
184
		$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = yes/g" $POSTFIX_CONF_FILE
185
	else
186
		$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = no/g" $POSTFIX_CONF_FILE
187
	fi
3061 rexy 188
	if [ "$domainName" = "localdomain" ]; then  # use the domain name of $mail_Addr to avoid extern smtp servers reject
189
		domain_of_smtpIP=`echo $smtp_IP|rev|cut -d '.' -f1-2|rev`
190
		$SED "s/^myhostname =.*/myhostname = $domain_of_smtpIP/g" $POSTFIX_CONF_FILE
3038 rexy 191
	else
192
		$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
193
	fi
2997 rexy 194
	[ -d ${SASLPATH} ] || mkdir ${SASLPATH}
3022 rexy 195
	echo "[${smtpIP}]:${smtpPort} ${mailAddr}:${mailMdp}" > ${SASLPATH}/sasl_passwd
2997 rexy 196
	postmap ${SASLPATH}/sasl_passwd
3001 rexy 197
	chmod -R 644 ${SASLPATH}
198
	chown root:root ${SASLPATH}/sasl_passwd*
199
	chmod 0600 ${SASLPATH}/sasl_passwd*
2993 rexy 200
else
201
	echo "Erreur ! Aucun type de messagerie sélectionné !"
202
	exit 0
203
fi
204
/usr/local/bin/alcasar-iptables.sh
3016 rexy 205
systemctl restart postfix.service
2993 rexy 206
exit 0