forked from msimerson/Mail-Toaster-6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision-haraka.sh
executable file
·422 lines (356 loc) · 10.5 KB
/
provision-haraka.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/sh
# shellcheck disable=1091
. mail-toaster.sh || exit
export JAIL_START_EXTRA="devfs_ruleset=7"
export JAIL_CONF_EXTRA="
devfs_ruleset = 7;"
HARAKA_CONF="$STAGE_MNT/usr/local/haraka/config"
install_haraka()
{
tell_status "installing node & npm"
stage_pkg_install node npm gmake || exit
tell_status "installing Haraka"
stage_exec npm install -g Haraka ws express || exit
tell_status "updating files from GitHub repo"
local _ghi="$STAGE_MNT/usr/local/lib/node_modules/Haraka"
local _ghu='https://raw.githubusercontent.com/haraka/Haraka/master'
# remove after Haraka > 2.7.2 release
for f in plugins/spamassassin.js plugins/karma.js plugins/redis.js plugins/rspamd.js plugins/watch/index.js plugins/watch/html/client.js plugins/connect.geoip.js plugins/connect.p0f.js
do
echo "fetching $f"
fetch -o "$_ghi/$f" "$_ghu/$f"
done
}
install_geoip_dbs()
{
if ! zfs_filesystem_exists "$ZFS_DATA_VOL/geoip"; then
tell_status "GeoIP jail not present, SKIPPING geoip plugin"
return
fi
tell_status "enabling Haraka geoip plugin"
mkdir -p "$STAGE_MNT/usr/local/share/GeoIP"
sed -i -e 's/^;calc_distance=false/calc_distance=true/' "$HARAKA_CONF/connect.geoip.ini"
sed -i -e 's/^# connect.geoip/connect.geoip/' "$HARAKA_CONF/plugins"
JAIL_CONF_EXTRA="$JAIL_CONF_EXTRA
mount += \"$ZFS_DATA_MNT/geoip \$path/usr/local/share/GeoIP nullfs ro 0 0\";"
}
add_devfs_rule()
{
if grep -qs devfsrules_jail_bpf /etc/devfs.rules; then
tell_status "devfs BPF ruleset already present"
return
fi
tell_status "installing devfs ruleset for p0f"
tee -a /etc/devfs.rules <<EO_DEVFS
[devfsrules_jail_bpf=7]
add include \$devfsrules_hide_all
add include \$devfsrules_unhide_basic
add include \$devfsrules_unhide_login
add path zfs unhide
add path 'bpf*' unhide
EO_DEVFS
}
install_p0f()
{
tell_status "install p0f"
stage_pkg_install p0f
tell_status "installing p0f startup file"
mkdir -p "$STAGE_MNT/usr/local/etc/rc.d"
local _start="$STAGE_MNT/usr/local/etc/rc.d/p0f"
cp "$STAGE_MNT/usr/local/lib/node_modules/Haraka/contrib/bsd-rc.d/p0f" "$_start" || exit
chmod 755 "$_start" || exit
get_public_facing_nic
if [ "$PUBLIC_NIC" != "bce1" ]; then
sed -i -e "s/ bce1 / $PUBLIC_NIC /" "$_start" || exit
fi
stage_sysrc p0f_enable=YES
stage_exec service p0f start
}
config_haraka_syslog()
{
tell_status "switch Haraka logging to syslog"
sed -i -e 's/# log.syslog$/log.syslog/' "$HARAKA_CONF/plugins"
# sed -i -e 's/^daemon_log_file=.*/daemon_log_file=\/dev\/null/' "$HARAKA_CONF/smtp.ini"
sed -i -e 's/always_ok=false/always_ok=true/' "$HARAKA_CONF/log.syslog.ini"
}
config_haraka_vpopmail()
{
tell_status "configure smtp forward to vpopmail jail"
sed -i .bak \
-e "s/^host=localhost/host=$(get_jail_ip vpopmail)/" \
-e 's/^port=2555/port=25/' \
"$HARAKA_CONF/smtp_forward.ini"
tell_status "config SMTP AUTH using vpopmaild"
echo "host=$(get_jail_ip vpopmail)" > "$HARAKA_CONF/auth_vpopmaild.ini"
# shellcheck disable=1004
sed -i -e '/^# auth\/auth_ldap$/a\
auth\/auth_vpopmaild
' "$HARAKA_CONF/plugins"
}
config_haraka_qmail_deliverable()
{
tell_status "config recipient validation with Qmail::Deliverable"
sed -i -e "s/^host=127.0.0.1/host=$(get_jail_ip vpopmail)/" "$HARAKA_CONF/rcpt_to.qmail_deliverable.ini"
sed -i .bak \
-e 's/^#rcpt_to.qmail_deliverable/rcpt_to.qmail_deliverable/' \
-e 's/^rcpt_to.in_host_list/# rcpt_to.in_host_list/' \
"$HARAKA_CONF/plugins"
}
config_haraka_p0f()
{
install_p0f
tell_status "enable Haraka p0f plugin"
sed -i -e 's/^# connect.p0f/connect.p0f/' "$HARAKA_CONF/plugins"
}
config_haraka_spamassassin()
{
if [ ! -d "$ZFS_JAIL_MNT/spamassassin" ]; then
tell_status "skipping spamassassin setup, no jail exists"
return
fi
tell_status "configuring Haraka spamassassin plugin"
sed -i.bak \
-e "/^spamd_socket/ s/127.0.0.1:/$(get_jail_ip spamassassin):/" \
-e 's/^;spamd_user=$/spamd_user=first-recipient/' \
-e '/^; reject_threshold/ s/^; //' \
-e 's/^; relay_reject_threshold$/relay_reject_threshold/' \
"$HARAKA_CONF/spamassassin.ini"
sed -i -e 's/^#spamassassin$/spamassassin/' "$HARAKA_CONF/plugins"
}
config_haraka_avg()
{
if ! zfs_filesystem_exists "$ZFS_DATA_VOL/avg"; then
echo "AVG not installed, skipping"
return
fi
tell_status "configuring Haraka avg plugin"
mkdir -p "$STAGE_MNT/data/avg" || exit
JAIL_CONF_EXTRA="$JAIL_CONF_EXTRA
mount += \"$ZFS_DATA_MNT/avg \$path/data/avg nullfs rw 0 0\";"
sed -i .bak \
-e "s/;host.*/host = $(get_jail_ip avg)/" \
-e 's/;tmpdir.*/tmpdir=\/data\/avg/' \
"$HARAKA_CONF/avg.ini"
# shellcheck disable=1004
sed -i -e '/clamd$/a\
avg
' "$HARAKA_CONF/plugins"
}
config_haraka_clamav()
{
if ! zfs_filesystem_exists "$ZFS_DATA_VOL/clamav"; then
tell_status "WARNING: skipping clamav plugin, no clamav jail exists"
return
fi
tell_status "configure Haraka clamav plugin"
echo "clamd_socket=$(get_jail_ip clamav):3310" >> "$HARAKA_CONF/clamd.ini"
sed -i -e 's/^#clamd$/clamd/' "$HARAKA_CONF/plugins"
}
config_haraka_tls() {
tell_status "enable TLS encryption"
sed -i -e 's/^# tls$/tls/' "$HARAKA_CONF/plugins"
cp /etc/ssl/certs/server.crt "$HARAKA_CONF/tls_cert.pem"
cp /etc/ssl/private/server.key "$HARAKA_CONF/tls_key.pem"
}
config_haraka_dnsbl()
{
tell_status "configuring dnsbls"
echo 'reject=0' > "$HARAKA_CONF/dnsbl.ini"
echo 'periodic_checks=30' >> "$HARAKA_CONF/dnsbl.ini"
echo "; zen.spamhaus.org
b.barracudacentral.org
truncate.gbudb.net
cbl.abuseat.org
psbl.surriel.com
bl.spamcop.net
dnsbl-1.uceprotect.net
pbl.spamhaus.org
xbl.spamhaus.org
" | tee "$HARAKA_CONF/dnsbl.zones"
}
cleanup_deprecated_haraka()
{
rm "$HARAKA_CONF/lookup_rdns.strict.ini"
rm "$HARAKA_CONF/lookup_rdns.strict.timeout"
rm "$HARAKA_CONF/lookup_rdns.strict.whitelist"
rm "$HARAKA_CONF/lookup_rdns.strict.whitelist_regex"
rm "$HARAKA_CONF/mail_from.access.blacklist_regex"
rm "$HARAKA_CONF/rcpt_to.blocklist"
rm "$HARAKA_CONF/rdns.allow_regexps"
rm "$HARAKA_CONF/rdns.deny_regexps"
}
config_haraka_rspamd()
{
if [ ! -d "$ZFS_JAIL_MNT/rspamd" ]; then
tell_status "skipping rspamd, no jail exists"
return
fi
tell_status "configure Haraka rspamd plugin"
sed -i .bak \
-e "s/;host.*/host = $(get_jail_ip rspamd)/" \
-e 's/;always_add_headers = false/always_add_headers = true/' \
"$HARAKA_CONF/rspamd.ini" || exit
# shellcheck disable=1004
sed -i -e '/spamassassin$/a\
rspamd
' "$HARAKA_CONF/plugins" || exit
}
config_haraka_watch()
{
echo 'watch' >> "$HARAKA_CONF/plugins" || exit
local _libdir="$STAGE_MNT/usr/local/lib/node_modules/Haraka/plugins/watch"
sed -i .bak \
-e '/^var rcpt_to_plugins/ s/in_host_list/qmail_deliverable/' \
-e "/^var data_plugins/ s/uribl/uribl', 'limit/; s/clamd/clamd', 'avg/" \
"$_libdir/html/client.js"
}
config_haraka_smtp_ini()
{
sed -i .bak \
-e 's/^;listen=\[.*$/listen=0.0.0.0:25,0.0.0.0:465,0.0.0.0:587/' \
-e 's/^;nodes=cpus/nodes=2/' \
-e 's/^;daemonize=true/daemonize=true/' \
-e 's/^;daemon_pid_file/daemon_pid_file/' \
-e 's/^;daemon_log_file/daemon_log_file/' \
"$HARAKA_CONF/smtp.ini" || exit
}
config_haraka_plugins()
{
# enable a bunch of plugins
sed -i .bak \
-e 's/^#process_title$/process_title/' \
-e 's/^#spf$/spf/' \
-e 's/^#bounce$/bounce/' \
-e 's/^#data.uribl$/data.uribl/' \
-e 's/^#attachment$/attachment/' \
-e 's/^#dkim_sign$/dkim_sign/' \
-e 's/^#karma$/karma/' \
-e 's/^# connect.fcrdns/connect.fcrdns/' \
"$HARAKA_CONF/plugins"
}
config_haraka_limit()
{
tell_status "configuring limit plugin"
sed -i .bak \
-e 's/^max_unrecognized_commands/limit/' \
"$HARAKA_CONF/plugins"
sed -i .bak \
-e 's/^; max/max/' \
-e 's/^; history/history/' \
-e 's/^; backend=ram/backend=redis/' \
-e 's/^; discon/discon/' \
"$HARAKA_CONF/limit.ini"
}
config_haraka_dkim()
{
sed -i .bak -e 's/^disabled = true/disabled = false/' \
"$HARAKA_CONF/dkim_sign.ini"
_dkim_dir="$ZFS_JAIL_MNT/haraka/usr/local/haraka/config/dkim/$TOASTER_MAIL_DOMAIN"
if [ -d "$_dkim_dir" ]; then
tell_status "copying active DKIM keys"
cp -R "$_dkim_dir/" "$HARAKA_CONF/dkim/"
return
else
tell_status "generating DKIM keys"
cd "$HARAKA_CONF/dkim" || exit
sh dkim_key_gen.sh "$TOASTER_MAIL_DOMAIN"
cat "$HARAKA_CONF/dkim/$TOASTER_MAIL_DOMAIN/dns"
tell_status "NOTICE: action required for DKIM validation. See message ^^^"
sleep 5
fi
}
config_haraka_karma()
{
sed -i .bak \
-e '/^dbid/ s/= 0/= 1/' \
-e "/^server_ip/ s/127.0.0.1/$(get_jail_ip redis)/" \
-e '/^plugins=/ s/clamd, //' \
"$HARAKA_CONF/karma.ini"
}
config_haraka_redis()
{
tell_status "configuring redis plugin"
echo 'redis' | tee -a "$HARAKA_CONF/plugins"
tee "$HARAKA_CONF/redis.ini" <<EO_REDIS_CONF
[server]
host=$(get_jail_ip redis)
; port=6379
db=3
EO_REDIS_CONF
}
config_haraka_geoip() {
sed -i .bak \
-e '/^;[asn]/ s/^;//' \
-e '/^;report_as=/ s/^;//' \
"$HARAKA_CONF/connect.geoip.ini"
}
config_haraka_http()
{
tell_status "enable Haraka HTTP server"
sed -i -e 's/; listen=\[::\]:80/listen=0.0.0.0:80/' "$HARAKA_CONF/http.ini"
}
configure_haraka()
{
tell_status "installing Haraka, stage 2"
stage_exec haraka -i /usr/local/haraka || exit
tell_status "configuring Haraka"
echo 'LOGINFO' > "$HARAKA_CONF/loglevel"
echo '3' > "$HARAKA_CONF/tarpit.timeout"
config_haraka_smtp_ini
config_haraka_plugins
config_haraka_limit
config_haraka_syslog
config_haraka_vpopmail
config_haraka_qmail_deliverable
config_haraka_dnsbl
sed -i -e 's/^; reject=.*/reject=no/' "$HARAKA_CONF/data.headers.ini"
config_haraka_http
config_haraka_tls
config_haraka_dkim
config_haraka_p0f
config_haraka_spamassassin
config_haraka_rspamd
config_haraka_clamav
config_haraka_avg
config_haraka_watch
config_haraka_karma
config_haraka_redis
config_haraka_geoip
install_geoip_dbs
cleanup_deprecated_haraka
}
start_haraka()
{
tell_status "starting haraka"
cp "$STAGE_MNT/usr/local/lib/node_modules/Haraka/contrib/bsd-rc.d/haraka" \
"$STAGE_MNT/usr/local/etc/rc.d/haraka"
chmod 555 "$STAGE_MNT/usr/local/etc/rc.d/haraka"
stage_sysrc haraka_enable=YES
sysrc -f "$STAGE_MNT/etc/rc.conf" haraka_flags='-c /usr/local/haraka'
mkdir -p "$STAGE_MNT/usr/local/haraka/queue" || exit
stage_exec service haraka start || exit
}
test_haraka()
{
tell_status "waiting for Haraka to start listeners"
sleep 3
tell_status "testing Haraka"
stage_exec sockstat -l -4 | grep :25 || exit
echo "it worked"
}
preinstall_checks() {
base_snapshot_exists || exit
if ! zfs_filesystem_exists "$ZFS_DATA_VOL/redis"; then
tell_status "FATAL: redis jail required but not provisioned."
exit
fi
}
preinstall_checks
create_staged_fs haraka
add_devfs_rule
start_staged_jail haraka
install_haraka
configure_haraka
start_haraka
test_haraka
promote_staged_jail haraka