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 ###############################################################################
26 [ -d "${NETWORK_ZONES_DIR}/${zone}" ]
33 for i
in ${VALID_ZONES}; do
34 match
="${match}|${i}[0-9]{1,5}"
37 echo "${match:1:${#match}}"
40 zone_name_is_valid
() {
43 # Don't accept empty strings.
44 [ -z "${zone}" ] && return ${EXIT_FALSE}
46 [[ ${zone} =~ $
(zone_match
) ]]
52 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
58 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
65 config_get_hook
"${NETWORK_ZONES_DIR}/${zone}/settings"
69 # This function will bring up the zone
70 # 'asynchronously' with help of systemd.
73 assert zone_exists
${zone}
75 service_start
"network@${zone}.service"
80 assert zone_exists
"${zone}"
82 # If the zone has already been started, we
83 # will reload it so the current configuration
85 if zone_is_active
"${zone}"; then
89 # If the zone is still down, but in auto-start mode,
91 elif zone_is_enabled
"${zone}"; then
96 # Otherwise, nothing will be done.
101 # This function will bring down the zone
102 # 'asynchronously' with help of systemd.
105 assert zone_exists
${zone}
107 service_stop
"network@${zone}.service"
112 assert zone_exists
"${zone}"
114 service_reload
"network@${zone}.service"
117 zone_hotplug_event
() {
121 hotplug_assert_in_hotplug_event
123 zone_cmd
"hotplug" "${zone}"
127 # This function will enable the zone
128 # with help of systemd.
131 assert zone_exists
"${zone}"
133 # Enable service for the zone
134 service_enable
"network@${zone}.service"
137 if [ ${ret} -eq ${EXIT_OK} ]; then
138 log INFO
"Auto-start enabled for zone ${zone}"
142 log ERROR
"Could not enable zone ${zone}: ${ret}"
147 # This function will disable the zone
148 # with help of systemd.
151 assert zone_exists
"${zone}"
153 # Disable service for the zone
154 service_disable
"network@${zone}.service"
157 if [ ${ret} -eq ${EXIT_OK} ]; then
158 log INFO
"Auto-start disabled for zone ${zone}"
162 log ERROR
"Could not disable zone ${zone}: ${ret}"
170 # Ask systemd if the zone is enabled.
171 if service_is_enabled
"network@${zone}.service"; then
182 if service_is_active
"network@${zone}.service"; then
189 zone_is_enabled_or_active
() {
193 zone_is_enabled
"${zone}" || zone_is_active
"${zone}"
204 local hook
="$(zone_get_hook ${zone})"
207 hook_exec zone
"${hook}" "${cmd}" "${zone}" "$@"
215 if ! zone_name_is_valid
${zone}; then
216 error
"Zone name '${zone}' is not valid."
220 if zone_exists
${zone}; then
221 error
"Zone '${zone}' does already exist."
225 if ! hook_zone_exists
${hook}; then
226 error
"Hook '${hook}' does not exist."
230 # Create directories for configs and ports
232 for what
in configs ports
; do
233 make_directory
"${NETWORK_ZONES_DIR}/${zone}/${what}"
236 hook_zone_exec
"${hook}" "new" "${zone}" "$@"
239 # Maybe the zone new hook did not exit correctly.
240 # If this is the case we remove the created zone immediately.
241 if [ "${ret}" != "${EXIT_OK}" ]; then
242 zone_destroy
"${zone}"
246 # Automatically enable zone.
247 zone_enable
"${zone}"
249 # Bring up the zone immediately after
257 if ! zone_exists
${zone}; then
258 error
"Zone '${zone}' does not exist."
262 local hook
="$(zone_get_hook "${zone}")"
263 if [ -z "${hook}" ]; then
264 error
"Config file did not provide any hook."
268 if ! hook_zone_exists
${hook}; then
269 error
"Hook '${hook}' does not exist."
273 hook_zone_exec
${hook} edit
${zone} "$@"
282 assert zone_exists
"${zone}"
283 assert not zone_exists
"${name}"
285 # The zone must be shut down before, is then renamed and
286 # potentially brought up again
288 # Save if the zone is running right now
289 zone_is_active
"${zone}"
290 local zone_was_active
="${?}"
292 # Save if the zone is enabled (i.e. auto-start)
293 zone_is_enabled
"${zone}"
294 local zone_was_enabled
="${?}"
300 zone_disable
"${zone}"
302 # Rename the configuration files
303 mv -f "${NETWORK_ZONES_DIR}/${zone}" "${NETWORK_ZONES_DIR}/${name}"
305 # Enable the zone if it was enabled before
306 [ ${zone_was_enabled} -eq ${EXIT_TRUE} ] && zone_enable "${name}"
308 # Start the zone if it was up before
309 [ ${zone_was_active} -eq ${EXIT_TRUE} ] && zone_start "${name}"
311 log INFO
"Zone ${zone} was renamed to ${name}"
319 # Cannot delete a zone that does not exist
320 if ! zone_exists
"${zone}"; then
321 log ERROR
"Zone ${zone} does not exist"
325 # Force the zone down.
326 zone_is_active
"${zone}" && zone_stop
"${zone}"
328 # Disable zone auto-start
329 zone_disable
"${zone}"
331 if ! rm -rf "${NETWORK_ZONES_DIR}/${zone}"; then
332 log ERROR
"Could not destroy zone ${zone}"
336 log INFO
"Destroyed zone ${zone}"
344 if ! zone_exists
${zone}; then
345 error
"Zone '${zone}' does not exist."
349 local hook
="$(zone_get_hook "${zone}")"
350 if [ -z "${hook}" ]; then
351 error
"Config file did not provide any hook."
355 if ! hook_zone_exists
${hook}; then
356 error
"Hook '${hook}' does not exist."
360 zone_db
${zone} starting
362 hook_zone_exec
${hook} up
${zone} "$@"
364 zone_db
${zone} started
366 # Execute all triggers after the zone got up
367 triggers_execute_all
"up" ZONE
="${zone}"
374 if ! zone_exists
${zone}; then
375 error
"Zone '${zone}' does not exist."
379 local hook
="$(zone_get_hook "${zone}")"
380 if [ -z "${hook}" ]; then
381 error
"Config file did not provide any hook."
385 if ! hook_zone_exists
${hook}; then
386 error
"Hook '${hook}' does not exist."
390 zone_db
${zone} stopping
392 hook_zone_exec
${hook} down
${zone} "$@"
394 zone_db
${zone} stopped
396 # Execute all triggers after the zone went down
397 triggers_execute_all
"down" ZONE
="${zone}"
405 if ! zone_exists
"${zone}"; then
406 error
"Zone '${zone}' does not exist."
410 local hook
="$(zone_get_hook "${zone}")"
411 if [ -z "${hook}" ]; then
412 error
"Config file did not provide any hook."
416 if ! hook_zone_exists
"${hook}"; then
417 error
"Hook '${hook}' does not exist."
421 hook_zone_exec
"${hook}" "status" "${zone}" "$@"
430 assert zone_exists
"${zone}"
432 log INFO
"Identifying zone ${zone}"
437 for port
in $
(zone_get_ports
"${zone}"); do
438 # Identify all the ports
439 port_identify
"${port}" --background "$@"
441 # Save the PIDs of the subprocesses
442 list_append pids
"$(cmd_background_get_pid)"
445 # Wait until all port_identfy processes have finished
446 for pid
in ${pids}; do
447 cmd_background_result
"${pid}"
459 for port
in $
(list_directory
"${NETWORK_ZONES_DIR}/${zone}/ports"); do
460 if port_exists
"${port}"; then
466 zone_get_ports_num
() {
472 for port
in $
(list_directory
"${NETWORK_ZONES_DIR}/${zone}/ports"); do
473 if port_exists
"${port}"; then
474 counter
=$
(( ${counter} + 1 ))
483 # Check, if the given port is configured
493 [ -e "${NETWORK_ZONES_DIR}/${zone}/ports/${port}" ]
503 assert zone_exists
"${zone}"
507 zone_config_new
"${zone}" "$@"
510 # usually ${1} is a valid hid
514 # We convert the hid into an id
515 local id
=$
(zone_config_convert_hid_to_id
${zone} ${hid})
517 # If id isset the hid is valid and we can go on with the id
519 zone_config_destroy
"${zone}" "${id}" "$@"
521 # If we can't get a valid hid we check if we got a valid id
523 if zone_config_id_is_valid
${zone} ${hid}; then
524 zone_config_destroy
"${zone}" ${hid} "$@"
526 log ERROR
"${id} is not a valid id or hid"
531 zone_config_list
"${zone}" "$@"
534 # usually ${1} is a valid hid
539 local id
=$
(zone_config_convert_hid_to_id
${zone} ${hid})
541 # If id isset the hid is valid and we can go on with the id
542 if isset id
&& [[ ${cmd} == "edit" ]]; then
543 zone_config_edit
"${zone}" "${id}" "$@"
545 # If we didn't get a valid hid we check if we got a valid id
547 if zone_config_id_is_valid
${zone} ${id} && [[ ${cmd} == "edit" ]]; then
549 zone_config_edit
"${zone}" "${id}" "$@"
551 # in ${hid} is saved the command after network zone ${zone} config
552 error
"Unrecognized argument: ${hid}"
553 cli_usage root-zone-config-subcommands
568 local hook
="$(zone_get_hook "${zone}")"
571 hook_zone_exec
"${hook}" "config_${cmd}" "${zone}" "$@"
578 # Create a new configuration, but exit when that was
580 zone_config_cmd
"new" "${zone}" "$@" ||
return ${?}
582 # If the config could be created, we will try to bring
583 # it up if the zone is up, too.
584 if zone_is_up
"${zone}"; then
585 zone_configs_up
"${zone}"
589 zone_config_destroy
() {
590 zone_config_cmd
"destroy" "$@"
594 zone_config_cmd
"edit" "$@"
598 # This function list in an nice way all configs of a zone
602 # Print a nice header
603 local format
="%-3s %-20s %-20s"
604 print
"${format}" "ID" "HOOK" "HID"
611 # Print for all config:
613 for config
in $
(zone_configs_list
"${zone}"); do
615 hook
=$
(zone_config_get_hook
"${zone}" "${config}")
616 hid
=$
(zone_config_get_hid
"${zone}" "${config}")
618 print
"${format}" "${id}" "${hook}" "${hid}"
622 # Returns a list of all used ids for a zone
623 zone_config_list_ids
() {
630 for config
in $
(zone_configs_list
${zone}); do
631 list_append ids
"$(config_get_id_from_config ${config})"
637 # List all hids of a zone
638 zone_config_list_hids
() {
644 for config
in $
(zone_configs_list
${zone}); do
645 zone_config_get_hid
"${zone}" "${config}"
649 # get the hid from a given config
650 zone_config_get_hid
() {
656 local hook
="$(zone_config_get_hook "${zone}" "${config}")"
658 hook_exec
"config" "${hook}" "hid" "${zone}" "${config}"
661 # Checks if a hid is valid for a given zone
662 zone_config_hid_is_valid
() {
669 for _hid
in $
(zone_config_list_hids
"${zone}"); do
670 if [[ ${_hid} = ${hid} ]]; then
678 # This function converts a hid to a id
679 zone_config_convert_hid_to_id
() {
686 for config
in $
(zone_configs_list
${zone}); do
687 # Get hook from config
688 local hook
="$(zone_config_get_hook "${zone}" "${config}")"
690 if [[ "$(hook_exec "config
" "${hook}" "hid" "${zone}" "${config}")" == "${hid}" ]]; then
691 config_get_id_from_config "${config}"
701 for zone in $(list_directory "${NETWORK_ZONES_DIR}"); do
702 if zone_exists ${zone}; then
708 zones_get_next_free() {
709 # This function return the next free zones.
710 # Example net0 upl0 upl1 are configured so the next free zones are:
714 for zone_name in ${VALID_ZONES}; do
718 local zone="${zone_name}${i}"
719 if ! zone_exists ${zone}; then
730 for zone in $(zones_get_all); do
731 zone_is_local ${zone} && echo "${zone}"
735 zones_get_nonlocal() {
737 for zone in $(zones_get_all); do
738 zone_is_nonlocal ${zone} && echo "${zone}"
748 while [ $# -gt 0 ]; do
763 if zone_name_is_valid ${1}; then
764 zones="${zones} ${1}"
766 warning "Unrecognized argument
'${1}'"
773 if [ -n "${zones}" ]; then
775 for zone in ${zones}; do
776 zone_exists ${zone} && echo "${zone}"
781 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
783 elif [ ${local} -eq 1 ]; then
785 elif [ ${remote} -eq 1 ]; then
793 list_directory "${NETWORK_ZONES_DIR}/${zone}/ports
"
805 # Check if the port actually exists.
806 if ! port_exists "${port}"; then
807 error "Cannot attach port
'${port}' which does not exist
"
811 # Check if the port is already connected to this or any other zone.
813 for z in $(zones_get_all); do
814 if zone_has_port "${z}" "${port}"; then
815 error "Port
'${port}' is already attached to zone
'${z}'"
820 local hook="$
(zone_get_hook
"${zone}")"
823 # Make the port briefly flash if supported
824 if device_exists ${port}; then
825 port_identify "${port}" --background
828 hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@
"
833 log INFO "${port} has been attached to
${zone}"
835 # Automatically connect the port
836 zone_port_start "${zone}" "${port}"
839 log CRITICAL "${port} could not be attached to
${zone}"
855 # Check if the port actually exists.
856 if ! port_exists "${port}"; then
857 error "Port
'${port}' does not exist
"
861 # Check if the zone actually has this port.
862 if ! zone_has_port "${zone}" "${port}"; then
863 error "Port
'${port}' is not attached to zone
'${zone}'"
867 local hook=$(zone_get_hook "${zone}")
870 hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@
"
882 # Check if the zone actually has this port.
883 if ! zone_has_port "${zone}" "${port}"; then
884 error "Port
'${port}' is not attached to zone
'${zone}'"
888 local hook=$(zone_get_hook "${zone}")
891 # Make the port briefly flash if supported
892 port_identify "${port}" --background
894 hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@
"
899 log INFO "${port} has been detached from
${zone}"
901 # Bring down the port if needed
902 zone_port_stop "${zone}" "${port}"
905 log CRITICAL "${port} could not be detached from
${zone}"
924 local hook="$
(zone_get_hook
"${zone}")"
927 # Dispatch command to hook
928 hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" "$@
"
932 zone_port_cmd "port_create
" "$@
"
936 zone_port_cmd "port_remove
" "$@
"
940 zone_port_cmd "port_up
" "$@
"
944 zone_port_cmd "port_down
" "$@
"
947 # The next two functions automagically bring up and down
948 # port that are attached to a bridge or similar.
949 # The problem that is tried to overcome here is that there
950 # are ports which exist all the time (like ethernet ports)
951 # and therefore do not dispatch a hotplug event when
952 # port_create is called.
958 if zone_is_active "${zone}"; then
959 if device_exists "${port}"; then
960 zone_port_up "${zone}" "${port}"
963 zone_port_create "${zone}" "${port}"
975 # Shut down the port if necessary
976 if zone_is_active "${zone}" && port_is_up "${port}"; then
977 zone_port_down "${zone}" "${port}"
981 zone_port_remove "${zone}" "${port}"
985 zone_port_cmd "port_status
" "$@
"
997 local hook="$
(zone_get_hook
"${zone}")"
1000 for port in $(zone_get_ports ${zone}); do
1001 hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" "$@
"
1005 zone_ports_create() {
1006 zone_ports_cmd "port_create
" "$@
"
1009 zone_ports_remove() {
1010 zone_ports_cmd "port_remove
" "$@
"
1014 zone_ports_cmd "port_up
" "$@
"
1018 zone_ports_cmd "port_down
" "$@
"
1021 zone_ports_status() {
1022 zone_ports_cmd "port_status
" "$@
"
1025 zone_configs_cmd() {
1032 assert zone_exists "${zone}"
1035 for config in $(zone_configs_list "${zone}"); do
1036 local config_hook="$
(zone_config_get_hook
"${zone}" "${config}")"
1037 assert isset config_hook
1039 hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" "$@
"
1044 zone_configs_cmd "up
" "$@
"
1047 zone_configs_down() {
1048 zone_configs_cmd "down
" "$@
"
1051 zone_configs_status() {
1052 zone_configs_cmd "status
" "$@
"
1055 zone_configs_list() {
1058 list_directory "${NETWORK_ZONES_DIR}/${zone}/configs
"
1061 zone_config_get_new_id() {
1062 # This functions returns the next free id for a zone
1067 local zone_path="${NETWORK_ZONES_DIR}/${zone}"
1071 if [ ! -f ${zone_path}/configs/*.${i} ]; then
1079 zone_config_check_same_setting() {
1080 # This functions checks if a config hook
1081 # with the same setting is already configured for this zone.
1082 # Returns True when yes and False when no.
1092 # The key should be local for this function
1096 for config in $(zone_configs_list ${zone}); do
1097 # Check if the config is eqal with the config we want to edit, when continue
1098 if [[ "${config}" = "${hook}.${id}" ]]; then
1102 # Check if the config is from the given hook, when not continue
1103 if [[ $(zone_config_get_hook "${zone}" "${config}") != ${hook} ]]; then
1107 # Get the value of the key for a given function
1108 zone_config_settings_read
"${zone}" "${config}" \
1109 --ignore-superfluous-settings "${key}"
1110 # Check if the value of the config and the passed value are eqal
1111 if [[ "${value}" == "${!key}" ]]; then
1116 return ${EXIT_FALSE}
1119 zone_config_get_hook
() {
1129 zone_config_settings_read
"${zone}" "${config}" \
1130 --ignore-superfluous-settings HOOK
1135 zone_config_hook_is_configured
() {
1136 # Checks if a zone has already at least one config with the given hook.
1137 # Returns True when yes and False when no
1144 for config
in $
(zone_configs_list
"${zone}"); do
1145 local config_hook
="$(zone_config_get_hook "${zone}" "${config}")"
1146 assert isset config_hook
1147 if [[ ${hook} == ${config_hook} ]]; then
1153 # If we get here the zone has no config with the given hook
1154 return ${EXIT_FALSE}
1157 zone_config_id_is_valid
() {
1158 # This function checks if a given id is valid for a zone
1159 # Return True when yes and false when no
1165 local zone_path
="${NETWORK_ZONES_DIR}/${zone}"
1167 [ -f ${zone_path}/configs
/*.
${id} ];
1170 # This function checks if a given hid is valid for a zone
1171 # Return True when yes and false when no
1172 zone_config_hid_is_valid
() {
1178 for _hid
in $
(zone_config_list_hids
${zone}); do
1179 if [[ ${_hid} == ${hid} ]]; then
1184 return ${EXIT_FALSE}
1187 zone_config_get_hook_from_id
() {
1188 # Returns the hook for a given id
1194 for config
in $
(zone_configs_list
"${zone}"); do
1195 if [[ ${config} == *.
${id} ]]; then
1196 local config_hook
="$(zone_config_get_hook "${zone}" "${config}")"
1197 assert isset config_hook
1198 print
"${config_hook}"
1203 # If we get here the zone has no config with the given id
1204 return ${EXIT_ERROR}
1217 starting|started|stopping|stopped
)
1218 db_connection_update
${zone} ${action}
1226 device_is_up
${zone}
1233 zone_get_supported_port_hooks
() {
1236 local hook
=$
(zone_get_hook
${zone})
1238 hook_zone_ports_get_all
${hook}
1241 zone_get_supported_config_hooks
() {
1245 zone_settings_read
() {
1251 if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS[*]}" ]; then
1252 list_append args
${HOOK_SETTINGS[*]}
1254 list_append args
"$@"
1257 # Save the HOOK variable.
1258 local hook
="${HOOK}"
1260 settings_read
"${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
1266 zone_settings_write() {
1270 settings_write "${NETWORK_ZONES_DIR}/${zone}/settings
" \
1271 --check="hook_check_settings
" HOOK ${HOOK_SETTINGS[*]}
1274 zone_settings_set() {
1282 zone_settings_read ${zone}
1284 for arg in ${args}; do
1288 zone_settings_write ${zone}
1292 zone_settings_get() {
1300 zone_settings_read "${zone}" "${key}" \
1301 --ignore-superfluous-settings
1307 zone_config_settings_read() {
1315 if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS[*]}" ]; then
1316 list_append args ${HOOK_SETTINGS[*]}
1318 list_append args "$@
"
1321 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
1322 settings_read "${path}" ${args}
1325 zone_config_settings_write() {
1334 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${hook}.${id}"
1335 settings_write "${path}" \
1336 --check="hook_check_config_settings
" HOOK ${HOOK_SETTINGS[*]}
1339 zone_config_settings_destroy() {
1340 # This function deletes the config file for a given zone and config
1345 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
1347 # Check if path is valid
1348 if [ ! -f ${path} ]; then
1349 log ERROR "Path
: '${path}' is not valid
"
1350 return ${EXIT_ERROR}
1353 log DEBUG "Deleting config
file ${path}"
1358 zone_config_find_by_hook() {
1366 for config in $(zone_configs_list "${zone}"); do
1367 local h="$
(zone_config_get_hook
"${zone}" "${config}")"
1369 [[ "${hook}" = "${h}" ]] && echo "${config}"
1375 zone_config_settings_read_by_hook() {
1383 for config in $(zone_config_find_by_hook "${zone}" "${hook}"); do
1384 zone_config_settings_read "${zone}" "${config}"
1390 zone_port_settings_read() {
1398 if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then
1399 list_append args ${HOOK_PORT_SETTINGS}
1401 list_append args "$@
"
1404 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1405 settings_read "${path}" ${args}
1408 zone_port_settings_write() {
1414 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1415 settings_write "${path}" \
1416 --check="hook_check_port_settings
" ${HOOK_PORT_SETTINGS[*]}
1419 zone_port_settings_remove() {
1425 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1426 settings_remove "${path}"
1430 # This function return the color of a zone
1434 color_read "zone
" ${name}
1437 zone_get_description_title() {
1441 description_title_read $(description_format_filename "zone
" "${name}")