]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/hooks/configs/ipv4-static
network fix parameter passing when using ""
[people/stevee/network.git] / src / hooks / configs / ipv4-static
index f8a21b83dac299c31673907c28c2f31554608945..7aea0b978ca95b0fab06e27b6b64add5b07469e3 100644 (file)
@@ -23,9 +23,9 @@
 
 HOOK_MANPAGE="network-config-ipv4-static"
 
-HOOK_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
+HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"
 
-hook_check() {
+hook_check_config_settings() {
        assert isset ADDRESS
        assert isinteger PREFIX
 
@@ -35,12 +35,9 @@ hook_check() {
        fi
 }
 
-hook_create() {
-       local zone="${1}"
-       assert zone_exists "${zone}"
-       shift
-
+hook_parse_cmdline() {
        local arg
+
        while read -r arg; do
                local key="$(cli_get_key "${arg}")"
                local val="$(cli_get_val "${arg}")"
@@ -90,7 +87,7 @@ hook_create() {
                                exit ${EXIT_CONF_ERROR}
                                ;;
                esac
-       done <<< "$(args $@)"
+       done <<< "$(args "$@")"
 
        if ! isset ADDRESS; then
                error "You need to provide an IPv4 address"
@@ -102,12 +99,28 @@ hook_create() {
                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
+}
 
-       # XXX maybe we can add some hashing to identify a configuration again
-       zone_config_settings_write "${zone}" "${HOOK}.$(uuid)" ${HOOK_SETTINGS}
+hook_new() {
+       local zone="${1}"
+       shift
+
+       assert zone_exists "${zone}"
+
+       if ! hook_parse_cmdline "$@"; then
+               # Return an error if the parsing of the cmd line fails
+               return ${EXIT_ERROR}
+       fi
+
+       zone_config_settings_write "${zone}" "${HOOK}"
 
        exit ${EXIT_OK}
 }
@@ -122,15 +135,15 @@ hook_up() {
                exit ${EXIT_ERROR}
        fi
 
-       zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
+       zone_config_settings_read "${zone}" "${config}"
 
        ip_address_add ${zone} ${ADDRESS}/${PREFIX}
 
        # Save configuration
-       routing_db_set ${zone} ipv4 type "${HOOK}"
-       routing_db_set ${zone} ipv4 local-ip-address "${ADDRESS}/${PREFIX}"
-       routing_db_set ${zone} ipv4 remote-ip-address "${GATEWAY}"
-       routing_db_set ${zone} ipv4 active 1
+       db_set "${zone}/ipv4/type" "${HOOK}"
+       db_set "${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}"
+       db_set "${zone}/ipv4/remote-ip-address" "${GATEWAY}"
+       db_set "${zone}/ipv4/active" 1
 
        routing_update ${zone} ipv4
        routing_default_update
@@ -147,8 +160,11 @@ hook_down() {
                error "Zone '${zone}' doesn't exist."
                exit ${EXIT_ERROR}
        fi
-       
-       zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
+
+       # Remove routing information from database.
+       db_delete "${zone}/ipv4"
+
+       zone_config_settings_read "${zone}" "${config}"
 
        ip_address_del ${zone} ${ADDRESS}/${PREFIX}
 
@@ -172,7 +188,7 @@ hook_status() {
                exit ${EXIT_ERROR}
        fi
 
-       zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
+       zone_config_settings_read "${zone}" "${config}"
 
        local status
        if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
@@ -190,4 +206,3 @@ hook_status() {
 
        exit ${EXIT_OK}
 }
-