diff --git a/bin/hardening/8.7.1_journald_config_compress.sh b/bin/hardening/8.7.1_journald_config_compress.sh new file mode 100755 index 0000000..a5d62e1 --- /dev/null +++ b/bin/hardening/8.7.1_journald_config_compress.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# +# harbian-audit for Debian GNU/Linux 9/10/11/12 +# + +# +# 8.7.1 Ensure journald is configured to compress large log files (Scored) +# Author : Samson wen, Samson +# + +set -e # One error, it's over +set -u # One variable unset, it's over + +HARDENING_LEVEL=3 + +CONFFILE='/etc/systemd/journald.conf' +OPTION='Compress' +OPTION_VAL='yes' + +# This function will be called if the script status is on enabled / audit mode +audit () { + check_param_pair_by_str $CONFFILE $OPTION $OPTION_VAL + if [ $FNRET = 0 ]; then + ok "$OPTION set is $OPTION_VAL in $CONFFILE." + elif [ $FNRET = 1 ]; then + crit "$CONFFILE is not found!" + elif [ $FNRET = 2 ]; then + crit "$OPTION set is not $OPTION_VAL in $CONFFILE!" + elif [ $FNRET = 3 ]; then + crit "$OPTION is not present in $CONFFILE!" + fi +} + +apply () { + if [ $FNRET = 0 ]; then + ok "$OPTION set is $OPTION_VAL in $CONFFILE." + elif [ $FNRET = 1 ]; then + crit "$CONFFILE is not found, please check!" + elif [ $FNRET = 2 ]; then + warn "$OPTION set is not $OPTION_VAL in $CONFFILE, reset to $OPTION_VAL" + reset_option_str_to_journald $CONFFILE $OPTION $OPTION_VAL + elif [ $FNRET = 3 ]; then + warn "$OPTION is not present in $CONFFILE, add to $CONFFILE" + add_end_of_file $CONFFILE "${OPTION}=${OPTION_VAL}" + fi +} + +# This function will check config parameters required +check_config() { + : +} + +# Source Root Dir Parameter +if [ -r /etc/default/cis-hardening ]; then + . /etc/default/cis-hardening +fi +if [ -z "$CIS_ROOT_DIR" ]; then + echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment." + echo "Cannot source CIS_ROOT_DIR variable, aborting." + exit 128 +fi + +# Main function, will call the proper functions given the configuration (audit, enabled, disabled) +if [ -r $CIS_ROOT_DIR/lib/main.sh ]; then + . $CIS_ROOT_DIR/lib/main.sh +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 diff --git a/lib/utils.sh b/lib/utils.sh index f4662a2..2d5719b 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -780,6 +780,54 @@ verify_integrity_all_packages() fi } +# Check paramer with str +# example: Storage=persistent +# return: 0 1 2 3 +check_param_pair_by_str () +{ + FILENAME=$1 + OPTION=$2 + EXPECT_OPSTR=$3 + + #Example: + # FILENAME="/etc/systemd/journald.conf" + # OPTION="Storage" + # EXPECT_OPSTR="persistent" + + if [ ! -f $FILENAME ]; then + debug "$FILENAME file is not exist!" + FNRET=1 + else + if [ $(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' $FILENAME | grep "$OPTION=" | wc -l) -gt 0 ]; then + debug "$OPTION is exist in $FILENAME." + OP_STR=$(sed -e '/^#/d' -e '/^[ \t][ \t]*#/d' -e 's/#.*$//' -e '/^$/d' $FILENAME | grep $OPTION | awk -F'=' '{print $2}') + if [ $OP_STR == $EXPECT_OPSTR ]; then + debug "The str value is eq to expect str." + FNRET=0 + else + debug "The str value is not eq to expect str." + FNRET=2 + fi + else + debug "The options $OPTION is not exist in $FILENAME" + FNRET=3 + fi + fi +} + +reset_option_str_to_journald () +{ + FILENAME=$1 + OPTION=$2 + SET_OPSTR=$3 + + #Example: + # FILENAME="/etc/systemd/journald.conf" + # OPTION="Storage" + # SET_OPSTR="persistent" + sed -i "s/${OPTION}=.*/${OPTION}=${SET_OPSTR}/" $FILENAME +} + # Check paramer with value # example : minlen = 9 # ruturn: 0 1 2 3