2016-04-14 22:47:34 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-11-23 17:10:37 +01:00
|
|
|
# run-shellcheck
|
2016-04-14 22:47:34 +02:00
|
|
|
#
|
2019-02-06 15:19:14 +01:00
|
|
|
# CIS Debian Hardening
|
2016-04-14 22:47:34 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
#
|
2020-12-22 10:51:39 +01:00
|
|
|
# 4.2.2.4 Create and Set Permissions on syslog-ng Log Files (Scored)
|
2016-04-14 22:47:34 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
set -e # One error, it's over
|
|
|
|
set -u # One variable unset, it's over
|
|
|
|
|
2020-11-16 14:06:39 +01:00
|
|
|
# Note: this is not exacly the same check as the one described in CIS PDF
|
|
|
|
|
|
|
|
# shellcheck disable=2034
|
2017-05-18 18:40:09 +02:00
|
|
|
HARDENING_LEVEL=3
|
2020-11-16 14:06:39 +01:00
|
|
|
# shellcheck disable=2034
|
|
|
|
DESCRIPTION="Create and set permissions on syslog-ng logfiles."
|
2017-05-18 18:40:09 +02:00
|
|
|
|
2020-11-16 14:06:39 +01:00
|
|
|
PERMISSIONS=''
|
|
|
|
USER=''
|
|
|
|
GROUP=''
|
|
|
|
EXCEPTIONS=''
|
2016-04-14 22:47:34 +02:00
|
|
|
|
|
|
|
# This function will be called if the script status is on enabled / audit mode
|
2020-12-04 14:08:01 +01:00
|
|
|
audit() {
|
2020-11-16 14:06:39 +01:00
|
|
|
FILES=$(grep "file(" "$SYSLOG_BASEDIR"/syslog-ng.conf | grep '"' | cut -d'"' -f 2)
|
2019-02-22 12:39:41 +01:00
|
|
|
for FILE in $FILES; do
|
2020-11-16 14:06:39 +01:00
|
|
|
does_file_exist "$FILE"
|
|
|
|
if [ "$FNRET" != 0 ]; then
|
|
|
|
warn "$FILE does not exist"
|
|
|
|
else
|
|
|
|
FOUND_EXC=0
|
2020-12-04 14:08:01 +01:00
|
|
|
if grep -q "$FILE" <(tr ' ' '\n' <<<"$EXCEPTIONS" | cut -d ":" -f 1); then
|
2020-11-16 14:06:39 +01:00
|
|
|
debug "$FILE is found in exceptions"
|
|
|
|
debug "Setting special user:group:perm"
|
|
|
|
FOUND_EXC=1
|
|
|
|
local user_bak="$USER"
|
|
|
|
local group_bak="$GROUP"
|
|
|
|
local perm_bak="$PERMISSIONS"
|
2020-12-04 14:08:01 +01:00
|
|
|
USER="$(tr ' ' '\n' <<<"$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 2)"
|
|
|
|
GROUP="$(tr ' ' '\n' <<<"$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 3)"
|
|
|
|
PERMISSIONS="$(tr ' ' '\n' <<<"$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 4)"
|
2020-11-16 14:06:39 +01:00
|
|
|
fi
|
|
|
|
has_file_correct_ownership "$FILE" "$USER" "$GROUP"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
|
|
|
ok "$FILE has correct ownership ($USER:$GROUP)"
|
|
|
|
else
|
|
|
|
crit "$FILE ownership was not set to $USER:$GROUP"
|
|
|
|
fi
|
|
|
|
has_file_correct_permissions "$FILE" "$PERMISSIONS"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
|
|
|
ok "$FILE has correct permissions ($PERMISSIONS)"
|
|
|
|
else
|
|
|
|
crit "$FILE permissions were not set to $PERMISSIONS"
|
|
|
|
fi
|
|
|
|
if [ "$FOUND_EXC" = 1 ]; then
|
|
|
|
debug "Resetting user:group:perm"
|
|
|
|
USER="$user_bak"
|
|
|
|
GROUP="$group_bak"
|
|
|
|
PERMISSIONS="$perm_bak"
|
|
|
|
fi
|
2019-02-22 12:39:41 +01:00
|
|
|
fi
|
|
|
|
done
|
2016-04-14 22:47:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# This function will be called if the script status is on enabled mode
|
2020-12-04 14:08:01 +01:00
|
|
|
apply() {
|
2019-02-22 12:39:41 +01:00
|
|
|
for FILE in $FILES; do
|
2020-11-16 14:06:39 +01:00
|
|
|
does_file_exist "$FILE"
|
|
|
|
if [ "$FNRET" != 0 ]; then
|
|
|
|
info "$FILE does not exist"
|
|
|
|
filedir=$(dirname "${FILE#/var/log/}")
|
|
|
|
if [ ! "$filedir" = "." ] && [ ! -d /var/log/"$filedir" ]; then
|
|
|
|
debug "Creating /var/log/$filedir for $FILE"
|
|
|
|
debug "mkdir -p /var/log/$filedir"
|
|
|
|
mkdir -p /var/log/"$filedir"
|
|
|
|
fi
|
|
|
|
touch "$FILE"
|
|
|
|
fi
|
|
|
|
FOUND_EXC=0
|
2020-12-04 14:08:01 +01:00
|
|
|
if grep "$FILE" <(tr ' ' '\n' <<<"$EXCEPTIONS" | cut -d ":" -f 1); then
|
2020-11-16 14:06:39 +01:00
|
|
|
debug "$FILE is found in exceptions"
|
|
|
|
debug "Setting special user:group:perm"
|
|
|
|
FOUND_EXC=1
|
|
|
|
local user_bak="$USER"
|
|
|
|
local group_bak="$GROUP"
|
|
|
|
local perm_bak="$PERMISSIONS"
|
2020-12-04 14:08:01 +01:00
|
|
|
USER="$(tr ' ' '\n' <<<"$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 2)"
|
|
|
|
GROUP="$(tr ' ' '\n' <<<"$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 3)"
|
|
|
|
PERMISSIONS="$(tr ' ' '\n' <<<"$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 4)"
|
2020-11-16 14:06:39 +01:00
|
|
|
fi
|
|
|
|
has_file_correct_ownership "$FILE" "$USER" "$GROUP"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
|
|
|
ok "$FILE has correct ownership"
|
|
|
|
else
|
|
|
|
warn "fixing $FILE ownership to $USER:$GROUP"
|
|
|
|
chown "$USER":"$GROUP" "$FILE"
|
|
|
|
fi
|
|
|
|
has_file_correct_permissions "$FILE" "$PERMISSIONS"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
|
|
|
ok "$FILE has correct permissions"
|
|
|
|
else
|
|
|
|
info "fixing $FILE permissions to $PERMISSIONS"
|
|
|
|
chmod 0"$PERMISSIONS" "$FILE"
|
|
|
|
fi
|
|
|
|
if [ "$FOUND_EXC" = 1 ]; then
|
|
|
|
debug "Resetting user:group:perm"
|
|
|
|
USER="$user_bak"
|
|
|
|
GROUP="$group_bak"
|
|
|
|
PERMISSIONS="$perm_bak"
|
2019-02-22 12:39:41 +01:00
|
|
|
fi
|
|
|
|
done
|
2016-04-14 22:47:34 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 18:40:09 +02:00
|
|
|
# This function will create the config file for this check with default values
|
|
|
|
create_config() {
|
|
|
|
cat <<EOF
|
2019-02-14 14:33:21 +01:00
|
|
|
status=audit
|
2017-05-18 18:40:09 +02:00
|
|
|
SYSLOG_BASEDIR='/etc/syslog-ng'
|
2020-11-16 14:06:39 +01:00
|
|
|
PERMISSIONS='640'
|
|
|
|
USER='root'
|
|
|
|
GROUP='adm'
|
|
|
|
# Put exceptions here with file:user:group:permissions
|
|
|
|
# example: /dev/null:root:root:666
|
|
|
|
EXCEPTIONS=''
|
2017-05-18 18:40:09 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2016-04-14 22:47:34 +02:00
|
|
|
# This function will check config parameters required
|
|
|
|
check_config() {
|
2020-11-16 14:06:39 +01:00
|
|
|
does_user_exist "$USER"
|
|
|
|
if [ "$FNRET" != 0 ]; then
|
|
|
|
crit "$USER does not exist"
|
|
|
|
exit 128
|
|
|
|
fi
|
2020-12-07 16:49:11 +01:00
|
|
|
does_group_exist "$GROUP"
|
2020-11-16 14:06:39 +01:00
|
|
|
if [ "$FNRET" != 0 ]; then
|
|
|
|
crit "$GROUP does not exist"
|
|
|
|
exit 128
|
|
|
|
fi
|
2016-04-14 22:47:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Source Root Dir Parameter
|
2017-10-25 14:50:39 +02:00
|
|
|
if [ -r /etc/default/cis-hardening ]; then
|
2020-12-04 14:08:01 +01:00
|
|
|
# shellcheck source=../../debian/default
|
2016-04-18 17:39:14 +02:00
|
|
|
. /etc/default/cis-hardening
|
2017-10-25 14:50:39 +02:00
|
|
|
fi
|
|
|
|
if [ -z "$CIS_ROOT_DIR" ]; then
|
2020-12-04 14:08:01 +01:00
|
|
|
echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment."
|
|
|
|
echo "Cannot source CIS_ROOT_DIR variable, aborting."
|
2017-10-25 14:50:39 +02:00
|
|
|
exit 128
|
|
|
|
fi
|
2016-04-14 22:47:34 +02:00
|
|
|
|
|
|
|
# Main function, will call the proper functions given the configuration (audit, enabled, disabled)
|
2020-11-27 09:29:11 +01:00
|
|
|
if [ -r "$CIS_ROOT_DIR"/lib/main.sh ]; then
|
2020-11-27 09:18:00 +01:00
|
|
|
# shellcheck source=../../lib/main.sh
|
2020-11-27 09:29:11 +01:00
|
|
|
. "$CIS_ROOT_DIR"/lib/main.sh
|
2016-04-21 23:19:50 +02:00
|
|
|
else
|
|
|
|
echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_ROOT_DIR in /etc/default/cis-hardening"
|
|
|
|
exit 128
|
|
|
|
fi
|