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_is_nonlocal
${zone}
63 function zone_is_nonlocal
() {
68 [[ ${zone} =~ ^red
[0-9]{1,5} ]]
71 function zone_get_hook
() {
76 config_get_hook $
(zone_dir
${zone})/settings
79 function zone_create
() {
84 if ! zone_name_is_valid
${zone}; then
85 error
"Zone name '${zone}' is not valid."
89 if zone_exists
${zone}; then
90 error
"Zone '${zone}' does already exist."
94 if ! hook_zone_exists
${hook}; then
95 error
"Hook '${hook}' does not exist."
99 mkdir
-p $
(zone_dir
${zone})
101 # Create directories for configs and ports
102 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
104 hook_zone_exec
${hook} create
${zone} $@
107 # Maybe the zone create hook did not exit correctly.
108 # If this is the case we remove the created zone immediately.
109 if [ "${ret}" = "${EXIT_ERROR}" ]; then
114 function zone_edit
() {
118 if ! zone_exists
${zone}; then
119 error
"Zone '${zone}' does not exist."
123 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
125 if [ -z "${hook}" ]; then
126 error
"Config file did not provide any hook."
130 if ! hook_zone_exists
${hook}; then
131 error
"Hook '${hook}' does not exist."
135 hook_zone_exec
${hook} edit
${zone} $@
138 function zone_remove
() {
142 if ! zone_exists
${zone}; then
143 error
"Zone '${zone}' does not exist."
147 # XXX Tear this down here?
149 rm -rf $
(zone_dir
${zone})
156 if ! zone_exists
${zone}; then
157 error
"Zone '${zone}' does not exist."
161 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
163 if [ -z "${hook}" ]; then
164 error
"Config file did not provide any hook."
168 if ! hook_zone_exists
${hook}; then
169 error
"Hook '${hook}' does not exist."
173 zone_db
${zone} starting
175 hook_zone_exec
${hook} up
${zone} $@
177 zone_db
${zone} started
180 function zone_down
() {
184 if ! zone_exists
${zone}; then
185 error
"Zone '${zone}' does not exist."
189 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
191 if [ -z "${hook}" ]; then
192 error
"Config file did not provide any hook."
196 if ! hook_zone_exists
${hook}; then
197 error
"Hook '${hook}' does not exist."
201 zone_db
${zone} stopping
203 hook_zone_exec
${hook} down
${zone} $@
205 zone_db
${zone} stopped
208 function zone_status
() {
212 if ! zone_exists
${zone}; then
213 error
"Zone '${zone}' does not exist."
217 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
219 if [ -z "${hook}" ]; then
220 error
"Config file did not provide any hook."
224 if ! hook_zone_exists
${hook}; then
225 error
"Hook '${hook}' does not exist."
229 hook_zone_exec
${hook} status
${zone} $@
232 function zone_port
() {
239 assert zone_exists
${zone}
250 zone_port_
${action} ${zone} $@
253 error
"Unrecognized argument: ${action}"
254 cli_usage root-zone-port-subcommands
260 function zone_port_add
() {
266 local hook
=$
(zone_get_hook
${zone})
270 hook_zone_exec
${hook} port_add
${zone} $@
273 function zone_port_edit
() {
274 zone_port_cmd edit $@
277 function zone_port_rem
() {
281 function zone_port_cmd
() {
290 local hook_zone
=$
(zone_get_hook
${zone})
291 local hook_port
=$
(port_get_hook
${port})
293 assert isset hook_zone
294 assert isset hook_port
296 assert hook_zone_port_exists
${hook_zone} ${hook_port}
298 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
301 function zone_port_up
() {
305 function zone_port_down
() {
306 zone_port_cmd down $@
309 function zone_get_ports
() {
315 for port
in $
(zone_dir
${zone})/ports
/*; do
316 port
=$
(basename ${port})
318 if port_exists
${port}; then
324 # XXX overwritten some lines below
325 function zone_config
() {
329 if ! zone_exists
${zone}; then
330 error
"Zone '${zone}' does not exist."
334 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
336 if [ -z "${hook}" ]; then
337 error
"Config file did not provide any hook."
341 if ! hook_zone_exists
${hook}; then
342 error
"Hook '${hook}' does not exist."
346 hook_zone_exec
${hook} config
${zone} $@
349 function zone_config
() {
356 assert zone_exists
${zone}
367 zone_config_
${action} ${zone} $@
370 error
"Unrecognized argument: ${action}"
371 cli_usage root-zone-config-subcommands
377 function zone_config_create
() {
383 local hook
=$
(zone_get_hook
${zone})
387 hook_zone_exec
${hook} config_create
${zone} $@
390 function zone_show
() {
394 echo " Type: $(zone_get_hook ${zone})"
398 function zones_show
() {
401 for zone
in $
(zones_get $@
); do
406 function zones_get_all
() {
408 for zone
in $
(zone_dir
)/*; do
409 zone
=$
(basename ${zone})
410 zone_exists
${zone} ||
continue
416 function zones_get_local
() {
418 for zone
in $
(zones_get_all
); do
419 zone_is_local
${zone} && echo "${zone}"
423 function zones_get_nonlocal
() {
425 for zone
in $
(zones_get_all
); do
426 zone_is_nonlocal
${zone} && echo "${zone}"
430 function zones_get
() {
436 while [ $# -gt 0 ]; do
451 if zone_name_is_valid
${1}; then
452 zones
="${zones} ${1}"
454 warning
"Unrecognized argument '${1}'"
461 if [ -n "${zones}" ]; then
463 for zone
in ${zones}; do
464 zone_exists
${zone} && echo "${zone}"
469 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
471 elif [ ${local} -eq 1 ]; then
473 elif [ ${remote} -eq 1 ]; then
478 function zone_ports_list
() {
482 for port
in $
(zone_dir
${zone})/ports
/*; do
483 [ -e "${port}" ] ||
continue
485 echo $
(basename ${port})
489 function zone_ports_cmd
() {
497 assert zone_exists
${zone}
499 local hook
=$
(zone_get_hook
${zone})
502 for port
in $
(zone_get_ports
${zone}); do
503 #zone_port_cmd ${cmd} ${zone} ${port} $@
504 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
508 function zone_ports_up
() {
509 zone_ports_cmd port_up $@
512 function zone_ports_down
() {
513 zone_ports_cmd port_down $@
516 function zone_ports_status
() {
517 zone_ports_cmd port_status $@
520 function zone_configs_list
() {
524 for config
in $
(zone_dir
${zone})/configs
/*; do
525 [ -e "${config}" ] ||
continue
527 echo $
(basename ${config})
531 function zone_configs_cmd
() {
536 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
540 for config
in $
(zone_configs_list
${zone}); do
541 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
543 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
547 function zone_configs_up
() {
548 zone_configs_cmd up $@
551 function zone_configs_down
() {
552 zone_configs_cmd down $@
555 function zone_configs_status
() {
556 zone_configs_cmd config_status $@
559 function zone_has_ip
() {
569 starting|started|stopping|stopped
)
570 db_connection_update
${zone} ${action}
575 function zone_is_up
() {
581 function zone_is_down
() {
585 function zone_get_supported_port_hooks
() {
588 local hook
=$
(zone_get_hook
${zone})
590 hook_zone_ports_get_all
${hook}
593 function zone_get_supported_config_hooks
() {
596 local hook
=$
(zone_get_hook
${zone})
598 hook_zone_configs_get_all
${hook}
601 function zone_file
() {
606 echo "$(zone_dir ${zone})/settings"
609 function zone_config_read
() {
614 config_read $
(zone_file
${zone})
617 function zone_config_write
() {
622 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
625 function zone_config_set
() {
633 zone_config_read
${zone}
635 for arg
in ${args}; do
639 zone_config_write
${zone}
643 function zone_config_get
() {
651 zone_config_read
${zone}