2020-11-27 09:22:47 +01:00
|
|
|
# shellcheck shell=bash
|
2019-02-06 15:19:14 +01:00
|
|
|
# CIS Debian Hardening common functions
|
2016-04-01 09:32:17 +02:00
|
|
|
|
2020-11-23 17:10:37 +01:00
|
|
|
# run-shellcheck
|
2016-04-04 15:05:10 +02:00
|
|
|
#
|
|
|
|
# File Backup functions
|
|
|
|
#
|
|
|
|
backup_file() {
|
|
|
|
FILE=$1
|
2020-12-07 17:11:32 +01:00
|
|
|
if [ ! -f "$FILE" ]; then
|
2016-04-04 15:05:10 +02:00
|
|
|
crit "Cannot backup $FILE, it's not a file"
|
|
|
|
FNRET=1
|
|
|
|
else
|
2020-12-07 17:11:32 +01:00
|
|
|
TARGET=$(echo "$FILE" | sed -s -e 's/\//./g' -e 's/^.//' -e "s/$/.$(date +%F-%H_%M_%S)/")
|
2016-04-04 15:05:10 +02:00
|
|
|
TARGET="$BACKUPDIR/$TARGET"
|
|
|
|
debug "Backuping $FILE to $TARGET"
|
2020-12-07 17:11:32 +01:00
|
|
|
cp -a "$FILE" "$TARGET"
|
2020-12-07 14:53:10 +01:00
|
|
|
# shellcheck disable=2034
|
2016-04-04 15:05:10 +02:00
|
|
|
FNRET=0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
2016-04-01 16:48:31 +02:00
|
|
|
# Logging functions
|
2016-04-04 15:05:10 +02:00
|
|
|
#
|
2016-04-01 16:48:31 +02:00
|
|
|
|
|
|
|
case $LOGLEVEL in
|
2021-10-20 13:22:59 +02:00
|
|
|
silent)
|
|
|
|
MACHINE_LOG_LEVEL=0
|
|
|
|
;;
|
2020-12-04 14:08:01 +01:00
|
|
|
error)
|
|
|
|
MACHINE_LOG_LEVEL=1
|
|
|
|
;;
|
|
|
|
warning)
|
|
|
|
MACHINE_LOG_LEVEL=2
|
|
|
|
;;
|
|
|
|
ok)
|
|
|
|
MACHINE_LOG_LEVEL=3
|
|
|
|
;;
|
|
|
|
info)
|
|
|
|
MACHINE_LOG_LEVEL=4
|
|
|
|
;;
|
|
|
|
debug)
|
|
|
|
MACHINE_LOG_LEVEL=5
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
MACHINE_LOG_LEVEL=4 ## Default loglevel value to info
|
|
|
|
;;
|
2016-04-01 16:48:31 +02:00
|
|
|
esac
|
|
|
|
|
|
|
|
_logger() {
|
|
|
|
COLOR=$1
|
|
|
|
shift
|
2020-12-07 17:11:32 +01:00
|
|
|
test -z "$SCRIPT_NAME" && SCRIPT_NAME=$(basename "$0")
|
2019-02-06 17:25:16 +01:00
|
|
|
builtin echo "$*" | /usr/bin/logger -t "CIS_Hardening[$$] $SCRIPT_NAME" -p "user.info"
|
2017-05-18 18:40:09 +02:00
|
|
|
SCRIPT_NAME_FIXEDLEN=$(printf "%-25.25s" "$SCRIPT_NAME")
|
2020-12-07 17:11:32 +01:00
|
|
|
cecho "$COLOR" "$SCRIPT_NAME_FIXEDLEN $*"
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 17:25:16 +01:00
|
|
|
becho() {
|
2019-03-19 10:38:41 +01:00
|
|
|
toprint=$(echo "$*" | /usr/bin/tr '\n' ' ')
|
|
|
|
builtin echo "$toprint" | /usr/bin/logger -t "CIS_Hardening[$$]" -p "user.info"
|
|
|
|
builtin echo "$toprint"
|
2019-02-06 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:08:01 +01:00
|
|
|
cecho() {
|
2016-04-01 16:48:31 +02:00
|
|
|
COLOR=$1
|
|
|
|
shift
|
2016-04-19 09:31:01 +02:00
|
|
|
builtin echo -e "${COLOR}$*${NC}"
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:08:01 +01:00
|
|
|
crit() {
|
2020-12-07 17:11:32 +01:00
|
|
|
if [ "${BATCH_MODE:-0}" -eq 1 ]; then
|
2017-10-31 17:44:15 +01:00
|
|
|
BATCH_OUTPUT="$BATCH_OUTPUT KO{$*}"
|
|
|
|
else
|
2020-12-10 09:50:33 +01:00
|
|
|
if [ "$MACHINE_LOG_LEVEL" -ge 1 ]; then _logger "$BRED" "[ KO ] $*"; fi
|
2017-10-31 17:44:15 +01:00
|
|
|
fi
|
2016-04-17 23:10:47 +02:00
|
|
|
# This variable incrementation is used to measure failure or success in tests
|
2020-12-04 14:08:01 +01:00
|
|
|
CRITICAL_ERRORS_NUMBER=$((CRITICAL_ERRORS_NUMBER + 1))
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:08:01 +01:00
|
|
|
warn() {
|
2020-12-07 17:11:32 +01:00
|
|
|
if [ "${BATCH_MODE:-0}" -eq 1 ]; then
|
2017-10-31 17:44:15 +01:00
|
|
|
BATCH_OUTPUT="$BATCH_OUTPUT WARN{$*}"
|
|
|
|
else
|
2020-12-10 09:50:33 +01:00
|
|
|
if [ "$MACHINE_LOG_LEVEL" -ge 2 ]; then _logger "$BYELLOW" "[WARN] $*"; fi
|
2017-10-31 17:44:15 +01:00
|
|
|
fi
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:08:01 +01:00
|
|
|
ok() {
|
2020-12-07 17:11:32 +01:00
|
|
|
if [ "${BATCH_MODE:-0}" -eq 1 ]; then
|
2017-10-31 17:44:15 +01:00
|
|
|
BATCH_OUTPUT="$BATCH_OUTPUT OK{$*}"
|
|
|
|
else
|
2020-12-10 09:50:33 +01:00
|
|
|
if [ "$MACHINE_LOG_LEVEL" -ge 3 ]; then _logger "$BGREEN" "[ OK ] $*"; fi
|
2017-10-31 17:44:15 +01:00
|
|
|
fi
|
2016-04-04 11:23:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:08:01 +01:00
|
|
|
info() {
|
2020-12-07 17:11:32 +01:00
|
|
|
if [ "$MACHINE_LOG_LEVEL" -ge 4 ]; then _logger '' "[INFO] $*"; fi
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 14:08:01 +01:00
|
|
|
debug() {
|
2020-12-07 17:11:32 +01:00
|
|
|
if [ "$MACHINE_LOG_LEVEL" -ge 5 ]; then _logger "$GRAY" "[DBG ] $*"; fi
|
2016-04-01 09:32:17 +02:00
|
|
|
}
|
2018-03-16 12:06:56 +01:00
|
|
|
|
2022-01-31 15:38:38 +01:00
|
|
|
exception() {
|
|
|
|
# Trap exit code is the same as the trapped one unless we call an explicit exit
|
|
|
|
TRAP_CODE=$?
|
|
|
|
if [ "$ACTIONS_DONE" -ne 1 ]; then
|
|
|
|
if [ "$BATCH_MODE" -eq 1 ]; then
|
|
|
|
BATCH_OUTPUT="KO $SCRIPT_NAME $BATCH_OUTPUT KO{Unexpected exit code: $TRAP_CODE}"
|
|
|
|
becho "$BATCH_OUTPUT"
|
|
|
|
else
|
|
|
|
crit "Check failed with unexpected exit code: $TRAP_CODE"
|
|
|
|
fi
|
|
|
|
exit 1 # Means critical status
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-03-16 12:06:56 +01:00
|
|
|
#
|
|
|
|
# sudo wrapper
|
|
|
|
# issue crit state if not allowed to perform sudo
|
|
|
|
# for the specified command
|
|
|
|
#
|
|
|
|
sudo_wrapper() {
|
2020-12-04 14:08:01 +01:00
|
|
|
if sudo -l "$@" >/dev/null 2>&1; then
|
2018-03-16 12:06:56 +01:00
|
|
|
sudo -n "$@"
|
|
|
|
else
|
|
|
|
crit "Not allowed to \"sudo -n $*\" "
|
|
|
|
fi
|
|
|
|
}
|
2021-01-22 09:31:53 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Math functions
|
|
|
|
#
|
|
|
|
|
2021-02-04 16:21:49 +01:00
|
|
|
div() {
|
2021-01-22 09:31:53 +01:00
|
|
|
local _d=${3:-2}
|
|
|
|
local _n=0000000000
|
|
|
|
_n=${_n:0:$_d}
|
2021-03-23 08:36:36 +01:00
|
|
|
if (($1 == 0)); then
|
|
|
|
echo "0"
|
|
|
|
return
|
|
|
|
fi
|
2021-01-22 09:31:53 +01:00
|
|
|
if (($2 == 0)); then
|
|
|
|
echo "N.A"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
local _r=$(($1$_n / $2))
|
|
|
|
_r=${_r:0:-$_d}.${_r: -$_d}
|
|
|
|
echo $_r
|
|
|
|
}
|