]>
git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.hook
8146277bbc169f3a8f6c12f4a7118a3de98d150f
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_edit hook_status hook_remove"
23 HOOK_COMMANDS_CONFIG
="${HOOK_COMMANDS_CONFIG} hook_up"
25 HOOK_COMMANDS_PORT
="hook_new hook_edit hook_create hook_remove hook_up hook_down \
26 hook_hotplug hook_hotplug_rename hook_info hook_status"
28 HOOK_COMMANDS_ZONE
="hook_add hook_create hook_discover hook_down hook_edit hook_help \
29 hook_info hook_remove hook_status hook_up hook_hotplug \
31 hook_config_create hook_config_edit hook_config_remove hook_config_show \
33 hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
34 hook_port_status hook_port_up hook_port_down \
36 hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
37 hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
42 if [ -n "${type}" ]; then
46 echo "${NETWORK_HOOKS_DIR}${type}"
48 NETWORK_HOOKS_DIR_ZONES
="$(hook_dir zone)"
50 function hook_exists
() {
57 # Add the path prefix.
58 hook
="$(hook_dir ${type})/${hook}"
60 [ ! -d "${hook}" ] && [ -x "${hook}" ]
63 function hook_exec
() {
73 assert hook_exists
"${type}" "${hook}"
76 # Complete the hook command by prepending "hook_"
77 local hook_cmd
="hook_${cmd}"
79 # Check if the hook action is valid.
83 valid_commands
="${HOOK_COMMANDS_CONFIG}"
86 valid_commands
="${HOOK_COMMANDS_PORT}"
89 valid_commands
="${HOOK_COMMANDS_ZONE}"
92 isset valid_commands
&& assert list_match
"${hook_cmd}" ${valid_commands}
94 local hook_path
="$(hook_dir ${type})/${hook}"
96 # For performance reasons, all hooks are executed
97 # in a subshell and so will inherit the currently
98 # running environment.
100 # Set the name of the hook.
101 HOOK
=$
(basename ${hook})
103 # Source the code of the hook.
104 source "${hook_path}"
106 # Make sure HOOK is still properly set.
109 # Execute the requested command.
115 ${EXIT_COMMAND_NOT_FOUND}|
${EXIT_NOT_SUPPORTED})
116 log ERROR
"Hook '${hook}' does not implement the method '${cmd}':"
117 log ERROR
" arguments: $@"
118 exit ${EXIT_COMMAND_NOT_FOUND}
120 ${EXIT_ERROR_ASSERT})
121 log ERROR
"Hook exited with an assertion error."
122 exit ${EXIT_ERROR_ASSERT}
129 function hook_list
() {
132 local dir
="$(hook_dir "${type}")"
136 for hook
in ${dir}/*; do
137 hook
="$(basename "${hook}")"
139 if hook_exists
"${type}" "${hook}"; then
145 # The default help function.
146 function hook_help
() {
147 # If no man page has been configured, we print an error message.
148 if [ -z "${HOOK_MANPAGE}" ]; then
149 error
"There is no help available for hook '${HOOK}'. Exiting."
153 cli_show_man
"${HOOK_MANPAGE}"
158 function config_get_hook
() {
162 assert
[ -e "${config}" ]
170 function hook_zone_exists
() {
174 function hook_zone_exec
() {
178 function hook_zone_get_all
() {
182 function hook_config_exists
() {
183 hook_exists config $@
186 function hook_config_exec
() {
190 function hook_config_get_all
() {