From e078010ebd538bbfe9b9dc02402e8ce9b90a6dab Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Wed, 13 Mar 2024 11:00:33 +0100 Subject: [PATCH] add validation of ini files --- includes/dbdetect.class.sh | 29 ++++---- includes/dbdetect_validate_profile_ini.sh | 52 ++++++++++++++ vendor/ini.class.sh | 86 +++++++++++++++++++++++ 3 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 includes/dbdetect_validate_profile_ini.sh diff --git a/includes/dbdetect.class.sh b/includes/dbdetect.class.sh index dd08dd4..87ed5c9 100644 --- a/includes/dbdetect.class.sh +++ b/includes/dbdetect.class.sh @@ -77,9 +77,12 @@ function dbdetect.exists(){ local _found=0 + # show errors in profile ini files + ini.validate "$_config" "$( dirname $0)/includes/dbdetect_validate_profile_ini.sh" && dbdetect._wd "Ini validation OK" + # set file and inisection we read values from ini.set "$_config" "detect" - + # --- check tcp local tcpport; tcpport=$( ini.value "tcp" ) if [ -n "$tcpport" ]; then @@ -104,18 +107,6 @@ function dbdetect.exists(){ dbdetect._wd "... tcp $tcpport is used by $tcpprocess." fi - # --- check binaries - local binary; binary=$( ini.value "binary" ) - if [ -n "${binary}" ]; then - for mybinary in $( echo "${binary}" | tr "," " " ); do - if ! which "$mybinary" >/dev/null 2>&1; then - dbdetect._wd "... Missing binary: ${mybinary}" - return 1 - fi - dbdetect._wd "... Binary: ${mybinary} was found" - done - fi - # --- check process local process; process=$( ini.value "process" ) if [ -n "${process}" ]; then @@ -150,6 +141,18 @@ function dbdetect.exists(){ fi fi + # --- check binaries + local binary; binary=$( ini.value "binary" ) + if [ -n "${binary}" ]; then + for mybinary in $( echo "${binary}" | tr "," " " ); do + if ! which "$mybinary" >/dev/null 2>&1; then + dbdetect._wd "... Missing binary: ${mybinary}" + return 1 + fi + dbdetect._wd "... Binary: ${mybinary} was found" + done + fi + # --- OK, everything was found ... we initialize it dbdetect._wd "OK, match: $_config" diff --git a/includes/dbdetect_validate_profile_ini.sh b/includes/dbdetect_validate_profile_ini.sh new file mode 100644 index 0000000..03b8902 --- /dev/null +++ b/includes/dbdetect_validate_profile_ini.sh @@ -0,0 +1,52 @@ +# ---------------------------------------------------------------------- +# +# VALIDATE PROFILE INI +# +# ---------------------------------------------------------------------- + + +# ---------------------------------------------------------------------- +# section names +# +# SYNTAX: +# comma separated list of sections +# ---------------------------------------------------------------------- + +# sections that MUST be present +sectionsMust="detect,set" + +# sections that CAN be present +sectionsCan="" + + +# ---------------------------------------------------------------------- +# variables +# +# SYNTAX: +# - one line per ini entry. +# - <section>.<variable> OR <section>.<variable>:<regex-to-match> +# The regey is applied with grep -E parameter for extended regex +# and "^<regex-to-match>$" for complete value +# ---------------------------------------------------------------------- + +varsMust="" + +varsCan=" + +detect.binary +detect.process +detect.tcp:[0-9]* +detect.tcp-target +detect.tcp-process +detect.type:sqlite +detect.file[] + +set.dbuser +set.dbpassword +set.env +set.params +set.su + +" + +# ---------------------------------------------------------------------- diff --git a/vendor/ini.class.sh b/vendor/ini.class.sh index 1bd38e1..d6fced0 100644 --- a/vendor/ini.class.sh +++ b/vendor/ini.class.sh @@ -241,4 +241,90 @@ function ini.varexport(){ done } + +# validate the ini file +function ini.validate(){ + local myinifile="$1" + local myvalidationfile="$2" + local bShowAll="${3:-0}" + + local ERROR="\e[1;31mERROR\e[0m" + local iErr; typeset -i iErr=0 + + if [ ! -f "${myvalidationfile}" ]; then + echo -e "$ERROR: Validation file '${myvalidationfile}' does not exist." + return 1 + fi + . "${myvalidationfile}" || return 1 + + if [ -n "$sectionsMust" ]; then + test $bShowAll -ne 0 && echo "--- Validate MUST sections $sectionsMust" + for section in $( tr "," " " <<< "$sectionsMust"); + do + if ini.sections "$myinifile" | grep -q "^$section$" ; then + test $bShowAll -ne 0 && echo "OK: Section $section is present." + else + echo -e "$ERROR: Section $section is not present." + iErr+=1 + fi + done + fi + + test $bShowAll -ne 0 && echo "--- Validate section names" + for section in $( ini.sections "$myinifile" ) + do + if ! grep -Fq ",${section}," <<< ",${sectionsMust},${sectionsCan},"; then + echo -e "$ERROR: unknown section name: [$section]" + iErr+=1 + else + test $bShowAll -ne 0 && echo "OK: [$section] ... checking its keys..." + # TODO: verify values + + for mustkey in $( echo "${varsMust}" | grep "^[/t ]*${section}\." | cut -f 2 -d '.' | cut -f 1 -d ':' ); do + if ini.keys "$myinifile" "$section" | grep "^${mustkey}$"; then + test $bShowAll -ne 0 && echo " OK: $section.$mustkey" + else + echo -e " $ERROR: A MUST key '$mustkey' was not found im section [$section]." + iErr+=1 + fi + done + + for mykey in $(ini.keys "$myinifile" "$section"); do + if ! echo " + ${varsMust} + ${varsCan} + " | cut -f 1 -d ':' | grep -q "^[/t ]*${section}\.${mykey}$"; then + echo -e " $ERROR: invald key name: $section.$mykey" + iErr+=1 + else + + check=$(echo " + ${varsMust} + ${varsCan} + " | grep "^[/t ]*${section}\.${mykey}[:$]" | cut -f 2 -d ':' ) + if [ -n "$check" ]; then + value="$(ini.value "$myinifile" "$section" "$mykey")" + if ! grep -Eq "^${check}$" <<< "$value" ; then + echo -e " $ERROR: key name $section.$mykey is valid but value '$value' does NOT match '$check'" + else + test $bShowAll -ne 0 && echo " OK: key name $mykey is valid and value matches '$check'" + fi + else + test $bShowAll -ne 0 && echo " OK: key name $mykey is valid" + fi + + fi + done + fi + done + + if [ $iErr -gt 0 ]; then + echo "RESULT: Errors were found for $myinifile" + else + test $bShowAll -ne 0 && echo "RESULT: OK, Ini file $myinifile looks fine." + fi + return $iErr + +} + # ---------------------------------------------------------------------- -- GitLab