forked from msimerson/Mail-Toaster-6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision-host.sh
executable file
·478 lines (397 loc) · 11.1 KB
/
provision-host.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/sh
# shellcheck disable=1091
. mail-toaster.sh || exit
export JAIL_START_EXTRA=""
export JAIL_CONF_EXTRA=""
mt6-include shell
configure_ntp()
{
if [ "$TOASTER_NTP" = "ntimed" ]; then
configure_ntimed
else
configure_ntpd
fi
}
configure_ntimed()
{
tell_status "installing and enabling Ntimed"
if grep -q ^ntpd_enable /etc/rc.conf; then
sysrc ntpd_enable=NO
fi
pkg install -y ntimed
sysrc ntimed_enable=YES
service ntimed start
}
configure_ntpd()
{
if grep -q ^ntpd_enable /etc/rc.conf; then
return
fi
tell_status "enabling NTPd"
sysrc ntpd_enable=YES || exit
sysrc ntpd_sync_on_start=YES
/etc/rc.d/ntpd restart
}
update_syslogd()
{
if grep -q ^syslogd_flags /etc/rc.conf; then
tell_status "preserving syslogd_flags"
echo "CAUTION: double check syslogd_flags in /etc/rc.conf"
echo "existing:"
grep ^syslogd_flags /etc/rc.conf
echo "desired:"
echo "syslogd_flags=-b $JAIL_NET_PREFIX.1 -a $JAIL_NET_PREFIX.0$JAIL_NET_MASK:* -cc"
return
fi
tell_status "configuring syslog to accept messages from jails"
sysrc syslogd_flags="-b $JAIL_NET_PREFIX.1 -a $JAIL_NET_PREFIX.0$JAIL_NET_MASK:* -cc"
service syslogd restart
}
update_sendmail()
{
if grep -q ^sendmail_enable /etc/rc.conf; then
tell_status "preserving sendmail config"
return
fi
tell_status "disable sendmail network listening"
sysrc sendmail_enable=NO
service sendmail onestop
}
install_periodic_conf()
{
if [ -f /etc/periodic.conf ]; then
return
fi
tell_status "installing /etc/periodic.conf"
tee -a /etc/periodic.conf <<EO_PERIODIC
# older versions of FreeBSD bark b/c these are defined in
# /etc/defaults/periodic.conf and do not exist. Hush.
daily_local=""
weekly_local=""
monthly_local=""
# in case /etc/aliases isn't set up properly
daily_output="$TOASTER_ADMIN_EMAIL"
weekly_output="$TOASTER_ADMIN_EMAIL"
monthly_output="$TOASTER_ADMIN_EMAIL"
security_show_success="NO"
security_show_info="YES"
security_status_chksetuid_enable="NO"
security_status_neggrpperm_enable="NO"
security_status_pkgaudit_enable="YES"
security_status_tcpwrap_enable="YES"
security_status_ipfwlimit_enable="NO"
security_status_ipfwdenied_enable="NO"
security_status_pfdenied_enable="NO"
daily_accounting_enable="NO"
daily_accounting_compress="YES"
daily_clean_disks_enable="NO"
daily_clean_disks_days=14
daily_clean_disks_verbose="NO"
daily_clean_hoststat_enable="NO"
daily_clean_tmps_enable="YES"
daily_clean_tmps_verbose="NO"
daily_news_expire_enable="NO"
daily_scrub_zfs_enable="YES"
daily_show_success="NO"
daily_show_info="NO"
daily_show_badconfig="YES"
daily_status_disks_enable="NO"
daily_status_include_submit_mailq="NO"
daily_status_mail_rejects_enable="YES"
daily_status_mailq_enable="NO"
daily_status_rwho_enable="NO"
daily_submit_queuerun="NO"
daily_status_smart_enable=YES
daily_status_smart_devices="AUTO"
weekly_show_success="NO"
weekly_show_info="NO"
weekly_show_badconfig="YES"
weekly_whatis_enable="NO"
monthly_show_success="NO"
monthly_show_info="NO"
monthly_show_badconfig="YES"
EO_PERIODIC
}
constrain_sshd_to_host()
{
if grep -q ^ListenAddress /etc/ssh/sshd_config; then
tell_status "preserving sshd_config ListenAddress"
return
fi
tell_status "checking sshd listening scope"
if ! sockstat -L | egrep '\*:22 '; then
return
fi
get_public_ip
get_public_ip ipv6
local _sshd_conf="/etc/ssh/sshd_config"
local _confirm_msg="
To not interfere with the jails, sshd must be constrained to
listening on your hosts public facing IP(s).
Your public IPs are detected as $PUBLIC_IP4
and $PUBLIC_IP6
May I update $_sshd_conf?
"
dialog --yesno "$_confirm_msg" 13 70 || return
tell_status "Limiting SSHd to host IP address"
sed -i .bak -e "s/#ListenAddress 0.0.0.0/ListenAddress $PUBLIC_IP4/" $_sshd_conf
if [ -n "$PUBLIC_IP6" ]; then
sed -i .bak6 -e "s/#ListenAddress ::/ListenAddress $PUBLIC_IP6/" $_sshd_conf
fi
grep ^Listen /etc/ssh/sshd_config
service sshd configtest || exit
service sshd restart
}
configure_tls_certs()
{
if [ -s /etc/ssl/private/server.key ]; then
tell_status "TLS certificates already exist"
return
fi
if [ ! -d /etc/ssl/certs ]; then
mkdir "/etc/ssl/certs" "/etc/ssl/private"
chmod o-r "/etc/ssl/private"
fi
if ! grep -q commonName_default /etc/ssl/openssl.cnf; then
tell_status "updating openssl.cnf defaults"
local _geo; _geo=$(fetch -o - https://freegeoip.net/csv)
local _cc; _cc=$(echo "$_geo" | cut -d',' -f2)
local _state; _state=$(echo "$_geo" | cut -d',' -f5)
local _city; _city=$(echo "$_geo" | cut -d',' -f6)
sed -i .bak \
-e "/^commonName_max.*/ a\
commonName_default = $TOASTER_HOSTNAME" \
-e "/^emailAddress_max.*/ a\
emailAddress_default = $TOASTER_ADMIN_EMAIL" \
-e "/^localityName.*/ a\
localityName_default = $_city" \
-e "/^countryName_default/ s/AU/$_cc/" \
-e "/^stateOrProvinceName_default/ s/Some-State/$_state/" \
/etc/ssl/openssl.cnf
fi
if [ -f /etc/ssl/private/server.key ]; then
tell_status "preserving existing TLS certificates"
return
fi
echo
echo "A number of daemons use TLS to encrypt connections. Setting up TLS now"
echo " saves having to do it multiple times later."
tell_status "Generating self-signed SSL certificates"
openssl req -x509 -nodes -days 2190 \
-newkey rsa:2048 \
-keyout /etc/ssl/private/server.key \
-out /etc/ssl/certs/server.crt
if [ ! -f /etc/ssl/private/server.key ]; then
fatal_err "no TLS key was generated!"
fi
}
check_global_listeners()
{
tell_status "checking for host listeners on all IPs"
if sockstat -L -4 | egrep '\*:[0-9]' | grep -v 123; then
echo "oops!, you should not having anything listening
on all your IP addresses!"
exit 2
fi
}
add_jail_nat()
{
if grep -qs bruteforce /etc/pf.conf; then
tell_status "preserving pf.conf settings"
return
fi
get_public_ip
get_public_ip ipv6
if [ -z "$PUBLIC_NIC" ]; then echo "PUBLIC_NIC unset!"; exit; fi
if [ -z "$PUBLIC_IP4" ]; then echo "PUBLIC_IP4 unset!"; exit; fi
tell_status "enabling NAT for jails"
tee -a /etc/pf.conf <<EO_PF_RULES
ext_if="$PUBLIC_NIC"
table <ext_ips> { $PUBLIC_IP4 $PUBLIC_IP6 }
table <bruteforce> persist
# default route to the internet for jails
nat on \$ext_if from $JAIL_NET_PREFIX.0${JAIL_NET_MASK} to any -> (\$ext_if)
# POP3 & IMAP traffic to dovecot jail
rdr proto tcp from any to <ext_ips> port { 110 143 993 995 } -> $(get_jail_ip dovecot)
# SMTP traffic to the Haraka jail
rdr proto tcp from any to <ext_ips> port { 25 465 587 } -> $(get_jail_ip haraka)
# HTTP traffic to HAproxy
rdr proto tcp from any to <ext_ips> port { 80 443 } -> $(get_jail_ip haproxy)
# DHCP traffic
rdr proto udp from any to any port { 67 68 } -> $(get_jail_ip dhcp)
block in quick from <bruteforce>
EO_PF_RULES
_pf_loaded=$(kldstat -m pf | grep pf)
if [ -n "$_pf_loaded" ]; then
pfctl -f /etc/pf.conf 2>/dev/null || exit
else
kldload pf
fi
sysrc pf_enable=YES
/etc/rc.d/pf restart 2>/dev/null || exit
}
install_jailmanage()
{
if [ -s /usr/local/sbin/jailmanage ]; then return; fi
tell_status "installing jailmanage"
fetch -o /usr/local/sbin/jailmanage https://raw.githubusercontent.com/msimerson/jailmanage/master/jailmanage.sh
chmod 755 /usr/local/sbin/jailmanage
}
set_jail_start_order()
{
if grep -q ^jail_list /etc/rc.conf; then
tell_status "preserving jail order"
return
fi
tell_status "setting jail startup order"
sysrc jail_list="$JAIL_STARTUP_LIST"
}
jail_reverse_shutdown()
{
local _fbsd_major; _fbsd_major=$(freebsd-version | cut -f1 -d'.')
if [ "$_fbsd_major" = "11" ]; then
if grep -q jail_reverse_stop /etc/rc.conf; then
return
fi
tell_status "reverse jails when shutting down"
sysrc jail_reverse_stop=YES
return
fi
if grep -q _rev_jail_list /etc/rc.d/jail; then
echo "rc.d/jail is already patched"
return
fi
tell_status "patching jail so shutdown reverses jail order"
patch -d / <<'EO_JAIL_RCD'
Index: etc/rc.d/jail
===================================================================
--- etc/rc.d/jail
+++ etc/rc.d/jail
@@ -516,7 +516,10 @@
command=$jail_program
rc_flags=$jail_flags
command_args="-f $jail_conf -r"
- $jail_jls name | while read _j; do
+ for _j in $($jail_jls name); do
+ _rev_jail_list="${_j} ${_rev_jail_list}"
+ done
+ for _j in ${_rev_jail_list}; do
echo -n " $_j"
_tmp=`mktemp -t jail` || exit 3
$command $rc_flags $command_args $_j >> $_tmp 2>&1
@@ -532,6 +535,9 @@
;;
esac
for _j in $@; do
+ _rev_jail_list="${_j} ${_rev_jail_list}"
+ done
+ for _j in ${_rev_jail_list}; do
_j=$(echo $_j | tr /. _)
parse_options $_j || continue
if ! $jail_jls -j $_j > /dev/null 2>&1; then
EO_JAIL_RCD
}
enable_jails()
{
sysrc jail_enable=YES
jail_reverse_shutdown
set_jail_start_order
if grep -sq 'exec' /etc/jail.conf; then
return
fi
jail_conf_header
}
update_ports_tree()
{
tell_status "updating FreeBSD ports tree"
portsnap fetch || exit
if [ -d /usr/ports/mail/vpopmail ]; then
portsnap update || portsnap extract || exit
else
portsnap extract || exit
fi
}
update_freebsd() {
if grep -q '^Components src' /etc/freebsd-update.conf; then
tell_status "remove src from freebsd-update"
sed -i .bak -e '/^Components/ s/src //' /etc/freebsd-update.conf
fi
tell_status "updating FreeBSD with security patches"
freebsd-update fetch install
tell_status "updating FreeBSD pkg collection"
pkg update || exit
if ! pkg info -e ca_root_nss; then
tell_status "install CA root certs, so https URLs work"
pkg install -y ca_root_nss
fi
tell_status "upgrading installed FreeBSD packages"
pkg upgrade
update_ports_tree
}
plumb_jail_nic()
{
if [ "$JAIL_NET_INTERFACE" != "lo1" ]; then
tell_status "plumb_jail_nic: using $JAIL_NET_INTERFACE"
return;
fi
if ! grep -q cloned_interfaces /etc/rc.conf; then
tell_status "plumb lo1 interface at startup"
sysrc cloned_interfaces=lo1 || exit
fi
local _missing;
_missing=$(ifconfig lo1 2>&1 | grep 'does not exist')
if [ -n "$_missing" ]; then
tell_status "plumb lo1 interface"
ifconfig lo1 create || exit
fi
}
assign_syslog_ip()
{
if ! grep -q ifconfig_lo1 /etc/rc.conf; then
tell_status "adding syslog IP to lo1"
sysrc ifconfig_lo1="$JAIL_NET_PREFIX.1 netmask 255.255.255.0" || exit
fi
local _present
_present=$(ifconfig lo1 2>&1 | grep "$JAIL_NET_PREFIX.1 ")
if [ -z "$_present" ]; then
echo "assigning $JAIL_NET_PREFIX.1 to lo1"
ifconfig lo1 "$JAIL_NET_PREFIX.1" netmask 255.255.255.0 || exit
fi
}
configure_etc_hosts()
{
# this is really important since syslog does a DNS lookup for the remote
# hosts DNS on *every* incoming syslog message.
if grep -q "^$JAIL_NET_PREFIX" /etc/hosts; then
tell_status "removing /etc/hosts toaster additions"
sed -i .bak -e "/^$JAIL_NET_PREFIX.*/d" /etc/hosts
fi
tell_status "adding /etc/hosts entries"
local _hosts
for j in $JAIL_ORDERED_LIST;
do
_hosts="$_hosts
$(get_jail_ip "$j") $j"
done
echo "$_hosts" | tee -a "/etc/hosts"
}
update_host() {
update_freebsd
configure_ntp
update_sendmail
install_periodic_conf
constrain_sshd_to_host
plumb_jail_nic
assign_syslog_ip
update_syslogd
check_global_listeners
add_jail_nat
configure_tls_certs
enable_jails
install_jailmanage
configure_etc_hosts
configure_csh_shell ""
configure_bourne_shell ""
echo; echo "Success! Your host is ready to install Mail Toaster 6!"; echo
}
update_host