2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
32 [ -d "$(zone_dir ${zone})" ]
39 for i
in ${VALID_ZONES}; do
40 match
="${match}|${i}[0-9]{1,5}"
43 echo "${match:1:${#match}}"
46 zone_name_is_valid
() {
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
52 [[ ${zone} =~ $
(zone_match
) ]]
58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
71 config_get_hook $
(zone_dir
${zone})/settings
75 # This function will bring up the zone
76 # 'asynchronously' with help of systemd.
79 assert zone_exists
${zone}
81 service_start
"network@${zone}.service"
86 assert zone_exists
"${zone}"
88 # If the zone has already been started, we
89 # will reload it so the current configuration
91 if zone_is_active
"${zone}"; then
95 # If the zone is still down, but in auto-start mode,
97 elif zone_is_enabled
"${zone}"; then
102 # Otherwise, nothing will be done.
107 # This function will bring down the zone
108 # 'asynchronously' with help of systemd.
111 assert zone_exists
${zone}
113 service_stop
"network@${zone}.service"
118 assert zone_exists
"${zone}"
120 service_reload
"network@${zone}.service"
123 zone_hotplug_event
() {
127 hotplug_assert_in_hotplug_event
129 zone_cmd
"hotplug" "${zone}"
133 # This function will enable the zone
134 # with help of systemd.
137 assert zone_exists
"${zone}"
139 # Enable service for the zone
140 service_enable
"network@${zone}.service"
143 if [ ${ret} -eq ${EXIT_OK} ]; then
144 log INFO
"Auto-start enabled for zone ${zone}"
148 log ERROR
"Could not enable zone ${zone}: ${ret}"
153 # This function will disable the zone
154 # with help of systemd.
157 assert zone_exists
"${zone}"
159 # Disable service for the zone
160 service_disable
"network@${zone}.service"
163 if [ ${ret} -eq ${EXIT_OK} ]; then
164 log INFO
"Auto-start disabled for zone ${zone}"
168 log ERROR
"Could not disable zone ${zone}: ${ret}"
176 # Ask systemd if the zone is enabled.
177 if service_is_enabled
"network@${zone}.service"; then
188 if service_is_active
"network@${zone}.service"; then
195 zone_is_enabled_or_active
() {
199 zone_is_enabled
"${zone}" || zone_is_active
"${zone}"
210 local hook
="$(zone_get_hook ${zone})"
213 hook_exec zone
"${hook}" "${cmd}" "${zone}" $@
221 if ! zone_name_is_valid
${zone}; then
222 error
"Zone name '${zone}' is not valid."
226 if zone_exists
${zone}; then
227 error
"Zone '${zone}' does already exist."
231 if ! hook_zone_exists
${hook}; then
232 error
"Hook '${hook}' does not exist."
236 mkdir
-p $
(zone_dir
${zone})
238 # Create directories for configs and ports
239 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
241 hook_zone_exec
"${hook}" "new" "${zone}" $@
244 # Maybe the zone new hook did not exit correctly.
245 # If this is the case we remove the created zone immediately.
246 if [ "${ret}" = "${EXIT_ERROR}" ]; then
247 zone_destroy_now
"${zone}"
251 # Automatically enable zone.
252 zone_enable
"${zone}"
254 # Bring up the zone immediately after
262 if ! zone_exists
${zone}; then
263 error
"Zone '${zone}' does not exist."
267 # Check if the zone is tagged for removal.
268 if zone_has_destroy_tag
${zone}; then
269 error
"You cannot edit a zone that is tagged for removal."
273 local hook
="$(zone_get_hook "${zone}")"
274 if [ -z "${hook}" ]; then
275 error
"Config file did not provide any hook."
279 if ! hook_zone_exists
${hook}; then
280 error
"Hook '${hook}' does not exist."
284 hook_zone_exec
${hook} edit
${zone} $@
290 assert zone_exists
"${zone}"
292 # Make the zone for removal.
293 touch "$(zone_dir "${zone}")/.destroy"
295 log INFO
"Zone '${zone}' has been tagged for removal."
298 zone_has_destroy_tag
() {
300 assert zone_exists
"${zone}"
302 [ -e "$(zone_dir "${zone}")/.destroy" ]
305 # This function will remove the given zone
306 # RIGHT NOW. Use zone_destroy to remove it
307 # at the next status change.
310 assert zone_exists
"${zone}"
312 log INFO
"Removing zone '${zone}' right now."
314 # Force the zone down.
315 zone_is_active
"${zone}" && zone_stop
"${zone}"
318 zone_disable
"${zone}"
320 rm -rf "$(zone_dir "${zone}")"
327 if ! zone_exists
${zone}; then
328 error
"Zone '${zone}' does not exist."
332 # Check if a zone has got the remove tag.
333 if zone_has_destroy_tag
${zone}; then
334 error
"Cannot bring up any zone which is to be removed."
338 local hook
="$(zone_get_hook "${zone}")"
339 if [ -z "${hook}" ]; then
340 error
"Config file did not provide any hook."
344 if ! hook_zone_exists
${hook}; then
345 error
"Hook '${hook}' does not exist."
349 zone_db
${zone} starting
351 hook_zone_exec
${hook} up
${zone} $@
353 zone_db
${zone} started
355 # Execute all triggers after the zone got up
356 triggers_execute_all
"up" ZONE
="${zone}"
363 if ! zone_exists
${zone}; then
364 error
"Zone '${zone}' does not exist."
368 local hook
="$(zone_get_hook "${zone}")"
369 if [ -z "${hook}" ]; then
370 error
"Config file did not provide any hook."
374 if ! hook_zone_exists
${hook}; then
375 error
"Hook '${hook}' does not exist."
379 zone_db
${zone} stopping
381 hook_zone_exec
${hook} down
${zone} $@
383 zone_db
${zone} stopped
385 # Execute all triggers after the zone went down
386 triggers_execute_all
"down" ZONE
="${zone}"
388 # Remove the zone, if it has got a remove tag.
389 if zone_has_destroy_tag
"${zone}"; then
390 zone_destroy_now
"${zone}"
399 if ! zone_exists
"${zone}"; then
400 error
"Zone '${zone}' does not exist."
404 local hook
="$(zone_get_hook "${zone}")"
405 if [ -z "${hook}" ]; then
406 error
"Config file did not provide any hook."
410 if ! hook_zone_exists
"${hook}"; then
411 error
"Hook '${hook}' does not exist."
415 hook_zone_exec
"${hook}" "status" "${zone}" "$@"
417 # Show that the zone it to be removed soon.
418 if zone_has_destroy_tag
${zone}; then
419 warning
"This zone is tagged for removal."
429 for port
in $
(zone_dir
${zone})/ports
/*; do
430 port
=$
(basename ${port})
432 if port_exists
${port}; then
438 zone_get_ports_num
() {
444 for port
in $
(zone_dir
"${zone}")/ports
/*; do
445 port
="$(basename "${port}")"
447 if port_exists
"${port}"; then
448 counter
=$
(( ${counter} + 1 ))
457 # Check, if the given port is configured
467 [ -e "$(zone_dir ${zone})/ports/${port}" ]
477 assert zone_exists
"${zone}"
481 zone_config_new
"${zone}" "$@"
484 zone_config_destroy
"${zone}" "$@"
487 zone_config_edit
"${zone}" "$@"
490 error
"Unrecognized argument: ${cmd}"
491 cli_usage root-zone-config-subcommands
504 local hook
="$(zone_get_hook "${zone}")"
507 hook_zone_exec
"${hook}" "config_${cmd}" "${zone}" "$@"
514 # Create a new configuration, but exit when that was
516 zone_config_cmd
"new" "${zone}" "$@" ||
return ${?}
518 # If the config could be created, we will try to bring
519 # it up if the zone is up, too.
520 if zone_is_up
"${zone}"; then
521 zone_configs_up
"${zone}"
525 zone_config_destroy
() {
526 zone_config_cmd
"destroy" "$@"
530 zone_config_cmd
"edit" "$@"
534 zone_config_cmd
"show" "$@"
541 echo " Type: $(zone_get_hook ${zone})"
548 for zone
in $
(zones_get $@
); do
555 for zone
in $
(zone_dir
)/*; do
556 zone
=$
(basename ${zone})
557 zone_exists
${zone} ||
continue
565 for zone
in $
(zones_get_all
); do
566 zone_is_local
${zone} && echo "${zone}"
570 zones_get_nonlocal
() {
572 for zone
in $
(zones_get_all
); do
573 zone_is_nonlocal
${zone} && echo "${zone}"
583 while [ $# -gt 0 ]; do
598 if zone_name_is_valid
${1}; then
599 zones
="${zones} ${1}"
601 warning
"Unrecognized argument '${1}'"
608 if [ -n "${zones}" ]; then
610 for zone
in ${zones}; do
611 zone_exists
${zone} && echo "${zone}"
616 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
618 elif [ ${local} -eq 1 ]; then
620 elif [ ${remote} -eq 1 ]; then
629 for port
in $
(zone_dir
${zone})/ports
/*; do
630 [ -e "${port}" ] ||
continue
632 echo $
(basename ${port})
645 # Check if the port actually exists.
646 if ! port_exists
"${port}"; then
647 error
"Cannot attach port '${port}' which does not exist"
651 # Check if the port is already connected to this or any other zone.
653 for z
in $
(zones_get_all
); do
654 if zone_has_port
"${z}" "${port}"; then
655 error
"Port '${port}' is already attached to zone '${z}'"
660 local hook
="$(zone_get_hook "${zone}")"
663 hook_zone_exec
"${hook}" "port_attach" "${zone}" "${port}" "$@"
668 log INFO
"${port} has been attached to ${zone}"
670 # Automatically connect the port
671 zone_port_start
"${zone}" "${port}"
674 log CRITICAL
"${port} could not be attached to ${zone}"
690 # Check if the port actually exists.
691 if ! port_exists
"${port}"; then
692 error
"Port '${port}' does not exist"
696 # Check if the zone actually has this port.
697 if ! zone_has_port
"${zone}" "${port}"; then
698 error
"Port '${port}' is not attached to zone '${zone}'"
702 local hook
=$
(zone_get_hook
"${zone}")
705 hook_zone_exec
"${hook}" "port_edit" "${zone}" "${port}" "$@"
717 # Check if the zone actually has this port.
718 if ! zone_has_port
"${zone}" "${port}"; then
719 error
"Port '${port}' is not attached to zone '${zone}'"
723 local hook
=$
(zone_get_hook
"${zone}")
726 hook_zone_exec
"${hook}" "port_detach" "${zone}" "${port}" "$@"
731 log INFO
"${port} has been detached from ${zone}"
733 # Bring down the port if needed
734 zone_port_stop
"${zone}" "${port}"
737 log CRITICAL
"${port} could not be detached from ${zone}"
756 local hook
="$(zone_get_hook "${zone}")"
759 # Dispatch command to hook
760 hook_zone_exec
"${hook}" "${cmd}" "${zone}" "${port}" $@
764 zone_port_cmd
"port_create" $@
768 zone_port_cmd
"port_remove" $@
772 zone_port_cmd
"port_up" $@
776 zone_port_cmd
"port_down" $@
779 # The next two functions automagically bring up and down
780 # port that are attached to a bridge or similar.
781 # The problem that is tried to overcome here is that there
782 # are ports which exist all the time (like ethernet ports)
783 # and therefore do not dispatch a hotplug event when
784 # port_create is called.
790 if zone_is_active
"${zone}"; then
791 if device_exists
"${port}"; then
792 zone_port_up
"${zone}" "${port}"
795 zone_port_create
"${zone}" "${port}"
807 # Shut down the port if necessary
808 if zone_is_active
"${zone}" && port_is_up
"${port}"; then
809 zone_port_down
"${zone}" "${port}"
813 zone_port_remove
"${zone}" "${port}"
817 zone_port_cmd
"port_status" $@
829 local hook
="$(zone_get_hook "${zone}")"
832 for port
in $
(zone_get_ports
${zone}); do
833 hook_zone_exec
"${hook}" "${cmd}" "${zone}" "${port}" $@
837 zone_ports_create
() {
838 zone_ports_cmd
"port_create" $@
841 zone_ports_remove
() {
842 zone_ports_cmd
"port_remove" $@
846 zone_ports_cmd
"port_up" $@
850 zone_ports_cmd
"port_down" $@
853 zone_ports_status
() {
854 zone_ports_cmd
"port_status" $@
864 assert zone_exists
"${zone}"
867 for config
in $
(zone_configs_list
"${zone}"); do
868 local config_hook
="$(zone_config_get_hook "${zone}" "${config}")"
869 assert isset config_hook
871 hook_config_exec
"${config_hook}" "${cmd}" "${zone}" "${config}" $@
876 zone_configs_cmd
"up" $@
879 zone_configs_down
() {
880 zone_configs_cmd
"down" $@
883 zone_configs_status
() {
884 zone_configs_cmd
"status" $@
887 zone_configs_list
() {
891 for config
in $
(zone_dir
${zone})/configs
/*; do
892 [ -e "${config}" ] ||
continue
898 zone_config_get_hook
() {
908 zone_config_settings_read
"${zone}" "${config}" \
909 --ignore-superfluous-settings HOOK
924 starting|started|stopping|stopped
)
925 db_connection_update
${zone} ${action}
940 zone_get_supported_port_hooks
() {
943 local hook
=$
(zone_get_hook
${zone})
945 hook_zone_ports_get_all
${hook}
948 zone_get_supported_config_hooks
() {
957 echo "$(zone_dir ${zone})/settings"
960 zone_settings_read
() {
966 if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS}" ]; then
967 list_append args
${HOOK_SETTINGS}
972 # Save the HOOK variable.
975 settings_read
"$(zone_file "${zone}")" ${args}
981 zone_settings_write
() {
986 if function_exists
"hook_check_settings"; then
987 list_append args
"--check=\"hook_check_settings\""
989 list_append args
${HOOK_SETTINGS}
991 settings_write
"$(zone_file ${zone})" ${args}
994 zone_settings_set
() {
1002 zone_settings_read
${zone}
1004 for arg
in ${args}; do
1008 zone_settings_write
${zone}
1012 zone_settings_get
() {
1020 zone_settings_read
"${zone}" "${key}" \
1021 --ignore-superfluous-settings
1027 zone_config_settings_read
() {
1035 if [ $# -eq 0 ] && [ -n "${HOOK_CONFIG_SETTINGS}" ]; then
1036 list_append args
${HOOK_CONFIG_SETTINGS}
1041 local path
="$(zone_dir "${zone}")/configs/${config}"
1042 settings_read
"${path}" ${args}
1045 zone_config_settings_write
() {
1053 if function_exists
"hook_check_config_settings"; then
1054 list_append args
"--check=\"hook_check_config_settings\""
1056 list_append args
${HOOK_CONFIG_SETTINGS}
1058 local path
="$(zone_dir "${zone}")/configs/${config}"
1059 settings_write
"${path}" ${args}
1062 zone_port_settings_read
() {
1070 if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then
1071 list_append args
${HOOK_PORT_SETTINGS}
1076 local path
="$(zone_dir "${zone}")/ports/${port}"
1077 settings_read
"${path}" ${args}
1080 zone_port_settings_write
() {
1088 if function_exists
"hook_check_port_settings"; then
1089 list_append args
"--check=\"hook_check_port_settings\""
1091 list_append args
${HOOK_PORT_SETTINGS}
1093 local path
="$(zone_dir "${zone}")/ports/${port}"
1094 settings_write
"${path}" ${args}
1097 zone_port_settings_remove
() {
1103 local path
="$(zone_dir "${zone}")/ports/${port}"
1104 settings_remove
"${path}"