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 directory for ports
102 mkdir
-p $
(zone_dir
${zone})/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 function zone_config
() {
328 if ! zone_exists
${zone}; then
329 error
"Zone '${zone}' does not exist."
333 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
335 if [ -z "${hook}" ]; then
336 error
"Config file did not provide any hook."
340 if ! hook_zone_exists
${hook}; then
341 error
"Hook '${hook}' does not exist."
345 hook_zone_exec
${hook} config
${zone} $@
348 function zone_show
() {
352 echo " Type: $(zone_get_hook ${zone})"
356 function zones_show
() {
359 for zone
in $
(zones_get $@
); do
364 function zones_get_all
() {
366 for zone
in $
(zone_dir
)/*; do
367 zone
=$
(basename ${zone})
368 zone_exists
${zone} ||
continue
374 function zones_get_local
() {
376 for zone
in $
(zones_get_all
); do
377 zone_is_local
${zone} && echo "${zone}"
381 function zones_get_nonlocal
() {
383 for zone
in $
(zones_get_all
); do
384 zone_is_nonlocal
${zone} && echo "${zone}"
388 function zones_get
() {
394 while [ $# -gt 0 ]; do
409 if zone_name_is_valid
${1}; then
410 zones
="${zones} ${1}"
412 warning
"Unrecognized argument '${1}'"
419 if [ -n "${zones}" ]; then
421 for zone
in ${zones}; do
422 zone_exists
${zone} && echo "${zone}"
427 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
429 elif [ ${local} -eq 1 ]; then
431 elif [ ${remote} -eq 1 ]; then
436 function zone_ports_list
() {
440 for port
in $
(zone_dir
${zone})/port.
*; do
441 [ -e "${port}" ] ||
continue
443 echo $
(basename ${port})
447 function zone_ports_cmd
() {
455 assert zone_exists
${zone}
457 local hook
=$
(zone_get_hook
${zone})
460 for port
in $
(zone_get_ports
${zone}); do
461 #zone_port_cmd ${cmd} ${zone} ${port} $@
462 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
466 function zone_ports_up
() {
467 zone_ports_cmd port_up $@
470 function zone_ports_down
() {
471 zone_ports_cmd port_down $@
474 function zone_ports_status
() {
475 zone_ports_cmd port_status $@
478 function zone_configs_list
() {
482 for config
in $
(zone_dir
${zone})/config.
*; do
483 [ -e "${config}" ] ||
continue
485 echo $
(basename ${config})
489 function zone_configs_cmd
() {
494 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
498 for config
in $
(zone_configs_list
${zone}); do
499 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/${config})
501 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
505 function zone_configs_up
() {
506 zone_configs_cmd up $@
509 function zone_configs_down
() {
510 zone_configs_cmd down $@
513 function zone_has_ipv4
() {
517 function zone_has_ipv6
() {
527 starting|started|stopping|stopped
)
528 db_connection_update
${zone} ${action}
533 function zone_is_up
() {
539 function zone_is_down
() {
543 function zone_get_supported_hooks
() {
546 local hook
=$
(zone_get_hook
${zone})
548 hook_zone_ports_get_all
${hook}
551 function zone_file
() {
556 echo "$(zone_dir ${zone})/settings"
559 function zone_config_read
() {
564 config_read $
(zone_file
${zone})
567 function zone_config_write
() {
572 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
575 function zone_config_set
() {
583 zone_config_read
${zone}
585 for arg
in ${args}; do
589 zone_config_write
${zone}