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

148 lines
4.3 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
usage() {
2025-05-18 17:16:17 -06:00
error_notify "Usage: bastille pkg [option(s)] TARGET ARGS"
cat << EOF
2025-04-30 13:39:05 -06:00
Options:
-a | --auto Auto mode. Start/stop jail(s) if required.
2025-02-27 17:14:13 -07:00
-H | --host Use the hosts 'pkg' instead of the jails.
2025-05-05 13:27:08 -06:00
-y | --yes Assume always yes for pkg command. Do not prompt.
-x | --debug Enable debug mode.
EOF
exit 1
2018-11-07 10:36:54 -07:00
}
# Handle options.
AUTO=0
2025-05-05 13:27:08 -06:00
AUTO_YES=0
USE_HOST_PKG=0
while [ "$#" -gt 0 ]; do
case "${1}" in
-h|--help|help)
usage
;;
-a|--auto)
AUTO=1
shift
;;
-H|--host)
USE_HOST_PKG=1
shift
;;
2025-05-05 13:27:08 -06:00
-y|--yes)
AUTO_YES=1
shift
;;
-x|--debug)
enable_debug
shift
;;
-*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
2025-06-22 12:15:27 -06:00
a) AUTO=1 ;;
H) USE_HOST_PKG=1 ;;
2025-05-05 13:27:08 -06:00
y) AUTO_YES=1 ;;
x) enable_debug ;;
2025-05-01 17:39:50 -06:00
*) error_exit "[ERROR]: Unknown Option: \"${1}\"" ;;
esac
done
shift
;;
*)
break
;;
esac
done
2018-11-07 10:36:54 -07:00
if [ $# -lt 2 ]; then
2018-11-07 10:36:54 -07:00
usage
fi
TARGET="${1}"
shift
# Use mktemp to store exit codes
export TMP_BASTILLE_EXIT_CODE="$(mktemp)"
echo 0 > "${TMP_BASTILLE_EXIT_CODE}"
bastille_root_check
set_target "${TARGET}"
2025-05-05 13:27:08 -06:00
pkg_run_command() {
2025-05-01 17:39:50 -06:00
# Validate jail state
check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then
bastille start "${_jail}"
2025-04-30 18:49:14 -06:00
else
info "\n[${_jail}]:"
error_notify "Jail is not running."
2025-05-01 17:39:50 -06:00
error_continue "Use [-a|--auto] to auto-start the jail."
fi
2025-04-30 18:49:14 -06:00
info "\n[${_jail}]:"
bastille_jail_path="${bastille_jailsdir}/${_jail}/root"
2025-04-30 18:49:14 -06:00
if [ -f "/usr/sbin/mport" ]; then
2025-06-22 12:06:04 -06:00
jexec -l -U root "${_jail}" /usr/sbin/mport "$@"
elif [ -f "${bastille_jail_path}/usr/bin/apt" ]; then
2025-06-22 12:06:04 -06:00
jexec -l "${_jail}" /usr/bin/apt "$@"
2025-05-05 13:27:08 -06:00
elif [ "${USE_HOST_PKG}" -eq 1 ]; then
if [ "${AUTO_YES}" -eq 1 ]; then
2025-06-22 12:15:27 -06:00
env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg -j ${_jail} "$@"
2025-05-05 13:27:08 -06:00
else
2025-06-22 12:15:27 -06:00
/usr/sbin/pkg -j ${_jail} "$@"
fi
else
2025-05-05 13:27:08 -06:00
if [ "${AUTO_YES}" -eq 1 ]; then
2025-06-22 12:15:27 -06:00
jexec -l -U root ${_jail} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg "$@"
2025-05-05 13:27:08 -06:00
else
2025-06-22 12:15:27 -06:00
jexec -l -U root ${_jail} /usr/sbin/pkg "$@"
fi
fi
2025-05-05 13:27:08 -06:00
2025-06-22 12:06:04 -06:00
bastille_check_exit_code "${_jail}" "$?"
}
2025-05-05 13:27:08 -06:00
for _jail in ${JAILS}; do
2025-07-12 21:25:34 -06:00
pkg_run_command "$@"
2018-11-07 10:36:54 -07:00
done
echo
2025-06-22 12:06:04 -06:00
bastille_return_exit_code