2018-11-07 10:36:54 -07:00
|
|
|
#!/bin/sh
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2023-07-14 21:02:14 -06:00
|
|
|
# Copyright (c) 2018-2023, 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.
|
|
|
|
|
|
2020-08-30 10:57:14 -04:00
|
|
|
. /usr/local/share/bastille/common.sh
|
2018-11-07 10:36:54 -07:00
|
|
|
. /usr/local/etc/bastille/bastille.conf
|
|
|
|
|
|
|
|
|
|
usage() {
|
2021-03-03 00:30:17 +01:00
|
|
|
error_exit "Usage: bastille destroy [force] | [container|release]"
|
2018-11-07 10:36:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy_jail() {
|
2020-05-25 19:35:38 -04:00
|
|
|
local OPTIONS
|
2020-02-02 02:42:22 -04:00
|
|
|
bastille_jail_base="${bastille_jailsdir}/${TARGET}" ## dir
|
|
|
|
|
bastille_jail_log="${bastille_logsdir}/${TARGET}_console.log" ## file
|
2018-11-07 10:36:54 -07:00
|
|
|
|
2021-12-17 19:09:49 -07:00
|
|
|
if [ "$(/usr/sbin/jls name | awk "/^${TARGET}$/")" ]; then
|
2020-02-02 19:55:18 -04:00
|
|
|
if [ "${FORCE}" = "1" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
bastille stop "${TARGET}"
|
2020-02-02 06:50:58 -04:00
|
|
|
else
|
2020-08-30 10:57:14 -04:00
|
|
|
error_notify "Jail running."
|
|
|
|
|
error_exit "See 'bastille stop ${TARGET}'."
|
2020-02-02 06:50:58 -04:00
|
|
|
fi
|
2018-11-07 10:36:54 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ ! -d "${bastille_jail_base}" ]; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "Jail not found."
|
2018-11-07 10:36:54 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -d "${bastille_jail_base}" ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "Deleting Jail: ${TARGET}."
|
2019-06-22 09:28:42 -06:00
|
|
|
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
if [ -n "${bastille_zfs_zpool}" ]; then
|
|
|
|
|
if [ -n "${TARGET}" ]; then
|
2020-05-25 19:35:38 -04:00
|
|
|
OPTIONS="-r"
|
2020-05-22 21:46:03 -04:00
|
|
|
if [ "${FORCE}" = "1" ]; then
|
2020-05-25 19:35:38 -04:00
|
|
|
OPTIONS="-rf"
|
2020-05-22 21:46:03 -04:00
|
|
|
fi
|
2019-11-18 03:51:06 -04:00
|
|
|
## remove jail zfs dataset recursively
|
2020-05-22 21:46:03 -04:00
|
|
|
zfs destroy "${OPTIONS}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}"
|
2019-11-18 03:51:06 -04:00
|
|
|
fi
|
2019-06-22 09:28:42 -06:00
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2019-10-24 17:02:50 -04:00
|
|
|
if [ -d "${bastille_jail_base}" ]; then
|
|
|
|
|
## removing all flags
|
2020-02-20 18:06:31 -04:00
|
|
|
chflags -R noschg "${bastille_jail_base}"
|
2019-06-22 09:28:42 -06:00
|
|
|
|
2019-10-24 17:02:50 -04:00
|
|
|
## remove jail base
|
2020-02-20 18:06:31 -04:00
|
|
|
rm -rf "${bastille_jail_base}"
|
2019-10-24 17:02:50 -04:00
|
|
|
fi
|
2019-06-22 09:28:42 -06:00
|
|
|
|
2021-01-10 18:55:02 -04:00
|
|
|
# Remove target from bastille_list if exist
|
|
|
|
|
# Mute sysrc output here as it may be undesirable on large startup list
|
|
|
|
|
if [ -n "$(sysrc -qn bastille_list | tr -s " " "\n" | awk "/^${TARGET}$/")" ]; then
|
|
|
|
|
sysrc bastille_list-="${TARGET}" > /dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
2019-06-22 09:28:42 -06:00
|
|
|
## archive jail log
|
|
|
|
|
if [ -f "${bastille_jail_log}" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
mv "${bastille_jail_log}" "${bastille_jail_log}"-"$(date +%F)"
|
2020-11-25 21:19:08 -05:00
|
|
|
info "Note: jail console logs archived."
|
|
|
|
|
info "${bastille_jail_log}-$(date +%F)"
|
2019-06-22 09:28:42 -06:00
|
|
|
fi
|
2020-03-30 22:33:32 +02:00
|
|
|
|
|
|
|
|
## clear any active rdr rules
|
|
|
|
|
if [ ! -z "$(pfctl -a "rdr/${TARGET}" -Psn 2>/dev/null)" ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "Clearing RDR rules:"
|
2020-03-30 22:33:32 +02:00
|
|
|
pfctl -a "rdr/${TARGET}" -Fn
|
|
|
|
|
fi
|
2018-11-07 10:36:54 -07:00
|
|
|
echo
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 17:02:50 -04:00
|
|
|
destroy_rel() {
|
2020-07-09 22:26:06 -04:00
|
|
|
local OPTIONS
|
|
|
|
|
|
2020-02-02 02:42:22 -04:00
|
|
|
## check release name match before destroy
|
|
|
|
|
if [ -n "${NAME_VERIFY}" ]; then
|
|
|
|
|
TARGET="${NAME_VERIFY}"
|
|
|
|
|
else
|
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
bastille_rel_base="${bastille_releasesdir}/${TARGET}" ## dir
|
2019-11-03 00:15:02 -04:00
|
|
|
|
2019-11-02 16:05:49 -04:00
|
|
|
## check if this release have containers child
|
2019-11-03 00:15:02 -04:00
|
|
|
BASE_HASCHILD="0"
|
2019-12-08 17:46:15 -04:00
|
|
|
if [ -d "${bastille_jailsdir}" ]; then
|
2019-11-03 00:15:02 -04:00
|
|
|
JAIL_LIST=$(ls "${bastille_jailsdir}" | sed "s/\n//g")
|
|
|
|
|
for _jail in ${JAIL_LIST}; do
|
2020-02-20 18:06:31 -04:00
|
|
|
if grep -qwo "${TARGET}" "${bastille_jailsdir}/${_jail}/fstab" 2>/dev/null; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_notify "Notice: (${_jail}) depends on ${TARGET} base."
|
2019-11-03 00:15:02 -04:00
|
|
|
BASE_HASCHILD="1"
|
2022-01-15 11:32:28 -04:00
|
|
|
elif [ "${bastille_zfs_enable}" = "YES" ]; then
|
|
|
|
|
if [ -n "${bastille_zfs_zpool}" ]; then
|
|
|
|
|
## check if this release have child clones
|
|
|
|
|
if zfs list -H -t snapshot -r "${bastille_rel_base}" > /dev/null 2>&1; then
|
|
|
|
|
SNAP_CLONE=$(zfs list -H -t snapshot -r "${bastille_rel_base}" 2> /dev/null | awk '{print $1}')
|
|
|
|
|
for _snap_clone in ${SNAP_CLONE}; do
|
|
|
|
|
if zfs list -H -o clones "${_snap_clone}" > /dev/null 2>&1; then
|
|
|
|
|
CLONE_JAIL=$(zfs list -H -o clones "${_snap_clone}" | tr ',' '\n')
|
|
|
|
|
CLONE_CHECK="${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${_jail}/root"
|
|
|
|
|
if echo "${CLONE_JAIL}" | grep -qw "${CLONE_CHECK}"; then
|
|
|
|
|
error_notify "Notice: (${_jail}) depends on ${TARGET} base."
|
|
|
|
|
BASE_HASCHILD="1"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2019-11-03 00:15:02 -04:00
|
|
|
fi
|
|
|
|
|
done
|
2019-11-02 16:05:49 -04:00
|
|
|
fi
|
2019-10-24 17:02:50 -04:00
|
|
|
|
|
|
|
|
if [ ! -d "${bastille_rel_base}" ]; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "Release base not found."
|
2019-11-03 00:15:02 -04:00
|
|
|
else
|
|
|
|
|
if [ "${BASE_HASCHILD}" -eq "0" ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "Deleting base: ${TARGET}"
|
2019-11-03 00:15:02 -04:00
|
|
|
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
if [ -n "${bastille_zfs_zpool}" ]; then
|
2020-07-09 22:26:06 -04:00
|
|
|
if [ -n "${TARGET}" ]; then
|
|
|
|
|
OPTIONS="-r"
|
|
|
|
|
if [ "${FORCE}" = "1" ]; then
|
|
|
|
|
OPTIONS="-rf"
|
|
|
|
|
fi
|
|
|
|
|
zfs destroy "${OPTIONS}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/releases/${TARGET}"
|
|
|
|
|
if [ "${FORCE}" = "1" ]; then
|
|
|
|
|
if [ -d "${bastille_cachedir}/${TARGET}" ]; then
|
|
|
|
|
zfs destroy "${OPTIONS}" "${bastille_zfs_zpool}/${bastille_zfs_prefix}/cache/${TARGET}"
|
|
|
|
|
fi
|
2020-02-02 19:55:18 -04:00
|
|
|
fi
|
|
|
|
|
fi
|
2019-11-03 00:15:02 -04:00
|
|
|
fi
|
2019-10-24 17:02:50 -04:00
|
|
|
fi
|
|
|
|
|
|
2019-11-03 00:15:02 -04:00
|
|
|
if [ -d "${bastille_rel_base}" ]; then
|
|
|
|
|
## removing all flags
|
2020-02-20 18:06:31 -04:00
|
|
|
chflags -R noschg "${bastille_rel_base}"
|
2019-10-24 17:02:50 -04:00
|
|
|
|
2019-11-03 00:15:02 -04:00
|
|
|
## remove jail base
|
2020-02-20 18:06:31 -04:00
|
|
|
rm -rf "${bastille_rel_base}"
|
2019-11-03 00:15:02 -04:00
|
|
|
fi
|
2020-02-02 19:55:18 -04:00
|
|
|
|
|
|
|
|
if [ "${FORCE}" = "1" ]; then
|
|
|
|
|
## remove cache on force
|
|
|
|
|
if [ -d "${bastille_cachedir}/${TARGET}" ]; then
|
|
|
|
|
rm -rf "${bastille_cachedir}/${TARGET}"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2019-11-03 00:15:02 -04:00
|
|
|
echo
|
|
|
|
|
else
|
2020-08-30 10:57:14 -04:00
|
|
|
error_notify "Cannot destroy base with child containers."
|
2019-10-24 17:02:50 -04:00
|
|
|
fi
|
|
|
|
|
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 10:41:41 -04:00
|
|
|
## reset this options
|
|
|
|
|
FORCE=""
|
2018-11-07 10:36:54 -07:00
|
|
|
|
2020-02-02 06:50:58 -04:00
|
|
|
## handle additional options
|
2020-02-20 10:41:41 -04:00
|
|
|
case "${1}" in
|
|
|
|
|
-f|--force|force)
|
|
|
|
|
FORCE="1"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-*)
|
2020-08-30 10:57:14 -04:00
|
|
|
error_notify "Unknown Option."
|
2020-02-02 06:50:58 -04:00
|
|
|
usage
|
2020-02-20 10:41:41 -04:00
|
|
|
;;
|
2020-02-02 06:50:58 -04:00
|
|
|
esac
|
2018-11-07 10:36:54 -07:00
|
|
|
|
2020-02-20 10:41:41 -04:00
|
|
|
TARGET="${1}"
|
|
|
|
|
|
|
|
|
|
if [ $# -gt 1 ] || [ $# -lt 1 ]; then
|
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
2023-03-16 20:58:11 +01:00
|
|
|
bastille_root_check
|
|
|
|
|
|
2019-10-24 17:02:50 -04:00
|
|
|
## check what should we clean
|
2020-02-02 02:42:22 -04:00
|
|
|
case "${TARGET}" in
|
2020-08-28 14:13:54 -06:00
|
|
|
*-CURRENT|*-CURRENT-I386|*-CURRENT-i386|*-current)
|
|
|
|
|
## check for FreeBSD releases name
|
|
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '^([1-9]{2,2})\.[0-9](-CURRENT|-CURRENT-i386)$' | tr '[:lower:]' '[:upper:]' | sed 's/I/i/g')
|
|
|
|
|
destroy_rel
|
|
|
|
|
;;
|
2023-11-10 08:09:51 -10:00
|
|
|
*-RELEASE|*-RELEASE-I386|*-RELEASE-i386|*-release|*-RC[1-9]|*-rc[1-9]|*-BETA[1-9])
|
2019-11-22 22:02:34 -07:00
|
|
|
## check for FreeBSD releases name
|
2023-11-10 08:09:51 -10:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '^([1-9]{2,2})\.[0-9](-RELEASE|-RELEASE-i386|-RC[1-9]|-BETA[1-9])$' | tr '[:lower:]' '[:upper:]' | sed 's/I/i/g')
|
2020-02-02 02:42:22 -04:00
|
|
|
destroy_rel
|
2019-11-08 02:37:45 -04:00
|
|
|
;;
|
|
|
|
|
*-stable-LAST|*-STABLE-last|*-stable-last|*-STABLE-LAST)
|
2019-11-22 22:02:34 -07:00
|
|
|
## check for HardenedBSD releases name
|
2021-08-07 13:17:32 -04:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '^([1-9]{2,2})(-stable-last)$' | sed 's/STABLE/stable/g;s/last/LAST/g')
|
2020-02-02 02:42:22 -04:00
|
|
|
destroy_rel
|
2019-11-08 02:37:45 -04:00
|
|
|
;;
|
2020-02-02 02:42:22 -04:00
|
|
|
*-stable-build-[0-9]*|*-STABLE-BUILD-[0-9]*)
|
|
|
|
|
## check for HardenedBSD(specific stable build releases)
|
2021-08-07 13:17:32 -04:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '([0-9]{1,2})(-stable-build)-([0-9]{1,3})$' | sed 's/BUILD/build/g;s/STABLE/stable/g')
|
2020-02-02 02:42:22 -04:00
|
|
|
destroy_rel
|
|
|
|
|
;;
|
2020-02-10 15:39:14 -04:00
|
|
|
*-stable-build-latest|*-stable-BUILD-LATEST|*-STABLE-BUILD-LATEST)
|
2020-02-02 02:42:22 -04:00
|
|
|
## check for HardenedBSD(latest stable build release)
|
2021-08-07 13:17:32 -04:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '([0-9]{1,2})(-stable-build-latest)$' | sed 's/STABLE/stable/;s/build/BUILD/g;s/latest/LATEST/g')
|
2020-02-02 02:42:22 -04:00
|
|
|
destroy_rel
|
|
|
|
|
;;
|
2020-02-02 04:08:24 -04:00
|
|
|
current-build-[0-9]*|CURRENT-BUILD-[0-9]*)
|
2020-02-02 02:42:22 -04:00
|
|
|
## check for HardenedBSD(specific current build releases)
|
2021-08-07 13:17:32 -04:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '(current-build)-([0-9]{1,3})' | sed 's/BUILD/build/g;s/CURRENT/current/g')
|
2020-02-02 02:42:22 -04:00
|
|
|
destroy_rel
|
|
|
|
|
;;
|
2020-02-10 15:39:14 -04:00
|
|
|
current-build-latest|current-BUILD-LATEST|CURRENT-BUILD-LATEST)
|
2020-02-02 02:42:22 -04:00
|
|
|
## check for HardenedBSD(latest current build release)
|
2021-08-07 13:17:32 -04:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '(current-build-latest)$' | sed 's/CURRENT/current/;s/build/BUILD/g;s/latest/LATEST/g')
|
2021-08-06 19:44:25 -04:00
|
|
|
destroy_rel
|
|
|
|
|
;;
|
2023-07-10 12:19:30 +02:00
|
|
|
Ubuntu_1804|Ubuntu_2004|Ubuntu_2204|UBUNTU_1804|UBUNTU_2004|UBUNTU_2204)
|
2021-12-22 18:35:49 -07:00
|
|
|
## check for Linux releases
|
2023-07-10 12:19:30 +02:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '(Ubuntu_1804)$|(Ubuntu_2004)$|(Ubuntu_2204)$' | sed 's/UBUNTU/Ubuntu/g;s/ubuntu/Ubuntu/g')
|
2021-08-07 13:17:32 -04:00
|
|
|
destroy_rel
|
|
|
|
|
;;
|
2023-07-10 12:19:30 +02:00
|
|
|
Debian10|Debian11|Debian12|DEBIAN10|DEBIAN11|DEBIAN12)
|
2021-12-22 18:35:49 -07:00
|
|
|
## check for Linux releases
|
2023-07-10 12:19:30 +02:00
|
|
|
NAME_VERIFY=$(echo "${TARGET}" | grep -iwE '(Debian10)$|(Debian11)$|(Debian12)$' | sed 's/DEBIAN/Debian/g')
|
2020-02-02 02:42:22 -04:00
|
|
|
destroy_rel
|
2019-12-02 03:10:45 -04:00
|
|
|
;;
|
2019-11-08 02:37:45 -04:00
|
|
|
*)
|
|
|
|
|
## just destroy a jail
|
|
|
|
|
destroy_jail
|
|
|
|
|
;;
|
|
|
|
|
esac
|