]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.hook
b8049db5b7a62336e6bcb522407dcc790db71086
[people/stevee/network.git] / src / functions / functions.hook
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
22 hook_dir() {
23 local type=${1}
24
25 if [ -n "${type}" ]; then
26 type="/${type}s"
27 fi
28
29 echo "${NETWORK_HOOKS_DIR}${type}"
30 }
31 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
32
33 hook_list() {
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
49 hook_exists() {
50 local type=${1}
51 local hook=${2}
52
53 assert isset type
54 assert isset hook
55
56 # Add the path prefix.
57 hook="$(hook_dir ${type})/${hook}"
58
59 [ ! -d "${hook}" ] && [ -x "${hook}" ]
60 }
61
62 hook_exec() {
63 local type="${1}"
64 assert isset type
65
66 local hook="${2}"
67 assert isset hook
68
69 local cmd="${3}"
70 assert isset cmd
71
72 assert hook_exists "${type}" "${hook}"
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.
79 assert hook_valid_command "${type}" "${cmd}"
80
81 local hook_path="$(hook_dir ${type})/${hook}"
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.
91 source "${hook_path}"
92
93 # Make sure HOOK is still properly set.
94 assert isset HOOK
95
96 # Execute the requested command.
97 "${hook_cmd}" "$@"
98 )
99 local ret=$?
100
101 case "${ret}" in
102 ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED})
103 log ERROR "Hook '${hook}' does not implement the method '${cmd}':"
104 log ERROR " arguments: $@"
105 return ${EXIT_COMMAND_NOT_FOUND}
106 ;;
107 ${EXIT_ERROR_ASSERT})
108 log ERROR "Hook exited with an assertion error."
109 return ${EXIT_ERROR_ASSERT}
110 ;;
111 esac
112
113 return ${ret}
114 }
115
116 hook_list() {
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
132 # The default help function.
133 hook_help() {
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
145 config_get_hook() {
146 local config=${1}
147
148 assert isset config
149 assert [ -e "${config}" ]
150
151 (
152 . ${config}
153 echo "${HOOK}"
154 )
155 }
156
157 hook_zone_exists() {
158 hook_exists zone $@
159 }
160
161 hook_zone_exec() {
162 hook_exec zone $@
163 }
164
165 hook_zone_get_all() {
166 hook_list zone
167 }
168
169 hook_config_exists() {
170 hook_exists config $@
171 }
172
173 hook_config_exec() {
174 hook_exec config $@
175 }
176
177 hook_config_get_all() {
178 hook_list config
179 }
180
181 hook_valid_command() {
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
203 hook_valid_command_config() {
204 local cmd="${1}"
205
206 case "${cmd}" in
207 new|destroy|edit|up|down|status|hid)
208 return ${EXIT_TRUE}
209 ;;
210 esac
211
212 return ${EXIT_FALSE}
213 }
214
215 hook_valid_command_port() {
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|children)
236 return ${EXIT_TRUE}
237 ;;
238 esac
239
240 return ${EXIT_FALSE}
241 }
242
243 hook_valid_command_zone() {
244 local cmd="${1}"
245
246 case "${cmd}" in
247 # Configuration hooks
248 new|edit|destroy)
249 return ${EXIT_TRUE}
250 ;;
251
252 config_new|config_destroy|config_edit|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
267 port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down)
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 }