Merge pull request #287 from chriswells0/default-args

Provide JAIL_NAME and JAIL_IP as default template args.
This commit is contained in:
Christer Edwards
2020-12-12 11:51:05 -07:00
committed by GitHub
2 changed files with 13 additions and 6 deletions

View File

@@ -718,7 +718,8 @@ followed by its arguments (omitting the target, which is deduced from the
Variables can also be defined using `ARG` with one `name=value` pair per Variables can also be defined using `ARG` with one `name=value` pair per
line. Subsequent references to `${name}` would be replaced by `value`. line. Subsequent references to `${name}` would be replaced by `value`.
Note that argument values are not available for use until after the point Note that argument values are not available for use until after the point
at which they are defined in the file. at which they are defined in the file. Both `${JAIL_NAME}` and `${JAIL_IP}`
are made available in templates without having to define them as args.
Bastillefile example: Bastillefile example:

View File

@@ -169,12 +169,17 @@ fi
## global variables ## global variables
bastille_template=${bastille_templatesdir}/${TEMPLATE} bastille_template=${bastille_templatesdir}/${TEMPLATE}
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
## jail-specific variables.
bastille_jail_path=$(jls -j "${_jail}" path)
info "[${_jail}]:" info "[${_jail}]:"
info "Applying template: ${TEMPLATE}..." info "Applying template: ${TEMPLATE}..."
## jail-specific variables.
bastille_jail_path=$(jls -j "${_jail}" path)
_jail_ip=$(jls -j "${_jail}" ip4.addr 2>/dev/null)
if [ -z "${_jail_ip}" -o "${_jail_ip}" = "-" ]; then
error_notify "Jail IP not found: ${_jail}"
_jail_ip='' # In case it was -. -- cwells
fi
## TARGET ## TARGET
if [ -s "${bastille_template}/TARGET" ]; then if [ -s "${bastille_template}/TARGET" ]; then
if grep -qw "${_jail}" "${bastille_template}/TARGET"; then if grep -qw "${_jail}" "${bastille_template}/TARGET"; then
@@ -189,8 +194,10 @@ for _jail in ${JAILS}; do
fi fi
fi fi
# Build a list of sed commands like this: -e 's/${username}/root/g' -e 's/${domain}/example.com/g'
# Values provided by default (without being defined by the user) are listed here. -- cwells
ARG_REPLACEMENTS="-e 's/\${JAIL_IP}/${_jail_ip}/g' -e 's/\${JAIL_NAME}/${_jail}/g'"
# This is parsed outside the HOOKS loop so an ARG file can be used with a Bastillefile. -- cwells # This is parsed outside the HOOKS loop so an ARG file can be used with a Bastillefile. -- cwells
ARG_REPLACEMENTS=''
if [ -s "${bastille_template}/ARG" ]; then if [ -s "${bastille_template}/ARG" ]; then
while read _line; do while read _line; do
if [ -z "${_line}" ]; then if [ -z "${_line}" ]; then
@@ -201,7 +208,6 @@ for _jail in ${JAILS}; do
if [ -z "${_arg_value}" ]; then if [ -z "${_arg_value}" ]; then
warn "No value provided for arg: ${_arg_name}" warn "No value provided for arg: ${_arg_name}"
fi fi
# Build a list of sed commands like this: -e 's/${username}/root/g' -e 's/${domain}/example.com/g'
ARG_REPLACEMENTS="${ARG_REPLACEMENTS} -e 's/\${${_arg_name}}/${_arg_value}/g'" ARG_REPLACEMENTS="${ARG_REPLACEMENTS} -e 's/\${${_arg_name}}/${_arg_value}/g'"
done < "${bastille_template}/ARG" done < "${bastille_template}/ARG"
fi fi