2018-11-13 10:38:33 -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-13 10:38:33 -07:00
|
|
|
# All rights reserved.
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2018-11-13 10:38:33 -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-13 10:38:33 -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-13 10:38:33 -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-13 10:38:33 -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-13 10:38:33 -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
|
|
|
|
|
|
2019-12-07 17:48:39 -07:00
|
|
|
bastille_usage() {
|
2019-11-22 22:05:37 -07:00
|
|
|
echo -e "${COLOR_RED}Usage: bastille template TARGET project/template.${COLOR_RESET}"
|
2018-11-13 10:38:33 -07:00
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Handle special-case commands first.
|
|
|
|
|
case "$1" in
|
|
|
|
|
help|-h|--help)
|
2019-12-07 17:48:39 -07:00
|
|
|
bastille_usage
|
2018-11-13 10:38:33 -07:00
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
if [ $# -gt 2 ] || [ $# -lt 2 ]; then
|
2019-12-07 17:48:39 -07:00
|
|
|
bastille_usage
|
2018-11-13 10:38:33 -07:00
|
|
|
fi
|
|
|
|
|
|
2019-11-21 16:49:00 -07:00
|
|
|
TARGET="${1}"
|
2019-11-22 22:05:37 -07:00
|
|
|
shift
|
2019-11-21 16:49:00 -07:00
|
|
|
|
|
|
|
|
if [ "${TARGET}" = 'ALL' ]; then
|
2018-11-24 09:55:16 -07:00
|
|
|
JAILS=$(jls name)
|
2018-11-13 10:38:33 -07:00
|
|
|
fi
|
2019-11-21 16:49:00 -07:00
|
|
|
if [ "${TARGET}" != 'ALL' ]; then
|
2020-02-05 07:00:11 -04:00
|
|
|
JAILS=$(jls name | awk "/^${TARGET}$/")
|
2019-11-21 16:49:00 -07:00
|
|
|
fi
|
|
|
|
|
|
2019-11-22 22:05:37 -07:00
|
|
|
TEMPLATE="${1}"
|
|
|
|
|
shift
|
2019-11-21 16:49:00 -07:00
|
|
|
|
2020-05-24 20:41:11 -04:00
|
|
|
case ${TEMPLATE} in
|
|
|
|
|
http?://github.com/*/*|http?://gitlab.com/*/*)
|
|
|
|
|
TEMPLATE_DIR=$(echo "${TEMPLATE}" | awk -F / '{ print $4 "/" $5 }')
|
|
|
|
|
if [ ! -d "${bastille_templatesdir}/${TEMPLATE_DIR}" ]; then
|
|
|
|
|
echo -e "${COLOR_GREEN}Bootstrapping ${TEMPLATE}...${COLOR_RESET}"
|
|
|
|
|
if ! bastille bootstrap "${TEMPLATE}"; then
|
|
|
|
|
echo -e "${COLOR_RED}Failed to bootstrap template: ${TEMPLATE}.${COLOR_RESET}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
TEMPLATE="${TEMPLATE_DIR}"
|
|
|
|
|
;;
|
|
|
|
|
*/*)
|
|
|
|
|
if [ ! -d "${bastille_templatesdir}/${TEMPLATE}" ]; then
|
|
|
|
|
echo -e "${COLOR_RED}${TEMPLATE} not found.${COLOR_RESET}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo -e "${COLOR_RED}Template name/URL not recognized.${COLOR_RESET}"
|
|
|
|
|
exit 1
|
|
|
|
|
esac
|
2018-11-13 10:38:33 -07:00
|
|
|
|
2019-12-11 21:17:09 +01:00
|
|
|
if [ -z "${JAILS}" ]; then
|
|
|
|
|
echo -e "${COLOR_RED}Container ${TARGET} is not running.${COLOR_RESET}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2020-05-24 20:41:11 -04:00
|
|
|
if [ -z "${HOOKS}" ]; then
|
|
|
|
|
HOOKS='LIMITS INCLUDE PRE FSTAB PF PKG OVERLAY CONFIG SYSRC SERVICE CMD'
|
|
|
|
|
fi
|
|
|
|
|
|
2018-11-24 09:55:16 -07:00
|
|
|
## global variables
|
2018-11-13 10:38:33 -07:00
|
|
|
bastille_template=${bastille_templatesdir}/${TEMPLATE}
|
|
|
|
|
for _jail in ${JAILS}; do
|
2020-04-14 11:52:29 +02:00
|
|
|
## jail-specific variables.
|
2018-11-24 09:55:16 -07:00
|
|
|
bastille_jail_path=$(jls -j "${_jail}" path)
|
|
|
|
|
|
2018-11-13 10:38:33 -07:00
|
|
|
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
|
2020-05-24 20:41:11 -04:00
|
|
|
echo -e "${COLOR_GREEN}Applying template: ${TEMPLATE}...${COLOR_RESET}"
|
2018-11-13 10:38:33 -07:00
|
|
|
|
2018-12-02 08:23:27 -07:00
|
|
|
## TARGET
|
2019-12-07 17:48:39 -07:00
|
|
|
if [ -s "${bastille_template}/TARGET" ]; then
|
2020-02-20 18:06:31 -04:00
|
|
|
if grep -qw "${_jail}" "${bastille_template}/TARGET"; then
|
2018-12-02 08:23:27 -07:00
|
|
|
echo -e "${COLOR_GREEN}TARGET: !${_jail}.${COLOR_RESET}"
|
2019-12-07 17:48:39 -07:00
|
|
|
echo
|
2018-12-02 08:23:27 -07:00
|
|
|
continue
|
|
|
|
|
fi
|
2020-02-20 18:06:31 -04:00
|
|
|
if ! grep -Eq "(^|\b)(${_jail}|ALL)($|\b)" "${bastille_template}/TARGET"; then
|
2018-12-02 08:23:27 -07:00
|
|
|
echo -e "${COLOR_GREEN}TARGET: ?${_jail}.${COLOR_RESET}"
|
2019-12-07 17:48:39 -07:00
|
|
|
echo
|
2018-12-02 08:23:27 -07:00
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2020-05-24 20:41:11 -04:00
|
|
|
for _hook in ${HOOKS}; do
|
|
|
|
|
if [ -s "${bastille_template}/${_hook}" ]; then
|
|
|
|
|
# Default command is the lowercase hook name and default args are the line from the file. -- cwells
|
|
|
|
|
_cmd=$(echo "${_hook}" | awk '{print tolower($1);}')
|
|
|
|
|
_args_template='${_line}'
|
|
|
|
|
|
|
|
|
|
# Override default command/args for some hooks. -- cwells
|
|
|
|
|
case ${_hook} in
|
|
|
|
|
CONFIG)
|
|
|
|
|
echo -e "${COLOR_YELLOW}CONFIG deprecated; rename to OVERLAY.${COLOR_RESET}"
|
|
|
|
|
_args_template='${bastille_template}/${_line} /'
|
|
|
|
|
_cmd='cp' ;;
|
|
|
|
|
FSTAB)
|
|
|
|
|
_cmd='mount' ;;
|
|
|
|
|
INCLUDE)
|
|
|
|
|
_cmd='template' ;;
|
|
|
|
|
OVERLAY)
|
|
|
|
|
_args_template='${bastille_template}/${_line} /'
|
|
|
|
|
_cmd='cp' ;;
|
|
|
|
|
PF)
|
|
|
|
|
echo -e "${COLOR_GREEN}NOT YET IMPLEMENTED.${COLOR_RESET}"
|
|
|
|
|
continue ;;
|
|
|
|
|
PRE)
|
|
|
|
|
_cmd='cmd' ;;
|
2019-12-07 17:48:39 -07:00
|
|
|
esac
|
2019-07-15 07:44:45 -06:00
|
|
|
|
2020-05-24 20:41:11 -04:00
|
|
|
echo -e "${COLOR_GREEN}[${_jail}]:${_hook} -- START${COLOR_RESET}"
|
|
|
|
|
if [ "${_hook}" = 'CMD' ] || [ "${_hook}" = 'PRE' ]; then
|
|
|
|
|
bastille cmd "${_jail}" /bin/sh < "${bastille_template}/${_hook}" || exit 1
|
|
|
|
|
elif [ "${_hook}" = 'PKG' ]; then
|
|
|
|
|
bastille pkg "${_jail}" install -y $(cat "${bastille_template}/PKG") || exit 1
|
|
|
|
|
bastille pkg "${_jail}" audit -F
|
2020-01-12 07:32:10 -07:00
|
|
|
else
|
2020-05-24 20:41:11 -04:00
|
|
|
while read _line; do
|
|
|
|
|
if [ -z "${_line}" ]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
eval "_args=\"${_args_template}\""
|
|
|
|
|
bastille "${_cmd}" "${_jail}" ${_args} || exit 1
|
|
|
|
|
done < "${bastille_template}/${_hook}"
|
2020-01-12 07:32:10 -07:00
|
|
|
fi
|
2020-05-24 20:41:11 -04:00
|
|
|
echo -e "${COLOR_GREEN}[${_jail}]:${_hook} -- END${COLOR_RESET}"
|
|
|
|
|
echo
|
|
|
|
|
fi
|
|
|
|
|
done
|
2019-11-21 16:49:00 -07:00
|
|
|
|
2020-05-24 20:41:11 -04:00
|
|
|
echo -e "${COLOR_GREEN}Template complete.${COLOR_RESET}"
|
2018-11-13 10:38:33 -07:00
|
|
|
echo
|
|
|
|
|
done
|