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
|
|
|
|
if [ ! -f $FILE ]; then
|
|
|
|
crit "Cannot backup $FILE, it's not a file"
|
|
|
|
FNRET=1
|
|
|
|
else
|
2016-04-20 11:29:44 +02: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"
|
|
|
|
cp -a $FILE $TARGET
|
|
|
|
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
|
|
|
|
error )
|
|
|
|
MACHINE_LOG_LEVEL=1
|
|
|
|
;;
|
|
|
|
warning )
|
|
|
|
MACHINE_LOG_LEVEL=2
|
|
|
|
;;
|
2016-04-04 11:23:03 +02:00
|
|
|
ok )
|
2016-04-01 16:48:31 +02:00
|
|
|
MACHINE_LOG_LEVEL=3
|
|
|
|
;;
|
2016-04-04 11:23:03 +02:00
|
|
|
info )
|
2016-04-01 16:48:31 +02:00
|
|
|
MACHINE_LOG_LEVEL=4
|
|
|
|
;;
|
2016-04-04 11:23:03 +02:00
|
|
|
debug )
|
|
|
|
MACHINE_LOG_LEVEL=5
|
|
|
|
;;
|
2016-04-01 16:48:31 +02:00
|
|
|
*)
|
2016-04-04 11:23:03 +02:00
|
|
|
MACHINE_LOG_LEVEL=4 ## Default loglevel value to info
|
2016-04-01 16:48:31 +02:00
|
|
|
esac
|
|
|
|
|
|
|
|
_logger() {
|
|
|
|
COLOR=$1
|
|
|
|
shift
|
2016-04-01 09:32:17 +02: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")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-01 16:48:31 +02:00
|
|
|
cecho () {
|
|
|
|
COLOR=$1
|
|
|
|
shift
|
2016-04-19 09:31:01 +02:00
|
|
|
builtin echo -e "${COLOR}$*${NC}"
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 11:23:03 +02:00
|
|
|
crit () {
|
2017-10-31 17:44:15 +01:00
|
|
|
if [ ${BATCH_MODE:-0} -eq 1 ]; then
|
|
|
|
BATCH_OUTPUT="$BATCH_OUTPUT KO{$*}"
|
|
|
|
else
|
|
|
|
if [ $MACHINE_LOG_LEVEL -ge 1 ]; then _logger $BRED "[ KO ] $*"; fi
|
|
|
|
fi
|
2016-04-17 23:10:47 +02:00
|
|
|
# This variable incrementation is used to measure failure or success in tests
|
|
|
|
CRITICAL_ERRORS_NUMBER=$((CRITICAL_ERRORS_NUMBER+1))
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
warn () {
|
2017-10-31 17:44:15 +01:00
|
|
|
if [ ${BATCH_MODE:-0} -eq 1 ]; then
|
|
|
|
BATCH_OUTPUT="$BATCH_OUTPUT WARN{$*}"
|
|
|
|
else
|
|
|
|
if [ $MACHINE_LOG_LEVEL -ge 2 ]; then _logger $BYELLOW "[WARN] $*"; fi
|
|
|
|
fi
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 11:23:03 +02:00
|
|
|
ok () {
|
2017-10-31 17:44:15 +01:00
|
|
|
if [ ${BATCH_MODE:-0} -eq 1 ]; then
|
|
|
|
BATCH_OUTPUT="$BATCH_OUTPUT OK{$*}"
|
|
|
|
else
|
|
|
|
if [ $MACHINE_LOG_LEVEL -ge 3 ]; then _logger $BGREEN "[ OK ] $*"; fi
|
|
|
|
fi
|
2016-04-04 11:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
info () {
|
2017-05-18 18:40:09 +02:00
|
|
|
if [ $MACHINE_LOG_LEVEL -ge 4 ]; then _logger '' "[INFO] $*"; fi
|
2016-04-01 16:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug () {
|
2016-04-18 14:01:03 +02: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
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# sudo wrapper
|
|
|
|
# issue crit state if not allowed to perform sudo
|
|
|
|
# for the specified command
|
|
|
|
#
|
|
|
|
sudo_wrapper() {
|
|
|
|
if sudo -l "$@" >/dev/null 2>&1 ; then
|
|
|
|
sudo -n "$@"
|
|
|
|
else
|
|
|
|
crit "Not allowed to \"sudo -n $*\" "
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|