debian-cis/lib/common.sh

81 lines
1.7 KiB
Bash
Raw Permalink Normal View History

# CIS Debian 7 Hardening common functions
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
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
test -z "$SCRIPT_NAME" && SCRIPT_NAME=$(basename $0)
builtin echo "$*" | /usr/bin/logger -t "[CIS_Hardening] $SCRIPT_NAME" -p "user.info"
SCRIPT_NAME_FIXEDLEN=$(printf "%-25.25s" "$SCRIPT_NAME")
cecho $COLOR "$SCRIPT_NAME_FIXEDLEN $*"
2016-04-01 16:48:31 +02:00
}
cecho () {
COLOR=$1
shift
builtin echo -e "${COLOR}$*${NC}"
2016-04-01 16:48:31 +02:00
}
2016-04-04 11:23:03 +02:00
crit () {
if [ $MACHINE_LOG_LEVEL -ge 1 ]; then _logger $BRED "[ KO ] $*"; 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 () {
if [ $MACHINE_LOG_LEVEL -ge 2 ]; then _logger $BYELLOW "[WARN] $*"; fi
2016-04-01 16:48:31 +02:00
}
2016-04-04 11:23:03 +02:00
ok () {
if [ $MACHINE_LOG_LEVEL -ge 3 ]; then _logger $BGREEN "[ OK ] $*"; fi
2016-04-04 11:23:03 +02:00
}
info () {
if [ $MACHINE_LOG_LEVEL -ge 4 ]; then _logger '' "[INFO] $*"; fi
2016-04-01 16:48:31 +02:00
}
debug () {
if [ $MACHINE_LOG_LEVEL -ge 5 ]; then _logger $GRAY "[DBG ] $*"; fi
}