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
() {
50 [[ ${zone} =~ $
(zone_match
) ]]
53 function zone_is_local
() {
56 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
59 function zone_is_nonlocal
() {
62 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
65 function zone_get_hook
() {
69 config_get_hook $
(zone_dir
${zone})/settings
72 function zone_start
() {
73 # This function will bring up the zone
74 # 'asynchronously' with help of systemd.
77 assert zone_exists
${zone}
79 service_start
"network@${zone}"
82 function zone_stop
() {
83 # This function will bring down the zone
84 # 'asynchronously' with help of systemd.
87 assert zone_exists
${zone}
89 service_stop
"network@${zone}"
92 function zone_create
() {
97 if ! zone_name_is_valid
${zone}; then
98 error
"Zone name '${zone}' is not valid."
102 if zone_exists
${zone}; then
103 error
"Zone '${zone}' does already exist."
107 if ! hook_zone_exists
${hook}; then
108 error
"Hook '${hook}' does not exist."
112 mkdir
-p $
(zone_dir
${zone})
114 # Create directories for configs and ports
115 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
117 hook_zone_exec
${hook} create
${zone} $@
120 # Maybe the zone create hook did not exit correctly.
121 # If this is the case we remove the created zone immediately.
122 if [ "${ret}" = "${EXIT_ERROR}" ]; then
123 zone_remove_now
${zone}
127 function zone_edit
() {
131 if ! zone_exists
${zone}; then
132 error
"Zone '${zone}' does not exist."
136 # Check if the zone is tagged for removal.
137 if zone_has_remove_tag
${zone}; then
138 error
"You cannot edit a zone that is tagged for removal."
142 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
144 if [ -z "${hook}" ]; then
145 error
"Config file did not provide any hook."
149 if ! hook_zone_exists
${hook}; then
150 error
"Hook '${hook}' does not exist."
154 hook_zone_exec
${hook} edit
${zone} $@
158 function zone_remove
() {
160 assert zone_exists
${zone}
162 # Make the zone for removal.
163 touch $
(zone_dir
${zone})/.remove
165 log INFO
"Zone '${zone}' has been tagged for removal."
168 function zone_has_remove_tag
() {
170 assert zone_exists
${zone}
172 [ -e "$(zone_dir ${zone})/.remove" ]
175 # This function will remove the given zone
176 # RIGHT NOW. Use zone_remove to remove it
177 # at the next status change.
178 function zone_remove_now
() {
180 assert zone_exists
${zone}
182 log INFO
"Removing zone '${zone}' right now."
184 # Force the zone down.
185 zone_is_up
${zone} && zone_set_down
${zone}
187 rm -rf $
(zone_dir
${zone})
194 if ! zone_exists
${zone}; then
195 error
"Zone '${zone}' does not exist."
199 # Check if a zone has got the remove tag.
200 if zone_has_remove_tag
${zone}; then
201 error
"Cannot bring up any zone which is to be removed."
205 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
207 if [ -z "${hook}" ]; then
208 error
"Config file did not provide any hook."
212 if ! hook_zone_exists
${hook}; then
213 error
"Hook '${hook}' does not exist."
217 zone_db
${zone} starting
219 hook_zone_exec
${hook} up
${zone} $@
221 zone_db
${zone} started
224 function zone_down
() {
228 if ! zone_exists
${zone}; then
229 error
"Zone '${zone}' does not exist."
233 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
235 if [ -z "${hook}" ]; then
236 error
"Config file did not provide any hook."
240 if ! hook_zone_exists
${hook}; then
241 error
"Hook '${hook}' does not exist."
245 zone_db
${zone} stopping
247 hook_zone_exec
${hook} down
${zone} $@
249 zone_db
${zone} stopped
251 # Remove the zone, if it has got a remove tag.
252 if zone_has_remove_tag
${zone}; then
253 zone_remove_now
${zone}
257 function zone_status
() {
261 if ! zone_exists
${zone}; then
262 error
"Zone '${zone}' does not exist."
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 hook_zone_exec
${hook} status
${zone} $@
280 # Show that the zone it to be removed soon.
281 if zone_has_remove_tag
${zone}; then
282 warning
"This zone is tagged for removal."
286 function zone_port
() {
293 assert zone_exists
${zone}
304 zone_port_
${action} ${zone} $@
307 error
"Unrecognized argument: ${action}"
308 cli_usage root-zone-port-subcommands
314 function zone_port_add
() {
320 local hook
=$
(zone_get_hook
${zone})
324 hook_zone_exec
${hook} port_add
${zone} $@
327 function zone_port_edit
() {
328 zone_port_cmd edit $@
331 function zone_port_rem
() {
335 function zone_port_cmd
() {
344 local hook_zone
=$
(zone_get_hook
${zone})
345 local hook_port
=$
(port_get_hook
${port})
347 assert isset hook_zone
348 assert isset hook_port
350 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
353 function zone_port_up
() {
357 function zone_port_down
() {
358 zone_port_cmd down $@
361 function zone_get_ports
() {
367 for port
in $
(zone_dir
${zone})/ports
/*; do
368 port
=$
(basename ${port})
370 if port_exists
${port}; then
376 function zone_has_port
() {
377 # Check, if the given port is configured
387 [ -e "$(zone_dir ${zone})/ports/${port}" ]
390 # XXX overwritten some lines below
391 function zone_config
() {
395 if ! zone_exists
${zone}; then
396 error
"Zone '${zone}' does not exist."
400 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
402 if [ -z "${hook}" ]; then
403 error
"Config file did not provide any hook."
407 if ! hook_zone_exists
${hook}; then
408 error
"Hook '${hook}' does not exist."
412 hook_zone_exec
${hook} config
${zone} $@
415 function zone_config
() {
422 assert zone_exists
${zone}
433 zone_config_
${action} ${zone} $@
436 error
"Unrecognized argument: ${action}"
437 cli_usage root-zone-config-subcommands
443 function zone_config_option
() {
454 zone_config_read
${zone}
461 function zone_config_create
() {
467 local hook
=$
(zone_get_hook
${zone})
471 hook_zone_exec
${hook} config_create
${zone} $@
474 function zone_show
() {
478 echo " Type: $(zone_get_hook ${zone})"
482 function zones_show
() {
485 for zone
in $
(zones_get $@
); do
490 function zones_get_all
() {
492 for zone
in $
(zone_dir
)/*; do
493 zone
=$
(basename ${zone})
494 zone_exists
${zone} ||
continue
500 function zones_get_local
() {
502 for zone
in $
(zones_get_all
); do
503 zone_is_local
${zone} && echo "${zone}"
507 function zones_get_nonlocal
() {
509 for zone
in $
(zones_get_all
); do
510 zone_is_nonlocal
${zone} && echo "${zone}"
514 function zones_get
() {
520 while [ $# -gt 0 ]; do
535 if zone_name_is_valid
${1}; then
536 zones
="${zones} ${1}"
538 warning
"Unrecognized argument '${1}'"
545 if [ -n "${zones}" ]; then
547 for zone
in ${zones}; do
548 zone_exists
${zone} && echo "${zone}"
553 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
555 elif [ ${local} -eq 1 ]; then
557 elif [ ${remote} -eq 1 ]; then
562 function zone_ports_list
() {
566 for port
in $
(zone_dir
${zone})/ports
/*; do
567 [ -e "${port}" ] ||
continue
569 echo $
(basename ${port})
573 function zone_ports_cmd
() {
581 assert zone_exists
${zone}
583 local hook
=$
(zone_get_hook
${zone})
586 for port
in $
(zone_get_ports
${zone}); do
587 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
591 function zone_ports_up
() {
592 zone_ports_cmd port_up $@
595 function zone_ports_down
() {
596 zone_ports_cmd port_down $@
599 function zone_ports_status
() {
600 zone_ports_cmd port_status $@
603 function zone_configs_list
() {
607 for config
in $
(zone_dir
${zone})/configs
/*; do
608 [ -e "${config}" ] ||
continue
614 function zone_configs_cmd
() {
619 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
623 for config
in $
(zone_configs_list
${zone}); do
624 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
626 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
630 function zone_configs_up
() {
631 zone_configs_cmd up $@
634 function zone_configs_down
() {
635 zone_configs_cmd down $@
638 function zone_configs_status
() {
639 zone_configs_cmd config_status $@
642 function zone_has_ip
() {
652 starting|started|stopping|stopped
)
653 db_connection_update
${zone} ${action}
658 function zone_is_up
() {
664 function zone_is_down
() {
668 function zone_get_supported_port_hooks
() {
671 local hook
=$
(zone_get_hook
${zone})
673 hook_zone_ports_get_all
${hook}
676 function zone_get_supported_config_hooks
() {
679 local hook
=$
(zone_get_hook
${zone})
681 hook_zone_configs_get_all
${hook}
684 function zone_file
() {
689 echo "$(zone_dir ${zone})/settings"
692 function zone_config_read
() {
697 config_read $
(zone_file
${zone})
700 function zone_config_write
() {
705 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
708 function zone_config_set
() {
716 zone_config_read
${zone}
718 for arg
in ${args}; do
722 zone_config_write
${zone}
726 function zone_config_get
() {
734 zone_config_read
${zone}