]> git.ipfire.org Git - people/stevee/network.git/blobdiff - functions.ports
Add support for pptp dialin.
[people/stevee/network.git] / functions.ports
index d0d7dee62398c0ad7d6717351adb3c22966b24fc..198a08ba3694576e1102c41087c8b04971907958 100644 (file)
@@ -1,13 +1,74 @@
 #!/bin/bash
-# XXX header missing
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2010  Michael Tremer & Christian Schmidt                      #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
 
 function port_dir() {
-       echo "${CONFIG_DIR}/ports"
+       echo "${NETWORK_CONFIG_DIR}/ports"
 }
 
-function port_file() {
+function port_get_hook() {
        local port=${1}
+       assert isset port
+
+       config_get_hook $(port_file ${port})
+}
+
+function port_config_dir() {
+       local port=${1}
+
+       print "${RUN_DIR}/ports/${port}"
+       return ${EXIT_OK}
+}
+
+function port_config_read() {
+       local port=${1}
+       assert isset port
+
+       # Save the HOOK variable.
+       local hook="${HOOK}"
+
+       config_read $(port_file ${port})
+
+       # Restore hook.
+       HOOK="${hook}"
+}
+
+function port_config_write() {
+       local port=${1}
+       assert isset port
+
+       config_write $(port_file ${port})
+}
+
+function ports_get_all() {
+       local port
 
+       for port in $(port_dir)/*; do
+               [ -f "${port}" ] || continue
+
+               basename ${port}
+       done
+}
+
+function port_file() {
+       local port=${1}
        assert isset port
 
        echo "$(port_dir)/${port}"
@@ -16,7 +77,7 @@ function port_file() {
 function port_exists() {
        local port=${1}
 
-       [ -f "${CONFIG_DIR}/ports/${port}" ]
+       [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
 }
 
 function port_get_hook() {
@@ -81,10 +142,34 @@ function port_destroy() {
 
        port_exists ${port} || return ${EXIT_OK}
 
-       local attached_zone=$(port_is_attached ${port})
+       # Check if the port is attached to any zone and don't delete it.
+       local ok=${EXIT_OK}
 
+       local attached_zone=$(port_is_attached ${port})
        if [ -n "${attached_zone}" ]; then
-               error "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
+               error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
+               ok=${EXIT_ERROR}
+       fi
+
+       # Check if the port is linked to any other port and don't allow the user
+       # to delete it.
+       local other_port
+       for other_port in $(ports_get); do
+               [ "${other_port}" = "${port}" ] && continue
+
+               if listmatch ${port} $(port_get_parents ${other_port}); then
+                       error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
+                       ok=${EXIT_ERROR}
+               fi
+
+               if listmatch ${port} $(port_get_children ${other_port}); then
+                       error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
+                       ok=${EXIT_ERROR}
+               fi
+       done
+
+       # If ok says we are not okay --> exit
+       if [ ${ok} -ne ${EXIT_OK} ]; then
                return ${EXIT_ERROR}
        fi
 
@@ -118,6 +203,10 @@ function port_status() {
        port_cmd status $@
 }
 
+function port_info() {
+       port_cmd info $@
+}
+
 function port_cmd() {
        local cmd=${1}
        local port=${2}
@@ -142,3 +231,67 @@ function ports_get() {
                fi
        done
 }
+
+function port_find_free() {
+       local pattern=${1}
+
+       assert isset pattern
+
+       local port
+       local i=0
+
+       while [ ${i} -lt 99 ]; do
+               port=${pattern//N/${i}}
+               if ! port_exists ${port} && ! device_exists ${port}; then
+                       echo "${port}"
+                       return ${EXIT_OK}
+               fi
+               i=$(( ${i} + 1 ))
+       done
+
+       return ${EXIT_ERROR}
+}
+
+function port_get_info() {
+       local port=${1}
+       local key=${2}
+
+       assert isset port
+       assert port_exists ${port}
+       assert isset key
+
+       (
+               eval $(port_info ${port})
+               echo "${!key}"
+       )
+}
+
+function port_get_parents() {
+       local port=${1}
+
+       port_get_info ${port} PORT_PARENTS
+}
+
+function port_get_children() {
+       local port=${1}
+
+       port_get_info ${port} PORT_CHILDREN
+}
+
+function port_zone() {
+       # Get name of the zones, this port is configured in.
+       local port=${1}
+       shift
+
+       assert isset port
+
+       local zone
+       for zone in $(zones_get_all); do
+               if zone_has_port ${zone} ${port}; then
+                       echo "${zone}"
+                       return ${EXIT_OK}
+               fi
+       done
+
+       return ${EXIT_OK}
+}