Subversion Repositories ALCASAR

Rev

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