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
() {
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
52 [[ ${zone} =~ $
(zone_match
) ]]
55 function zone_is_local
() {
58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
61 function zone_is_nonlocal
() {
64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
67 function zone_get_hook
() {
71 config_get_hook $
(zone_dir
${zone})/settings
74 function zone_start
() {
75 # This function will bring up the zone
76 # 'asynchronously' with help of systemd.
79 assert zone_exists
${zone}
81 service_start
"network@${zone}.service"
84 function zone_stop
() {
85 # This function will bring down the zone
86 # 'asynchronously' with help of systemd.
89 assert zone_exists
${zone}
91 service_stop
"network@${zone}.service"
94 function zone_reload
() {
96 assert zone_exists
"${zone}"
98 service_reload
"network@${zone}.service"
101 function zone_enable
() {
102 # This function will enable the zone
103 # with help of systemd.
106 assert zone_exists
"${zone}"
108 # Enable service for the zone
109 service_enable
"network@${zone}.service"
112 if [ ${ret} -eq ${EXIT_OK} ]; then
113 log INFO
"Auto-start enabled for zone ${zone}"
117 log ERROR
"Could not enable zone ${zone}: ${ret}"
121 function zone_disable
() {
122 # This function will disable the zone
123 # with help of systemd.
126 assert zone_exists
"${zone}"
128 # Disable service for the zone
129 service_disable
"network@${zone}.service"
132 if [ ${ret} -eq ${EXIT_OK} ]; then
133 log INFO
"Auto-start disabled for zone ${zone}"
137 log ERROR
"Could not disable zone ${zone}: ${ret}"
141 function zone_is_enabled
() {
145 # Ask systemd if the zone is enabled.
146 if service_is_enabled
"network@${zone}.service"; then
153 function zone_is_active
() {
157 if service_is_active
"network@${zone}.service"; then
164 function zone_create
() {
169 if ! zone_name_is_valid
${zone}; then
170 error
"Zone name '${zone}' is not valid."
174 if zone_exists
${zone}; then
175 error
"Zone '${zone}' does already exist."
179 if ! hook_zone_exists
${hook}; then
180 error
"Hook '${hook}' does not exist."
184 mkdir
-p $
(zone_dir
${zone})
186 # Create directories for configs and ports
187 mkdir
-p $
(zone_dir
${zone})/{configs
,ports
}
189 hook_zone_exec
${hook} create
${zone} $@
192 # Maybe the zone create hook did not exit correctly.
193 # If this is the case we remove the created zone immediately.
194 if [ "${ret}" = "${EXIT_ERROR}" ]; then
195 zone_remove_now
${zone}
199 # Automatically enable zone.
200 zone_enable
"${zone}"
203 function zone_edit
() {
207 if ! zone_exists
${zone}; then
208 error
"Zone '${zone}' does not exist."
212 # Check if the zone is tagged for removal.
213 if zone_has_remove_tag
${zone}; then
214 error
"You cannot edit a zone that is tagged for removal."
218 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
220 if [ -z "${hook}" ]; then
221 error
"Config file did not provide any hook."
225 if ! hook_zone_exists
${hook}; then
226 error
"Hook '${hook}' does not exist."
230 hook_zone_exec
${hook} edit
${zone} $@
234 function zone_remove
() {
236 assert zone_exists
${zone}
238 # Make the zone for removal.
239 touch $
(zone_dir
${zone})/.remove
241 log INFO
"Zone '${zone}' has been tagged for removal."
244 function zone_has_remove_tag
() {
246 assert zone_exists
${zone}
248 [ -e "$(zone_dir ${zone})/.remove" ]
251 # This function will remove the given zone
252 # RIGHT NOW. Use zone_remove to remove it
253 # at the next status change.
254 function zone_remove_now
() {
256 assert zone_exists
${zone}
258 log INFO
"Removing zone '${zone}' right now."
260 # Force the zone down.
261 zone_is_up
${zone} && zone_set_down
${zone}
264 zone_disable
"${zone}"
266 rm -rf $
(zone_dir
${zone})
273 if ! zone_exists
${zone}; then
274 error
"Zone '${zone}' does not exist."
278 # Check if a zone has got the remove tag.
279 if zone_has_remove_tag
${zone}; then
280 error
"Cannot bring up any zone which is to be removed."
284 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
286 if [ -z "${hook}" ]; then
287 error
"Config file did not provide any hook."
291 if ! hook_zone_exists
${hook}; then
292 error
"Hook '${hook}' does not exist."
296 zone_db
${zone} starting
298 hook_zone_exec
${hook} up
${zone} $@
300 zone_db
${zone} started
303 function zone_down
() {
307 if ! zone_exists
${zone}; then
308 error
"Zone '${zone}' does not exist."
312 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
314 if [ -z "${hook}" ]; then
315 error
"Config file did not provide any hook."
319 if ! hook_zone_exists
${hook}; then
320 error
"Hook '${hook}' does not exist."
324 zone_db
${zone} stopping
326 hook_zone_exec
${hook} down
${zone} $@
328 zone_db
${zone} stopped
330 # Remove the zone, if it has got a remove tag.
331 if zone_has_remove_tag
${zone}; then
332 zone_remove_now
${zone}
336 function zone_status
() {
340 if ! zone_exists
${zone}; then
341 error
"Zone '${zone}' does not exist."
345 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
347 if [ -z "${hook}" ]; then
348 error
"Config file did not provide any hook."
352 if ! hook_zone_exists
${hook}; then
353 error
"Hook '${hook}' does not exist."
357 hook_zone_exec
${hook} status
${zone} $@
359 # Show that the zone it to be removed soon.
360 if zone_has_remove_tag
${zone}; then
361 warning
"This zone is tagged for removal."
365 function zone_port
() {
372 assert zone_exists
${zone}
376 zone_port_
${action} ${zone} $@
379 error
"Unrecognized argument: ${action}"
380 cli_usage root-zone-port-subcommands
386 function zone_port_add
() {
395 # Check if the port actually exists.
396 if ! port_exists
"${port}"; then
397 error
"Cannot add port '${port}' which does not exist"
401 # Check if the port is already connected to this or any other zone.
403 for z
in $
(zones_get_all
); do
404 if zone_has_port
"${z}" "${port}"; then
405 error
"Port '${port}' is already assigned to zone '${z}'"
410 local hook
=$
(zone_get_hook
"${zone}")
413 hook_zone_exec
"${hook}" "port_add" "${zone}" "${port}" "$@"
416 function zone_port_edit
() {
425 # Check if the port actually exists.
426 if ! port_exists
"${port}"; then
427 error
"Port '${port}' does not exist"
431 # Check if the zone actually has this port.
432 if ! zone_has_port
"${zone}" "${port}"; then
433 error
"Port '${port}' is not attached to zone '${zone}'"
437 local hook
=$
(zone_get_hook
"${zone}")
440 hook_zone_exec
"${hook}" "port_edit" "${zone}" "${port}" "$@"
443 function zone_port_remove
() {
452 # Check if the zone actually has this port.
453 if ! zone_has_port
"${zone}" "${port}"; then
454 error
"Port '${port}' is not attached to zone '${zone}'"
458 local hook
=$
(zone_get_hook
"${zone}")
461 hook_zone_exec
"${hook}" "port_remove" "${zone}" "${port}" "$@"
464 function zone_get_ports
() {
470 for port
in $
(zone_dir
${zone})/ports
/*; do
471 port
=$
(basename ${port})
473 if port_exists
${port}; then
479 function zone_get_ports_num
() {
485 for port
in $
(zone_dir
"${zone}")/ports
/*; do
486 port
="$(basename "${port}")"
488 if port_exists
"${port}"; then
489 counter
=$
(( ${counter} + 1 ))
497 function zone_has_port
() {
498 # Check, if the given port is configured
508 [ -e "$(zone_dir ${zone})/ports/${port}" ]
511 # XXX overwritten some lines below
512 function zone_config
() {
516 if ! zone_exists
${zone}; then
517 error
"Zone '${zone}' does not exist."
521 local hook
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
523 if [ -z "${hook}" ]; then
524 error
"Config file did not provide any hook."
528 if ! hook_zone_exists
${hook}; then
529 error
"Hook '${hook}' does not exist."
533 hook_zone_exec
${hook} config
${zone} $@
536 function zone_config
() {
543 assert zone_exists
${zone}
554 zone_config_
${action} ${zone} $@
557 error
"Unrecognized argument: ${action}"
558 cli_usage root-zone-config-subcommands
564 function zone_config_create
() {
569 local hook
=$
(zone_get_hook
"${zone}")
572 hook_zone_exec
"${hook}" "config_create" "${zone}" "$@"
575 function zone_show
() {
579 echo " Type: $(zone_get_hook ${zone})"
583 function zones_show
() {
586 for zone
in $
(zones_get $@
); do
591 function zones_get_all
() {
593 for zone
in $
(zone_dir
)/*; do
594 zone
=$
(basename ${zone})
595 zone_exists
${zone} ||
continue
601 function zones_get_local
() {
603 for zone
in $
(zones_get_all
); do
604 zone_is_local
${zone} && echo "${zone}"
608 function zones_get_nonlocal
() {
610 for zone
in $
(zones_get_all
); do
611 zone_is_nonlocal
${zone} && echo "${zone}"
615 function zones_get
() {
621 while [ $# -gt 0 ]; do
636 if zone_name_is_valid
${1}; then
637 zones
="${zones} ${1}"
639 warning
"Unrecognized argument '${1}'"
646 if [ -n "${zones}" ]; then
648 for zone
in ${zones}; do
649 zone_exists
${zone} && echo "${zone}"
654 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
656 elif [ ${local} -eq 1 ]; then
658 elif [ ${remote} -eq 1 ]; then
663 function zone_ports_list
() {
667 for port
in $
(zone_dir
${zone})/ports
/*; do
668 [ -e "${port}" ] ||
continue
670 echo $
(basename ${port})
674 function zone_ports_cmd
() {
682 assert zone_exists
${zone}
684 local hook
=$
(zone_get_hook
${zone})
687 for port
in $
(zone_get_ports
${zone}); do
688 hook_zone_exec
${hook} ${cmd} ${zone} ${port} $@
692 function zone_ports_up
() {
693 zone_ports_cmd port_up $@
696 function zone_ports_down
() {
697 zone_ports_cmd port_down $@
700 function zone_ports_status
() {
701 zone_ports_cmd port_status $@
704 function zone_configs_list
() {
708 for config
in $
(zone_dir
${zone})/configs
/*; do
709 [ -e "${config}" ] ||
continue
715 function zone_configs_cmd
() {
720 local hook_zone
=$
(config_get_hook $
(zone_dir
${zone})/settings
)
724 for config
in $
(zone_configs_list
${zone}); do
725 hook_config
=$
(config_get_hook $
(zone_dir
${zone})/configs
/${config})
727 hook_zone_config_exec
${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
731 function zone_configs_up
() {
732 zone_configs_cmd up $@
735 function zone_configs_down
() {
736 zone_configs_cmd down $@
739 function zone_configs_status
() {
740 zone_configs_cmd config_status $@
743 function zone_has_ip
() {
753 starting|started|stopping|stopped
)
754 db_connection_update
${zone} ${action}
759 function zone_is_up
() {
765 function zone_is_down
() {
769 function zone_get_supported_port_hooks
() {
772 local hook
=$
(zone_get_hook
${zone})
774 hook_zone_ports_get_all
${hook}
777 function zone_get_supported_config_hooks
() {
780 local hook
=$
(zone_get_hook
${zone})
782 hook_zone_configs_get_all
${hook}
785 function zone_file
() {
790 echo "$(zone_dir ${zone})/settings"
793 function zone_settings_read
() {
798 # Save the HOOK variable.
801 settings_read $
(zone_file
${zone})
807 function zone_settings_write
() {
812 settings_write $
(zone_file
${zone}) ${HOOK_SETTINGS}
815 function zone_settings_set
() {
823 zone_settings_read
${zone}
825 for arg
in ${args}; do
829 zone_settings_write
${zone}
833 function zone_settings_get
() {
841 zone_settings_read
${zone}
847 function zone_config_settings_read
() {
854 local path
="$(zone_dir "${zone}")/configs/${config}"
855 settings_read
"${path}" "$@"
858 function zone_config_settings_write
() {
865 local path
="$(zone_dir "${zone}")/configs/${config}"
866 settings_write
"${path}" "$@"
869 function zone_port_settings_read
() {
876 local path
="$(zone_dir "${zone}")/ports/${port}"
877 settings_read
"${path}" "$@"
880 function zone_port_settings_write
() {
887 local path
="$(zone_dir "${zone}")/ports/${port}"
888 settings_write
"${path}" "$@"
891 function zone_port_settings_remove
() {
897 local path
="$(zone_dir "${zone}")/ports/${port}"
898 settings_remove
"${path}"