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 ###############################################################################
27 echo "${ZONE_DIR}/zones/${zone}"
30 function zone_exists
() {
35 [ -d "$(zone_dir ${zone})" ]
38 function zone_match
() {
42 for i
in ${VALID_ZONES}; do
43 match
="${match}|${i}[0-9]{1,5}"
46 echo "${match:1:${#match}}"
49 function zone_name_is_valid
() {
54 [[ ${zone} =~ $
(zone_match
) ]]
57 function zone_is_local
() {
60 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
63 function zone_is_nonlocal
() {
66 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
69 function zone_get_hook
() {
74 config_get_hook $
(zone_dir
${zone})/settings
77 function zone_start
() {
78 # This function will bring up the zone
79 # 'asynchronously' with help of systemd.
82 assert zone_exists
${zone}
84 service_start
"network@${zone}"
87 function zone_stop
() {
88 # This function will bring down the zone
89 # 'asynchronously' with help of systemd.
92 assert zone_exists
${zone}
94 service_stop
"network@${zone}"
97 function zone_create
() {
102 if ! zone_name_is_valid
${zone}; then
103 error
"Zone name '${zone}' is not valid."
107 if zone_exists
${zone}; then
108 error
"Zone '${zone}' does already exist."
112 if ! hook_zone_exists
${hook}; then
113 error
"Hook '${hook}' does not exist."
117 mkdir
-p $
(zone_dir
${zone})
119 # Create directories for configs and ports
120 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
122 hook_zone_exec
${hook} create
${zone} $@
125 # Maybe the zone create hook did not exit correctly.
126 # If this is the case we remove the created zone immediately.
127 if [ "${ret}" = "${EXIT_ERROR}" ]; then
132 function zone_edit
() {
136 if ! zone_exists
${zone}; then
137 error
"Zone '${zone}' does not exist."
141 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
143 if [ -z "${hook}" ]; then
144 error
"Config file did not provide any hook."
148 if ! hook_zone_exists
${hook}; then
149 error
"Hook '${hook}' does not exist."
153 hook_zone_exec
${hook} edit
${zone} $@
156 function zone_remove
() {
160 if ! zone_exists
${zone}; then
161 error
"Zone '${zone}' does not exist."
165 # XXX Tear this down here?
167 rm -rf $
(zone_dir
${zone})
174 if ! zone_exists
${zone}; then
175 error
"Zone '${zone}' does not exist."
179 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
181 if [ -z "${hook}" ]; then
182 error
"Config file did not provide any hook."
186 if ! hook_zone_exists
${hook}; then
187 error
"Hook '${hook}' does not exist."
191 zone_db
${zone} starting
193 hook_zone_exec
${hook} up
${zone} $@
195 zone_db
${zone} started
198 function zone_down
() {
202 if ! zone_exists
${zone}; then
203 error
"Zone '${zone}' does not exist."
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} stopping
221 hook_zone_exec
${hook} down
${zone} $@
223 zone_db
${zone} stopped
226 function zone_status
() {
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 hook_zone_exec
${hook} status
${zone} $@
250 function zone_port
() {
257 assert zone_exists
${zone}
268 zone_port_
${action} ${zone} $@
271 error
"Unrecognized argument: ${action}"
272 cli_usage root-zone-port-subcommands
278 function zone_port_add
() {
284 local hook
=$
(zone_get_hook
${zone})
288 hook_zone_exec
${hook} port_add
${zone} $@
291 function zone_port_edit
() {
292 zone_port_cmd edit $@
295 function zone_port_rem
() {
299 function zone_port_cmd
() {
308 local hook_zone
=$
(zone_get_hook
${zone})
309 local hook_port
=$
(port_get_hook
${port})
311 assert isset hook_zone
312 assert isset hook_port
314 assert hook_zone_port_exists
${hook_zone} ${hook_port}
316 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
319 function zone_port_up
() {
323 function zone_port_down
() {
324 zone_port_cmd down $@
327 function zone_get_ports
() {
333 for port
in $
(zone_dir
${zone})/ports
/*; do
334 port
=$
(basename ${port})
336 if port_exists
${port}; then
342 function zone_has_port
() {
343 # Check, if the given port is configured
353 [ -e "$(zone_dir ${zone})/ports/${port}" ]
356 # XXX overwritten some lines below
357 function zone_config
() {
361 if ! zone_exists
${zone}; then
362 error
"Zone '${zone}' does not exist."
366 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
368 if [ -z "${hook}" ]; then
369 error
"Config file did not provide any hook."
373 if ! hook_zone_exists
${hook}; then
374 error
"Hook '${hook}' does not exist."
378 hook_zone_exec
${hook} config
${zone} $@
381 function zone_config
() {
388 assert zone_exists
${zone}
399 zone_config_
${action} ${zone} $@
402 error
"Unrecognized argument: ${action}"
403 cli_usage root-zone-config-subcommands
409 function zone_config_option
() {
420 zone_config_read
${zone}
427 function zone_config_create
() {
433 local hook
=$
(zone_get_hook
${zone})
437 hook_zone_exec
${hook} config_create
${zone} $@
440 function zone_show
() {
444 echo " Type: $(zone_get_hook ${zone})"
448 function zones_show
() {
451 for zone
in $
(zones_get $@
); do
456 function zones_get_all
() {
458 for zone
in $
(zone_dir
)/*; do
459 zone
=$
(basename ${zone})
460 zone_exists
${zone} ||
continue
466 function zones_get_local
() {
468 for zone
in $
(zones_get_all
); do
469 zone_is_local
${zone} && echo "${zone}"
473 function zones_get_nonlocal
() {
475 for zone
in $
(zones_get_all
); do
476 zone_is_nonlocal
${zone} && echo "${zone}"
480 function zones_get
() {
486 while [ $# -gt 0 ]; do
501 if zone_name_is_valid
${1}; then
502 zones
="${zones} ${1}"
504 warning
"Unrecognized argument '${1}'"
511 if [ -n "${zones}" ]; then
513 for zone
in ${zones}; do
514 zone_exists
${zone} && echo "${zone}"
519 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
521 elif [ ${local} -eq 1 ]; then
523 elif [ ${remote} -eq 1 ]; then
528 function zone_ports_list
() {
532 for port
in $
(zone_dir
${zone})/ports
/*; do
533 [ -e "${port}" ] ||
continue
535 echo $
(basename ${port})
539 function zone_ports_cmd
() {
547 assert zone_exists
${zone}
549 local hook
=$
(zone_get_hook
${zone})
552 for port
in $
(zone_get_ports
${zone}); do
553 #zone_port_cmd ${cmd} ${zone} ${port} $@
554 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
558 function zone_ports_up
() {
559 zone_ports_cmd port_up $@
562 function zone_ports_down
() {
563 zone_ports_cmd port_down $@
566 function zone_ports_status
() {
567 zone_ports_cmd port_status $@
570 function zone_configs_list
() {
574 for config
in $
(zone_dir
${zone})/configs
/*; do
575 [ -e "${config}" ] ||
continue
577 echo $
(basename ${config})
581 function zone_configs_cmd
() {
586 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
590 for config
in $
(zone_configs_list
${zone}); do
591 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
593 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
597 function zone_configs_up
() {
598 zone_configs_cmd up $@
601 function zone_configs_down
() {
602 zone_configs_cmd down $@
605 function zone_configs_status
() {
606 zone_configs_cmd config_status $@
609 function zone_has_ip
() {
619 starting|started|stopping|stopped
)
620 db_connection_update
${zone} ${action}
625 function zone_is_up
() {
631 function zone_is_down
() {
635 function zone_get_supported_port_hooks
() {
638 local hook
=$
(zone_get_hook
${zone})
640 hook_zone_ports_get_all
${hook}
643 function zone_get_supported_config_hooks
() {
646 local hook
=$
(zone_get_hook
${zone})
648 hook_zone_configs_get_all
${hook}
651 function zone_file
() {
656 echo "$(zone_dir ${zone})/settings"
659 function zone_config_read
() {
664 config_read $
(zone_file
${zone})
667 function zone_config_write
() {
672 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
675 function zone_config_set
() {
683 zone_config_read
${zone}
685 for arg
in ${args}; do
689 zone_config_write
${zone}
693 function zone_config_get
() {
701 zone_config_read
${zone}