Files
hackacad/usr/local/share/bastille/verify.sh

200 lines
6.4 KiB
Bash
Raw Normal View History

2018-11-07 10:36:54 -07:00
#!/bin/sh
2020-04-14 11:52:29 +02:00
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2018-2025, 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/common.sh
2018-11-07 10:36:54 -07:00
2025-02-23 23:34:57 -07:00
usage() {
2025-05-18 12:00:51 -06:00
error_notify "Usage: bastille verify [option(s)] RELEASE|TEMPLATE"
2025-02-23 23:34:57 -07:00
cat << EOF
2025-04-30 13:39:05 -06:00
2025-02-23 23:34:57 -07:00
Options:
-x | --debug Enable debug mode.
EOF
exit 1
2018-11-07 10:36:54 -07:00
}
verify_release() {
2025-04-30 18:49:14 -06:00
2021-07-14 13:57:09 -04:00
if [ -f "/bin/midnightbsd-version" ]; then
2025-05-01 17:39:50 -06:00
error_exit "[ERROR]: Not yet supported on MidnightBSD."
fi
if freebsd-version | grep -qi HBSD; then
2025-05-01 17:39:50 -06:00
error_exit "[ERROR]: Not yet supported on HardenedBSD."
fi
if [ -d "${bastille_releasesdir}/${RELEASE}" ]; then
freebsd-update -b "${bastille_releasesdir}/${RELEASE}" --currently-running "${RELEASE}" IDS
else
2025-05-01 17:39:50 -06:00
error_exit "[ERROR]: ${RELEASE} not found. See 'bastille bootstrap'."
fi
}
handle_template_include() {
2025-04-30 18:49:14 -06:00
case ${TEMPLATE_INCLUDE} in
http?://*/*/*)
bastille bootstrap "${TEMPLATE_INCLUDE}"
;;
*/*)
BASTILLE_TEMPLATE_USER=$(echo "${TEMPLATE_INCLUDE}" | awk -F / '{ print $1 }')
BASTILLE_TEMPLATE_REPO=$(echo "${TEMPLATE_INCLUDE}" | awk -F / '{ print $2 }')
bastille verify "${BASTILLE_TEMPLATE_USER}/${BASTILLE_TEMPLATE_REPO}"
;;
*)
2025-05-01 17:39:50 -06:00
error_exit "[ERROR]: Template INCLUDE content not recognized."
;;
esac
}
verify_template() {
2025-04-30 18:49:14 -06:00
_template_path=${bastille_templatesdir}/${BASTILLE_TEMPLATE}
_hook_validate=0
for _hook in TARGET INCLUDE PRE OVERLAY FSTAB PF PKG SYSRC SERVICE CMD Bastillefile; do
_path=${_template_path}/${_hook}
if [ -s "${_path}" ]; then
_hook_validate=$((_hook_validate+1))
2025-05-01 17:39:50 -06:00
info "\nDetected ${_hook} hook."
## line count must match newline count
# shellcheck disable=SC2046
# shellcheck disable=SC3003
2025-02-23 23:48:05 -07:00
if [ $(wc -l "${_path}" | awk '{print $1}') -ne "$(tr -d -c '\n' < "${_path}" | wc -c)" ]; then
info "[${_hook}]:"
2025-05-01 17:39:50 -06:00
error_notify "[ERROR]: ${BASTILLE_TEMPLATE}:${_hook} [failed]."
error_notify "Line numbers don't match line breaks."
error_exit "Template validation failed."
## if INCLUDE; recursive verify
2021-07-14 13:57:09 -04:00
elif [ "${_hook}" = 'INCLUDE' ]; then
info "[${_hook}]:"
cat "${_path}"
while read _include; do
info "[${_hook}]:[${_include}]:"
TEMPLATE_INCLUDE="${_include}"
handle_template_include
done < "${_path}"
## if tree; tree -a bastille_template/_dir
2021-07-14 13:57:09 -04:00
elif [ "${_hook}" = 'OVERLAY' ]; then
info "[${_hook}]:"
cat "${_path}"
while read _dir; do
info "[${_hook}]:[${_dir}]:"
2021-07-14 13:57:09 -04:00
if [ -x "/usr/local/bin/tree" ]; then
/usr/local/bin/tree -a "${_template_path}/${_dir}"
else
find "${_template_path}/${_dir}" -print | sed -e 's;[^/]*/;|___;g;s;___|; |;g'
fi
done < "${_path}"
elif [ "${_hook}" = 'Bastillefile' ]; then
info "[${_hook}]:"
cat "${_path}"
while read _line; do
_cmd=$(echo "${_line}" | awk '{print tolower($1);}')
## if include; recursive verify
if [ "${_cmd}" = 'include' ]; then
TEMPLATE_INCLUDE=$(echo "${_line}" | awk '{print $2;}')
handle_template_include
fi
done < "${_path}"
else
info "[${_hook}]:"
cat "${_path}"
fi
fi
done
# Remove bad templates
2021-07-14 13:57:09 -04:00
if [ "${_hook_validate}" -lt 1 ]; then
rm -rf "${_template_path}"
2025-05-01 17:39:50 -06:00
error_notify "[ERROR]: No valid template hooks found."
error_exit "Template discarded."
fi
## if validated; ready to use
2021-07-14 13:57:09 -04:00
if [ "${_hook_validate}" -gt 0 ]; then
2025-05-01 17:39:50 -06:00
info "\nTemplate ready to use."
fi
}
2025-02-23 23:34:57 -07:00
# Handle options.
while [ "$#" -gt 0 ]; do
case "${1}" in
-h|--help|help)
usage
;;
2025-02-23 23:34:57 -07:00
-x|--debug)
enable_debug
shift
;;
-*)
2025-05-01 17:39:50 -06:00
error_exit "[ERROR]: Unknown Option: \"${1}\""
2025-02-23 23:34:57 -07:00
;;
*)
break
;;
esac
done
2018-11-07 10:36:54 -07:00
2025-02-23 23:34:57 -07:00
if [ "$#" -ne 1 ]; then
usage
2018-11-07 10:36:54 -07:00
fi
bastille_root_check
2025-02-23 23:34:57 -07:00
case "${1}" in
*-RELEASE|*-release|*-RC[1-9]|*-rc[1-9])
RELEASE="${1}"
verify_release
;;
*-stable-LAST|*-STABLE-last|*-stable-last|*-STABLE-LAST)
RELEASE="${1}"
verify_release
;;
http?*)
bastille_usage
;;
*/*)
BASTILLE_TEMPLATE="${1}"
verify_template
;;
*)
usage
;;
2025-05-01 17:39:50 -06:00
esac
2025-05-18 12:00:51 -06:00
echo