-
Notifications
You must be signed in to change notification settings - Fork 8
/
plesk_mail_autoconfig.sh
286 lines (246 loc) · 9.63 KB
/
plesk_mail_autoconfig.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
# Deploy mail autoconfig for Plesk
# Support for Thunderbird autoconfig, Outlook autodiscover & Apple config generator
# Website: haisoft.net
## Settings
# These will be visible inside your config files
companyname="HaiSoft"
companyshortname="HaiSoft"
companylowercasename="haisoft"
companyurl="haisoft.net"
docurl="http://help.haisoft.net/"
hostname="$(hostname)"
applewebpagelanguage="fr"
# Git repo, useful if you want to fork it
gituser="HaiSoftSARL"
gitrepo="plesk_mail_autoconfig"
gitbranch="master"
# Hosting directory
# The place where we host our configuration files
defaulthostingdir="/var/www/vhosts/default/htdocs"
# Thunderbird autoconfig paths
autoconfigpath="${defaulthostingdir}/mail"
autoconfigfile="config-v1.1.xml"
autoconfigpathfile="${autoconfigpath}/${autoconfigfile}"
# Outlook autodiscover paths
autodiscoverpath="${defaulthostingdir}/mail"
autodiscoverpathfile="${autodiscoverpath}/autodiscover.xml"
autodiscoverpathfilealt="${autodiscoverpath}/Autodiscover.xml"
autodiscoverhtaccess="${autodiscoverpath}/.htaccess"
# Apple configurator paths
applepath="${defaulthostingdir}/mail"
applewebpagepath="${applepath}/apple.xml"
applemobileconfpathfile="${autodiscoverpath}/apple.mobileconfig"
appleweblogo="logo.png"
appleweblogourl="https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/${appleweblogo}"
# URLs used for tests
autoconfigurl="http://autoconfig.${hostname}/mail/config-v1.1.xml"
autodiscoverurl="https://${hostname}/autodiscover/autodiscover.xml"
autodiscoverurlalt="https://${hostname}/Autodiscover/Autodiscover.xml"
appleconfigurl="http://${hostname}/apple"
##############
### Script ###
##############
# Misc Vars
selfname="Mail Autoconfig"
# Download bash API
if [ ! -f "ultimate-bash-api.sh" ]; then
wget https://raw.githubusercontent.com/UltimateByte/ultimate-bash-api/master/ultimate-bash-api.sh
chmod +x ultimate-bash-api.sh
fi
source ultimate-bash-api.sh
fn_logecho "#############################"
fn_logecho "### Plesk Mail Autoconfig ###"
fn_logecho "#############################"
echo ""
sleep 1.5
# Detect if apache is named apache2 or httpd
fn_logecho "[ INFO ] Detecting if Apache is named apache2 or httpd"
sleep 1
if [ -d "/etc/httpd/conf.d" ]&&[ -d "/etc/apache2/conf-enabled" ]; then
fn_logecho "[ ERROR ] Both httpd and apache2 dirs were found in /etc"
exit 1
elif [ -d "/etc/httpd/conf.d/" ]&&[ ! -d "/etc/apache2/conf-enabled" ]; then
fn_logecho "[ INFO ] Detected httpd, hello RedHat based system"
apacheautodiscoverconf="/etc/httpd/conf.d/autodiscover.conf"
apacheservicename="httpd"
elif [ ! -d "/etc/httpd/conf.d" ]&&[ -d "/etc/apache2/conf-enabled" ]; then
fn_logecho "[ INFO ] Detected apache2, hello Debian based system"
apacheautodiscoverconf="/etc/apache2/conf-enabled/autodiscover.conf"
apacheservicename="apache2"
else
fn_logecho "[ ERROR ] No apache conf.d in /etc/httpd or conf-enabled in /etc/apache2 were found"
exit 1
fi
fn_logecho "[ INFO ] Creating autoconfig config path if needed"
echo ""
sleep 1
# Files and directories creation
if [ ! -d "${autoconfigpath}" ]; then
fn_logecho "[ ... ] Creating autoconfig config path"
fn_logecho "${autoconfigpath}"
mkdir -p "${autoconfigpath}"
fi
if [ ! -f "${autoconfigpathfile}" ]; then
fn_logecho "[ ... ] Creating autoconfig config file"
fn_logecho "${autoconfigpathfile}"
touch "${autoconfigpathfile}"
fi
if [ ! -d "${autodiscoverpath}" ]; then
fn_logecho "[ ... ] Creating autodiscover config path"
fn_logecho "${autodiscoverpath}"
mkdir -p "${autodiscoverpath}"
fi
if [ ! -f "${autodiscoverpathfile}" ]; then
fn_logecho "[ ... ] Creating autodiscover config file"
fn_logecho "${autodiscoverpathfile}"
touch "${autodiscoverpathfile}"
fi
if [ ! -L "${autodiscoverpathfilealt}" ]; then
fn_logecho "[ ... ] Symlinking autodiscover alternative config file"
fn_logecho "${autodiscoverpathfile} -> ${autodiscoverpathfilealt}"
ln -s "${autodiscoverpathfile}" "${autodiscoverpathfilealt}"
fi
if [ ! -d "${applepath}" ]; then
fn_logecho "[ INFO ] Creating Apple mail configurator directory"
fn_logecho "${applepath}"
mkdir -p "${applepath}"
fi
if [ ! -f "${applewebpagepath}" ]; then
fn_logecho "[INFO] Creating Apple web page"
fn_logecho "${applewebpagepath}"
touch "${applewebpagepath}"
fi
if [ ! -f "${applemobileconfpathfile}" ]; then
fn_logecho "[ INFO ] Creating Apple mobileconfig file"
fn_logecho "${applemobileconfpathfile}"
touch "${applemobileconfpathfile}"
fi
if [ ! -f "${autodiscoverhtaccess}" ]; then
fn_logecho "[ INFO ] Creating autodiscover .htaccess file"
fn_logecho "${autodiscoverhtaccess}"
touch "${autodiscoverhtaccess}"
fi
if [ ! -f "${apacheautodiscoverconf}" ]; then
fn_logecho "[ INFO ] Creating autodiscover Apache configuration file"
fn_logecho "${apacheautodiscoverconf}"
touch "${apacheautodiscoverconf}"
fi
## Thunderbird autoconfig
echo ""
fn_logecho "[ INFO ] Writing Thunderbird autoconfig config file"
sleep 1
echo ""
curl "https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/config-v1.1.xml" > "${autoconfigpathfile}"
# Replace values with settings
fn_logecho "[ ... ] Populating Thunderbird autoconfig file"
sleep 0.5
sed -i -e "s/HOSTNAME/${hostname}/g" "${autoconfigpathfile}"
sed -i -e "s/COMPANYURL/${companyurl}/g" "${autoconfigpathfile}"
sed -i -e "s/COMPANYNAME/${companyname}/g" "${autoconfigpathfile}"
sed -i -e "s/COMPANYSHORTNAME/${companyshortname}/g" "${autoconfigpathfile}"
sed -i -e "s@DOCURL@${docurl}@g" "${autoconfigpathfile}"
# DNS for autoconfig
fn_logecho "[ INFO ] Correcting default DNS zone for Thunderbird autoconfig: adding cname autoconfig to ${hostname}"
echo ""
sleep 1
/usr/local/psa/bin/server_dns --add -cname autoconfig -canonical "${hostname}"
fn_logecho "[ ... ] Adding DNS entry for every website for Thunderbird autoconfig"
echo ""
sleep 1
for i in `mysql -uadmin -p\`cat /etc/psa/.psa.shadow\` psa -Ns -e "select name from domains"`; do
/usr/local/psa/bin/dns --add "$i" -cname autoconfig -canonical "${hostname}"
fn_logecho "Adding cname: autoconfig.$i - ${hostname}"
done
## Outlook autodiscover
echo ""
fn_logecho "[ INFO ] Writing Outlook autodiscover file"
sleep 1
curl "https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/autodiscover.xml" > "${autodiscoverpathfile}"
# Outlook settings
fn_logecho "[ ... ] Populating Outlook autodiscover file"
sleep 0.5
sed -i -e "s/HOSTNAME/${hostname}/g" "${autodiscoverpathfile}"
sed -i -e "s/COMPANYNAME/${companyname}/g" "${autodiscoverpathfile}"
## Apple config
echo ""
fn_logecho "[ INFO ] Writing Apple config files"
echo ""
sleep 0.5
if [ ${applewebpagelanguage} = fr ]; then
curl "https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/apple_fr.xml" > "${applewebpagepath}"
elif [ ${applewebpagelanguage} = en ]; then
curl "https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/apple_en.xml" > "${applewebpagepath}"
else
fn_logecho "[ Warning ] Selected language ${applewebpagelanguage} is not available. Reverting to english."
curl "https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/apple_en.xml" > "${applewebpagepath}"
fi
curl "https://raw.githubusercontent.com/${gituser}/${gitrepo}/${gitbranch}/apple.mobileconfig" > "${applemobileconfpathfile}"
# Apple web page Logo
fn_logecho "[ ... ] Downloading ${appleweblogo} logo for Apple web page"
sleep 0.5
wget -O "${applepath}/${appleweblogo}" "${appleweblogourl}"
# Apple Settings
fn_logecho "[ ... ] Populating Apple config files"
sleep 0.5
sed -i -e "s/APPLELOGO/${appleweblogo}/g" "${applewebpagepath}"
sed -i -e "s/COMPANYNAME/${companyname}/g" "${applewebpagepath}"
sed -i -e "s/HOSTNAME/${hostname}/g" "${applemobileconfpathfile}"
sed -i -e "s/COMPANYLOWERCASENAME/${companylowercasename}/g" "${applemobileconfpathfile}"
## .htaccess config
echo ""
fn_logecho "[ INFO ] Writing htaccess file"
sleep 0.5
echo "AddHandler php-script .php .xml
RewriteEngine on
RewriteCond %{REQUEST_URI} !apple.xml
RewriteCond %{REQUEST_URI} !${appleweblogo}
RewriteCond %{REQUEST_URI} apple
RewriteRule .* /apple/apple.xml [R]" > "${autodiscoverhtaccess}"
## Apache aliases config
echo ""
fn_logecho "[ INFO ] Writing autodiscover Apache configuration file"
sleep 0.5
echo "Alias /autodiscover \"${autodiscoverpath}\"
Alias /Autodiscover \"${autodiscoverpath}\"
Alias /apple \"${applepath}\"" > "${apacheautodiscoverconf}"
echo ""
fn_logecho "[ INFO ] Restarting Apache"
service ${apacheservicename} restart
## Testing
fn_logecho "#############"
fn_logecho "## Testing ##"
fn_logecho "#############"
echo ""
sleep 1
# Test Thunderbird autoconfig
if [ -n "$(curl "${autoconfigurl}" | grep "<socketType>SSL</socketType>")" ]; then
fn_logecho "[ OK ] Thunderbird ${autoconfigurl} is accessible"
else
fn_logecho "[ Warning ] Thunderbird ${autoconfigurl} does not seem to be accessible"
fi
echo ""
sleep 0.5
# Test Outlook autodiscover
if [ -n "$(curl "${autodiscoverurl}" | grep "<DisplayName>${companyname}</DisplayName>")" ]; then
fn_logecho "[ OK ] Outlook ${autodiscoverurl} is accessible"
else
fn_logecho "[ Warning ] Outlook ${autodiscoverurl} does not seem to be accessible (or certificate is not valid)"
fi
echo ""
sleep 0.5
# Test Outlook Autodiscover
if [ -n "$(curl "${autodiscoverurlalt}" | grep "<DisplayName>${companyname}</DisplayName>")" ]; then
fn_logecho "[ OK ] Outlook ${autodiscoverurlalt} is accessible"
else
fn_logecho "[ Warning ] Outlook ${autodiscoverurlalt} does not seem to be accessible (or certificate is not valid)"
fi
echo ""
sleep 0.5
# Test Apple configurator
if [ -n "$(curl -L "${appleconfigurl}" | grep "<form method=\"post\" action=\"apple.xml\">")" ]; then
fn_logecho "[ OK ] Apple ${appleconfigurl} is accessible"
else
fn_logecho "[ Warning ] Apple ${appleconfigurl} does not seem to be accessible"
fi
fn_logecho "[ OK ] Done"