From 72cdff40fa47ee3e9f1788280c325c4677e4dda4 Mon Sep 17 00:00:00 2001 From: nemo Date: Sat, 1 Jun 2019 10:51:24 +0200 Subject: [PATCH] Replace parts by new common function --- recipes/common_functions.sh | 30 ++++-- recipes/recipe_check_snmp_config.sh | 152 ++++++++++------------------ recipes/recipe_check_ssh_config.sh | 2 +- 3 files changed, 78 insertions(+), 106 deletions(-) diff --git a/recipes/common_functions.sh b/recipes/common_functions.sh index cbde74a..c715d6a 100644 --- a/recipes/common_functions.sh +++ b/recipes/common_functions.sh @@ -16,16 +16,30 @@ check_value_in_conf_file () { # Check the number of parameters if [ ${#} -ne 4 ] then - echo -e "ERROR : when call check_value_in_conf_file function, bad parameters number expected : 4.\n" + echo -e "${RED}ERROR : when call check_value_in_conf_file function, bad parameters number expected : 4.${NC}\n" + return 1 + fi + + # Check if on param is empty or just contains space(s) + if [[ -z "${1// }" ]] || [[ -z "${2// }" ]] || [[ -z "${3// }" ]] || [[ -z "${4// }" ]] + then + echo -e "${RED}ERROR : at least one parameter is empty, please provide all required params.${NC}\n" return 1 fi - # Get parameters - local name=${1} - local conf_file=${2} - local param_key=${3} - local search_ok=${4} + # Get parameters and delete 'start' en 'end' spaces. For name conf_file + local name=$(echo -e ${1} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/[[:space:]]/[[:space:]]*/g') + local conf_file=$(echo -e ${2} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/[[:space:]]/[[:space:]]*/g') + local param_key=$(echo -e ${3} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e 's/[[:space:]]/[[:space:]]*/g') + local search_ok=$(echo -e ${4} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + # Check if conf file exist + if [[ ! -f "${conf_file}" ]] + then + echo -e "${RED}ERROR : file ${conf_file} NOT FOUND.${NC}\n" + return 1 + fi + # Print message to explain this check echo -e "Check ${BLUE}${name} ${param_key}${NC} config file ${conf_file} ..." @@ -39,10 +53,11 @@ check_value_in_conf_file () { search_ok_to_grep="" for var_search_ok in ${list_search_ok[@]} do + search_ok_to_grep=${search_ok_to_grep}'-e '"^[[:space:]]*${param_key}[[:space:]]*:\?[[:space:]]*${var_search_ok}[[:space:]]*$"' ' done fi - echo 1 + # Check if unexpected value is set (grep -v) (success if return code = 1) grep "^[[:space:]]*"${param_key} ${conf_file} | grep -q -v ${search_ok_to_grep} @@ -57,7 +72,6 @@ check_value_in_conf_file () { # Unexpected value is not set else - echo 2 search_ok_to_grep="^[[:space:]]*"${param_key}"[[:space:]]*:\?[[:space:]]*"${search_ok}"[[:space:]]*$" # Boolean for final Return Code, if one return code in the loop -ne 0 -> set finalRC=1 diff --git a/recipes/recipe_check_snmp_config.sh b/recipes/recipe_check_snmp_config.sh index 1f2119e..c23ae28 100755 --- a/recipes/recipe_check_snmp_config.sh +++ b/recipes/recipe_check_snmp_config.sh @@ -2,107 +2,65 @@ print_config_title 'CHECK SNMP BASIC CONFIG' # This does NOT check if SNMP 'monitoring server' has configured this machine # Check /etc/snmp/snmpd.conf config file agentAddress, rocommunity and trapsink/trap2sink parameters -echo -e "Check ${BLUE}SNMPD${NC} config file /etc/snmp/snmpd.conf" -# Check if agentAddress exist but different than expected (success if return code = 1) -grep "^[[:space:]]*agentAddress" /etc/snmp/snmpd.conf \ - |grep -E -q -v \ - -e "^[[:space:]]*agentAddress[[:space:]]*udp:127.0.0.1:161[[:space:]]*\$" \ - -e "^[[:space:]]*agentAddress[[:space:]]*udp:${IPV4_ADMIN_NETWORK}[0-2]?[0-9]?[0-9]?:161[[:space:]]*\$" \ - -e "^[[:space:]]*agentAddress[[:space:]]*udp6:\[::1\]:161[[:space:]]*\$" \ - -e "^[[:space:]]*agentAddress[[:space:]]*udp6:\[${IPV6_ADMIN_NETWORK}[0-9,a-f,\:]*\]:161[[:space:]]*\$" +check_value_in_conf_file "SNMP" "/etc/snmp/snmpd.conf" "agentAddress" "udp:127.0.0.1:161 udp:${IPV4_ADMIN_LAN_IP}:161 udp6:\[::1\]:161 udp6:\[${IPV6_ADMIN_LAN_IP}\]:161" -# Return Code -RC=$? - -# agentAddress other IP than expected -if [ $RC -eq 0 ] -then - SNMP_CONFIG_CHECK_FAILED="${SNMP_CONFIG_CHECK_FAILED} SNMPD agentAddress allow connection from unexpected IP, expected :\n'agentAddress udp:IPv4AdminLANlistenAddress:161'\n'agentAddress udp6:[IPv6AdminLANlistenAddress]:161'\nOPTIONAL : 'agentAddress udp:127.0.0.1:161'\nOPTIONAL : 'agentAddress udp6:[::1]:161' ;\n" - echo -e "${RED}SNMPD agentAddress allow connection from UNEXPECTED IP : check KO${NC}\n" -# Does not agentAddress unexpected IP -else - # Check expected IP are configured (IPv4 and IPv6) (success if return code = 0) - grep -E -q "^[[:space:]]*agentAddress[[:space:]]*udp:${IPV4_ADMIN_NETWORK}[0-2]?[0-9]?[0-9]?:161[[:space:]]*\$" /etc/snmp/snmpd.conf - - # Return Code - RCa=$? - - grep -E -q "^[[:space:]]*agentAddress[[:space:]]*udp6:\[${IPV6_ADMIN_NETWORK}[0-9,a-f,\:]*\]:161[[:space:]]*\$" /etc/snmp/snmpd.conf - - # Return Code - RCb=$? - - # Expected IP are NOT configured (IPv4 and IPv6) - if [ $RCa -ne 0 ] || [ $RCb -ne 0 ] - then - SNMP_CONFIG_CHECK_FAILED="${SNMP_CONFIG_CHECK_FAILED} All SNMPD agentAddress expected IP (IPv4 and IPv6) are not well configured, expected :\n'agentAddress udp:IPv4AdminLANlistenAddress:161'\n'agentAddress udp6:[IPv6AdminLANlistenAddress]:161'\nOPTIONAL : 'agentAddress udp:127.0.0.1:161'\nOPTIONAL : 'agentAddress udp6:[::1]:161'\n" - echo -e "${RED}All SNMPD agentAddress expected IP (IPv4 and IPv6) are NOT well CONFIGURED : check KO${NC}\n" - # Expected IP are configured (IPv4 and IPv6) - else - echo -e "${GREEN}SNMPD agentAddress expected IP are CONFIGURED : check OK${NC}\n" - fi -fi - -# Check if rocommunity exist but different than expected (success if return code = 1) -grep "^[[:space:]]*rocommunity" /etc/snmp/snmpd.conf \ - |grep -q -v -e "^[[:space:]]*rocommunity[[:space:]]*public[[:space:]]*default[[:space:]]*\$" - -# Return Code -RC=$? - -# Unexpected rocommunity found -if [ $RC -eq 0 ] -then - SNMP_CONFIG_CHECK_FAILED="${SNMP_CONFIG_CHECK_FAILED} Unexpected rocommunity found, expected : rocommunity public default ;\n" - echo -e "${RED}UNEXPECTED rocommunity found : check KO${NC}\n" -# No unexpected rocommunity -else - # Check if expected rocommunity configured (success if return code = 0) - grep -q "^[[:space:]]*rocommunity[[:space:]]*public[[:space:]]*default[[:space:]]*\$" /etc/snmp/snmpd.conf - - # Return Code - RC=$? - - # Expected rocommunity NOT configured - if [ $RC -ne 0 ] - then - SNMP_CONFIG_CHECK_FAILED="${SNMP_CONFIG_CHECK_FAILED} Expected rocommunity not configured, expected : rocommunity public default ;\n" - echo -e "${RED} Expected rocommunity NOT CONFIGURED : check KO${NC}\n" - # Expected rocommunity configured - else - echo -e "${GREEN}Expected rocommunity CONFIGURED : check OK${NC}\n" - fi -fi +case ${?} in +0) # OK, nothing to do + ;; +1) # Error (wrong number of param or other) + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" Error during agentAddress check with function check_value_in_conf_file, maybe incorrect number of parameter;" + ;; +2) # Unexpected value is set + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" agentAddress are not well configured or has other value, set agentAddress for IPv4 and IPv6 in Admin LAN and localhost;" + ;; +3) # All expected values are NOT configured + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" agentAddress are not well configured, set agentAddress for IPv4 and IPv6 in Admin LAN and localhost;" + ;; +*) # Unknown return code... + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" Error, unknown return code when calling check_value_in_conf_file to check agentAddress;" + ;; +esac -# Check if trapsink/trap2sink exist but different than expected (success if return code = 1) -grep -E "^[[:space:]]*trap[2]?sink" /etc/snmp/snmpd.conf \ - |grep -E -q -v -e "^[[:space:]]*trap[2]?sink[[:space:]]*${NAME_MASTER_MONITORING}[[:space:]]*public[[:space:]]*\$" +# No IPv6 here.... +check_value_in_conf_file "SNMP" "/etc/snmp/snmpd.conf" "rocommunity" "public[[:space:]]*127.0.0.1 public[[:space:]]*${IPV4_ADMIN_NETWORK}0/24" -# Return Code -RC=$? +case ${?} in +0) # OK, nothing to do + ;; +1) # Error (wrong number of param or other) + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" Error during rocommunity check with function check_value_in_conf_file, maybe incorrect number of parameter;" + ;; +2) # Unexpected value is set + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" rocommunity are not well configured or has other value, set 'rocommunity public 127.0.0.1' and 'rocommunity public ${IPV4_ADMIN_NETWORK}0/24';" + ;; +3) # All expected values are NOT configured + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" rocommunity are not well configured, set 'rocommunity public 127.0.0.1' and 'rocommunity public ${IPV4_ADMIN_NETWORK}0/24';" + ;; +*) # Unknown return code... + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" Error, unknown return code when calling check_value_in_conf_file to check rocommunity;" + ;; +esac + + +check_value_in_conf_file "SNMP" "/etc/snmp/snmpd.conf" "trap2sink" "${NAME_MASTER_MONITORING}[[:space:]]*public" + +case ${?} in +0) # OK, nothing to do + ;; +1) # Error (wrong number of param or other) + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" Error during trap2sink check with function check_value_in_conf_file, maybe incorrect number of parameter;" + ;; +2) # Unexpected value is set + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" trap2sink are not well configured or has other value, set 'trap2sink ${NAME_MASTER_MONITORING} public;" + ;; +3) # All expected values are NOT configured + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" trap2sink are not well configured, set 'trap2sink ${NAME_MASTER_MONITORING} public';" + ;; +*) # Unknown return code... + SNMP_CONFIG_CHECK_FAILED=${SNMP_CONFIG_CHECK_FAILED}" Error, unknown return code when calling check_value_in_conf_file to check trap2sink;" + ;; +esac -# Unexpected trapsink/trap2sink found -if [ $RC -eq 0 ] -then - SNMP_CONFIG_CHECK_FAILED="${SNMP_CONFIG_CHECK_FAILED} Unexpected trapsink/trap2sink found, expected : trap2sink ${NAME_MASTER_MONITORING} public ;" - echo -e "${RED}UNEXPECTED trapsink/trap2sink found : check KO${NC}\n" -# No unexpected trapsink/trap2sink -else - # Check if expected trapsink/trap2sink configured (success if return code = 0) - grep -E -q "^[[:space:]]*trap2sink[[:space:]]*${NAME_MASTER_MONITORING}[[:space:]]*public[[:space:]]*\$" /etc/snmp/snmpd.conf - - # Return Code - RC=$? - # Expected trapsink/trap2sink NOT configured - if [ $RC -ne 0 ] - then - SNMP_CONFIG_CHECK_FAILED="${SNMP_CONFIG_CHECK_FAILED} Expected trapsink/trap2sink not configured, expected : trap2sink ${NAME_MASTER_MONITORING} public ;" - echo -e "${RED} Expected trapsink/trap2sink NOT CONFIGURED : check KO${NC}\n" - # Expected trapsink/trap2sink configured - else - echo -e "${GREEN}Expected trapsink/trap2sink CONFIGURED : check OK${NC}\n" - fi -fi diff --git a/recipes/recipe_check_ssh_config.sh b/recipes/recipe_check_ssh_config.sh index 6534766..1e3c5c8 100755 --- a/recipes/recipe_check_ssh_config.sh +++ b/recipes/recipe_check_ssh_config.sh @@ -25,7 +25,7 @@ case ${?} in 0) # OK, nothing to do ;; 1) # Error (wrong number of param or other) - SSH_CONFIG_CHECK_FAILED=${SSH_CONFIG_CHECK_FAILED}" Error during PermitRootLogin with function check_value_in_conf_file, maybe incorrect number of parameter;" + SSH_CONFIG_CHECK_FAILED=${SSH_CONFIG_CHECK_FAILED}" Error during PermitRootLogin with function check_value_in_conf_file, maybe incorrect number of parameter or incorrect parameter;" ;; 2) # Unexpected value is set SSH_CONFIG_CHECK_FAILED=${SSH_CONFIG_CHECK_FAILED}" PermitRootLogin is not set to 'no' or has other value, set 'PermitRootLogin no' ;"