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}
365 zone_port_
${action} ${zone} $@
368 error
"Unrecognized argument: ${action}"
369 cli_usage root-zone-port-subcommands
375 function zone_port_add
() {
381 local hook
=$
(zone_get_hook
${zone})
385 hook_zone_exec
${hook} port_add
${zone} $@
388 function zone_port_edit
() {
389 zone_port_cmd edit $@
392 function zone_port_rem
() {
396 function zone_port_cmd
() {
405 local hook_zone
=$
(zone_get_hook
${zone})
406 local hook_port
=$
(port_get_hook
${port})
408 assert isset hook_zone
409 assert isset hook_port
411 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
414 function zone_port_up
() {
418 function zone_port_down
() {
419 zone_port_cmd down $@
422 function zone_get_ports
() {
428 for port
in $
(zone_dir
${zone})/ports
/*; do
429 port
=$
(basename ${port})
431 if port_exists
${port}; then
437 function zone_has_port
() {
438 # Check, if the given port is configured
448 [ -e "$(zone_dir ${zone})/ports/${port}" ]
451 # XXX overwritten some lines below
452 function zone_config
() {
456 if ! zone_exists
${zone}; then
457 error
"Zone '${zone}' does not exist."
461 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
463 if [ -z "${hook}" ]; then
464 error
"Config file did not provide any hook."
468 if ! hook_zone_exists
${hook}; then
469 error
"Hook '${hook}' does not exist."
473 hook_zone_exec
${hook} config
${zone} $@
476 function zone_config
() {
483 assert zone_exists
${zone}
494 zone_config_
${action} ${zone} $@
497 error
"Unrecognized argument: ${action}"
498 cli_usage root-zone-config-subcommands
504 function zone_config_option
() {
515 zone_config_read
${zone}
522 function zone_config_create
() {
528 local hook
=$
(zone_get_hook
${zone})
532 hook_zone_exec
${hook} config_create
${zone} $@
535 function zone_show
() {
539 echo " Type: $(zone_get_hook ${zone})"
543 function zones_show
() {
546 for zone
in $
(zones_get $@
); do
551 function zones_get_all
() {
553 for zone
in $
(zone_dir
)/*; do
554 zone
=$
(basename ${zone})
555 zone_exists
${zone} ||
continue
561 function zones_get_local
() {
563 for zone
in $
(zones_get_all
); do
564 zone_is_local
${zone} && echo "${zone}"
568 function zones_get_nonlocal
() {
570 for zone
in $
(zones_get_all
); do
571 zone_is_nonlocal
${zone} && echo "${zone}"
575 function zones_get
() {
581 while [ $# -gt 0 ]; do
596 if zone_name_is_valid
${1}; then
597 zones
="${zones} ${1}"
599 warning
"Unrecognized argument '${1}'"
606 if [ -n "${zones}" ]; then
608 for zone
in ${zones}; do
609 zone_exists
${zone} && echo "${zone}"
614 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
616 elif [ ${local} -eq 1 ]; then
618 elif [ ${remote} -eq 1 ]; then
623 function zone_ports_list
() {
627 for port
in $
(zone_dir
${zone})/ports
/*; do
628 [ -e "${port}" ] ||
continue
630 echo $
(basename ${port})
634 function zone_ports_cmd
() {
642 assert zone_exists
${zone}
644 local hook
=$
(zone_get_hook
${zone})
647 for port
in $
(zone_get_ports
${zone}); do
648 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
652 function zone_ports_up
() {
653 zone_ports_cmd port_up $@
656 function zone_ports_down
() {
657 zone_ports_cmd port_down $@
660 function zone_ports_status
() {
661 zone_ports_cmd port_status $@
664 function zone_configs_list
() {
668 for config
in $
(zone_dir
${zone})/configs
/*; do
669 [ -e "${config}" ] ||
continue
675 function zone_configs_cmd
() {
680 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
684 for config
in $
(zone_configs_list
${zone}); do
685 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
687 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
691 function zone_configs_up
() {
692 zone_configs_cmd up $@
695 function zone_configs_down
() {
696 zone_configs_cmd down $@
699 function zone_configs_status
() {
700 zone_configs_cmd config_status $@
703 function zone_has_ip
() {
713 starting|started|stopping|stopped
)
714 db_connection_update
${zone} ${action}
719 function zone_is_up
() {
725 function zone_is_down
() {
729 function zone_get_supported_port_hooks
() {
732 local hook
=$
(zone_get_hook
${zone})
734 hook_zone_ports_get_all
${hook}
737 function zone_get_supported_config_hooks
() {
740 local hook
=$
(zone_get_hook
${zone})
742 hook_zone_configs_get_all
${hook}
745 function zone_file
() {
750 echo "$(zone_dir ${zone})/settings"
753 function zone_config_read
() {
758 # Save the HOOK variable.
761 config_read $
(zone_file
${zone})
767 function zone_config_write
() {
772 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
775 function zone_config_set
() {
783 zone_config_read
${zone}
785 for arg
in ${args}; do
789 zone_config_write
${zone}
793 function zone_config_get
() {
801 zone_config_read
${zone}