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_start_auto
() {
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.
106 function zone_stop
() {
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"
116 function zone_reload
() {
118 assert zone_exists
"${zone}"
120 service_reload
"network@${zone}.service"
123 function zone_hotplug_event
() {
127 hotplug_assert_in_hotplug_event
129 zone_cmd
"hotplug" "${zone}"
132 function zone_enable
() {
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}"
152 function zone_disable
() {
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}"
172 function zone_is_enabled
() {
176 # Ask systemd if the zone is enabled.
177 if service_is_enabled
"network@${zone}.service"; then
184 function zone_is_active
() {
188 if service_is_active
"network@${zone}.service"; then
195 function zone_is_enabled_or_active
() {
199 zone_is_enabled
"${zone}" || zone_is_active
"${zone}"
202 function zone_cmd
() {
210 local hook
="$(zone_get_hook ${zone})"
213 hook_exec zone
"${hook}" "${cmd}" "${zone}" $@
216 function zone_new
() {
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}"
255 function zone_edit
() {
259 if ! zone_exists
${zone}; then
260 error
"Zone '${zone}' does not exist."
264 # Check if the zone is tagged for removal.
265 if zone_has_destroy_tag
${zone}; then
266 error
"You cannot edit a zone that is tagged for removal."
270 local hook
="$(zone_get_hook "${zone}")"
271 if [ -z "${hook}" ]; then
272 error
"Config file did not provide any hook."
276 if ! hook_zone_exists
${hook}; then
277 error
"Hook '${hook}' does not exist."
281 hook_zone_exec
${hook} edit
${zone} $@
285 function zone_destroy
() {
287 assert zone_exists
"${zone}"
289 # Make the zone for removal.
290 touch "$(zone_dir "${zone}")/.destroy"
292 log INFO
"Zone '${zone}' has been tagged for removal."
295 function zone_has_destroy_tag
() {
297 assert zone_exists
"${zone}"
299 [ -e "$(zone_dir "${zone}")/.destroy" ]
302 # This function will remove the given zone
303 # RIGHT NOW. Use zone_destroy to remove it
304 # at the next status change.
305 function zone_destroy_now
() {
307 assert zone_exists
"${zone}"
309 log INFO
"Removing zone '${zone}' right now."
311 # Force the zone down.
312 zone_is_up
"${zone}" && zone_set_down
"${zone}"
315 zone_disable
"${zone}"
317 rm -rf "$(zone_dir "${zone}")"
324 if ! zone_exists
${zone}; then
325 error
"Zone '${zone}' does not exist."
329 # Check if a zone has got the remove tag.
330 if zone_has_remove_tag
${zone}; then
331 error
"Cannot bring up any zone which is to be removed."
335 local hook
="$(zone_get_hook "${zone}")"
336 if [ -z "${hook}" ]; then
337 error
"Config file did not provide any hook."
341 if ! hook_zone_exists
${hook}; then
342 error
"Hook '${hook}' does not exist."
346 zone_db
${zone} starting
348 hook_zone_exec
${hook} up
${zone} $@
350 zone_db
${zone} started
353 function zone_down
() {
357 if ! zone_exists
${zone}; then
358 error
"Zone '${zone}' does not exist."
362 local hook
="$(zone_get_hook "${zone}")"
363 if [ -z "${hook}" ]; then
364 error
"Config file did not provide any hook."
368 if ! hook_zone_exists
${hook}; then
369 error
"Hook '${hook}' does not exist."
373 zone_db
${zone} stopping
375 hook_zone_exec
${hook} down
${zone} $@
377 zone_db
${zone} stopped
379 # Remove the zone, if it has got a remove tag.
380 if zone_has_destroy_tag
"${zone}"; then
381 zone_destroy_now
"${zone}"
385 function zone_status
() {
390 if ! zone_exists
"${zone}"; then
391 error
"Zone '${zone}' does not exist."
395 local hook
="$(zone_get_hook "${zone}")"
396 if [ -z "${hook}" ]; then
397 error
"Config file did not provide any hook."
401 if ! hook_zone_exists
"${hook}"; then
402 error
"Hook '${hook}' does not exist."
406 hook_zone_exec
"${hook}" "status" "${zone}" "$@"
408 # Show that the zone it to be removed soon.
409 if zone_has_remove_tag
${zone}; then
410 warning
"This zone is tagged for removal."
414 function zone_port
() {
421 assert zone_exists
${zone}
425 zone_port_
${action} ${zone} $@
428 error
"Unrecognized argument: ${action}"
429 cli_usage root-zone-port-subcommands
435 function zone_port_add
() {
444 # Check if the port actually exists.
445 if ! port_exists
"${port}"; then
446 error
"Cannot add port '${port}' which does not exist"
450 # Check if the port is already connected to this or any other zone.
452 for z
in $
(zones_get_all
); do
453 if zone_has_port
"${z}" "${port}"; then
454 error
"Port '${port}' is already assigned to zone '${z}'"
459 local hook
=$
(zone_get_hook
"${zone}")
462 hook_zone_exec
"${hook}" "port_add" "${zone}" "${port}" "$@"
465 function zone_port_edit
() {
474 # Check if the port actually exists.
475 if ! port_exists
"${port}"; then
476 error
"Port '${port}' does not exist"
480 # Check if the zone actually has this port.
481 if ! zone_has_port
"${zone}" "${port}"; then
482 error
"Port '${port}' is not attached to zone '${zone}'"
486 local hook
=$
(zone_get_hook
"${zone}")
489 hook_zone_exec
"${hook}" "port_edit" "${zone}" "${port}" "$@"
492 function zone_port_remove
() {
501 # Check if the zone actually has this port.
502 if ! zone_has_port
"${zone}" "${port}"; then
503 error
"Port '${port}' is not attached to zone '${zone}'"
507 local hook
=$
(zone_get_hook
"${zone}")
510 hook_zone_exec
"${hook}" "port_remove" "${zone}" "${port}" "$@"
513 function zone_get_ports
() {
519 for port
in $
(zone_dir
${zone})/ports
/*; do
520 port
=$
(basename ${port})
522 if port_exists
${port}; then
528 function zone_get_ports_num
() {
534 for port
in $
(zone_dir
"${zone}")/ports
/*; do
535 port
="$(basename "${port}")"
537 if port_exists
"${port}"; then
538 counter
=$
(( ${counter} + 1 ))
546 function zone_has_port
() {
547 # Check, if the given port is configured
557 [ -e "$(zone_dir ${zone})/ports/${port}" ]
560 function zone_config
() {
567 assert zone_exists
"${zone}"
571 zone_config_create
"${zone}" "$@"
574 zone_config_edit
"${zone}" "$@"
577 zone_config_remove
"${zone}" "$@"
580 error
"Unrecognized argument: ${cmd}"
581 cli_usage root-zone-config-subcommands
587 function zone_config_cmd
() {
594 local hook
="$(zone_get_hook "${zone}")"
597 hook_zone_exec
"${hook}" "config_${cmd}" "${zone}" "$@"
600 function zone_config_create
() {
601 zone_config_cmd
"create" "$@"
604 function zone_config_edit
() {
605 zone_config_cmd
"edit" "$@"
608 function zone_config_remove
() {
609 zone_config_cmd
"remove" "$@"
612 function zone_config_show
() {
613 zone_config_cmd
"show" "$@"
616 function zone_show
() {
620 echo " Type: $(zone_get_hook ${zone})"
624 function zones_show
() {
627 for zone
in $
(zones_get $@
); do
632 function zones_get_all
() {
634 for zone
in $
(zone_dir
)/*; do
635 zone
=$
(basename ${zone})
636 zone_exists
${zone} ||
continue
642 function zones_get_local
() {
644 for zone
in $
(zones_get_all
); do
645 zone_is_local
${zone} && echo "${zone}"
649 function zones_get_nonlocal
() {
651 for zone
in $
(zones_get_all
); do
652 zone_is_nonlocal
${zone} && echo "${zone}"
656 function zones_get
() {
662 while [ $# -gt 0 ]; do
677 if zone_name_is_valid
${1}; then
678 zones
="${zones} ${1}"
680 warning
"Unrecognized argument '${1}'"
687 if [ -n "${zones}" ]; then
689 for zone
in ${zones}; do
690 zone_exists
${zone} && echo "${zone}"
695 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
697 elif [ ${local} -eq 1 ]; then
699 elif [ ${remote} -eq 1 ]; then
704 function zone_ports_list
() {
708 for port
in $
(zone_dir
${zone})/ports
/*; do
709 [ -e "${port}" ] ||
continue
711 echo $
(basename ${port})
715 function zone_ports_cmd
() {
723 assert zone_exists
${zone}
725 local hook
=$
(zone_get_hook
${zone})
728 for port
in $
(zone_get_ports
${zone}); do
729 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
733 function zone_ports_create
() {
734 zone_ports_cmd
"port_create" $@
737 function zone_ports_remove
() {
738 zone_ports_cmd
"port_remove" $@
741 function zone_ports_up
() {
742 zone_ports_cmd port_up $@
745 function zone_ports_down
() {
746 zone_ports_cmd port_down $@
749 function zone_ports_status
() {
750 zone_ports_cmd port_status $@
753 function zone_configs_cmd
() {
760 assert zone_exists
"${zone}"
763 for config
in $
(zone_configs_list
"${zone}"); do
764 local config_hook
="$(zone_config_get_hook "${zone}" "${config}")"
765 assert isset config_hook
767 hook_config_exec
"${config_hook}" "${cmd}" "${zone}" "${config}" $@
771 function zone_configs_up
() {
772 zone_configs_cmd
"up" $@
775 function zone_configs_down
() {
776 zone_configs_cmd
"down" $@
779 function zone_configs_status
() {
780 zone_configs_cmd
"status" $@
783 function zone_configs_list
() {
787 for config
in $
(zone_dir
${zone})/configs
/*; do
788 [ -e "${config}" ] ||
continue
794 function zone_config_get_hook
() {
804 zone_config_settings_read
"${zone}" "${config}" \
805 --ignore-superfluous-settings HOOK
810 function zone_has_ip
() {
820 starting|started|stopping|stopped
)
821 db_connection_update
${zone} ${action}
826 function zone_is_up
() {
832 function zone_is_down
() {
836 function zone_get_supported_port_hooks
() {
839 local hook
=$
(zone_get_hook
${zone})
841 hook_zone_ports_get_all
${hook}
844 function zone_get_supported_config_hooks
() {
848 function zone_file
() {
853 echo "$(zone_dir ${zone})/settings"
856 function zone_settings_read
() {
861 # Save the HOOK variable.
864 settings_read $
(zone_file
${zone})
870 function zone_settings_write
() {
875 settings_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
878 function zone_settings_set
() {
886 zone_settings_read
${zone}
888 for arg
in ${args}; do
892 zone_settings_write
${zone}
896 function zone_settings_get
() {
904 zone_settings_read
${zone}
910 function zone_config_settings_read
() {
917 local path
="$(zone_dir "${zone}")/configs/${config}"
918 settings_read
"${path}" "$@"
921 function zone_config_settings_write
() {
928 local path
="$(zone_dir "${zone}")/configs/${config}"
929 settings_write
"${path}" "$@"
932 function zone_port_settings_read
() {
939 local path
="$(zone_dir "${zone}")/ports/${port}"
940 settings_read
"${path}" "$@"
943 function zone_port_settings_write
() {
950 local path
="$(zone_dir "${zone}")/ports/${port}"
951 settings_write
"${path}" "$@"
954 function zone_port_settings_remove
() {
960 local path
="$(zone_dir "${zone}")/ports/${port}"
961 settings_remove
"${path}"