2023-07-14 21:02:14 -06:00
|
|
|
#!/bin/sh
|
|
|
|
|
#
|
2025-01-26 20:45:37 -05:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
#
|
2025-01-11 14:07:41 -05:00
|
|
|
# Copyright (c) 2018-2025, 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.
|
|
|
|
|
|
|
|
|
|
. /usr/local/share/bastille/common.sh
|
|
|
|
|
|
|
|
|
|
usage() {
|
2025-01-26 20:45:37 -05:00
|
|
|
error_exit "Usage: bastille setup [pf|network|zfs|vnet]"
|
2023-07-14 21:02:14 -06:00
|
|
|
}
|
|
|
|
|
|
2025-01-26 20:45:37 -05:00
|
|
|
# Check for too many args
|
2023-07-14 21:02:14 -06:00
|
|
|
if [ $# -gt 1 ]; then
|
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
2025-01-26 20:45:37 -05:00
|
|
|
# Configure bastille loopback network interface
|
2024-11-24 09:47:57 +01:00
|
|
|
configure_network() {
|
2025-04-21 20:38:33 -06:00
|
|
|
if ! sysrc -n cloned_interfaces | grep -oq "lo1"; then
|
|
|
|
|
info "Configuring ${bastille_network_loopback} loopback interface"
|
|
|
|
|
sysrc cloned_interfaces+=lo1
|
|
|
|
|
sysrc ifconfig_lo1_name="${bastille_network_loopback}"
|
2023-07-14 21:02:14 -06:00
|
|
|
|
2025-04-21 20:38:33 -06:00
|
|
|
info "Bringing up new interface: ${bastille_network_loopback}"
|
|
|
|
|
service netif cloneup
|
|
|
|
|
else
|
|
|
|
|
info "Network has already been configured!"
|
|
|
|
|
fi
|
2023-07-14 21:02:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
configure_vnet() {
|
2025-04-22 11:16:25 -06:00
|
|
|
_bridge_name="bastille1"
|
|
|
|
|
if ! sysrc -n cloned_interfaces | grep -oq "${_bridge_name}"; then
|
|
|
|
|
info "Configuring bastille1 bridge interface..."
|
|
|
|
|
sysrc cloned_interfaces+="bridge1"
|
|
|
|
|
sysrc ifconfig_bridge1_name="${_bridge_name}"
|
2025-01-21 05:55:37 -04:00
|
|
|
|
2025-04-22 11:16:25 -06:00
|
|
|
info "Bringing up new interface: ${_bridge_name}..."
|
2025-04-21 20:38:33 -06:00
|
|
|
service netif cloneup
|
2023-11-25 17:19:57 -07:00
|
|
|
|
2025-04-21 20:38:33 -06:00
|
|
|
if [ ! -f /etc/devfs.rules ]; then
|
|
|
|
|
info "Creating bastille_vnet devfs.rules"
|
|
|
|
|
cat << EOF > /etc/devfs.rules
|
2023-11-25 17:19:57 -07:00
|
|
|
[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
|
2025-04-21 20:38:33 -06:00
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
info "VNET has already been configured!"
|
2023-11-25 17:19:57 -07:00
|
|
|
fi
|
2023-07-14 21:02:14 -06:00
|
|
|
}
|
|
|
|
|
|
2025-01-26 20:45:37 -05:00
|
|
|
# Configure pf firewall
|
2023-07-14 21:02:14 -06:00
|
|
|
configure_pf() {
|
2025-01-26 20:45:37 -05:00
|
|
|
# shellcheck disable=SC2154
|
|
|
|
|
if [ ! -f "${bastille_pf_conf}" ]; then
|
|
|
|
|
# shellcheck disable=SC3043
|
|
|
|
|
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..."
|
|
|
|
|
|
|
|
|
|
## creating pf.conf
|
|
|
|
|
cat << EOF > "${bastille_pf_conf}"
|
|
|
|
|
## generated by bastille setup
|
2023-07-14 21:02:14 -06:00
|
|
|
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
|
2025-01-26 20:45:37 -05:00
|
|
|
sysrc pf_enable=YES
|
|
|
|
|
warn "pf ruleset created, please review ${bastille_pf_conf} and enable it using 'service pf start'."
|
|
|
|
|
else
|
2025-04-21 20:38:33 -06:00
|
|
|
info "PF has already been configured!"
|
2025-01-26 20:45:37 -05:00
|
|
|
fi
|
2023-07-14 21:02:14 -06:00
|
|
|
}
|
|
|
|
|
|
2025-01-26 20:45:37 -05:00
|
|
|
# Configure ZFS
|
2023-07-14 21:02:14 -06:00
|
|
|
configure_zfs() {
|
2025-01-26 20:45:37 -05:00
|
|
|
if [ ! "$(kldstat -m zfs)" ]; then
|
|
|
|
|
info "ZFS module not loaded; skipping..."
|
2025-04-21 20:38:33 -06:00
|
|
|
elif sysrc -f ${BASTILLE_CONFIG} -n bastille_zfs_enable | grep -Eoq "([Y|y][E|e][S|s])"; then
|
|
|
|
|
info "ZFS has already been configured!"
|
2025-01-21 05:55:37 -04:00
|
|
|
else
|
2025-01-26 20:45:37 -05:00
|
|
|
## attempt to determine bastille_zroot from `zpool list`
|
|
|
|
|
bastille_zroot=$(zpool list | grep -v NAME | awk '{print $1}')
|
|
|
|
|
if [ "$(echo "${bastille_zroot}" | wc -l)" -gt 1 ]; then
|
|
|
|
|
error_notify "Error: Multiple ZFS pools available:\n${bastille_zroot}"
|
2025-04-19 12:21:08 -05:00
|
|
|
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\""
|
2025-01-21 05:55:37 -04:00
|
|
|
fi
|
2025-04-19 12:21:08 -05:00
|
|
|
sysrc -f "${BASTILLE_CONFIG}" bastille_zfs_enable=YES
|
|
|
|
|
sysrc -f "${BASTILLE_CONFIG}" bastille_zfs_zpool="${bastille_zroot}"
|
2025-01-21 05:55:37 -04:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 20:45:37 -05:00
|
|
|
# Run all base functions (w/o vnet) if no args
|
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
|
sysrc bastille_enable=YES
|
|
|
|
|
configure_network
|
|
|
|
|
configure_pf
|
|
|
|
|
configure_zfs
|
|
|
|
|
fi
|
2025-01-21 05:55:37 -04:00
|
|
|
|
2025-04-22 11:16:25 -06:00
|
|
|
# Handle options.
|
2025-01-26 20:45:37 -05:00
|
|
|
case "$1" in
|
2025-04-22 11:16:25 -06:00
|
|
|
-h|--help|help)
|
|
|
|
|
usage
|
|
|
|
|
;;
|
|
|
|
|
-p|pf|firewall)
|
|
|
|
|
configure_pf
|
|
|
|
|
;;
|
|
|
|
|
-n|-l|network|loopback)
|
|
|
|
|
configure_network
|
|
|
|
|
;;
|
|
|
|
|
-z|zfs|storage)
|
|
|
|
|
configure_zfs
|
|
|
|
|
;;
|
|
|
|
|
-v|vnet|bridge)
|
|
|
|
|
configure_vnet
|
|
|
|
|
;;
|
2023-07-14 21:02:14 -06:00
|
|
|
esac
|