Subversion Repositories ALCASAR

Rev

Rev 3038 | Rev 3052 | 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"
3039 rexy 21
hostName=`grep ^HOSTNAME= $CONF_FILE|cut -d"=" -f2`
22
domainName=`grep ^DOMAIN= $CONF_FILE|cut -d"=" -f2`
3018 rexy 23
usage="Usage: alcasar-mail_install.sh -h|-0|-1|-2|-3"
2993 rexy 24
 
25
nb_args=$#
3039 rexy 26
if [ $nb_args -eq 0 ]; then  # apply alcasar.conf
3018 rexy 27
	mail=`grep ^MAIL= $CONF_FILE|cut -d"=" -f2`
28
	if [ "$mail" = "off" ]; then
29
		TYPE_MAIL=0
30
	else
31
		TYPE_MAIL=`grep ^MAIL_TYPE= $CONF_FILE|cut -d"=" -f2`
3021 rexy 32
		smtpPort=`grep ^MAIL_SMTP_PORT= $CONF_FILE|cut -d"=" -f2`
3018 rexy 33
		smtpIP=`grep ^MAIL_SMTP_IP= $CONF_FILE|cut -d"=" -f2`
34
		mailAddr=`grep ^MAIL_ADDR= $CONF_FILE|cut -d"=" -f2`
3039 rexy 35
		[ -e ${SASLPATH}/sasl_passwd ] && mailMdp=`cat $SASLPATH/sasl_passwd|cut -d":" -f3`
3018 rexy 36
		adminMail=`grep ^MAIL_ADMIN= $CONF_FILE|cut -d"=" -f2`
3039 rexy 37
		whiteDomain=`grep ^MAIL_WHITEDOMAIN= $CONF_FILE|cut -d"=" -f2`
3018 rexy 38
	fi
39
else # apply args
40
	if [ "$1" = "-h" ] || [ "$1" = "--h" ]; then
41
		echo $usage
42
		exit 0
43
	fi
3020 rexy 44
	while getopts ":h:s:p:m:o:a:w:0123" option
2993 rexy 45
	do
46
		case $option in
47
			0)
48
				TYPE_MAIL=0
49
			;;
50
			1)
51
				TYPE_MAIL=1
52
			;;
53
			2)
54
				TYPE_MAIL=2
55
			;;
56
			3)
57
				TYPE_MAIL=3
58
			;;
59
			p)
3021 rexy 60
				smtpPort=$OPTARG
2993 rexy 61
			;;
3020 rexy 62
			s)
2993 rexy 63
				smtpIP=$OPTARG
64
			;;
65
			m)
66
				mailAddr=$OPTARG
67
			;;
68
			o)
69
				mailMdp=$OPTARG
70
			;;
71
			a)
72
				adminMail=$OPTARG
73
			;;
74
			w)
75
				whiteDomain=$OPTARG
76
			;;
77
			:)
78
				echo "L'option $OPTARG requiert un argument"
79
				exit 1
80
			;;
81
			\?)
82
				echo "$OPTARG : option invalide"
83
				exit 1
84
			;;
85
		esac
86
	done
87
fi
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
2997 rexy 94
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=/" $CONF_FILE
2993 rexy 95
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=/" $CONF_FILE
2997 rexy 96
	$SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
97
	$SED "/^SMTP_PORT=/ s/^/#/" $LOCAL_IPTABLE_FILE
3018 rexy 98
	$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
99
	$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 100
	$SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
3022 rexy 101
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
3038 rexy 102
	$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = no/g" $POSTFIX_CONF_FILE
103
	$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
