Files
BastilleBSD_bastille/usr/local/share/bastille/setup.sh

160 lines
5.0 KiB
Bash
Raw Normal View History

2023-07-14 21:02:14 -06:00
#!/bin/sh
#
# Copyright (c) 2018-2024, Christer Edwards <christer.edwards@gmail.com>
2023-07-14 21:02:14 -06:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bastille_config="/usr/local/etc/bastille/bastille.conf"
2023-07-14 21:02:14 -06:00
. /usr/local/share/bastille/common.sh
2024-11-23 20:20:14 -05:00
# shellcheck source=/usr/local/etc/bastille/bastille.conf
. ${bastille_config}
2023-07-14 21:02:14 -06:00
usage() {
error_exit "Usage: bastille setup [pf|network|zfs|vnet]"
2023-07-14 21:02:14 -06:00
}
# Check for too many args
if [ $# -gt 1 ]; then
usage
fi
# Configure bastille loopback network interface
configure_network() {
info "Configuring ${bastille_network_loopback} loopback interface"
2023-07-14 21:02:14 -06:00
sysrc cloned_interfaces+=lo1
sysrc ifconfig_lo1_name="${bastille_network_loopback}"
2023-07-14 21:02:14 -06:00
info "Bringing up new interface: ${bastille_network_loopback}"
2023-07-14 21:02:14 -06:00
service netif cloneup
}
configure_vnet() {
info "Configuring bridge interface"
sysrc cloned_interfaces+=bridge1
sysrc ifconfig_bridge1_name=bastille1
info "Bringing up new interface: bastille1"
service netif cloneup
if [ ! -f /etc/devfs.rules ]; then
info "Creating bastille_vnet devfs.rules"
cat << EOF > /etc/devfs.rules
[bastille_vnet=13]
add include \$devfsrules_hide_all
add include \$devfsrules_unhide_basic
add include \$devfsrules_unhide_login
add include \$devfsrules_jail
add include \$devfsrules_jail_vnet
add path 'bpf*' unhide
EOF
fi
2023-07-14 21:02:14 -06:00
}
# Configure pf firewall
configure_pf() {
2024-11-23 20:20:14 -05:00
# shellcheck disable=SC2154
if [ ! -f "${bastille_pf_conf}" ]; then
2024-11-23 20:20:14 -05:00
# shellcheck disable=SC3043
2023-07-14 21:02:14 -06:00
local ext_if
ext_if=$(netstat -rn | awk '/default/ {print $4}' | head -n1)
info "Determined default network interface: ($ext_if)"
info "${bastille_pf_conf} does not exist: creating..."
2023-07-14 21:02:14 -06:00
## creating pf.conf
2024-11-23 20:20:14 -05:00
cat << EOF > "${bastille_pf_conf}"
2023-07-14 21:02:14 -06:00
## generated by bastille setup
ext_if="$ext_if"
set block-policy return
scrub in on \$ext_if all fragment reassemble
set skip on lo
table <jails> persist
nat on \$ext_if from <jails> to any -> (\$ext_if:0)
rdr-anchor "rdr/*"
block in all
pass out quick keep state
antispoof for \$ext_if inet
pass in inet proto tcp from any to any port ssh flags S/SA keep state
EOF
sysrc pf_enable=YES
warn "pf ruleset created, please review ${bastille_pf_conf} and enable it using 'service pf start'."
2023-07-14 21:02:14 -06:00
else
error_exit "${bastille_pf_conf} already exists. Exiting."
2023-07-14 21:02:14 -06:00
fi
}
# Configure ZFS
configure_zfs() {
if [ ! "$(kldstat -m zfs)" ]; then
2023-07-14 21:02:14 -06:00
info "ZFS module not loaded; skipping..."
else
## attempt to determine bastille_zroot from `zpool list`
2023-07-14 21:02:14 -06:00
bastille_zroot=$(zpool list | grep -v NAME | awk '{print $1}')
2024-12-08 15:12:15 -05:00
if [ "$(echo "${bastille_zroot}" | wc -l)" -gt 1 ]; then
error_notify "Error: Multiple ZFS pools available:\n${bastille_zroot}"
error_notify "Set desired pool using \"sysrc -f ${bastille_config} bastille_zfs_zpool=ZPOOL_NAME\""
error_exit "Don't forget to also enable ZFS using \"sysrc -f ${bastille_config} bastille_zfs_enable=YES\""
fi
sysrc -f "${bastille_config}" bastille_zfs_enable=YES
sysrc -f "${bastille_config}" bastille_zfs_zpool="${bastille_zroot}"
2023-07-14 21:02:14 -06:00
fi
}
# Run all base functions (w/o vnet) if no args
2023-07-14 21:02:14 -06:00
if [ $# -eq 0 ]; then
sysrc bastille_enable=YES
configure_network
2023-07-14 21:02:14 -06:00
configure_pf
configure_zfs
fi
# Handle special-case commands first.
case "$1" in
help|-h|--help)
usage
;;
pf|firewall)
configure_pf
;;
bastille0)
# TODO remove in future release 0.13
warn "'bastille setup bastille0' will be deprecated in the next 0.13 version."
configure_network
;;
network|loopback)
configure_network
2023-07-14 21:02:14 -06:00
;;
zfs|storage)
2023-07-14 21:02:14 -06:00
configure_zfs
;;
bastille1|vnet|bridge)
configure_vnet
;;
2023-07-14 21:02:14 -06:00
esac