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}"
28 function zone_exists
() {
32 [ -d "$(zone_dir ${zone})" ]
35 function zone_match
() {
39 for i
in ${VALID_ZONES}; do
40 match
="${match}|${i}[0-9]{1,5}"
43 echo "${match:1:${#match}}"
46 function zone_name_is_valid
() {
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
52 [[ ${zone} =~ $
(zone_match
) ]]
55 function zone_is_local
() {
58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
61 function zone_is_nonlocal
() {
64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
67 function zone_get_hook
() {
71 config_get_hook $
(zone_dir
${zone})/settings
74 function zone_start
() {
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"
84 function zone_stop
() {
85 # This function will bring down the zone
86 # 'asynchronously' with help of systemd.
89 assert zone_exists
${zone}
91 service_stop
"network@${zone}.service"
94 function zone_reload
() {
96 assert zone_exists
"${zone}"
98 service_reload
"network@${zone}.service"
101 function zone_hotplug_event
() {
103 assert zone_exists
"${zone}"
105 # If the zone has already been started, we
106 # will reload it so the current configuration
108 if zone_is_active
"${zone}"; then
109 zone_reload
"${zone}"
112 # If the zone is still down, but in auto-start mode,
114 elif zone_is_enabled
"${zone}"; then
119 # Otherwise, nothing will be done.
123 function zone_enable
() {
124 # This function will enable the zone
125 # with help of systemd.
128 assert zone_exists
"${zone}"
130 # Enable service for the zone
131 service_enable
"network@${zone}.service"
134 if [ ${ret} -eq ${EXIT_OK} ]; then
135 log INFO
"Auto-start enabled for zone ${zone}"
139 log ERROR
"Could not enable zone ${zone}: ${ret}"
143 function zone_disable
() {
144 # This function will disable the zone
145 # with help of systemd.
148 assert zone_exists
"${zone}"
150 # Disable service for the zone
151 service_disable
"network@${zone}.service"
154 if [ ${ret} -eq ${EXIT_OK} ]; then
155 log INFO
"Auto-start disabled for zone ${zone}"
159 log ERROR
"Could not disable zone ${zone}: ${ret}"
163 function zone_is_enabled
() {
167 # Ask systemd if the zone is enabled.
168 if service_is_enabled
"network@${zone}.service"; then
175 function zone_is_active
() {
179 if service_is_active
"network@${zone}.service"; then
186 function zone_create
() {
191 if ! zone_name_is_valid
${zone}; then
192 error
"Zone name '${zone}' is not valid."
196 if zone_exists
${zone}; then
197 error
"Zone '${zone}' does already exist."
201 if ! hook_zone_exists
${hook}; then
202 error
"Hook '${hook}' does not exist."
206 mkdir
-p $
(zone_dir
${zone})
208 # Create directories for configs and ports
209 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
211 hook_zone_exec
${hook} create
${zone} $@
214 # Maybe the zone create hook did not exit correctly.
215 # If this is the case we remove the created zone immediately.
216 if [ "${ret}" = "${EXIT_ERROR}" ]; then
217 zone_remove_now
${zone}
221 # Automatically enable zone.
222 zone_enable
"${zone}"
225 function zone_edit
() {
229 if ! zone_exists
${zone}; then
230 error
"Zone '${zone}' does not exist."
234 # Check if the zone is tagged for removal.
235 if zone_has_remove_tag
${zone}; then
236 error
"You cannot edit a zone that is tagged for removal."
240 local hook
="$(zone_get_hook "${zone}")"
241 if [ -z "${hook}" ]; then
242 error
"Config file did not provide any hook."
246 if ! hook_zone_exists
${hook}; then
247 error
"Hook '${hook}' does not exist."
251 hook_zone_exec
${hook} edit
${zone} $@
255 function zone_remove
() {
257 assert zone_exists
${zone}
259 # Make the zone for removal.
260 touch $
(zone_dir
${zone})/.remove
262 log INFO
"Zone '${zone}' has been tagged for removal."
265 function zone_has_remove_tag
() {
267 assert zone_exists
${zone}
269 [ -e "$(zone_dir ${zone})/.remove" ]
272 # This function will remove the given zone
273 # RIGHT NOW. Use zone_remove to remove it
274 # at the next status change.
275 function zone_remove_now
() {
277 assert zone_exists
${zone}
279 log INFO
"Removing zone '${zone}' right now."
281 # Force the zone down.
282 zone_is_up
${zone} && zone_set_down
${zone}
285 zone_disable
"${zone}"
287 rm -rf $
(zone_dir
${zone})
294 if ! zone_exists
${zone}; then
295 error
"Zone '${zone}' does not exist."
299 # Check if a zone has got the remove tag.
300 if zone_has_remove_tag
${zone}; then
301 error
"Cannot bring up any zone which is to be removed."
305 local hook
="$(zone_get_hook "${zone}")"
306 if [ -z "${hook}" ]; then
307 error
"Config file did not provide any hook."
311 if ! hook_zone_exists
${hook}; then
312 error
"Hook '${hook}' does not exist."
316 zone_db
${zone} starting
318 hook_zone_exec
${hook} up
${zone} $@
320 zone_db
${zone} started
323 function zone_down
() {
327 if ! zone_exists
${zone}; then
328 error
"Zone '${zone}' does not exist."
332 local hook
="$(zone_get_hook "${zone}")"
333 if [ -z "${hook}" ]; then
334 error
"Config file did not provide any hook."
338 if ! hook_zone_exists
${hook}; then
339 error
"Hook '${hook}' does not exist."
343 zone_db
${zone} stopping
345 hook_zone_exec
${hook} down
${zone} $@
347 zone_db
${zone} stopped
349 # Remove the zone, if it has got a remove tag.
350 if zone_has_remove_tag
${zone}; then
351 zone_remove_now
${zone}
355 function zone_status
() {
360 if ! zone_exists
"${zone}"; then
361 error
"Zone '${zone}' does not exist."
365 local hook
="$(zone_get_hook "${zone}")"
366 if [ -z "${hook}" ]; then
367 error
"Config file did not provide any hook."
371 if ! hook_zone_exists
"${hook}"; then
372 error
"Hook '${hook}' does not exist."
376 hook_zone_exec
"${hook}" "status" "${zone}" "$@"
378 # Show that the zone it to be removed soon.
379 if zone_has_remove_tag
${zone}; then
380 warning
"This zone is tagged for removal."
384 function zone_port
() {
391 assert zone_exists
${zone}
395 zone_port_
${action} ${zone} $@
398 error
"Unrecognized argument: ${action}"
399 cli_usage root-zone-port-subcommands
405 function zone_port_add
() {
414 # Check if the port actually exists.
415 if ! port_exists
"${port}"; then
416 error
"Cannot add port '${port}' which does not exist"
420 # Check if the port is already connected to this or any other zone.
422 for z
in $
(zones_get_all
); do
423 if zone_has_port
"${z}" "${port}"; then
424 error
"Port '${port}' is already assigned to zone '${z}'"
429 local hook
=$
(zone_get_hook
"${zone}")
432 hook_zone_exec
"${hook}" "port_add" "${zone}" "${port}" "$@"
435 function zone_port_edit
() {
444 # Check if the port actually exists.
445 if ! port_exists
"${port}"; then
446 error
"Port '${port}' does not exist"
450 # Check if the zone actually has this port.
451 if ! zone_has_port
"${zone}" "${port}"; then
452 error
"Port '${port}' is not attached to zone '${zone}'"
456 local hook
=$
(zone_get_hook
"${zone}")
459 hook_zone_exec
"${hook}" "port_edit" "${zone}" "${port}" "$@"
462 function zone_port_remove
() {
471 # Check if the zone actually has this port.
472 if ! zone_has_port
"${zone}" "${port}"; then
473 error
"Port '${port}' is not attached to zone '${zone}'"
477 local hook
=$
(zone_get_hook
"${zone}")
480 hook_zone_exec
"${hook}" "port_remove" "${zone}" "${port}" "$@"
483 function zone_get_ports
() {
489 for port
in $
(zone_dir
${zone})/ports
/*; do
490 port
=$
(basename ${port})
492 if port_exists
${port}; then
498 function zone_get_ports_num
() {
504 for port
in $
(zone_dir
"${zone}")/ports
/*; do
505 port
="$(basename "${port}")"
507 if port_exists
"${port}"; then
508 counter
=$
(( ${counter} + 1 ))
516 function zone_has_port
() {
517 # Check, if the given port is configured
527 [ -e "$(zone_dir ${zone})/ports/${port}" ]
530 function zone_config
() {
537 assert zone_exists
"${zone}"
541 zone_config_create
"${zone}" "$@"
544 zone_config_edit
"${zone}" "$@"
547 zone_config_remove
"${zone}" "$@"
550 error
"Unrecognized argument: ${cmd}"
551 cli_usage root-zone-config-subcommands
557 function zone_config_cmd
() {
564 local hook
="$(zone_get_hook "${zone}")"
567 hook_zone_exec
"${hook}" "config_${cmd}" "${zone}" "$@"
570 function zone_config_create
() {
571 zone_config_cmd
"create" "$@"
574 function zone_config_edit
() {
575 zone_config_cmd
"edit" "$@"
578 function zone_config_remove
() {
579 zone_config_cmd
"remove" "$@"
582 function zone_config_show
() {
583 zone_config_cmd
"show" "$@"
586 function zone_show
() {
590 echo " Type: $(zone_get_hook ${zone})"
594 function zones_show
() {
597 for zone
in $
(zones_get $@
); do
602 function zones_get_all
() {
604 for zone
in $
(zone_dir
)/*; do
605 zone
=$
(basename ${zone})
606 zone_exists
${zone} ||
continue
612 function zones_get_local
() {
614 for zone
in $
(zones_get_all
); do
615 zone_is_local
${zone} && echo "${zone}"
619 function zones_get_nonlocal
() {
621 for zone
in $
(zones_get_all
); do
622 zone_is_nonlocal
${zone} && echo "${zone}"
626 function zones_get
() {
632 while [ $# -gt 0 ]; do
647 if zone_name_is_valid
${1}; then
648 zones
="${zones} ${1}"
650 warning
"Unrecognized argument '${1}'"
657 if [ -n "${zones}" ]; then
659 for zone
in ${zones}; do
660 zone_exists
${zone} && echo "${zone}"
665 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
667 elif [ ${local} -eq 1 ]; then
669 elif [ ${remote} -eq 1 ]; then
674 function zone_ports_list
() {
678 for port
in $
(zone_dir
${zone})/ports
/*; do
679 [ -e "${port}" ] ||
continue
681 echo $
(basename ${port})
685 function zone_ports_cmd
() {
693 assert zone_exists
${zone}
695 local hook
=$
(zone_get_hook
${zone})
698 for port
in $
(zone_get_ports
${zone}); do
699 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
703 function zone_ports_up
() {
704 zone_ports_cmd port_up $@
707 function zone_ports_down
() {
708 zone_ports_cmd port_down $@
711 function zone_ports_status
() {
712 zone_ports_cmd port_status $@
715 function zone_configs_cmd
() {
722 assert zone_exists
"${zone}"
725 for config
in $
(zone_configs_list
"${zone}"); do
726 local config_hook
="$(zone_config_get_hook "${zone}" "${config}")"
727 assert isset config_hook
729 hook_config_exec
"${config_hook}" "${cmd}" "${zone}" "${config}" $@
733 function zone_configs_up
() {
734 zone_configs_cmd
"up" $@
737 function zone_configs_down
() {
738 zone_configs_cmd
"down" $@
741 function zone_configs_status
() {
742 zone_configs_cmd
"status" $@
745 function zone_configs_list
() {
749 for config
in $
(zone_dir
${zone})/configs
/*; do
750 [ -e "${config}" ] ||
continue
756 function zone_config_get_hook
() {
766 zone_config_settings_read
"${zone}" "${config}" \
767 --ignore-superfluous-settings HOOK
772 function zone_has_ip
() {
782 starting|started|stopping|stopped
)
783 db_connection_update
${zone} ${action}
788 function zone_is_up
() {
794 function zone_is_down
() {
798 function zone_get_supported_port_hooks
() {
801 local hook
=$
(zone_get_hook
${zone})
803 hook_zone_ports_get_all
${hook}
806 function zone_get_supported_config_hooks
() {
810 function zone_file
() {
815 echo "$(zone_dir ${zone})/settings"
818 function zone_settings_read
() {
823 # Save the HOOK variable.
826 settings_read $
(zone_file
${zone})
832 function zone_settings_write
() {
837 settings_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
840 function zone_settings_set
() {
848 zone_settings_read
${zone}
850 for arg
in ${args}; do
854 zone_settings_write
${zone}
858 function zone_settings_get
() {
866 zone_settings_read
${zone}
872 function zone_config_settings_read
() {
879 local path
="$(zone_dir "${zone}")/configs/${config}"
880 settings_read
"${path}" "$@"
883 function zone_config_settings_write
() {
890 local path
="$(zone_dir "${zone}")/configs/${config}"
891 settings_write
"${path}" "$@"
894 function zone_port_settings_read
() {
901 local path
="$(zone_dir "${zone}")/ports/${port}"
902 settings_read
"${path}" "$@"
905 function zone_port_settings_write
() {
912 local path
="$(zone_dir "${zone}")/ports/${port}"
913 settings_write
"${path}" "$@"
916 function zone_port_settings_remove
() {
922 local path
="$(zone_dir "${zone}")/ports/${port}"
923 settings_remove
"${path}"