2019-12-23 10:54:09 +01:00
|
|
|
#!/bin/sh
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2025-01-11 14:07:41 -05:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2018-2025, Christer Edwards <christer.edwards@gmail.com>
|
2019-12-23 10:54:09 +01:00
|
|
|
# All rights reserved.
|
|
|
|
|
# Ressource limits added by Sven R github.com/hackacad
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2019-12-23 10:54:09 +01: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
|
|
|
#
|
2019-12-23 10:54:09 +01: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
|
|
|
#
|
2019-12-23 10:54:09 +01: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
|
|
|
#
|
2019-12-23 10:54:09 +01: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
|
|
|
#
|
2019-12-23 10:54:09 +01: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
|
2019-12-23 10:54:09 +01:00
|
|
|
|
|
|
|
|
usage() {
|
2025-03-22 23:50:31 -06:00
|
|
|
error_notify "Usage: bastille limits [option(s)] TARGET [add OPTION VALUE|remove OPTION|clear|reset|[list|show] (active)|stats]"
|
2025-03-22 23:36:25 -06:00
|
|
|
echo -e "Example: bastille limits TARGET add memoryuse 1G"
|
2025-02-23 12:11:35 -07:00
|
|
|
cat << EOF
|
2025-04-30 11:21:41 -06:00
|
|
|
|
2025-02-23 12:11:35 -07:00
|
|
|
Options:
|
|
|
|
|
|
|
|
|
|
-a | --auto Auto mode. Start/stop jail(s) if required.
|
|
|
|
|
-x | --debug Enable debug mode.
|
|
|
|
|
|
|
|
|
|
EOF
|
2019-12-23 10:54:09 +01:00
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-23 12:11:35 -07:00
|
|
|
# Handle options.
|
|
|
|
|
AUTO=0
|
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
|
|
|
case "${1}" in
|
|
|
|
|
-h|--help|help)
|
|
|
|
|
usage
|
|
|
|
|
;;
|
2025-04-30 11:21:41 -06:00
|
|
|
-a|--auto)
|
|
|
|
|
AUTO=1
|
|
|
|
|
shift
|
|
|
|
|
;;
|
2025-02-23 12:11:35 -07:00
|
|
|
-x|--debug)
|
|
|
|
|
enable_debug
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-*)
|
|
|
|
|
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
|
|
|
|
|
case ${_opt} in
|
|
|
|
|
a) AUTO=1 ;;
|
|
|
|
|
x) enable_debug ;;
|
|
|
|
|
*) error_exit "Unknown Option: \"${1}\"" ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
2019-12-23 10:54:09 +01:00
|
|
|
|
2025-03-23 09:02:12 -06:00
|
|
|
if [ "$#" -lt 2 ] || [ "$#" -gt 4 ]; then
|
2019-12-23 10:54:09 +01:00
|
|
|
usage
|
2025-02-23 12:11:35 -07:00
|
|
|
fi
|
2019-12-23 10:54:09 +01:00
|
|
|
|
2025-02-23 12:11:35 -07:00
|
|
|
TARGET="${1}"
|
2025-03-29 18:21:27 -06:00
|
|
|
ACTION="${2}"
|
2025-03-23 06:00:09 -06:00
|
|
|
# Retain support for no action (will default to add)
|
2025-03-29 18:23:51 -06:00
|
|
|
if [ "${ACTION}" != "add" ] && [ "${ACTION}" != "remove" ] && [ "${ACTION}" != "clear" ] && [ "${ACTION}" != "list" ] && [ "${ACTION}" != "show" ] && [ "${ACTION}" != "reset" ] && [ "${ACTION}" != "stats" ]; then
|
2025-03-23 06:00:09 -06:00
|
|
|
ACTION="add"
|
2025-03-29 10:08:57 -06:00
|
|
|
shift 1
|
|
|
|
|
else
|
|
|
|
|
ACTION="${2}"
|
|
|
|
|
shift 2
|
2025-03-23 06:00:09 -06:00
|
|
|
fi
|
|
|
|
|
|
2025-02-23 12:11:35 -07:00
|
|
|
RACCT_ENABLE="$(sysctl -n kern.racct.enable)"
|
|
|
|
|
if [ "${RACCT_ENABLE}" != '1' ]; then
|
|
|
|
|
error_exit "Racct not enabled. Append 'kern.racct.enable=1' to /boot/loader.conf and reboot"
|
2019-12-23 10:54:09 +01:00
|
|
|
fi
|
|
|
|
|
|
2023-03-16 20:58:11 +01:00
|
|
|
bastille_root_check
|
2025-02-23 12:11:35 -07:00
|
|
|
set_target "${TARGET}"
|
2019-12-23 10:54:09 +01:00
|
|
|
|
|
|
|
|
for _jail in ${JAILS}; do
|
2025-02-23 12:11:35 -07:00
|
|
|
|
|
|
|
|
check_target_is_running "${_jail}" || if [ "${AUTO}" -eq 1 ]; then
|
|
|
|
|
bastille start "${_jail}"
|
2025-04-29 21:51:24 -06:00
|
|
|
else
|
|
|
|
|
info "[${_jail}]:"
|
2025-02-23 12:11:35 -07:00
|
|
|
error_notify "Jail is not running."
|
2025-04-30 08:19:50 -06:00
|
|
|
error_continue_next_jail "Use [-a|--auto] to auto-start the jail."
|
2025-02-23 12:11:35 -07:00
|
|
|
fi
|
2025-04-29 21:51:24 -06:00
|
|
|
|
|
|
|
|
info "[${_jail}]:"
|
2025-03-21 14:46:14 -06:00
|
|
|
|
2025-03-22 23:32:39 -06:00
|
|
|
case "${ACTION}" in
|
2025-03-22 23:50:31 -06:00
|
|
|
add)
|
|
|
|
|
OPTION="${1}"
|
|
|
|
|
VALUE="${2}"
|
2025-03-23 06:00:09 -06:00
|
|
|
# Add rctl rule to rctl.conf
|
2025-03-22 23:50:31 -06:00
|
|
|
_rctl_rule="jail:${_jail}:${OPTION}:deny=${VALUE}/jail"
|
|
|
|
|
_rctl_rule_log="jail:${_jail}:${OPTION}:log=${VALUE}/jail"
|
2025-02-23 12:11:35 -07:00
|
|
|
|
2025-03-22 23:50:31 -06:00
|
|
|
# Check whether the entry already exists and, if so, update it. -- cwells
|
|
|
|
|
if grep -qs "jail:${_jail}:${OPTION}:deny" "${bastille_jailsdir}/${_jail}/rctl.conf"; then
|
|
|
|
|
_escaped_option=$(echo "${OPTION}" | sed 's/\//\\\//g')
|
|
|
|
|
_escaped_rctl_rule=$(echo "${_rctl_rule}" | sed 's/\//\\\//g')
|
|
|
|
|
_escaped_rctl_rule_log=$(echo "${_rctl_rule_log}" | sed 's/\//\\\//g')
|
2025-03-23 06:00:09 -06:00
|
|
|
sed -i '' -E "s/jail:${_jail}:${_escaped_option}:deny.+/${_escaped_rctl_rule}/" "${bastille_jailsdir}/${_jail}/rctl.conf"
|
2025-03-22 23:50:31 -06:00
|
|
|
sed -i '' -E "s/jail:${_jail}:${_escaped_option}:log.+/${_escaped_rctl_rule_log}/" "${bastille_jailsdir}/${_jail}/rctl.conf"
|
|
|
|
|
else # Just append the entry. -- cwells
|
|
|
|
|
echo "${_rctl_rule}" >> "${bastille_jailsdir}/${_jail}/rctl.conf"
|
|
|
|
|
echo "${_rctl_rule_log}" >> "${bastille_jailsdir}/${_jail}/rctl.conf"
|
|
|
|
|
fi
|
2025-02-23 12:11:35 -07:00
|
|
|
|
2025-03-22 23:50:31 -06:00
|
|
|
echo -e "${OPTION} ${VALUE}"
|
|
|
|
|
rctl -a "${_rctl_rule}" "${_rctl_rule_log}"
|
2025-03-23 06:00:09 -06:00
|
|
|
;;
|
2025-03-22 23:50:31 -06:00
|
|
|
remove)
|
|
|
|
|
OPTION="${1}"
|
|
|
|
|
# Remove rule from rctl.conf
|
|
|
|
|
if [ -s "${bastille_jailsdir}/${_jail}/rctl.conf" ]; then
|
|
|
|
|
if grep -qs "jail:${_jail}:${OPTION}:deny" "${bastille_jailsdir}/${_jail}/rctl.conf"; then
|
2025-03-22 23:58:27 -06:00
|
|
|
_rctl_rule="$(grep "jail:${_jail}:${OPTION}:deny" "${bastille_jailsdir}/${_jail}/rctl.conf")"
|
|
|
|
|
_rctl_rule_log="$(grep "jail:${_jail}:${OPTION}:log" "${bastille_jailsdir}/${_jail}/rctl.conf")"
|
2025-03-22 23:50:31 -06:00
|
|
|
rctl -r "${_rctl_rule}" "${_rctl_rule_log}" 2>/dev/null
|
|
|
|
|
sed -i '' "/.*${_jail}:${OPTION}.*/d" "${bastille_jailsdir}/${_jail}/rctl.conf"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2025-03-22 23:56:45 -06:00
|
|
|
;;
|
2025-03-21 14:46:14 -06:00
|
|
|
clear)
|
2025-03-23 06:00:09 -06:00
|
|
|
# Remove limits
|
2025-03-21 14:46:14 -06:00
|
|
|
if [ -s "${bastille_jailsdir}/${_jail}/rctl.conf" ]; then
|
|
|
|
|
while read _limits; do
|
2025-03-22 09:18:10 -06:00
|
|
|
rctl -r "${_limits}" 2>/dev/null
|
2025-03-21 14:46:14 -06:00
|
|
|
done < "${bastille_jailsdir}/${_jail}/rctl.conf"
|
2025-03-23 06:00:09 -06:00
|
|
|
info "[${_jail}]: RCTL limits cleared."
|
2025-03-21 14:46:14 -06:00
|
|
|
fi
|
2025-03-22 23:32:39 -06:00
|
|
|
;;
|
2025-03-22 20:02:53 -06:00
|
|
|
list|show)
|
2025-03-23 06:00:09 -06:00
|
|
|
# Show limits
|
2025-03-22 18:44:21 -06:00
|
|
|
if [ -s "${bastille_jailsdir}/${_jail}/rctl.conf" ]; then
|
2025-03-22 23:32:39 -06:00
|
|
|
if [ "${1}" = "active" ]; then
|
2025-03-22 20:02:53 -06:00
|
|
|
rctl jail:${_jail} 2>/dev/null
|
|
|
|
|
else
|
|
|
|
|
cat "${bastille_jailsdir}/${_jail}/rctl.conf"
|
|
|
|
|
fi
|
2025-03-22 18:44:21 -06:00
|
|
|
fi
|
2025-03-23 06:00:09 -06:00
|
|
|
;;
|
2025-03-22 18:44:21 -06:00
|
|
|
stats)
|
2025-03-23 06:00:09 -06:00
|
|
|
# Show statistics
|
2025-03-22 18:44:21 -06:00
|
|
|
if [ -s "${bastille_jailsdir}/${_jail}/rctl.conf" ]; then
|
|
|
|
|
rctl -hu jail:${_jail} 2>/dev/null
|
|
|
|
|
fi
|
2025-03-23 06:00:09 -06:00
|
|
|
;;
|
2025-03-21 14:46:14 -06:00
|
|
|
reset)
|
2025-03-23 06:00:09 -06:00
|
|
|
# Remove limits and delete rctl.conf
|
|
|
|
|
if [ -s "${bastille_jailsdir}/${_jail}/rctl.conf" ]; then
|
2025-03-21 14:46:14 -06:00
|
|
|
while read _limits; do
|
2025-03-22 09:18:10 -06:00
|
|
|
rctl -r "${_limits}" 2>/dev/null
|
2025-03-21 14:46:14 -06:00
|
|
|
done < "${bastille_jailsdir}/${_jail}/rctl.conf"
|
2025-03-23 06:00:09 -06:00
|
|
|
info "[${TARGET}]: RCTL limits cleared."
|
2025-03-21 14:46:14 -06:00
|
|
|
fi
|
|
|
|
|
if [ -s "${bastille_jailsdir}/${_jail}/rctl.conf" ]; then
|
|
|
|
|
rm -f "${bastille_jailsdir}/${_jail}/rctl.conf"
|
2025-03-23 06:00:09 -06:00
|
|
|
info "[${TARGET}]: rctl.conf removed."
|
2025-03-21 14:46:14 -06:00
|
|
|
else
|
2025-04-30 08:19:50 -06:00
|
|
|
error_continue_next_jail "[${TARGET}]: rctl.conf not found."
|
2025-03-23 06:00:09 -06:00
|
|
|
fi
|
|
|
|
|
;;
|
2025-03-21 14:55:47 -06:00
|
|
|
esac
|
2025-04-29 20:48:11 -06:00
|
|
|
|
2025-04-30 08:19:50 -06:00
|
|
|
# Print blank line
|
|
|
|
|
echo
|
2025-04-29 20:48:11 -06:00
|
|
|
|
2019-12-23 10:54:09 +01:00
|
|
|
done
|