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 hook_zone_exec
${hook} create
${zone} $@
104 # Maybe the zone create hook did not exit correctly.
105 # If this is the case we remove the created zone immediately.
106 if [ "${ret}" = "${EXIT_ERROR}" ]; then
111 function zone_edit
() {
115 if ! zone_exists
${zone}; then
116 error
"Zone '${zone}' does not exist."
120 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
122 if [ -z "${hook}" ]; then
123 error
"Config file did not provide any hook."
127 if ! hook_zone_exists
${hook}; then
128 error
"Hook '${hook}' does not exist."
132 hook_zone_exec
${hook} edit
${zone} $@
135 function zone_remove
() {
139 if ! zone_exists
${zone}; then
140 error
"Zone '${zone}' does not exist."
144 # XXX Tear this down here?
146 rm -rf $
(zone_dir
${zone})
153 if ! zone_exists
${zone}; then
154 error
"Zone '${zone}' does not exist."
158 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
160 if [ -z "${hook}" ]; then
161 error
"Config file did not provide any hook."
165 if ! hook_zone_exists
${hook}; then
166 error
"Hook '${hook}' does not exist."
170 zone_db
${zone} starting
172 hook_zone_exec
${hook} up
${zone} $@
174 zone_db
${zone} started
177 function zone_down
() {
181 if ! zone_exists
${zone}; then
182 error
"Zone '${zone}' does not exist."
186 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
188 if [ -z "${hook}" ]; then
189 error
"Config file did not provide any hook."
193 if ! hook_zone_exists
${hook}; then
194 error
"Hook '${hook}' does not exist."
198 zone_db
${zone} stopping
200 hook_zone_exec
${hook} down
${zone} $@
202 zone_db
${zone} stopped
205 function zone_status
() {
209 if ! zone_exists
${zone}; then
210 error
"Zone '${zone}' does not exist."
214 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
216 if [ -z "${hook}" ]; then
217 error
"Config file did not provide any hook."
221 if ! hook_zone_exists
${hook}; then
222 error
"Hook '${hook}' does not exist."
226 hook_zone_exec
${hook} status
${zone} $@
230 function zone_port
() {
234 if ! zone_exists
${zone}; then
235 error
"Zone '${zone}' does not exist."
239 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
241 if [ -z "${hook}" ]; then
242 error
"Config file did not provide any hook."
246 if ! hook_zone_exists
${hook}; then
247 error
"Hook '${hook}' does not exist."
251 hook_zone_exec
${hook} port
${zone} $@
254 function zone_port
() {
261 assert zone_exists
${zone}
265 zone_port_
${action} ${zone} $@
270 function zone_port_add
() {
277 assert zone_exists
${zone}
279 local hook_port
=$
(port_get_hook
${port})
281 assert isset hook_port
283 if ! listmatch
${hook_port} $
(zone_get_supported_hooks
${zone}); then
284 error
"Zone '${zone}' does not support ports with hook '${hook_port}'."
288 # XXX does this already exist?
290 # XXX I would rather like a relative symlink
291 ln -sf $
(port_file
${port}) $(zone_dir ${zone})/port.${port}
294 function zone_port_add
() {
300 local hook
=$
(zone_get_hook
${zone})
304 hook_zone_exec
${hook} port_add
${zone} $@
307 function zone_port_edit
() {
318 function zone_port_remove
() {
326 rm -f $
(zone_dir
${zone})/port.
${port}
329 function zone_port_cmd
() {
338 local hook_zone
=$
(zone_get_hook
${zone})
339 local hook_port
=$
(port_get_hook
${port})
341 assert isset hook_zone
342 assert isset hook_port
344 assert hook_zone_port_exists
${hook_zone} ${hook_port}
346 hook_zone_port_exec
${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
349 function zone_port_cmd
() {
350 error_log
"UNSUPPORTED FUNCTION CALLED: zone_port_cmd"
354 function zone_port_up
() {
358 function zone_port_down
() {
359 zone_port_cmd down $@
362 function zone_get_ports
() {
368 for port
in $
(zone_dir
${zone})/port.
*; do
369 port
=$
(basename ${port})
372 if port_exists
${port}; then
378 function zone_config
() {
382 if ! zone_exists
${zone}; then
383 error
"Zone '${zone}' does not exist."
387 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
389 if [ -z "${hook}" ]; then
390 error
"Config file did not provide any hook."
394 if ! hook_zone_exists
${hook}; then
395 error
"Hook '${hook}' does not exist."
399 hook_zone_exec
${hook} config
${zone} $@
402 function zone_show
() {
406 echo " Type: $(zone_get_hook ${zone})"
410 function zones_show
() {
413 for zone
in $
(zones_get $@
); do
418 function zones_get_all
() {
420 for zone
in $
(zone_dir
)/*; do
421 zone
=$
(basename ${zone})
422 zone_exists
${zone} ||
continue
428 function zones_get_local
() {
430 for zone
in $
(zones_get_all
); do
431 zone_is_local
${zone} && echo "${zone}"
435 function zones_get_nonlocal
() {
437 for zone
in $
(zones_get_all
); do
438 zone_is_nonlocal
${zone} && echo "${zone}"
442 function zones_get
() {
448 while [ $# -gt 0 ]; do
463 if zone_name_is_valid
${1}; then
464 zones
="${zones} ${1}"
466 warning
"Unrecognized argument '${1}'"
473 if [ -n "${zones}" ]; then
475 for zone
in ${zones}; do
476 zone_exists
${zone} && echo "${zone}"
481 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
483 elif [ ${local} -eq 1 ]; then
485 elif [ ${remote} -eq 1 ]; then
490 function zone_ports_list
() {
494 for port
in $
(zone_dir
${zone})/port.
*; do
495 [ -e "${port}" ] ||
continue
497 echo $
(basename ${port})
501 function zone_ports_cmd
() {
509 assert zone_exists
${zone}
511 local hook
=$
(zone_get_hook
${zone})
514 for port
in $
(zone_get_ports
${zone}); do
515 #zone_port_cmd ${cmd} ${zone} ${port} $@
516 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
520 function zone_ports_up
() {
521 zone_ports_cmd port_up $@
524 function zone_ports_down
() {
525 zone_ports_cmd port_down $@
528 function zone_ports_status
() {
529 zone_ports_cmd port_status $@
532 function zone_configs_list
() {
536 for config
in $
(zone_dir
${zone})/config.
*; do
537 [ -e "${config}" ] ||
continue
539 echo $
(basename ${config})
543 function zone_configs_cmd
() {
548 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
552 for config
in $
(zone_configs_list
${zone}); do
553 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/${config})
555 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
559 function zone_configs_up
() {
560 zone_configs_cmd up $@
563 function zone_configs_down
() {
564 zone_configs_cmd down $@
567 function zone_has_ipv4
() {
571 function zone_has_ipv6
() {
581 starting|started|stopping|stopped
)
582 db_connection_update
${zone} ${action}
587 function zone_is_up
() {
593 function zone_is_down
() {
597 function zone_get_supported_hooks
() {
600 local hook
=$
(zone_get_hook
${zone})
602 hook_zone_ports_get_all
${hook}
605 function zone_file
() {
610 echo "$(zone_dir ${zone})/settings"
613 function zone_config_read
() {
618 config_read $
(zone_file
${zone})
621 function zone_config_write
() {
626 config_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
629 function zone_config_set
() {
637 zone_config_read
${zone}
639 for arg
in ${args}; do
643 zone_config_write
${zone}