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