-
Notifications
You must be signed in to change notification settings - Fork 11
/
dovecot.sh
executable file
·305 lines (225 loc) · 8.33 KB
/
dovecot.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
#
#-----------------------------------------#
###Welcome to the dovecot setup script. Any variables that may need to be adjusted should be changed in the designated "variables" section in the main script. Some non variable file writes should be changed in this file if necessary though.
#-----------------------------------------#
sudo $package_manager install dovecot dovecot-pigeonhole dovecot-pgsql -y
status="$(ps ax | grep -v grep | grep dovecot)"
if [ "$status" = "" ]; then
sudo service dovecot stop
fi
start_tag="####CURRENT BUILD !!!! LEAVE THIS TAG LINE INTACT IF YOU PLAN TO EVER USE THE SETUP SCRIPT AGAIN OR BE READY TO REINSTALL DOVECOT... DO NOT REMOVE####"
end_tag="####END OF CURRENT BUILD... YOU MAY ADJUST SETTINGS OUTSIDE OF THIS TAG, OR IF YOU WISH TO CHANGE SETTINGS IN THIS TAG, ADJUST dovecot.sh AND RERUN THE SETUP"
dovecot_conf=(protocols mail_debug)
for var in ${dovecot_conf[*]}; do
temp="$(grep ^$var $dovecot_dir$dovecot_main)"
if [ "$temp" != "" ]; then
echo "Updating variable..."
sudo ./perl-find-replace "$temp" "$var = ${!var}" "$dovecot_dir$dovecot_main"
else
echo "Writing variable..."
echo "$var = ${!var}" | sudo tee -a "$dovecot_dir$dovecot_main"
fi
done
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_main"
echo "$start_tag
auth default {
socket listen {
client {
path = /var/spool/postfix/private/auth
mode = 0660
user = postfix
group = postfix
}
master {
path = /var/run/dovecot/auth-master
mode = 0600
user = mailreader
}
}
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_main"
dovecot_ssl=(ssl ssl_cert ssl_key)
file="10-ssl.conf"
for var in ${dovecot_ssl[*]}; do
temp="$(grep ^$var $dovecot_dir$dovecot_confd$file)"
if [ "$temp" != "" ]; then
echo "Updating variable..."
sudo ./perl-find-replace "$temp" "$var = ${!var}" "$dovecot_dir$dovecot_confd$file"
else
echo "Writing variable..."
echo "$var = ${!var}" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
fi
done
file="10-auth.conf"
dovecot_auth=(auth_mechanisms disable_plaintext_auth auth_debug_passwords)
for var in ${dovecot_auth[*]}; do
temp="$(grep ^$var $dovecot_dir$dovecot_confd$file)"
if [ "$temp" != "" ]; then
echo "Updating variable..."
sudo ./perl-find-replace "$temp" "$var = ${!var}" "$dovecot_dir$dovecot_confd$file"
else
echo "Writing variable..."
echo "$var = ${!var}" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
fi
done
var="!include"
temp="$(grep ^$var $dovecot_dir$dovecot_confd$file)"
if [ "$temp" != "" ]; then
echo "Updating variable..."
sudo ./perl-find-replace "$temp" "$var $special_sql_file" "$dovecot_dir$dovecot_confd$file"
else
echo "Writing variable..."
echo "$var $special_sql_file" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
fi
sudo touch $dovecot_dir$dovecot_confd$special_sql_file
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$special_sql_file"
echo "$start_tag
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf
}
userdb {
driver = prefetch
}
userdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$special_sql_file"
file="10-mail.conf"
dovecot_mail=(first_valid_uid mail_uid mail_gid mail_home mail_location)
for var in ${dovecot_mail[*]}; do
temp="$(grep ^$var $dovecot_dir$dovecot_confd$file)"
if [ "$temp" != "" ]; then
echo "Updating variable..."
sudo ./perl-find-replace "$temp" "$var = ${!var}" "$dovecot_dir$dovecot_confd$file"
else
echo "Writing variable..."
echo "$var = ${!var}" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
fi
done
file="10-master.conf"
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$file"
echo "$start_tag
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
group = postfix
mode = 0600
user = postfix
}
}
service auth {
unix_listener auth-userdb {
mode = 0600
user = mailreader
group = mail
}
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
file="15-lda.conf"
dovecot_lda=(lda_mailbox_autocreate lda_mailbox_autosubscribe)
for var in ${dovecot_lda[*]}; do
temp="$(grep ^$var $dovecot_dir$dovecot_confd$file)"
if [ "$temp" != "" ]; then
echo "Updating variable..."
sudo ./perl-find-replace "$temp" "$var = ${!var}" "$dovecot_dir$dovecot_confd$file"
else
echo "Writing variable..."
echo "$var = ${!var}" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
fi
done
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$file"
echo "$start_tag
protocol lda {
mail_plugins = \$mail_plugins sieve
auth_socket_path = /var/run/dovecot/auth-master
log_path = /var/log/dovecot-lda-errors.log
info_log_path = /var/log/dovecot-lda.log
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
file="20-lmtp.conf"
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$file"
echo "$start_tag
protocol lmtp {
mail_plugins = \$mail_plugins autocreate sieve quota
postmaster_address = postmaster@$mydomain
hostname = $myhostname
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
file="90-plugin.conf"
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$file"
echo "$start_tag
plugin {
autocreate = Trash
autocreate2 = Sent
autocreate3 = Junk
autosubscribe = Trash
autosubscribe2 = Sent
autosubscribe3 = Junk
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
file="20-managesieve.conf"
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$file"
echo "$start_tag
service managesieve-login {
inet_listener sieve {
port = 4190
}
}
protocol sieve {
managesieve_max_line_length = 65536
managesieve_implementation_string = dovecot
log_path = /var/log/dovecot-sieve-errors.log
info_log_path = /var/log/dovecot-sieve.log
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
file="90-sieve.conf"
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_confd$file"
echo "$start_tag
plugin {
sieve = ~/.dovecot.sieve
sieve_global_path = /etc/dovecot/sieve/default.sieve
sieve_dir = ~/sieve
sieve_global_dir = /etc/dovecot/sieve/global/
sieve_max_script_size = 1M
}
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_confd$file"
dovecot_sql="dovecot-sql.conf"
sudo touch "$dovecot_dir$dovecot_sql"
sudo sed -i "/$start_tag/,/$end_tag/d" "$dovecot_dir$dovecot_sql"
echo "$start_tag
driver = pgsql
connect = host=localhost dbname=$dbname user=$mailreader_user password=$database_pass
default_pass_scheme = SHA512
password_query = SELECT email as user, password FROM users WHERE email = '%u'
user_query = SELECT 200 AS uid, 12 AS gid, 'maildir:/mnt/vmail/%d/%n' FROM users WHERE email = '%u'
$end_tag" | sudo tee -a "$dovecot_dir$dovecot_sql"
sudo mkdir /etc/dovecot/sieve
sudo touch $default_sieve
sudo sed -i "/$start_tag/,/$end_tag/d" "$default_sieve"
echo "$start_tag" | sudo tee -a "$default_sieve"
echo "
#require \"vnd.dovecot.debug\";
require [\"fileinto\"];
# rule:[SPAM]
if header :contains \"X-Spam-Flag\" \"YES\" {
fileinto \"Junk\";
#debug_log \"This must be spam!\";
}
# rule:[SPAM2]
#elsif header :matches \"Subject\" [\"*money*\",\"*Viagra*\",\"Cialis\"] {
# fileinto \"Spam\";
#}
else {
#debug_log \"This isn't spam.\";
}
" | sudo tee -a "$default_sieve"
echo "$end_tag" | sudo tee -a "$default_sieve"
sudo touch /var/log/{dovecot-lda-errors.log,dovecot-lda.log}
sudo touch /var/log/{dovecot-sieve-errors.log,dovecot-sieve.log}
sudo touch /var/log/{dovecot-lmtp-errors.log,dovecot-lmtp.log}
sudo mkdir -p /etc/dovecot/sieve/global
sudo chown mailreader: -R /etc/dovecot/sieve
sudo chown mailreader:mail /var/log/dovecot-*
sudo service dovecot start