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 ###############################################################################
22 HOOK_COMMANDS_CONFIG
="hook_create hook_down hook_status hook_up"
24 HOOK_COMMANDS_PORT
="hook_create hook_down hook_hotplug hook_hotplug_rename \
25 hook_info hook_status hook_up"
27 HOOK_COMMANDS_ZONE
="hook_add hook_create hook_discover hook_down hook_edit hook_help \
28 hook_info hook_remove hook_status hook_up \
30 hook_config_create hook_config_edit hook_config_remove hook_config_show \
32 hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
33 hook_port_status hook_port_up hook_port_down \
35 hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
36 hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
41 if [ -n "${type}" ]; then
45 echo "${NETWORK_HOOKS_DIR}${type}"
47 NETWORK_HOOKS_DIR_ZONES
="$(hook_dir zone)"
49 function hook_exists
() {
56 # Add the path prefix.
57 hook
="$(hook_dir ${type})/${hook}"
59 [ ! -d "${hook}" ] && [ -x "${hook}" ]
62 function hook_exec
() {
72 assert hook_exists
"${type}" "${hook}"
75 # Complete the hook command by prepending "hook_"
76 local hook_cmd
="hook_${cmd}"
78 # Check if the hook action is valid.
82 valid_commands
="${HOOK_COMMANDS_CONFIG}"
85 valid_commands
="${HOOK_COMMANDS_PORT}"
88 valid_commands
="${HOOK_COMMANDS_ZONE}"
91 isset valid_commands
&& assert list_match
"${hook_cmd}" ${valid_commands}
93 local hook_path
="$(hook_dir ${type})/${hook}"
95 # For performance reasons, all hooks are executed
96 # in a subshell and so will inherit the currently
97 # running environment.
99 # Set the name of the hook.
100 HOOK
=$
(basename ${hook})
102 # Source the code of the hook.
103 source "${hook_path}"
105 # Make sure HOOK is still properly set.
108 # Execute the requested command.
109 cmd
"${hook_cmd}" "$@"
114 ${EXIT_COMMAND_NOT_FOUND})
115 log ERROR
"Hook command not implemented: ${hook_command} ($@)"
116 exit ${EXIT_COMMAND_NOT_FOUND}
118 ${EXIT_ERROR_ASSERT})
119 log ERROR
"Hook exited with an assertion error."
120 exit ${EXIT_ERROR_ASSERT}
127 function config_get_hook
() {
131 assert
[ -e "${config}" ]
139 function hook_zone_exists
() {
143 function hook_zone_port_exists
() {
147 hook_exists zone
"${hook_zone}.ports/${hook_port}"
150 function hook_zone_config_exists
() {
152 local hook_config
=${2}
154 hook_exists zone
"${hook_zone}.configs/${hook_config}"
157 function hook_zone_has_ports
() {
160 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.ports" ]
163 function hook_zone_has_configs
() {
166 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ]
169 function hook_zone_exec
() {
173 function hook_zone_port_exec
() {
178 hook_zone_exec
"${hook_zone}.ports/${hook_port}" $@
181 function hook_zone_config_exec
() {
186 hook_zone_exec
"${hook_zone}.configs/${hook_port}" $@
189 function hook_zone_get_all
() {
193 for hook
in $
(hook_dir zone
)/*; do
194 hook
=$
(basename ${hook})
195 hook_zone_exists
${hook} && echo "${hook}"
199 function hook_zone_ports_get_all
() {
202 if ! hook_exists zone
${hook}; then
203 error
"Hook '${hook}' does not exist."
207 # If the zone hook has got no ports we exit silently
208 if ! hook_zone_has_ports
${hook}; then
213 for h
in $
(hook_dir zone
)/${hook}.ports
/*; do
215 if hook_zone_port_exists
${hook} ${h}; then
221 function hook_zone_configs_get_all
() {
224 if ! hook_exists zone
${hook}; then
225 error
"Hook '${hook}' does not exist."
229 # If the zone hook has got no configurations we exit silently
230 if ! hook_zone_has_configs
${hook}; then
235 for h
in $
(hook_dir zone
)/${hook}.configs
/*; do
237 if hook_zone_config_exists
${hook} ${h}; then