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
() {
33 [ -d "$(zone_dir ${zone})" ]
36 function zone_match
() {
40 for i
in ${VALID_ZONES}; do
41 match
="${match}|${i}[0-9]{1,5}"
44 echo "${match:1:${#match}}"
47 function zone_name_is_valid
() {
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
() {
72 config_get_hook $
(zone_dir
${zone})/settings
75 function zone_start
() {
76 # This function will bring up the zone
77 # 'asynchronously' with help of systemd.
80 assert zone_exists
${zone}
82 service_start
"network@${zone}"
85 function zone_stop
() {
86 # This function will bring down the zone
87 # 'asynchronously' with help of systemd.
90 assert zone_exists
${zone}
92 service_stop
"network@${zone}"
95 function zone_create
() {
100 if ! zone_name_is_valid
${zone}; then
101 error
"Zone name '${zone}' is not valid."
105 if zone_exists
${zone}; then
106 error
"Zone '${zone}' does already exist."
110 if ! hook_zone_exists
${hook}; then
111 error
"Hook '${hook}' does not exist."
115 mkdir
-p $
(zone_dir
${zone})
117 # Create directories for configs and ports
118 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
120 hook_zone_exec
${hook} create
${zone} $@
123 # Maybe the zone create hook did not exit correctly.
124 # If this is the case we remove the created zone immediately.
125 if [ "${ret}" = "${EXIT_ERROR}" ]; then
126 zone_remove_now
${zone}
130 function zone_edit
() {
134 if ! zone_exists
${zone}; then
135 error
"Zone '${zone}' does not exist."
139 # Check if the zone is tagged for removal.
140 if zone_has_remove_tag
${zone}; then
141 error
"You cannot edit a zone that is tagged for removal."
145 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
147 if [ -z "${hook}" ]; then
148 error
"Config file did not provide any hook."
152 if ! hook_zone_exists
${hook}; then
153 error
"Hook '${hook}' does not exist."
157 hook_zone_exec
${hook} edit
${zone} $@
161 function zone_remove
() {
163 assert zone_exists
${zone}
165 # Make the zone for removal.
166 touch $
(zone_dir
${zone})/.remove
168 log INFO
"Zone '${zone}' has been tagged for removal."
171 function zone_has_remove_tag
() {
173 assert zone_exists
${zone}
175 [ -e "$(zone_dir ${zone})/.remove" ]
178 # This function will remove the given zone
179 # RIGHT NOW. Use zone_remove to remove it
180 # at the next status change.
181 function zone_remove_now
() {
183 assert zone_exists
${zone}
185 log INFO
"Removing zone '${zone}' right now."
187 # Force the zone down.
188 zone_is_up
${zone} && zone_set_down
${zone}
190 rm -rf $
(zone_dir
${zone})
197 if ! zone_exists
${zone}; then
198 error
"Zone '${zone}' does not exist."
202 # Check if a zone has got the remove tag.
203 if zone_has_remove_tag
${zone}; then
204 error
"Cannot bring up any zone which is to be removed."
208 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
210 if [ -z "${hook}" ]; then
211 error
"Config file did not provide any hook."
215 if ! hook_zone_exists
${hook}; then
216 error
"Hook '${hook}' does not exist."
220 zone_db
${zone} starting
222 hook_zone_exec
${hook} up
${zone} $@
224 zone_db
${zone} started
227 function zone_down
() {
231 if ! zone_exists
${zone}; then
232 error
"Zone '${zone}' does not exist."
236 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
238 if [ -z "${hook}" ]; then
239 error
"Config file did not provide any hook."
243 if ! hook_zone_exists
${hook}; then
244 error
"Hook '${hook}' does not exist."
248 zone_db
${zone} stopping
250 hook_zone_exec
${hook} down
${zone} $@
252 zone_db
${zone} stopped
254 # Remove the zone, if it has got a remove tag.
255 if zone_has_remove_tag
${zone}; then
256 zone_remove_now
${zone}
260 function zone_status
() {
264 if ! zone_exists
${zone}; then
265 error
"Zone '${zone}' does not exist."
269 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
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} status
${zone} $@
283 # Show that the zone it to be removed soon.
284 if zone_has_remove_tag
${zone}; then
285 warning
"This zone is tagged for removal."
289 function zone_port
() {
296 assert zone_exists
${zone}
307 zone_port_
${action} ${zone} $@
310 error
"Unrecognized argument: ${action}"
311 cli_usage root-zone-port-subcommands
317 function zone_port_add
() {
323 local hook
=$
(zone_get_hook
${zone})
327 hook_zone_exec
${hook} port_add
${zone} $@
330 function zone_port_edit
() {
331 zone_port_cmd edit $@
334 function zone_port_rem
() {
338 function zone_port_cmd
() {
347 local hook_zone
=$
(zone_get_hook
${zone})
348 local hook_port
=$
(port_get_hook
${port})
350 assert isset hook_zone
351 assert isset hook_port
353 assert hook_zone_port_exists
${hook_zone} ${hook_port}
355 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
358 function zone_port_up
() {
362 function zone_port_down
() {
363 zone_port_cmd down $@
366 function zone_get_ports
() {
372 for port
in $
(zone_dir
${zone})/ports
/*; do
373 port
=$
(basename ${port})
375 if port_exists
${port}; then
381 function zone_has_port
() {
382 # Check, if the given port is configured
392 [ -e "$(zone_dir ${zone})/ports/${port}" ]
395 # XXX overwritten some lines below
396 function zone_config
() {
400 if ! zone_exists
${zone}; then
401 error
"Zone '${zone}' does not exist."
405 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
407 if [ -z "${hook}" ]; then
408 error
"Config file did not provide any hook."
412 if ! hook_zone_exists
${hook}; then
413 error
"Hook '${hook}' does not exist."
417 hook_zone_exec
${hook} config
${zone} $@
420 function zone_config
() {
427 assert zone_exists
${zone}
438 zone_config_
${action} ${zone} $@
441 error
"Unrecognized argument: ${action}"
442 cli_usage root-zone-config-subcommands
448 function zone_config_option
() {
459 zone_config_read
${zone}
466 function zone_config_create
() {
472 local hook
=$
(zone_get_hook
${zone})
476 hook_zone_exec
${hook} config_create
${zone} $@
479 function zone_show
() {
483 echo " Type: $(zone_get_hook ${zone})"
487 function zones_show
() {
490 for zone
in $
(zones_get $@
); do
495 function zones_get_all
() {
497 for zone
in $
(zone_dir
)/*; do
498 zone
=$
(basename ${zone})
499 zone_exists
${zone} ||
continue
505 function zones_get_local
() {
507 for zone
in $
(zones_get_all
); do
508 zone_is_local
${zone} && echo "${zone}"
512 function zones_get_nonlocal
() {
514 for zone
in $
(zones_get_all
); do
515 zone_is_nonlocal
${zone} && echo "${zone}"
519 function zones_get
() {
525 while [ $# -gt 0 ]; do
540 if zone_name_is_valid
${1}; then
541 zones
="${zones} ${1}"
543 warning
"Unrecognized argument '${1}'"
550 if [ -n "${zones}" ]; then
552 for zone
in ${zones}; do
553 zone_exists
${zone} && echo "${zone}"
558 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
560 elif [ ${local} -eq 1 ]; then
562 elif [ ${remote} -eq 1 ]; then
567 function zone_ports_list
() {
571 for port
in $
(zone_dir
${zone})/ports
/*; do
572 [ -e "${port}" ] ||
continue
574 echo $
(basename ${port})
578 function zone_ports_cmd
() {
586 assert zone_exists
${zone}
588 local hook
=$
(zone_get_hook
${zone})
591 for port
in $
(zone_get_ports
${zone}); do
592 #zone_port_cmd ${cmd} ${zone} ${port} $@
593 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
597 function zone_ports_up
() {
598 zone_ports_cmd port_up $@
601 function zone_ports_down
() {
602 zone_ports_cmd port_down $@
605 function zone_ports_status
() {
606 zone_ports_cmd port_status $@
609 function zone_configs_list
() {
613 for config
in $
(zone_dir
${zone})/configs
/*; do
614 [ -e "${config}" ] ||
continue
616 echo $
(basename ${config})
620 function zone_configs_cmd
() {
625 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
629 for config
in $
(zone_configs_list
${zone}); do
630 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
632 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
636 function zone_configs_up
() {
637 zone_configs_cmd up $@
640 function zone_configs_down
() {
641 zone_configs_cmd down $@
644 function zone_configs_status
() {
645 zone_configs_cmd config_status $@
648 function zone_has_ip
() {
658 starting|started|stopping|stopped
)
659 db_connection_update
${zone} ${action}
664 function zone_is_up
() {
670 function zone_is_down
() {
674 function zone_get_supported_port_hooks
() {
677 local hook
=$
(zone_get_hook
${zone})
679 hook_zone_ports_get_all
${hook}
682 function zone_get_supported_config_hooks
() {
685 local hook
=$
(zone_get_hook
${zone})
687 hook_zone_configs_get_all
${hook}
690 function zone_file
() {
695 echo "$(zone_dir ${zone})/settings"
698 function zone_config_read
() {
703 config_read $
(zone_file
${zone})
706 function zone_config_write
() {
711 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
714 function zone_config_set
() {
722 zone_config_read
${zone}
724 for arg
in ${args}; do
728 zone_config_write
${zone}
732 function zone_config_get
() {
740 zone_config_read
${zone}