]>
Commit | Line | Data |
---|---|---|
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 | ||
d61a01d4 MT |
22 | function hook_dir() { |
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 | 31 | NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)" |
d61a01d4 | 32 | |
1848564d | 33 | function hook_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 | ||
d61a01d4 | 46 | function hook_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: $@" | |
2181765d MT |
89 | exit ${EXIT_COMMAND_NOT_FOUND} |
90 | ;; | |
91 | ${EXIT_ERROR_ASSERT}) | |
92 | log ERROR "Hook exited with an assertion error." | |
93 | exit ${EXIT_ERROR_ASSERT} | |
94 | ;; | |
95 | esac | |
d61a01d4 | 96 | |
f41fa3d7 | 97 | return ${ret} |
d61a01d4 MT |
98 | } |
99 | ||
ea699552 MT |
100 | function hook_list() { |
101 | local type="${1}" | |
102 | ||
103 | local dir="$(hook_dir "${type}")" | |
104 | assert isset dir | |
105 | ||
106 | local hook | |
107 | for hook in ${dir}/*; do | |
108 | hook="$(basename "${hook}")" | |
109 | ||
110 | if hook_exists "${type}" "${hook}"; then | |
111 | echo "${hook}" | |
112 | fi | |
113 | done | |
114 | } | |
115 | ||
ccbc0dd4 MT |
116 | # The default help function. |
117 | function hook_help() { | |
118 | # If no man page has been configured, we print an error message. | |
119 | if [ -z "${HOOK_MANPAGE}" ]; then | |
120 | error "There is no help available for hook '${HOOK}'. Exiting." | |
121 | exit ${EXIT_ERROR} | |
122 | fi | |
123 | ||
124 | cli_show_man "${HOOK_MANPAGE}" | |
125 | ||
126 | exit $? | |
127 | } | |
128 | ||
d61a01d4 MT |
129 | function config_get_hook() { |
130 | local config=${1} | |
131 | ||
943e3f7e | 132 | assert isset config |
a5ebb169 | 133 | assert [ -e "${config}" ] |
943e3f7e | 134 | |
d61a01d4 MT |
135 | ( |
136 | . ${config} | |
137 | echo "${HOOK}" | |
138 | ) | |
139 | } | |
140 | ||
d61a01d4 MT |
141 | function hook_zone_exists() { |
142 | hook_exists zone $@ | |
143 | } | |
144 | ||
d61a01d4 MT |
145 | function hook_zone_exec() { |
146 | hook_exec zone $@ | |
1848564d MT |
147 | } |
148 | ||
d61a01d4 | 149 | function hook_zone_get_all() { |
ea699552 | 150 | hook_list zone |
1848564d MT |
151 | } |
152 | ||
ea699552 MT |
153 | function hook_config_exists() { |
154 | hook_exists config $@ | |
155 | } | |
a5ebb169 | 156 | |
ea699552 MT |
157 | function hook_config_exec() { |
158 | hook_exec config $@ | |
159 | } | |
a5ebb169 | 160 | |
ea699552 MT |
161 | function hook_config_get_all() { |
162 | hook_list config | |
1848564d | 163 | } |
426d620e MT |
164 | |
165 | function hook_valid_command() { | |
166 | local type="${1}" | |
167 | local cmd="${2}" | |
168 | ||
169 | case "${type}" in | |
170 | config) | |
171 | hook_valid_command_config "${cmd}" | |
172 | return ${?} | |
173 | ;; | |
174 | port) | |
175 | hook_valid_command_port "${cmd}" | |
176 | return ${?} | |
177 | ;; | |
178 | zone) | |
179 | hook_valid_command_zone "${cmd}" | |
180 | return ${?} | |
181 | ;; | |
182 | esac | |
183 | ||
184 | return ${EXIT_FALSE} | |
185 | } | |
186 | ||
187 | function hook_valid_command_config() { | |
188 | local cmd="${1}" | |
189 | ||
190 | case "${cmd}" in | |
191 | create|remove|edit|up|down|status) | |
192 | return ${EXIT_TRUE} | |
193 | ;; | |
194 | esac | |
195 | ||
196 | return ${EXIT_FALSE} | |
197 | } | |
198 | ||
199 | function hook_valid_command_port() { | |
200 | local cmd="${1}" | |
201 | ||
202 | case "${cmd}" in | |
203 | # Configuration hooks | |
204 | new|edit|destroy) | |
205 | return ${EXIT_TRUE} | |
206 | ;; | |
207 | ||
208 | # Control hooks | |
209 | create|remove|up|down) | |
210 | return ${EXIT_TRUE} | |
211 | ;; | |
212 | ||
213 | # Hotplug | |
214 | hotplug|hotplug_rename) | |
215 | return ${EXIT_TRUE} | |
216 | ;; | |
217 | ||
218 | # Status | |
219 | status|info) | |
220 | return ${EXIT_TRUE} | |
221 | ;; | |
222 | esac | |
223 | ||
224 | return ${EXIT_FALSE} | |
225 | } | |
226 | ||
227 | function hook_valid_command_zone() { | |
228 | local cmd="${1}" | |
229 | ||
230 | case "${cmd}" in | |
231 | # Configuration hooks | |
232 | new|edit|destroy) | |
233 | return ${EXIT_TRUE} | |
234 | ;; | |
235 | ||
236 | config_create|config_edit|config_remove|config_show) | |
237 | return ${EXIT_TRUE} | |
238 | ;; | |
239 | ||
240 | # Control hooks | |
241 | up|down) | |
242 | return ${EXIT_TRUE} | |
243 | ;; | |
244 | ||
245 | # Hotplug | |
246 | hotplug) | |
247 | return ${EXIT_TRUE} | |
248 | ;; | |
249 | ||
250 | # Ports | |
09be0884 | 251 | port_add|port_edit|port_create|port_remove|port_show|port_status|port_up|port_down) |
426d620e MT |
252 | return ${EXIT_TRUE} |
253 | ;; | |
254 | ||
255 | # Status | |
256 | status|info|help) | |
257 | return ${EXIT_TRUE} | |
258 | ;; | |
259 | ||
260 | # Discovery | |
261 | discover) | |
262 | return ${EXIT_TRUE} | |
263 | ;; | |
264 | ||
265 | # PPP | |
266 | ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config) | |
267 | return ${EXIT_TRUE} | |
268 | ;; | |
269 | esac | |
270 | ||
271 | return ${EXIT_FALSE} | |
272 | } |