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_enable
() {
95 # This function will enable the zone
96 # with help of systemd.
99 assert zone_exists
"${zone}"
101 # Enable service for the zone
102 service_enable
"network@${zone}.service"
105 if [ ${ret} -eq ${EXIT_OK} ]; then
106 log INFO
"Auto-start enabled for zone ${zone}"
110 log ERROR
"Could not enable zone ${zone}: ${ret}"
114 function zone_disable
() {
115 # This function will disable the zone
116 # with help of systemd.
119 assert zone_exists
"${zone}"
121 # Disable service for the zone
122 service_disable
"network@${zone}.service"
125 if [ ${ret} -eq ${EXIT_OK} ]; then
126 log INFO
"Auto-start disabled for zone ${zone}"
130 log ERROR
"Could not disable zone ${zone}: ${ret}"
134 function zone_is_enabled
() {
138 # Ask systemd if the zone is enabled.
139 if service_is_enabled
"network@${zone}.service"; then
146 function zone_create
() {
151 if ! zone_name_is_valid
${zone}; then
152 error
"Zone name '${zone}' is not valid."
156 if zone_exists
${zone}; then
157 error
"Zone '${zone}' does already exist."
161 if ! hook_zone_exists
${hook}; then
162 error
"Hook '${hook}' does not exist."
166 mkdir
-p $
(zone_dir
${zone})
168 # Create directories for configs and ports
169 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
171 hook_zone_exec
${hook} create
${zone} $@
174 # Maybe the zone create hook did not exit correctly.
175 # If this is the case we remove the created zone immediately.
176 if [ "${ret}" = "${EXIT_ERROR}" ]; then
177 zone_remove_now
${zone}
181 # Automatically enable zone.
182 zone_enable
"${zone}"
185 function zone_edit
() {
189 if ! zone_exists
${zone}; then
190 error
"Zone '${zone}' does not exist."
194 # Check if the zone is tagged for removal.
195 if zone_has_remove_tag
${zone}; then
196 error
"You cannot edit a zone that is tagged for removal."
200 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
202 if [ -z "${hook}" ]; then
203 error
"Config file did not provide any hook."
207 if ! hook_zone_exists
${hook}; then
208 error
"Hook '${hook}' does not exist."
212 hook_zone_exec
${hook} edit
${zone} $@
216 function zone_remove
() {
218 assert zone_exists
${zone}
220 # Make the zone for removal.
221 touch $
(zone_dir
${zone})/.remove
223 log INFO
"Zone '${zone}' has been tagged for removal."
226 function zone_has_remove_tag
() {
228 assert zone_exists
${zone}
230 [ -e "$(zone_dir ${zone})/.remove" ]
233 # This function will remove the given zone
234 # RIGHT NOW. Use zone_remove to remove it
235 # at the next status change.
236 function zone_remove_now
() {
238 assert zone_exists
${zone}
240 log INFO
"Removing zone '${zone}' right now."
242 # Force the zone down.
243 zone_is_up
${zone} && zone_set_down
${zone}
246 zone_disable
"${zone}"
248 rm -rf $
(zone_dir
${zone})
255 if ! zone_exists
${zone}; then
256 error
"Zone '${zone}' does not exist."
260 # Check if a zone has got the remove tag.
261 if zone_has_remove_tag
${zone}; then
262 error
"Cannot bring up any zone which is to be removed."
266 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
268 if [ -z "${hook}" ]; then
269 error
"Config file did not provide any hook."
273 if ! hook_zone_exists
${hook}; then
274 error
"Hook '${hook}' does not exist."
278 zone_db
${zone} starting
280 hook_zone_exec
${hook} up
${zone} $@
282 zone_db
${zone} started
285 function zone_down
() {
289 if ! zone_exists
${zone}; then
290 error
"Zone '${zone}' does not exist."
294 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
296 if [ -z "${hook}" ]; then
297 error
"Config file did not provide any hook."
301 if ! hook_zone_exists
${hook}; then
302 error
"Hook '${hook}' does not exist."
306 zone_db
${zone} stopping
308 hook_zone_exec
${hook} down
${zone} $@
310 zone_db
${zone} stopped
312 # Remove the zone, if it has got a remove tag.
313 if zone_has_remove_tag
${zone}; then
314 zone_remove_now
${zone}
318 function zone_status
() {
322 if ! zone_exists
${zone}; then
323 error
"Zone '${zone}' does not exist."
327 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
329 if [ -z "${hook}" ]; then
330 error
"Config file did not provide any hook."
334 if ! hook_zone_exists
${hook}; then
335 error
"Hook '${hook}' does not exist."
339 hook_zone_exec
${hook} status
${zone} $@
341 # Show that the zone it to be removed soon.
342 if zone_has_remove_tag
${zone}; then
343 warning
"This zone is tagged for removal."
347 function zone_port
() {
354 assert zone_exists
${zone}
358 zone_port_
${action} ${zone} $@
361 error
"Unrecognized argument: ${action}"
362 cli_usage root-zone-port-subcommands
368 function zone_port_add
() {
377 # Check if the port actually exists.
378 if ! port_exists
"${port}"; then
379 error
"Cannot add port '${port}' which does not exist"
383 # Check if the port is already connected to this or any other zone.
385 for z
in $
(zones_get_all
); do
386 if zone_has_port
"${z}" "${port}"; then
387 error
"Port '${port}' is already assigned to zone '${z}'"
392 local hook
=$
(zone_get_hook
"${zone}")
395 hook_zone_exec
"${hook}" "port_add" "${zone}" "${port}" "$@"
398 function zone_port_edit
() {
407 # Check if the port actually exists.
408 if ! port_exists
"${port}"; then
409 error
"Port '${port}' does not exist"
413 # Check if the zone actually has this port.
414 if ! zone_has_port
"${zone}" "${port}"; then
415 error
"Port '${port}' is not attached to zone '${zone}'"
419 local hook
=$
(zone_get_hook
"${zone}")
422 hook_zone_exec
"${hook}" "port_edit" "${zone}" "${port}" "$@"
425 function zone_port_remove
() {
434 # Check if the zone actually has this port.
435 if ! zone_has_port
"${zone}" "${port}"; then
436 error
"Port '${port}' is not attached to zone '${zone}'"
440 local hook
=$
(zone_get_hook
"${zone}")
443 hook_zone_exec
"${hook}" "port_remove" "${zone}" "${port}" "$@"
446 function zone_get_ports
() {
452 for port
in $
(zone_dir
${zone})/ports
/*; do
453 port
=$
(basename ${port})
455 if port_exists
${port}; then
461 function zone_get_ports_num
() {
467 for port
in $
(zone_dir
"${zone}")/ports
/*; do
468 port
="$(basename "${port}")"
470 if port_exists
"${port}"; then
471 counter
=$
(( ${counter} + 1 ))
479 function zone_has_port
() {
480 # Check, if the given port is configured
490 [ -e "$(zone_dir ${zone})/ports/${port}" ]
493 # XXX overwritten some lines below
494 function zone_config
() {
498 if ! zone_exists
${zone}; then
499 error
"Zone '${zone}' does not exist."
503 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
505 if [ -z "${hook}" ]; then
506 error
"Config file did not provide any hook."
510 if ! hook_zone_exists
${hook}; then
511 error
"Hook '${hook}' does not exist."
515 hook_zone_exec
${hook} config
${zone} $@
518 function zone_config
() {
525 assert zone_exists
${zone}
536 zone_config_
${action} ${zone} $@
539 error
"Unrecognized argument: ${action}"
540 cli_usage root-zone-config-subcommands
546 function zone_config_option
() {
557 zone_config_read
${zone}
564 function zone_config_create
() {
570 local hook
=$
(zone_get_hook
${zone})
574 hook_zone_exec
${hook} config_create
${zone} $@
577 function zone_show
() {
581 echo " Type: $(zone_get_hook ${zone})"
585 function zones_show
() {
588 for zone
in $
(zones_get $@
); do
593 function zones_get_all
() {
595 for zone
in $
(zone_dir
)/*; do
596 zone
=$
(basename ${zone})
597 zone_exists
${zone} ||
continue
603 function zones_get_local
() {
605 for zone
in $
(zones_get_all
); do
606 zone_is_local
${zone} && echo "${zone}"
610 function zones_get_nonlocal
() {
612 for zone
in $
(zones_get_all
); do
613 zone_is_nonlocal
${zone} && echo "${zone}"
617 function zones_get
() {
623 while [ $# -gt 0 ]; do
638 if zone_name_is_valid
${1}; then
639 zones
="${zones} ${1}"
641 warning
"Unrecognized argument '${1}'"
648 if [ -n "${zones}" ]; then
650 for zone
in ${zones}; do
651 zone_exists
${zone} && echo "${zone}"
656 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
658 elif [ ${local} -eq 1 ]; then
660 elif [ ${remote} -eq 1 ]; then
665 function zone_ports_list
() {
669 for port
in $
(zone_dir
${zone})/ports
/*; do
670 [ -e "${port}" ] ||
continue
672 echo $
(basename ${port})
676 function zone_ports_cmd
() {
684 assert zone_exists
${zone}
686 local hook
=$
(zone_get_hook
${zone})
689 for port
in $
(zone_get_ports
${zone}); do
690 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
694 function zone_ports_up
() {
695 zone_ports_cmd port_up $@
698 function zone_ports_down
() {
699 zone_ports_cmd port_down $@
702 function zone_ports_status
() {
703 zone_ports_cmd port_status $@
706 function zone_configs_list
() {
710 for config
in $
(zone_dir
${zone})/configs
/*; do
711 [ -e "${config}" ] ||
continue
717 function zone_configs_cmd
() {
722 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
726 for config
in $
(zone_configs_list
${zone}); do
727 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
729 hook_zone_config_exec
${hook_zone} ${hook_config} ${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 config_status $@
745 function zone_has_ip
() {
755 starting|started|stopping|stopped
)
756 db_connection_update
${zone} ${action}
761 function zone_is_up
() {
767 function zone_is_down
() {
771 function zone_get_supported_port_hooks
() {
774 local hook
=$
(zone_get_hook
${zone})
776 hook_zone_ports_get_all
${hook}
779 function zone_get_supported_config_hooks
() {
782 local hook
=$
(zone_get_hook
${zone})
784 hook_zone_configs_get_all
${hook}
787 function zone_file
() {
792 echo "$(zone_dir ${zone})/settings"
795 function zone_config_read
() {
800 # Save the HOOK variable.
803 config_read $
(zone_file
${zone})
809 function zone_config_write
() {
814 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
817 function zone_config_set
() {
825 zone_config_read
${zone}
827 for arg
in ${args}; do
831 zone_config_write
${zone}
835 function zone_config_get
() {
843 zone_config_read
${zone}