debian-cis/bin/hardening/5.1.8_cron_users.sh

125 lines
3.5 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# run-shellcheck
#
# CIS Debian Hardening
#
#
Renum 9.1.x to 5.1.x cron checks renamed: bin/hardening/9.1.1_enable_cron.sh -> bin/hardening/5.1.1_enable_cron.sh renamed: bin/hardening/9.1.2_crontab_perm_ownership.sh -> bin/hardening/5.1.2_crontab_perm_ownership.sh renamed: bin/hardening/9.1.3_cron_hourly_perm_ownership.sh -> bin/hardening/5.1.3_cron_hourly_perm_ownership.sh renamed: bin/hardening/9.1.4_cron_daily_perm_ownership.sh -> bin/hardening/5.1.4_cron_daily_perm_ownership.sh renamed: bin/hardening/9.1.5_cron_weekly_perm_ownership.sh -> bin/hardening/5.1.5_cron_weekly_perm_ownership.sh renamed: bin/hardening/9.1.6_cron_monthly_perm_ownership.sh -> bin/hardening/5.1.6_cron_monthly_perm_ownership.sh renamed: bin/hardening/9.1.7_cron_d_perm_ownership.sh -> bin/hardening/5.1.7_cron_d_perm_ownership.sh renamed: bin/hardening/9.1.8_cron_users.sh -> bin/hardening/5.1.8_cron_users.sh renamed: tests/hardening/9.1.8_cron_users.sh -> tests/hardening/5.1.1_enable_cron.sh renamed: tests/hardening/9.1.7_cron_d_perm_ownership.sh -> tests/hardening/5.1.2_crontab_perm_ownership.sh renamed: tests/hardening/9.1.6_cron_monthly_perm_ownership.sh -> tests/hardening/5.1.3_cron_hourly_perm_ownership.sh renamed: tests/hardening/9.1.5_cron_weekly_perm_ownership.sh -> tests/hardening/5.1.4_cron_daily_perm_ownership.sh renamed: tests/hardening/9.1.4_cron_daily_perm_ownership.sh -> tests/hardening/5.1.5_cron_weekly_perm_ownership.sh renamed: tests/hardening/9.1.3_cron_hourly_perm_ownership.sh -> tests/hardening/5.1.6_cron_monthly_perm_ownership.sh renamed: tests/hardening/9.1.2_crontab_perm_ownership.sh -> tests/hardening/5.1.7_cron_d_perm_ownership.sh renamed: tests/hardening/9.1.1_enable_cron.sh -> tests/hardening/5.1.8_cron_users.sh
2019-09-11 12:16:50 +02:00
# 5.1.8 Ensure at/cron is restricted to authorized users (Scored)
#
set -e # One error, it's over
set -u # One variable unset, it's over
# shellcheck disable=2034
HARDENING_LEVEL=2
# shellcheck disable=2034
DESCRIPTION="Restrict at/cron to authorized users."
FILES_ABSENT='/etc/cron.deny /etc/at.deny'
FILES_PRESENT='/etc/cron.allow /etc/at.allow'
PERMISSIONS='644'
PERMISSIONSOK='644 640 600 440 400'
USER='root'
GROUP='root'
# This function will be called if the script status is on enabled / audit mode
2020-12-04 14:08:01 +01:00
audit() {
for FILE in $FILES_ABSENT; do
does_file_exist "$FILE"
if [ "$FNRET" = 0 ]; then
crit "$FILE exists"
else
ok "$FILE is absent"
fi
done
for FILE in $FILES_PRESENT; do
does_file_exist "$FILE"
if [ "$FNRET" != 0 ]; then
crit "$FILE is absent, should exist"
else
has_file_correct_ownership "$FILE" "$USER" "$GROUP"
if [ "$FNRET" = 0 ]; then
ok "$FILE has correct ownership"
else
crit "$FILE ownership was not set to $USER:$GROUP"
fi
has_file_one_of_permissions "$FILE" "$PERMISSIONSOK"
if [ "$FNRET" = 0 ]; then
ok "$FILE has correct permissions"
else
2016-04-21 18:32:36 +02:00
crit "$FILE permissions were not set to $PERMISSIONS"
2020-12-04 14:08:01 +01:00
fi
fi
done
}
# This function will be called if the script status is on enabled mode
2020-12-04 14:08:01 +01:00
apply() {
for FILE in $FILES_ABSENT; do
does_file_exist "$FILE"
if [ "$FNRET" = 0 ]; then
warn "$FILE exists"
rm "$FILE"
else
ok "$FILE is absent"
fi
done
for FILE in $FILES_PRESENT; do
does_file_exist "$FILE"
if [ "$FNRET" != 0 ]; then
warn "$FILE is absent, fixing (touch)"
touch "$FILE"
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_one_of_permissions "$FILE" "$PERMISSIONSOK"
if [ "$FNRET" = 0 ]; then
ok "$FILE has correct permissions"
else
2016-04-21 18:32:36 +02:00
warn "$FILE permissions were not set to $PERMISSIONS"
chmod 0"$PERMISSIONS" "$FILE"
fi
done
}
# This function will check config parameters required
check_config() {
2020-12-10 09:50:33 +01:00
does_user_exist "$USER"
if [ "$FNRET" != 0 ]; then
crit "$USER does not exist"
exit 128
fi
2020-12-10 09:50:33 +01:00
does_group_exist "$GROUP"
if [ "$FNRET" != 0 ]; then
crit "$GROUP does not exist"
exit 128
fi
}
# Source Root Dir Parameter
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
fi
if [ -z "$CIS_LIB_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_LIB_DIR variable, aborting."
exit 128
fi
# Main function, will call the proper functions given the configuration (audit, enabled, disabled)
if [ -r "${CIS_LIB_DIR}"/main.sh ]; then
2020-12-04 14:08:01 +01:00
# shellcheck source=../../lib/main.sh
. "${CIS_LIB_DIR}"/main.sh
else
echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_LIB_DIR in /etc/default/cis-hardening"
exit 128
fi