2019-05-01 17:14:28 +02:00
# Function to check if value is well configured in conf file
#
# Parameters
# - 1 : name of service (ex : SSH)
# - 2 : config file (ex : /etc/ssh/sshd_config)
# - 3 : expected pattern to search without start and end spaces, can be multiple (ex : ListenAddress[[:space:]]*${IPV4_ADMIN_LAN_IP} ListenAddress[[:space:]]*${IPV6_ADMIN_LAN_IP})
# - 4 : name of param key (ex : ListenAddress)
#
# Return = 0 -> OK value is set
# Return = 1 -> Error (wrong number of param or other)
# Return = 2 -> Unexpected value is set
# Return = 3 -> All expected values are NOT configured
check_value_in_conf_file ( ) {
# Check the number of parameters
if [ ${# } -ne 4 ]
then
2019-06-01 10:51:24 +02:00
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 "
2019-05-01 17:14:28 +02:00
return 1
fi
2019-06-01 10:51:24 +02:00
# 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:]]*$//' )
2019-05-01 17:14:28 +02:00
2019-06-01 10:51:24 +02:00
# Check if conf file exist
if [ [ ! -f " ${ conf_file } " ] ]
then
echo -e " ${ RED } ERROR : file ${ conf_file } NOT FOUND. ${ NC } \n "
return 1
fi
2019-05-01 17:14:28 +02:00
# Print message to explain this check
echo -e " Check ${ BLUE } ${ name } ${ param_key } ${ NC } config file ${ conf_file } ... "
# If multiple expected values
local list_search_ok = ( ${ search_ok } )
2019-06-01 09:30:07 +02:00
local search_ok_to_grep = "^[[:space:]]*" ${ param_key } "[[:space:]]*:\?[[:space:]]*" ${ search_ok } " [[:space:]]* $"
2019-05-01 17:14:28 +02:00
# If there is more than one unexpected values
if [ ${# list_search_ok [@] } -gt 1 ]
then
search_ok_to_grep = ""
for var_search_ok in ${ list_search_ok [@] }
do
2019-06-01 10:51:24 +02:00
2019-06-01 09:30:07 +02:00
search_ok_to_grep = ${ search_ok_to_grep } '-e ' " ^[[:space:]]* ${ param_key } [[:space:]]*:\?[[:space:]]* ${ var_search_ok } [[:space:]]* $" ' '
2019-05-01 17:14:28 +02:00
done
fi
2019-06-01 10:51:24 +02:00
2019-05-01 17:14:28 +02:00
# 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 }
# Return Code
local RC = ${ ? }
2019-06-01 09:30:07 +02:00
2019-05-01 17:14:28 +02:00
# Unexpected value is set
if [ ${ RC } -eq 0 ]
then
echo -e " ${ RED } Service ${ name } has BAD CONFIGURATION for ${ param_key } : check KO ${ NC } \n "
return 2
# Unexpected value is not set
else
2019-06-01 09:30:07 +02:00
search_ok_to_grep = "^[[:space:]]*" ${ param_key } "[[:space:]]*:\?[[:space:]]*" ${ search_ok } " [[:space:]]* $"
2019-05-01 17:14:28 +02:00
# Boolean for final Return Code, if one return code in the loop -ne 0 -> set finalRC=1
local finalRC = 0
# If there is more than one OK values
if [ ${# list_search_ok [@] } -gt 1 ]
then
for var_search_ok in ${ list_search_ok [@] }
do
2019-06-01 09:30:07 +02:00
grep -q "^[[:space:]]*" ${ param_key } "[[:space:]]*:\?[[:space:]]*" ${ var_search_ok } " [[:space:]]* $" ${ conf_file }
2019-05-01 17:14:28 +02:00
RC = ${ ? }
if [ ${ RC } -ne 0 ]
then
finalRC = 1
fi
done
# Else : do classical check
else
# Check if OK value is set (success if return code = 0)
2019-06-01 09:30:07 +02:00
grep -q "^[[:space:]]*" ${ param_key } "[[:space:]]*:\?[[:space:]]*" ${ search_ok } " [[:space:]]* $" ${ conf_file }
2019-05-01 17:14:28 +02:00
# Return Code
finalRC = ${ ? }
fi
# All expected values are not configured
if [ ${ finalRC } -ne 0 ]
then
echo -e " ${ RED } Service ${ name } has BAD CONFIGURATION for ${ param_key } : check KO ${ NC } \n "
return 3
# OK value is set
else
echo -e " ${ GREEN } Service ${ name } has GOOD CONFIGURATION for ${ param_key } : check OK ${ NC } \n "
return 0
fi
fi
}
# Function to print title
#
# Parameter
# - 1 : title to print (ex : SSH)
#
# Return = 0 -> OK value is set
# Return = 1 -> Error (wrong number of param or other)
print_config_title ( ) {
# Check the number of parameters
if [ ${# } -ne 1 ]
then
echo -e "ERROR : when call print_config_title function, one parameter (only one) expected.\n"
return 1
fi
title = ${ 1 }
basic_len = 48
# Find number of "-" for title
title_len = $( echo -n " ${ title } " | wc -c)
modulo2 = $(( ${ title_len } % 2 ))
# Echo title with 48 chars
echo "------------------------------------------------"
# If title_len <= 48
if [ ${ title_len } -lt ${ basic_len } ]
then
final_left_len = $(( ( ${ basic_len } - ( ${ title_len } - ${ modulo2 } )) /2) )
final_right_len = $(( ${ final_left_len } - ${ modulo2 } ))
printf %${ final_left_len } s | tr " " "-"
echo -n -e " ${ BLUE } ${ title } ${ NC } "
printf %${ final_right_len } s | tr " " "-"
else
echo -e -n " ${ BLUE } CHECK ${ title } CONFIG ${ NC } "
fi
echo -e "\n------------------------------------------------\n"
return 0
}