2018-11-07 10:36:54 -07:00
#!/bin/sh
2020-04-14 11:52:29 +02:00
#
2020-01-26 09:51:02 -07:00
# Copyright (c) 2018-2020, Christer Edwards <christer.edwards@gmail.com>
2018-11-07 10:36:54 -07:00
# All rights reserved.
2020-04-14 11:52:29 +02:00
#
2018-11-07 10:36:54 -07:00
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
2020-04-14 11:52:29 +02:00
#
2018-11-07 10:36:54 -07:00
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
2020-04-14 11:52:29 +02:00
#
2018-11-07 10:36:54 -07:00
# * 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.
2020-04-14 11:52:29 +02:00
#
2018-11-07 10:36:54 -07:00
# * 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.
2020-04-14 11:52:29 +02:00
#
2018-11-07 10:36:54 -07:00
# 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/colors.pre.sh
. /usr/local/etc/bastille/bastille.conf
usage( ) {
2019-11-22 22:02:09 -07:00
echo -e " ${ COLOR_RED } Usage: bastille create [option] name release ip [interface]. ${ COLOR_RESET } "
2018-11-07 10:36:54 -07:00
exit 1
}
2020-05-09 15:31:15 -04:00
error_notify( ) {
# Notify message on error and exit
echo -e " $* " >& 2
exit 1
}
2018-11-07 10:36:54 -07:00
running_jail( ) {
2019-12-29 23:04:19 -04:00
if [ -n " $( jls name | awk " /^ ${ NAME } $/ " ) " ] ; then
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } A running jail matches name. ${ COLOR_RESET } "
2019-12-29 23:04:19 -04:00
elif [ -d " ${ bastille_jailsdir } / ${ NAME } " ] ; then
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Jail: ${ NAME } already created. ${ COLOR_RESET } "
fi
}
validate_name( ) {
local NAME_VERIFY = ${ NAME }
local NAME_SANITY = $( echo " ${ NAME_VERIFY } " | tr -c -d 'a-zA-Z0-9-_' )
if [ " ${ NAME_VERIFY } " != " ${ NAME_SANITY } " ] ; then
error_notify " ${ COLOR_RED } Container names may not contain special characters! ${ COLOR_RESET } "
2019-12-29 23:04:19 -04:00
fi
2018-11-07 10:36:54 -07:00
}
validate_ip( ) {
2020-02-16 13:22:32 -04:00
IPX_ADDR = "ip4.addr"
IP6_MODE = "disable"
ip6 = $( echo " ${ IP } " | grep -E '^(([a-fA-F0-9:]+$)|([a-fA-F0-9:]+\/[0-9]{1,3}$))' )
if [ -n " ${ ip6 } " ] ; then
echo -e " ${ COLOR_GREEN } Valid: ( ${ ip6 } ). ${ COLOR_RESET } "
IPX_ADDR = "ip6.addr"
IP6_MODE = "new"
2018-11-07 10:36:54 -07:00
else
2020-02-16 13:22:32 -04:00
local IFS
2020-02-18 17:04:06 -04:00
if echo " ${ IP } " | grep -Eq '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))?$' ; then
2020-02-20 18:06:31 -04:00
TEST_IP = $( echo " ${ IP } " | cut -d / -f1)
2020-02-16 13:22:32 -04:00
IFS = .
2020-02-16 15:20:31 -07:00
set ${ TEST_IP }
2020-02-16 13:22:32 -04:00
for quad in 1 2 3 4; do
if eval [ \$ $quad -gt 255 ] ; then
2020-02-18 17:04:06 -04:00
echo " Invalid: ( ${ TEST_IP } ) "
2020-02-16 13:22:32 -04:00
exit 1
fi
done
2020-02-20 18:06:31 -04:00
if ifconfig | grep -qw " ${ TEST_IP } " ; then
2020-02-16 15:20:31 -07:00
echo -e " ${ COLOR_YELLOW } Warning: ip address already in use ( ${ TEST_IP } ). ${ COLOR_RESET } "
2020-02-16 13:22:32 -04:00
else
2020-02-16 15:20:31 -07:00
echo -e " ${ COLOR_GREEN } Valid: ( ${ IP } ). ${ COLOR_RESET } "
2020-02-16 13:22:32 -04:00
fi
else
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Invalid: ( ${ IP } ). ${ COLOR_RESET } "
2020-02-16 13:22:32 -04:00
fi
2018-11-07 10:36:54 -07:00
fi
}
2019-10-24 17:02:50 -04:00
validate_netif( ) {
local LIST_INTERFACES = $( ifconfig -l)
2020-01-28 17:36:17 -07:00
if echo " ${ LIST_INTERFACES } VNET " | grep -qwo " ${ INTERFACE } " ; then
echo -e " ${ COLOR_GREEN } Valid: ( ${ INTERFACE } ). ${ COLOR_RESET } "
2019-10-24 17:02:50 -04:00
else
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Invalid: ( ${ INTERFACE } ). ${ COLOR_RESET } "
2019-10-24 17:02:50 -04:00
fi
}
2019-11-20 16:09:26 -04:00
validate_netconf( ) {
2020-04-12 17:04:37 -06:00
if [ -n " ${ bastille_network_loopback } " ] && [ -n " ${ bastille_network_shared } " ] ; then
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Invalid network configuration. ${ COLOR_RESET } "
2019-11-20 16:09:26 -04:00
fi
}
2020-02-02 02:42:22 -04:00
validate_release( ) {
## check release name match, else show usage
if [ -n " ${ NAME_VERIFY } " ] ; then
RELEASE = " ${ NAME_VERIFY } "
else
usage
fi
}
2020-04-18 18:02:11 -04:00
generate_minimal_conf( ) {
cat << EOF > " ${ bastille_jail_conf } "
${ NAME } {
host.hostname = ${ NAME } ;
mount.fstab = ${ bastille_jail_fstab } ;
path = ${ bastille_jail_path } ;
}
EOF
touch " ${ bastille_jail_fstab } "
}
2020-02-16 15:20:31 -07:00
generate_jail_conf( ) {
2020-02-20 18:06:31 -04:00
cat << EOF > " ${ bastille_jail_conf } "
2020-02-16 15:20:31 -07:00
${ NAME } {
devfs_ruleset = 4;
enforce_statfs = 2;
exec.clean;
exec.consolelog = ${ bastille_jail_log } ;
exec.start = '/bin/sh /etc/rc' ;
exec.stop = '/bin/sh /etc/rc.shutdown' ;
host.hostname = ${ NAME } ;
mount.devfs;
mount.fstab = ${ bastille_jail_fstab } ;
path = ${ bastille_jail_path } ;
securelevel = 2;
interface = ${ bastille_jail_conf_interface } ;
${ IPX_ADDR } = ${ IP } ;
2020-02-16 15:22:32 -07:00
ip6 = ${ IP6_MODE } ;
2020-02-16 15:20:31 -07:00
}
EOF
}
generate_vnet_jail_conf( ) {
## determine number of containers + 1
## iterate num and grep all jail configs
## define uniq_epair
2020-03-30 08:48:04 +00:00
local jail_list = $( bastille list jails)
2020-03-30 08:41:33 +00:00
if [ -n " ${ jail_list } " ] ; then
2020-03-30 08:48:04 +00:00
local list_jails_num = $( echo " ${ jail_list } " | wc -l | awk '{print $1}' )
local num_range = $( expr " ${ list_jails_num } " + 1)
2020-03-30 08:41:33 +00:00
for _num in $( seq 0 " ${ num_range } " ) ; do
2020-02-20 12:22:25 -04:00
if ! grep -q " e0b_bastille ${ _num } " " ${ bastille_jailsdir } " /*/jail.conf; then
2020-02-18 17:04:06 -04:00
uniq_epair = " bastille ${ _num } "
break
fi
2020-03-30 08:41:33 +00:00
done
else
uniq_epair = "bastille0"
fi
2020-02-16 15:20:31 -07:00
## generate config
2020-02-20 18:06:31 -04:00
cat << EOF > " ${ bastille_jail_conf } "
2020-02-16 15:20:31 -07:00
${ NAME } {
devfs_ruleset = 13;
enforce_statfs = 2;
exec.clean;
exec.consolelog = ${ bastille_jail_log } ;
exec.start = '/bin/sh /etc/rc' ;
exec.stop = '/bin/sh /etc/rc.shutdown' ;
host.hostname = ${ NAME } ;
mount.devfs;
mount.fstab = ${ bastille_jail_fstab } ;
path = ${ bastille_jail_path } ;
securelevel = 2;
vnet;
vnet.interface = e0b_${ uniq_epair } ;
exec.prestart += " jib addm ${ uniq_epair } ${ INTERFACE } " ;
exec.poststop += " jib destroy ${ uniq_epair } " ;
}
EOF
}
2018-11-07 10:36:54 -07:00
create_jail( ) {
bastille_jail_base = " ${ bastille_jailsdir } / ${ NAME } /root/.bastille " ## dir
bastille_jail_template = " ${ bastille_jailsdir } / ${ NAME } /root/.template " ## dir
bastille_jail_path = " ${ bastille_jailsdir } / ${ NAME } /root " ## dir
bastille_jail_fstab = " ${ bastille_jailsdir } / ${ NAME } /fstab " ## file
bastille_jail_conf = " ${ bastille_jailsdir } / ${ NAME } /jail.conf " ## file
bastille_jail_log = " ${ bastille_logsdir } / ${ NAME } _console.log " ## file
2019-05-22 21:50:29 -06:00
bastille_jail_rc_conf = " ${ bastille_jailsdir } / ${ NAME } /root/etc/rc.conf " ## file
2018-11-07 10:36:54 -07:00
bastille_jail_resolv_conf = " ${ bastille_jailsdir } / ${ NAME } /root/etc/resolv.conf " ## file
2019-06-22 09:28:42 -06:00
if [ ! -d " ${ bastille_jailsdir } / ${ NAME } " ] ; then
if [ " ${ bastille_zfs_enable } " = "YES" ] ; then
2020-02-20 18:06:31 -04:00
if [ -n " ${ bastille_zfs_zpool } " ] ; then
2020-02-15 07:57:33 -04:00
## create required zfs datasets, mountpoint inherited from system
2020-02-20 18:06:31 -04:00
zfs create ${ bastille_zfs_options } " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /jails/ ${ NAME } "
2019-11-18 03:51:06 -04:00
if [ -z " ${ THICK_JAIL } " ] ; then
2020-02-20 18:06:31 -04:00
zfs create ${ bastille_zfs_options } " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /jails/ ${ NAME } /root "
2019-11-18 03:51:06 -04:00
fi
2019-06-22 09:28:42 -06:00
fi
else
2020-04-18 18:02:11 -04:00
mkdir -p " ${ bastille_jailsdir } / ${ NAME } /root "
2019-06-22 09:28:42 -06:00
fi
fi
2020-04-18 18:02:11 -04:00
if [ -z " ${ EMPTY_JAIL } " ] ; then
if [ ! -d " ${ bastille_jail_base } " ] ; then
mkdir -p " ${ bastille_jail_base } "
2019-11-17 18:15:19 -04:00
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
if [ ! -d " ${ bastille_jail_path } /usr/local " ] ; then
mkdir -p " ${ bastille_jail_path } /usr/local "
2020-02-16 15:20:31 -07:00
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
if [ ! -d " ${ bastille_jail_template } " ] ; then
mkdir -p " ${ bastille_jail_template } "
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
if [ ! -f " ${ bastille_jail_fstab } " ] ; then
if [ -z " ${ THICK_JAIL } " ] ; then
echo -e " ${ bastille_releasesdir } / ${ RELEASE } ${ bastille_jail_base } nullfs ro 0 0 " > " ${ bastille_jail_fstab } "
else
touch " ${ bastille_jail_fstab } "
fi
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
if [ ! -f " ${ bastille_jail_conf } " ] ; then
if [ -z " ${ bastille_network_loopback } " ] && [ -n " ${ bastille_network_shared } " ] ; then
local bastille_jail_conf_interface = ${ bastille_network_shared }
fi
if [ -n " ${ bastille_network_loopback } " ] && [ -z " ${ bastille_network_shared } " ] ; then
local bastille_jail_conf_interface = ${ bastille_network_loopback }
fi
if [ -n " ${ INTERFACE } " ] ; then
local bastille_jail_conf_interface = ${ INTERFACE }
2019-11-17 18:15:19 -04:00
fi
2019-11-18 13:24:18 -04:00
2020-04-18 18:02:11 -04:00
## generate the jail configuration file
if [ -n " ${ VNET_JAIL } " ] ; then
generate_vnet_jail_conf
else
generate_jail_conf
fi
fi
2019-11-18 13:24:18 -04:00
2020-04-18 18:02:11 -04:00
## using relative paths here
## MAKE SURE WE'RE IN THE RIGHT PLACE
cd " ${ bastille_jail_path } "
echo
echo -e " ${ COLOR_GREEN } NAME: ${ NAME } . ${ COLOR_RESET } "
echo -e " ${ COLOR_GREEN } IP: ${ IP } . ${ COLOR_RESET } "
if [ -n " ${ INTERFACE } " ] ; then
echo -e " ${ COLOR_GREEN } INTERFACE: ${ INTERFACE } . ${ COLOR_RESET } "
fi
echo -e " ${ COLOR_GREEN } RELEASE: ${ RELEASE } . ${ COLOR_RESET } "
echo
2019-11-18 03:51:06 -04:00
2020-04-18 18:02:11 -04:00
if [ -z " ${ THICK_JAIL } " ] ; then
2020-05-07 22:50:43 -04:00
LINK_LIST = "bin boot lib libexec rescue sbin usr/bin usr/include usr/lib usr/lib32 usr/libdata usr/libexec usr/sbin usr/share usr/src"
for _link in ${ LINK_LIST } ; do
2020-04-18 18:02:11 -04:00
ln -sf /.bastille/${ _link } ${ _link }
done
fi
2019-11-18 03:51:06 -04:00
2020-04-18 18:02:11 -04:00
if [ -z " ${ THICK_JAIL } " ] ; then
## rw
## copy only required files for thin jails
FILE_LIST = ".cshrc .profile COPYRIGHT dev etc media mnt net proc root tmp var usr/obj usr/tests"
for files in ${ FILE_LIST } ; do
if [ -f " ${ bastille_releasesdir } / ${ RELEASE } / ${ files } " ] || [ -d " ${ bastille_releasesdir } / ${ RELEASE } / ${ files } " ] ; then
cp -a " ${ bastille_releasesdir } / ${ RELEASE } / ${ files } " " ${ bastille_jail_path } / ${ files } "
if [ " $? " -ne 0 ] ; then
## notify and clean stale files/directories
bastille destroy " ${ NAME } "
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Failed to copy release files, please retry create! ${ COLOR_RESET } "
2020-04-18 18:02:11 -04:00
fi
fi
done
else
echo -e " ${ COLOR_GREEN } Creating a thickjail, this may take a while... ${ COLOR_RESET } "
if [ " ${ bastille_zfs_enable } " = "YES" ] ; then
if [ -n " ${ bastille_zfs_zpool } " ] ; then
## perform release base replication
## sane bastille zfs options
ZFS_OPTIONS = $( echo ${ bastille_zfs_options } | sed 's/-o//g' )
## take a temp snapshot of the base release
SNAP_NAME = " bastille- $( date +%Y-%m-%d-%H%M%S) "
zfs snapshot " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /releases/ ${ RELEASE } " @" ${ SNAP_NAME } "
## replicate the release base to the new thickjail and set the default mountpoint
zfs send -R " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /releases/ ${ RELEASE } " @" ${ SNAP_NAME } " | \
zfs receive " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /jails/ ${ NAME } /root "
zfs set ${ ZFS_OPTIONS } mountpoint = none " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /jails/ ${ NAME } /root "
zfs inherit mountpoint " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /jails/ ${ NAME } /root "
## cleanup temp snapshots initially
zfs destroy " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /releases/ ${ RELEASE } " @" ${ SNAP_NAME } "
zfs destroy " ${ bastille_zfs_zpool } / ${ bastille_zfs_prefix } /jails/ ${ NAME } /root " @" ${ SNAP_NAME } "
if [ " $? " -ne 0 ] ; then
## notify and clean stale files/directories
bastille destroy " ${ NAME } "
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Failed release base replication, please retry create! ${ COLOR_RESET } "
2020-04-18 18:02:11 -04:00
fi
fi
else
## copy all files for thick jails
cp -a " ${ bastille_releasesdir } / ${ RELEASE } / " " ${ bastille_jail_path } "
2020-02-20 18:06:31 -04:00
if [ " $? " -ne 0 ] ; then
2019-11-18 03:51:06 -04:00
## notify and clean stale files/directories
2020-02-20 18:06:31 -04:00
bastille destroy " ${ NAME } "
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Failed to copy release files, please retry create! ${ COLOR_RESET } "
2019-11-18 03:51:06 -04:00
fi
fi
2019-11-17 18:15:19 -04:00
fi
2018-11-07 10:36:54 -07:00
2020-05-07 22:50:43 -04:00
## create home directory if missing
if [ ! -d " ${ bastille_jail_path } /usr/home " ] ; then
mkdir -p " ${ bastille_jail_path } /usr/home "
fi
## link home properly
if [ ! -L "home" ] ; then
ln -s usr/home home
fi
2020-04-18 18:02:11 -04:00
## rc.conf
2020-04-25 08:26:12 -04:00
## + syslogd_flags="-ss"
## + sendmail_enable="NO"
## + sendmail_submit_enable="NO"
## + sendmail_outbound_enable="NO"
## + sendmail_msp_queue_enable="NO"
## + cron_flags="-J 60" ## cedwards 20181118
2020-04-18 18:02:11 -04:00
if [ ! -f " ${ bastille_jail_rc_conf } " ] ; then
touch " ${ bastille_jail_rc_conf } "
2020-04-25 08:26:12 -04:00
sysrc -f " ${ bastille_jail_rc_conf } " syslogd_flags = "-ss"
sysrc -f " ${ bastille_jail_rc_conf } " sendmail_enable = "NO"
sysrc -f " ${ bastille_jail_rc_conf } " sendmail_submit_enable = "NO"
sysrc -f " ${ bastille_jail_rc_conf } " sendmail_outbound_enable = "NO"
sysrc -f " ${ bastille_jail_rc_conf } " sendmail_msp_queue_enable = "NO"
sysrc -f " ${ bastille_jail_rc_conf } " cron_flags = "-J 60"
2020-04-18 18:02:11 -04:00
## VNET specific
if [ -n " ${ VNET_JAIL } " ] ; then
## rename interface to generic vnet0
uniq_epair = $( grep vnet.interface " ${ bastille_jailsdir } / ${ NAME } /jail.conf " | awk '{print $3}' | sed 's/;//' )
/usr/sbin/sysrc -f " ${ bastille_jail_rc_conf } " " ifconfig_ ${ uniq_epair } _name " = vnet0
## if 0.0.0.0 set DHCP
## else set static address
if [ " ${ IP } " = = "0.0.0.0" ] ; then
/usr/sbin/sysrc -f " ${ bastille_jail_rc_conf } " ifconfig_vnet0 = "SYNCDHCP"
2020-04-12 17:04:37 -06:00
else
2020-04-18 18:02:11 -04:00
/usr/sbin/sysrc -f " ${ bastille_jail_rc_conf } " ifconfig_vnet0 = " inet ${ IP } "
if [ -n " ${ bastille_network_gateway } " ] ; then
/usr/sbin/sysrc -f " ${ bastille_jail_rc_conf } " defaultrouter = " ${ bastille_network_gateway } "
else
/usr/sbin/sysrc -f " ${ bastille_jail_rc_conf } " defaultrouter = " $( netstat -rn | awk '/default/ {print $2}' ) "
fi
2020-04-12 17:04:37 -06:00
fi
2020-02-16 15:20:31 -07:00
2020-04-18 18:02:11 -04:00
## VNET requires jib script
if [ ! " $( command -v jib) " ] ; then
if [ -f /usr/share/examples/jails/jib ] && [ ! -f /usr/local/bin/jib ] ; then
install -m 0544 /usr/share/examples/jails/jib /usr/local/bin/jib
fi
2020-02-18 17:04:06 -04:00
fi
fi
2020-02-16 15:20:31 -07:00
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
## resolv.conf (default: copy from host)
if [ ! -f " ${ bastille_jail_resolv_conf } " ] ; then
cp -L " ${ bastille_resolv_conf } " " ${ bastille_jail_resolv_conf } "
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
## TZ: configurable (default: Etc/UTC)
ln -s " /usr/share/zoneinfo/ ${ bastille_tzdata } " etc/localtime
else
## Generate minimal configuration for empty jail
generate_minimal_conf
fi
2018-11-07 10:36:54 -07:00
}
# Handle special-case commands first.
case " $1 " in
help| -h| --help)
usage
; ;
esac
2020-02-20 18:06:31 -04:00
if echo " $3 " | grep '@' ; then
BASTILLE_JAIL_IP = $( echo " $3 " | awk -F@ '{print $2}' )
BASTILLE_JAIL_INTERFACES = $( echo " $3 " | awk -F@ '{print $1}' )
2019-07-15 07:44:45 -06:00
fi
2020-02-18 17:04:06 -04:00
## reset this options
2020-04-18 18:02:11 -04:00
EMPTY_JAIL = ""
2020-02-18 17:04:06 -04:00
THICK_JAIL = ""
VNET_JAIL = ""
2020-02-19 19:53:25 -04:00
## handle combined options then shift
2020-02-18 17:04:06 -04:00
if [ " ${ 1 } " = "-T" -o " ${ 1 } " = "--thick" -o " ${ 1 } " = "thick" ] && \
[ " ${ 2 } " = "-V" -o " ${ 2 } " = "--vnet" -o " ${ 2 } " = "vnet" ] ; then
2020-02-16 15:20:31 -07:00
THICK_JAIL = "1"
VNET_JAIL = "1"
2020-02-19 19:53:25 -04:00
shift 2
2020-02-18 17:04:06 -04:00
else
## handle single options
case " ${ 1 } " in
2020-04-18 18:02:11 -04:00
-E| --empty| empty)
shift
EMPTY_JAIL = "1"
; ;
2020-02-19 19:53:25 -04:00
-T| --thick| thick)
2020-02-20 10:41:41 -04:00
shift
2020-02-19 19:53:25 -04:00
THICK_JAIL = "1"
; ;
-V| --vnet| vnet)
2020-02-20 10:41:41 -04:00
shift
2020-02-19 19:53:25 -04:00
VNET_JAIL = "1"
; ;
-*)
echo -e " ${ COLOR_RED } Unknown Option. ${ COLOR_RESET } "
2020-02-18 17:04:06 -04:00
usage
2020-02-19 19:53:25 -04:00
; ;
2020-02-18 17:04:06 -04:00
esac
fi
2018-11-07 10:36:54 -07:00
2020-02-19 19:53:25 -04:00
NAME = " $1 "
RELEASE = " $2 "
IP = " $3 "
INTERFACE = " $4 "
2020-04-18 18:02:11 -04:00
if [ -n " ${ EMPTY_JAIL } " ] ; then
if [ $# -ne 1 ] ; then
usage
fi
else
if [ $# -gt 4 ] || [ $# -lt 3 ] ; then
usage
fi
2020-02-19 19:53:25 -04:00
fi
2020-05-09 15:31:15 -04:00
## validate jail name
if [ -n " ${ NAME } " ] ; then
validate_name
2019-11-19 13:10:24 -04:00
fi
2020-04-18 18:02:11 -04:00
if [ -z " ${ EMPTY_JAIL } " ] ; then
## verify release
case " ${ RELEASE } " in
2020-07-24 10:47:28 -04:00
*-RELEASE| *-RELEASE-I386| *-RELEASE-i386| *-release| *-RC1| *-rc1| *-RC2| *-rc2)
2020-04-18 18:02:11 -04:00
## check for FreeBSD releases name
2020-07-24 10:47:28 -04:00
NAME_VERIFY = $( echo " ${ RELEASE } " | grep -iwE '^([1-9]{2,2})\.[0-9](-RELEASE|-RELEASE-i386|-RC[1-2])$' | tr '[:lower:]' '[:upper:]' | sed 's/I/i/g' )
2020-04-18 18:02:11 -04:00
validate_release
; ;
*-stable-LAST| *-STABLE-last| *-stable-last| *-STABLE-LAST)
## check for HardenedBSD releases name(previous infrastructure)
NAME_VERIFY = $( echo " ${ RELEASE } " | grep -iwE '^([1-9]{2,2})(-stable-last)$' | sed 's/STABLE/stable/g' | sed 's/last/LAST/g' )
validate_release
; ;
*-stable-build-[ 0-9] *| *-STABLE-BUILD-[ 0-9] *)
## check for HardenedBSD(specific stable build releases)
NAME_VERIFY = $( echo " ${ RELEASE } " | grep -iwE '([0-9]{1,2})(-stable-build)-([0-9]{1,3})$' | sed 's/BUILD/build/g' | sed 's/STABLE/stable/g' )
validate_release
; ;
*-stable-build-latest| *-stable-BUILD-LATEST| *-STABLE-BUILD-LATEST)
## check for HardenedBSD(latest stable build release)
NAME_VERIFY = $( echo " ${ RELEASE } " | grep -iwE '([0-9]{1,2})(-stable-build-latest)$' | sed 's/STABLE/stable/g' | sed 's/build/BUILD/g' | sed 's/latest/LATEST/g' )
validate_release
; ;
current-build-[ 0-9] *| CURRENT-BUILD-[ 0-9] *)
## check for HardenedBSD(specific current build releases)
NAME_VERIFY = $( echo " ${ RELEASE } " | grep -iwE '(current-build)-([0-9]{1,3})' | sed 's/BUILD/build/g' | sed 's/CURRENT/current/g' )
validate_release
; ;
current-build-latest| current-BUILD-LATEST| CURRENT-BUILD-LATEST)
## check for HardenedBSD(latest current build release)
NAME_VERIFY = $( echo " ${ RELEASE } " | grep -iwE '(current-build-latest)' | sed 's/CURRENT/current/g' | sed 's/build/BUILD/g' | sed 's/latest/LATEST/g' )
validate_release
; ;
*)
echo -e " ${ COLOR_RED } Unknown Release. ${ COLOR_RESET } "
usage
; ;
esac
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
## check for name/root/.bastille
if [ -d " ${ bastille_jailsdir } / ${ NAME } /root/.bastille " ] ; then
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Jail: ${ NAME } already created. ${ NAME } /root/.bastille exists. ${ COLOR_RESET } "
2020-04-18 18:02:11 -04:00
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
## check for required release
if [ ! -d " ${ bastille_releasesdir } / ${ RELEASE } " ] ; then
2020-05-09 15:31:15 -04:00
error_notify " ${ COLOR_RED } Release must be bootstrapped first; see 'bastille bootstrap'. ${ COLOR_RESET } "
2020-04-18 18:02:11 -04:00
fi
2019-06-22 09:28:42 -06:00
2020-04-18 18:02:11 -04:00
## check if ip address is valid
if [ -n " ${ IP } " ] ; then
validate_ip
else
usage
fi
2018-11-07 10:36:54 -07:00
2020-04-18 18:02:11 -04:00
## check if interface is valid
if [ -n " ${ INTERFACE } " ] ; then
validate_netif
validate_netconf
else
validate_netconf
fi
2019-10-24 17:02:50 -04:00
else
2020-04-18 18:02:11 -04:00
echo -e " ${ COLOR_GREEN } Creating empty jail: ${ NAME } . ${ COLOR_RESET } "
2019-10-24 17:02:50 -04:00
fi
2020-04-18 18:02:11 -04:00
## check if a running jail matches name or already exist
if [ -n " ${ NAME } " ] ; then
running_jail
2018-11-07 10:36:54 -07:00
fi
2020-02-20 18:06:31 -04:00
create_jail " ${ NAME } " " ${ RELEASE } " " ${ IP } " " ${ INTERFACE } "