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_create
() {
99 if ! zone_name_is_valid
${zone}; then
100 error
"Zone name '${zone}' is not valid."
104 if zone_exists
${zone}; then
105 error
"Zone '${zone}' does already exist."
109 if ! hook_zone_exists
${hook}; then
110 error
"Hook '${hook}' does not exist."
114 mkdir
-p $
(zone_dir
${zone})
116 # Create directories for configs and ports
117 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
119 hook_zone_exec
${hook} create
${zone} $@
122 # Maybe the zone create hook did not exit correctly.
123 # If this is the case we remove the created zone immediately.
124 if [ "${ret}" = "${EXIT_ERROR}" ]; then
125 zone_remove_now
${zone}
129 function zone_edit
() {
133 if ! zone_exists
${zone}; then
134 error
"Zone '${zone}' does not exist."
138 # Check if the zone is tagged for removal.
139 if zone_has_remove_tag
${zone}; then
140 error
"You cannot edit a zone that is tagged for removal."
144 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
146 if [ -z "${hook}" ]; then
147 error
"Config file did not provide any hook."
151 if ! hook_zone_exists
${hook}; then
152 error
"Hook '${hook}' does not exist."
156 hook_zone_exec
${hook} edit
${zone} $@
160 function zone_remove
() {
162 assert zone_exists
${zone}
164 # Make the zone for removal.
165 touch $
(zone_dir
${zone})/.remove
167 log INFO
"Zone '${zone}' has been tagged for removal."
170 function zone_has_remove_tag
() {
172 assert zone_exists
${zone}
174 [ -e "$(zone_dir ${zone})/.remove" ]
177 # This function will remove the given zone
178 # RIGHT NOW. Use zone_remove to remove it
179 # at the next status change.
180 function zone_remove_now
() {
182 assert zone_exists
${zone}
184 log INFO
"Removing zone '${zone}' right now."
186 # Force the zone down.
187 zone_is_up
${zone} && zone_set_down
${zone}
189 rm -rf $
(zone_dir
${zone})
196 if ! zone_exists
${zone}; then
197 error
"Zone '${zone}' does not exist."
201 # Check if a zone has got the remove tag.
202 if zone_has_remove_tag
${zone}; then
203 error
"Cannot bring up any zone which is to be removed."
207 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
209 if [ -z "${hook}" ]; then
210 error
"Config file did not provide any hook."
214 if ! hook_zone_exists
${hook}; then
215 error
"Hook '${hook}' does not exist."
219 zone_db
${zone} starting
221 hook_zone_exec
${hook} up
${zone} $@
223 zone_db
${zone} started
226 function zone_down
() {
230 if ! zone_exists
${zone}; then
231 error
"Zone '${zone}' does not exist."
235 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
237 if [ -z "${hook}" ]; then
238 error
"Config file did not provide any hook."
242 if ! hook_zone_exists
${hook}; then
243 error
"Hook '${hook}' does not exist."
247 zone_db
${zone} stopping
249 hook_zone_exec
${hook} down
${zone} $@
251 zone_db
${zone} stopped
253 # Remove the zone, if it has got a remove tag.
254 if zone_has_remove_tag
${zone}; then
255 zone_remove_now
${zone}
259 function zone_status
() {
263 if ! zone_exists
${zone}; then
264 error
"Zone '${zone}' does not exist."
268 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
270 if [ -z "${hook}" ]; then
271 error
"Config file did not provide any hook."
275 if ! hook_zone_exists
${hook}; then
276 error
"Hook '${hook}' does not exist."
280 hook_zone_exec
${hook} status
${zone} $@
282 # Show that the zone it to be removed soon.
283 if zone_has_remove_tag
${zone}; then
284 warning
"This zone is tagged for removal."
288 function zone_port
() {
295 assert zone_exists
${zone}
306 zone_port_
${action} ${zone} $@
309 error
"Unrecognized argument: ${action}"
310 cli_usage root-zone-port-subcommands
316 function zone_port_add
() {
322 local hook
=$
(zone_get_hook
${zone})
326 hook_zone_exec
${hook} port_add
${zone} $@
329 function zone_port_edit
() {
330 zone_port_cmd edit $@
333 function zone_port_rem
() {
337 function zone_port_cmd
() {
346 local hook_zone
=$
(zone_get_hook
${zone})
347 local hook_port
=$
(port_get_hook
${port})
349 assert isset hook_zone
350 assert isset hook_port
352 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
355 function zone_port_up
() {
359 function zone_port_down
() {
360 zone_port_cmd down $@
363 function zone_get_ports
() {
369 for port
in $
(zone_dir
${zone})/ports
/*; do
370 port
=$
(basename ${port})
372 if port_exists
${port}; then
378 function zone_has_port
() {
379 # Check, if the given port is configured
389 [ -e "$(zone_dir ${zone})/ports/${port}" ]
392 # XXX overwritten some lines below
393 function zone_config
() {
397 if ! zone_exists
${zone}; then
398 error
"Zone '${zone}' does not exist."
402 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
404 if [ -z "${hook}" ]; then
405 error
"Config file did not provide any hook."
409 if ! hook_zone_exists
${hook}; then
410 error
"Hook '${hook}' does not exist."
414 hook_zone_exec
${hook} config
${zone} $@
417 function zone_config
() {
424 assert zone_exists
${zone}
435 zone_config_
${action} ${zone} $@
438 error
"Unrecognized argument: ${action}"
439 cli_usage root-zone-config-subcommands
445 function zone_config_option
() {
456 zone_config_read
${zone}
463 function zone_config_create
() {
469 local hook
=$
(zone_get_hook
${zone})
473 hook_zone_exec
${hook} config_create
${zone} $@
476 function zone_show
() {
480 echo " Type: $(zone_get_hook ${zone})"
484 function zones_show
() {
487 for zone
in $
(zones_get $@
); do
492 function zones_get_all
() {
494 for zone
in $
(zone_dir
)/*; do
495 zone
=$
(basename ${zone})
496 zone_exists
${zone} ||
continue
502 function zones_get_local
() {
504 for zone
in $
(zones_get_all
); do
505 zone_is_local
${zone} && echo "${zone}"
509 function zones_get_nonlocal
() {
511 for zone
in $
(zones_get_all
); do
512 zone_is_nonlocal
${zone} && echo "${zone}"
516 function zones_get
() {
522 while [ $# -gt 0 ]; do
537 if zone_name_is_valid
${1}; then
538 zones
="${zones} ${1}"
540 warning
"Unrecognized argument '${1}'"
547 if [ -n "${zones}" ]; then
549 for zone
in ${zones}; do
550 zone_exists
${zone} && echo "${zone}"
555 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
557 elif [ ${local} -eq 1 ]; then
559 elif [ ${remote} -eq 1 ]; then
564 function zone_ports_list
() {
568 for port
in $
(zone_dir
${zone})/ports
/*; do
569 [ -e "${port}" ] ||
continue
571 echo $
(basename ${port})
575 function zone_ports_cmd
() {
583 assert zone_exists
${zone}
585 local hook
=$
(zone_get_hook
${zone})
588 for port
in $
(zone_get_ports
${zone}); do
589 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
593 function zone_ports_up
() {
594 zone_ports_cmd port_up $@
597 function zone_ports_down
() {
598 zone_ports_cmd port_down $@
601 function zone_ports_status
() {
602 zone_ports_cmd port_status $@
605 function zone_configs_list
() {
609 for config
in $
(zone_dir
${zone})/configs
/*; do
610 [ -e "${config}" ] ||
continue
616 function zone_configs_cmd
() {
621 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
625 for config
in $
(zone_configs_list
${zone}); do
626 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
628 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
632 function zone_configs_up
() {
633 zone_configs_cmd up $@
636 function zone_configs_down
() {
637 zone_configs_cmd down $@
640 function zone_configs_status
() {
641 zone_configs_cmd config_status $@
644 function zone_has_ip
() {
654 starting|started|stopping|stopped
)
655 db_connection_update
${zone} ${action}
660 function zone_is_up
() {
666 function zone_is_down
() {
670 function zone_get_supported_port_hooks
() {
673 local hook
=$
(zone_get_hook
${zone})
675 hook_zone_ports_get_all
${hook}
678 function zone_get_supported_config_hooks
() {
681 local hook
=$
(zone_get_hook
${zone})
683 hook_zone_configs_get_all
${hook}
686 function zone_file
() {
691 echo "$(zone_dir ${zone})/settings"
694 function zone_config_read
() {
699 # Save the HOOK variable.
702 config_read $
(zone_file
${zone})
708 function zone_config_write
() {
713 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
716 function zone_config_set
() {
724 zone_config_read
${zone}
726 for arg
in ${args}; do
730 zone_config_write
${zone}
734 function zone_config_get
() {
742 zone_config_read
${zone}