]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.hook
Introduce list_directory
[people/ms/network.git] / src / functions / functions.hook
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
1c6a4e30 22hook_dir() {
d61a01d4
MT
23 local type=${1}
24
943e3f7e
MT
25 if [ -n "${type}" ]; then
26 type="/${type}s"
27 fi
28
d2a21d01 29 echo "${NETWORK_HOOKS_DIR}${type}"
d61a01d4 30}
f41fa3d7 31NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
d61a01d4 32
1c6a4e30 33hook_exists() {
d61a01d4
MT
34 local type=${1}
35 local hook=${2}
1848564d 36
943e3f7e
MT
37 assert isset type
38 assert isset hook
39
f41fa3d7
MT
40 # Add the path prefix.
41 hook="$(hook_dir ${type})/${hook}"
1848564d 42
f41fa3d7 43 [ ! -d "${hook}" ] && [ -x "${hook}" ]
1848564d
MT
44}
45
1c6a4e30 46hook_exec() {
2181765d 47 local type="${1}"
943e3f7e 48 assert isset type
2181765d
MT
49
50 local hook="${2}"
943e3f7e 51 assert isset hook
2181765d
MT
52
53 local cmd="${3}"
f41fa3d7
MT
54 assert isset cmd
55
56 assert hook_exists "${type}" "${hook}"
2181765d
MT
57 shift 3
58
59 # Complete the hook command by prepending "hook_"
60 local hook_cmd="hook_${cmd}"
61
62 # Check if the hook action is valid.
426d620e 63 assert hook_valid_command "${type}" "${cmd}"
2181765d
MT
64
65 local hook_path="$(hook_dir ${type})/${hook}"
f41fa3d7
MT
66
67 # For performance reasons, all hooks are executed
68 # in a subshell and so will inherit the currently
69 # running environment.
70 (
71 # Set the name of the hook.
72 HOOK=$(basename ${hook})
73
74 # Source the code of the hook.
2181765d 75 source "${hook_path}"
f41fa3d7
MT
76
77 # Make sure HOOK is still properly set.
78 assert isset HOOK
943e3f7e 79
f41fa3d7 80 # Execute the requested command.
1ba6a2bb 81 "${hook_cmd}" "$@"
f41fa3d7
MT
82 )
83 local ret=$?
84
2181765d 85 case "${ret}" in
828eb94a
MT
86 ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED})
87 log ERROR "Hook '${hook}' does not implement the method '${cmd}':"
88 log ERROR " arguments: $@"
31cd8db9 89 return ${EXIT_COMMAND_NOT_FOUND}
2181765d
MT
90 ;;
91 ${EXIT_ERROR_ASSERT})
92 log ERROR "Hook exited with an assertion error."
31cd8db9 93 return ${EXIT_ERROR_ASSERT}
2181765d
MT
94 ;;
95 esac
d61a01d4 96
f41fa3d7 97 return ${ret}
d61a01d4
MT
98}
99
1c6a4e30 100hook_list() {
ea699552
MT
101 local type="${1}"
102
103 local dir="$(hook_dir "${type}")"
104 assert isset dir
105
106 local hook
60b1f378 107 for hook in $(list_directory "${dir}"); do
ea699552
MT
108 if hook_exists "${type}" "${hook}"; then
109 echo "${hook}"
110 fi
111 done
112}
113
ccbc0dd4 114# The default help function.
1c6a4e30 115hook_help() {
ccbc0dd4
MT
116 # If no man page has been configured, we print an error message.
117 if [ -z "${HOOK_MANPAGE}" ]; then
118 error "There is no help available for hook '${HOOK}'. Exiting."
119 exit ${EXIT_ERROR}
120 fi
121
122 cli_show_man "${HOOK_MANPAGE}"
123
124 exit $?
125}
126
1c6a4e30 127config_get_hook() {
d61a01d4
MT
128 local config=${1}
129
943e3f7e 130 assert isset config
a5ebb169 131 assert [ -e "${config}" ]
943e3f7e 132
d61a01d4
MT
133 (
134 . ${config}
135 echo "${HOOK}"
136 )
137}
138
1c6a4e30 139hook_zone_exists() {
2212045f 140 hook_exists zone "$@"
d61a01d4
MT
141}
142
1c6a4e30 143hook_zone_exec() {
2212045f 144 hook_exec zone "$@"
1848564d
MT
145}
146
1c6a4e30 147hook_zone_get_all() {
ea699552 148 hook_list zone
1848564d
MT
149}
150
1c6a4e30 151hook_config_exists() {
2212045f 152 hook_exists config "$@"
ea699552 153}
a5ebb169 154
1c6a4e30 155hook_config_exec() {
2212045f 156 hook_exec config "$@"
ea699552 157}
a5ebb169 158
1c6a4e30 159hook_config_get_all() {
ea699552 160 hook_list config
1848564d 161}
426d620e 162
1c6a4e30 163hook_valid_command() {
426d620e
MT
164 local type="${1}"
165 local cmd="${2}"
166
167 case "${type}" in
168 config)
169 hook_valid_command_config "${cmd}"
170 return ${?}
171 ;;
172 port)
173 hook_valid_command_port "${cmd}"
174 return ${?}
175 ;;
176 zone)
177 hook_valid_command_zone "${cmd}"
178 return ${?}
179 ;;
180 esac
181
182 return ${EXIT_FALSE}
183}
184
1c6a4e30 185hook_valid_command_config() {
426d620e
MT
186 local cmd="${1}"
187
188 case "${cmd}" in
e3b67c2f 189 new|destroy|edit|up|down|status|hid)
426d620e
MT
190 return ${EXIT_TRUE}
191 ;;
192 esac
193
194 return ${EXIT_FALSE}
195}
196
1c6a4e30 197hook_valid_command_port() {
426d620e
MT
198 local cmd="${1}"
199
200 case "${cmd}" in
201 # Configuration hooks
202 new|edit|destroy)
203 return ${EXIT_TRUE}
204 ;;
205
206 # Control hooks
207 create|remove|up|down)
208 return ${EXIT_TRUE}
209 ;;
210
211 # Hotplug
212 hotplug|hotplug_rename)
213 return ${EXIT_TRUE}
214 ;;
215
216 # Status
d0fde18f 217 status|children)
426d620e
MT
218 return ${EXIT_TRUE}
219 ;;
220 esac
221
222 return ${EXIT_FALSE}
223}
224
1c6a4e30 225hook_valid_command_zone() {
426d620e
MT
226 local cmd="${1}"
227
228 case "${cmd}" in
229 # Configuration hooks
230 new|edit|destroy)
231 return ${EXIT_TRUE}
232 ;;
233
2a6b2397 234 config_new|config_destroy|config_edit|config_show)
426d620e
MT
235 return ${EXIT_TRUE}
236 ;;
237
238 # Control hooks
239 up|down)
240 return ${EXIT_TRUE}
241 ;;
242
243 # Hotplug
244 hotplug)
245 return ${EXIT_TRUE}
246 ;;
247
248 # Ports
76be48f2 249 port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down)
426d620e
MT
250 return ${EXIT_TRUE}
251 ;;
252
253 # Status
254 status|info|help)
255 return ${EXIT_TRUE}
256 ;;
257
258 # Discovery
259 discover)
260 return ${EXIT_TRUE}
261 ;;
262
263 # PPP
264 ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config)
265 return ${EXIT_TRUE}
266 ;;
267 esac
268
269 return ${EXIT_FALSE}
270}