3039 rexy 104
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/*
3022 rexy 105
elif [[ $TYPE_MAIL -eq 1 ]]; then # Enable mail service (act as smtp server)
106
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
107
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=1/" $CONF_FILE
108
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=/" $CONF_FILE
3038 rexy 109
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
3022 rexy 110
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=/" $CONF_FILE
111
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
112
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
113
	$SED "/^SMTP_IP=/ s/^/#/" $LOCAL_IPTABLE_FILE
3039 rexy 114
	$SED "s/^SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
115
	$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
116
	$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 117
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
118
	$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
119
	$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 120
	$SED "s/^relayhost =.*/relayhost =/" $POSTFIX_CONF_FILE
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}/*
2997 rexy 125
elif [[ $TYPE_MAIL -eq 2 ]]; then # Enable mail service (relaying to an extern mail server)
3001 rexy 126
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
127
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=2/" $CONF_FILE
3016 rexy 128
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
3021 rexy 129
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
3016 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
3039 rexy 133
	$SED "s/^SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
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 -d \$SMTP_IP -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 -s \$SMTP_IP -m conntrack --ctstate ESTABLISHED     -j ACCEPT/" $LOCAL_IPTABLE_FILE
3016 rexy 137
	$SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
3021 rexy 138
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
3016 rexy 139
	$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
140
	$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
3022 rexy 141
	$SED "s/^relayhost =.*/relayhost = $smtpIP:$smtpPort/g" $POSTFIX_CONF_FILE
142
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = may/g" $POSTFIX_CONF_FILE
3038 rexy 143
	$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = no/g" $POSTFIX_CONF_FILE
144
	$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
3039 rexy 145
	[ -e ${SASLPATH}/sasl_passwd ] && rm -f ${SASLPATH}/*
2997 rexy 146
elif [[ $TYPE_MAIL -eq 3 ]]; then # Enable mail service (using an email address)
2994 rexy 147
	$SED "s/^MAIL=.*/MAIL=on/" $CONF_FILE
148
	$SED "s/^MAIL_TYPE=.*/MAIL_TYPE=3/" $CONF_FILE
3016 rexy 149
	$SED "s/^MAIL_SMTP_IP=.*/MAIL_SMTP_IP=$smtpIP/" $CONF_FILE
3021 rexy 150
	$SED "s/^MAIL_SMTP_PORT=.*/MAIL_SMTP_PORT=$smtpPort/" $CONF_FILE
2994 rexy 151
	$SED "s/^MAIL_ADDR=.*/MAIL_ADDR=$mailAddr/" $CONF_FILE
2997 rexy 152
	$SED "s/^MAIL_WHITEDOMAIN=.*/MAIL_WHITEDOMAIN=$whiteDomain/" $CONF_FILE
2994 rexy 153
	$SED "s/^MAIL_ADMIN=.*/MAIL_ADMIN=$adminMail/" $CONF_FILE
3039 rexy 154
	$SED "s/^SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
155
	$SED "s/^SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
156
	$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
157
	$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 158
	$SED "s/^#SMTP_IP=.*/SMTP_IP=$smtpIP/" $LOCAL_IPTABLE_FILE
3021 rexy 159
	$SED "s/^#SMTP_PORT=.*/SMTP_PORT=$smtpPort/" $LOCAL_IPTABLE_FILE
3016 rexy 160
	$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
161
	$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 162
	$SED "s/^relayhost =.*/relayhost = $smtpIP:$smtpPort/g" $POSTFIX_CONF_FILE
163
	$SED "s/^smtp_tls_security_level =.*/smtp_tls_security_level = encrypt/g" $POSTFIX_CONF_FILE
3038 rexy 164
	$SED "s/^smtp_tls_wrappermode =.*/smtp_tls_wrappermode = yes/g" $POSTFIX_CONF_FILE
165
	if [ "$domainName" = "localdomain" ]; then  # use a real domain name to avoid extern smtp server reject
166
		$SED "s/^myhostname =.*/myhostname = alcasar.net/g" $POSTFIX_CONF_FILE
167
	else
168
		$SED "s/^myhostname =.*/myhostname = $hostName.$domainName/g" $POSTFIX_CONF_FILE
169
	fi
2997 rexy 170
	[ -d ${SASLPATH} ] || mkdir ${SASLPATH}
3022 rexy 171
	echo "[${smtpIP}]:${smtpPort} ${mailAddr}:${mailMdp}" > ${SASLPATH}/sasl_passwd
2997 rexy 172
	postmap ${SASLPATH}/sasl_passwd
3001 rexy 173
	chmod -R 644 ${SASLPATH}
174
	chown root:root ${SASLPATH}/sasl_passwd*
175
	chmod 0600 ${SASLPATH}/sasl_passwd*
2993 rexy 176
else
177
	echo "Erreur ! Aucun type de messagerie sélectionné !"
178
	exit 0
179
fi
180
/usr/local/bin/alcasar-iptables.sh
3016 rexy 181
systemctl restart postfix.service
2993 rexy 182
exit 0