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