]> git.ipfire.org Git - people/stevee/network.git/commitdiff
config hook: prevent two hooks with the same settings
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Thu, 27 Jul 2017 12:07:01 +0000 (14:07 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 27 Jul 2017 14:21:23 +0000 (16:21 +0200)
A ipv4-static config with the same IPv4 address twice is senseless.
A new function zone_config_check_same_setting is introduced.
The function provides an easy way to check if a config
of the given hook has the same value for a given key.
We can now check inside hook_new if an ipv4-static or ipv6-static config
with the same value exist and break with an error.

Fixes: #11418
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/functions/functions.zone
src/hooks/configs/ipv4-static
src/hooks/configs/ipv6-static

index a3ca029c26576b39405c48b36ec9ed4e0d0a3ba5..1eb492f5ad3aa3677e77cddc3ca5dc2177cb376e 100644 (file)
@@ -1150,6 +1150,39 @@ zone_config_get_new_id() {
        done
 }
 
+zone_config_check_same_setting() {
+       # This functions checks if a config hook
+       # with the same setting is already configured for this zone.
+       # Returns True when yes and False when no.
+
+       assert [ $# -eq 4 ]
+
+       local zone=${1}
+       local hook=${2}
+       local key=${3}
+       local value=${4}
+
+       # The key should be local for this function
+       local ${key}
+       local config
+
+       for config in $(zone_configs_list ${zone}); do
+               # Check if the config is from the given hook, when not continue
+               if  [[ $(zone_config_get_hook "${zone}" "${config}") != ${hook} ]]; then
+                       continue
+               fi
+               # Get the value of the key for a given function
+               zone_config_settings_read "${zone}" "${config}" \
+                --ignore-superfluous-settings "${key}"
+               # Check if the value of the config and the passed value are eqal
+               if [[ "${value}" == "${!key}" ]]; then
+                       return ${EXIT_TRUE}
+               fi
+       done
+
+       return ${EXIT_FALSE}
+}
+
 zone_config_get_hook() {
        assert [ $# -eq 2 ]
 
index 36629e0f5c7147b5004139a764ae52c51cc241a5..ef74991ec44de20def2a80d3956ce41481bb3029 100644 (file)
@@ -99,6 +99,11 @@ hook_parse_cmdline() {
                exit ${EXIT_CONF_ERROR}
        fi
 
+       if zone_config_check_same_setting "${zone}" "ipv4-static" "ADDRESS" "${ADDRESS}"; then
+               error "An ipv4-static config with the same IPv4 address is already configured"
+               exit ${EXIT_CONF_ERROR}
+       fi
+
        if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then
                warning "You did not configure a gateway for a non-local zone"
        fi
index 4c1d7df96b4d28a9cf3799f2554777a668a16adc..c41401cb75890343741f2b105f5d3303e50afd0d 100644 (file)
@@ -49,6 +49,11 @@ hook_parse_cmdline() {
                shift
        done
 
+       if zone_config_check_same_setting "${zone}" "ipv6-static" "ADDRESS" "${ADDRESS}"; then
+               error "An ipv6-static config with the same IPv6 address is already configured"
+               exit ${EXIT_CONF_ERROR}
+       fi
+
        # Store IPv6 address in small format.
        ADDRESS=$(ipv6_format "${ADDRESS}")