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
|
|
|
|
|
|
2019-12-07 17:48:39 -07:00
|
|
|
bastille_usage() {
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "Usage: bastille verify [release|template]"
|
2018-11-07 10:36:54 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-07 17:48:39 -07:00
|
|
|
verify_release() {
|
2021-07-14 13:57:09 -04:00
|
|
|
if [ -f "/bin/midnightbsd-version" ]; then
|
2021-02-26 16:55:56 -05:00
|
|
|
echo -e "${COLOR_RED}Not yet supported on MidnightBSD.${COLOR_RESET}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2020-02-20 18:06:31 -04:00
|
|
|
if freebsd-version | grep -qi HBSD; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "Not yet supported on HardenedBSD."
|
2019-12-07 17:48:39 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -d "${bastille_releasesdir}/${RELEASE}" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
freebsd-update -b "${bastille_releasesdir}/${RELEASE}" --currently-running "${RELEASE}" IDS
|
2019-12-07 17:48:39 -07:00
|
|
|
else
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "${RELEASE} not found. See 'bastille bootstrap'."
|
2019-12-07 17:48:39 -07:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 21:45:15 +01:00
|
|
|
handle_template_include() {
|
|
|
|
|
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}"
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
error_exit "Template INCLUDE content not recognized."
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 17:48:39 -07:00
|
|
|
verify_template() {
|
|
|
|
|
_template_path=${bastille_templatesdir}/${BASTILLE_TEMPLATE}
|
|
|
|
|
_hook_validate=0
|
|
|
|
|
|
2020-08-07 09:24:24 -07:00
|
|
|
for _hook in TARGET INCLUDE PRE OVERLAY FSTAB PF PKG SYSRC SERVICE CMD Bastillefile; do
|
2019-12-07 17:48:39 -07:00
|
|
|
_path=${_template_path}/${_hook}
|
2019-12-11 20:35:05 +01:00
|
|
|
if [ -s "${_path}" ]; then
|
2019-12-07 17:48:39 -07:00
|
|
|
_hook_validate=$((_hook_validate+1))
|
2020-11-25 21:19:08 -05:00
|
|
|
info "Detected ${_hook} hook."
|
2019-12-07 17:48:39 -07:00
|
|
|
|
|
|
|
|
## line count must match newline count
|
2020-02-20 18:06:31 -04:00
|
|
|
if [ $(wc -l "${_path}" | awk '{print $1}') -ne $(grep -c $'\n' "${_path}") ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "[${_hook}]:"
|
2020-08-30 10:57:14 -04:00
|
|
|
error_notify "${BASTILLE_TEMPLATE}:${_hook} [failed]."
|
|
|
|
|
error_notify "Line numbers don't match line breaks."
|
2019-12-07 17:48:39 -07:00
|
|
|
echo
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "Template validation failed."
|
2019-12-07 17:48:39 -07:00
|
|
|
## if INCLUDE; recursive verify
|
2021-07-14 13:57:09 -04:00
|
|
|
elif [ "${_hook}" = 'INCLUDE' ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "[${_hook}]:"
|
2019-12-07 17:48:39 -07:00
|
|
|
cat "${_path}"
|
|
|
|
|
echo
|
|
|
|
|
while read _include; do
|
2020-11-25 21:19:08 -05:00
|
|
|
info "[${_hook}]:[${_include}]:"
|
2021-11-01 21:45:15 +01:00
|
|
|
TEMPLATE_INCLUDE="${_include}"
|
|
|
|
|
handle_template_include
|
2020-02-20 18:06:31 -04:00
|
|
|
done < "${_path}"
|
2019-12-07 17:48:39 -07:00
|
|
|
|
|
|
|
|
## if tree; tree -a bastille_template/_dir
|
2021-07-14 13:57:09 -04:00
|
|
|
elif [ "${_hook}" = 'OVERLAY' ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "[${_hook}]:"
|
2019-12-07 17:48:39 -07:00
|
|
|
cat "${_path}"
|
|
|
|
|
echo
|
|
|
|
|
while read _dir; do
|
2020-11-25 21:19:08 -05:00
|
|
|
info "[${_hook}]:[${_dir}]:"
|
2021-07-14 13:57:09 -04:00
|
|
|
if [ -x "/usr/local/bin/tree" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
/usr/local/bin/tree -a "${_template_path}/${_dir}"
|
2020-01-26 09:51:02 -07:00
|
|
|
else
|
|
|
|
|
find "${_template_path}/${_dir}" -print | sed -e 's;[^/]*/;|___;g;s;___|; |;g'
|
|
|
|
|
fi
|
2019-12-07 17:48:39 -07:00
|
|
|
echo
|
2020-02-20 18:06:31 -04:00
|
|
|
done < "${_path}"
|
2021-11-01 21:45:15 +01:00
|
|
|
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}"
|
|
|
|
|
echo
|
2019-12-07 17:48:39 -07:00
|
|
|
else
|
2020-11-25 21:19:08 -05:00
|
|
|
info "[${_hook}]:"
|
2019-12-07 17:48:39 -07:00
|
|
|
cat "${_path}"
|
|
|
|
|
echo
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
## remove bad templates
|
2021-07-14 13:57:09 -04:00
|
|
|
if [ "${_hook_validate}" -lt 1 ]; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_notify "No valid template hooks found."
|
|
|
|
|
error_notify "Template discarded."
|
2020-02-20 18:06:31 -04:00
|
|
|
rm -rf "${bastille_template}"
|
2019-12-07 17:48:39 -07:00
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
## if validated; ready to use
|
2021-07-14 13:57:09 -04:00
|
|
|
if [ "${_hook_validate}" -gt 0 ]; then
|
2020-11-25 21:19:08 -05:00
|
|
|
info "Template ready to use."
|
2019-12-07 17:48:39 -07:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 10:36:54 -07:00
|
|
|
# Handle special-case commands first.
|
|
|
|
|
case "$1" in
|
|
|
|
|
help|-h|--help)
|
2019-12-07 17:48:39 -07:00
|
|
|
bastille_usage
|
2018-11-07 10:36:54 -07:00
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
if [ $# -gt 1 ] || [ $# -lt 1 ]; then
|
2019-12-07 17:48:39 -07:00
|
|
|
bastille_usage
|
2018-11-07 10:36:54 -07:00
|
|
|
fi
|
|
|
|
|
|
2023-03-16 20:58:11 +01:00
|
|
|
bastille_root_check
|
|
|
|
|
|
2019-12-07 17:48:39 -07:00
|
|
|
case "$1" in
|
2023-11-10 08:09:51 -10:00
|
|
|
*-RELEASE|*-release|*-RC[1-9]|*-rc[1-9])
|
2019-12-07 17:48:39 -07:00
|
|
|
RELEASE=$1
|
|
|
|
|
verify_release
|
|
|
|
|
;;
|
|
|
|
|
*-stable-LAST|*-STABLE-last|*-stable-last|*-STABLE-LAST)
|
|
|
|
|
RELEASE=$1
|
|
|
|
|
verify_release
|
|
|
|
|
;;
|
|
|
|
|
http?*)
|
|
|
|
|
bastille_usage
|
|
|
|
|
;;
|
|
|
|
|
*/*)
|
|
|
|
|
BASTILLE_TEMPLATE=$1
|
|
|
|
|
verify_template
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
bastille_usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|