]>
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 | ############################################################################### | |
1848564d | 21 | |
1c6a4e30 | 22 | hook_info() { |
1848564d MT |
23 | echo "HOOK=\"${HOOK}\"" |
24 | } | |
25 | ||
1c6a4e30 | 26 | hook_hotplug() { |
2a969c27 MT |
27 | # If the hook does not handle the hotplug event, it |
28 | # must return EXIT_NOT_HANDLED. | |
29 | exit ${EXIT_NOT_HANDLED} | |
30 | } | |
31 | ||
1c6a4e30 | 32 | hook_new() { |
2181765d MT |
33 | local zone="${1}" |
34 | assert isset zone | |
1848564d MT |
35 | shift |
36 | ||
d389e96b | 37 | local ${HOOK_SETTINGS[*]} |
53e764a7 MT |
38 | |
39 | # Import all default variables | |
40 | hook_set_defaults | |
41 | ||
2212045f | 42 | if ! hook_parse_cmdline "$@"; then |
79e5203e JS |
43 | return ${EXIT_ERROR} |
44 | fi | |
1848564d | 45 | |
79e5203e JS |
46 | if ! zone_settings_write "${zone}"; then |
47 | log ERROR "Could not write settings for zone ${zone}" | |
48 | return ${EXIT_ERROR} | |
49 | fi | |
1848564d MT |
50 | |
51 | exit ${EXIT_OK} | |
52 | } | |
53 | ||
1c6a4e30 | 54 | hook_edit() { |
79e5203e JS |
55 | local zone="${1}" |
56 | assert isset zone | |
57 | shift | |
58 | ||
59 | if ! zone_settings_read "${zone}"; then | |
60 | log ERROR "Could not read settings for zone ${zone}" | |
61 | return ${EXIT_ERROR} | |
62 | fi | |
63 | ||
2212045f | 64 | if ! hook_parse_cmdline "$@"; then |
79e5203e JS |
65 | return ${EXIT_ERROR} |
66 | fi | |
67 | ||
68 | if ! zone_settings_write "${zone}"; then | |
69 | log ERROR "Could not write settings for zone ${zone}" | |
70 | return ${EXIT_ERROR} | |
71 | fi | |
72 | ||
73 | exit ${EXIT_OK} | |
74 | ||
1848564d MT |
75 | } |
76 | ||
1c6a4e30 | 77 | hook_remove() { |
2181765d | 78 | cmd_not_implemented |
1848564d MT |
79 | } |
80 | ||
1c6a4e30 | 81 | hook_status() { |
2181765d MT |
82 | local zone="${1}" |
83 | assert isset zone | |
1848564d MT |
84 | |
85 | if device_is_up ${zone}; then | |
86 | exit ${STATUS_UP} | |
87 | fi | |
88 | ||
89 | exit ${STATUS_DOWN} | |
90 | } | |
91 | ||
1c6a4e30 | 92 | hook_up() { |
2181765d | 93 | cmd_not_implemented |
1848564d MT |
94 | } |
95 | ||
1c6a4e30 | 96 | hook_down() { |
2181765d | 97 | cmd_not_implemented |
1848564d MT |
98 | } |
99 | ||
1c6a4e30 | 100 | hook_discover() { |
1848564d MT |
101 | # This hook does not support a discovery |
102 | exit ${DISCOVER_NOT_SUPPORTED} | |
103 | } | |
104 | ||
105 | # Do nothing | |
1c6a4e30 | 106 | hook_parse_cmdline() { |
1848564d MT |
107 | return ${EXIT_OK} |
108 | } | |
109 | ||
1c6a4e30 | 110 | hook_port() { |
2181765d MT |
111 | local zone="${1}" |
112 | assert isset zone | |
113 | ||
114 | local action="${2}" | |
1848564d MT |
115 | shift 2 |
116 | ||
117 | local ret | |
1848564d | 118 | case "${action}" in |
e5f78859 | 119 | add|create|edit|rem|show) |
2212045f | 120 | hook_port_${action} "${zone}" "$@" |
1848564d MT |
121 | ret=$? |
122 | ;; | |
123 | *) | |
124 | error "Unrecognized argument: '${action}'" | |
125 | exit ${EXIT_ERROR} | |
126 | ;; | |
127 | esac | |
128 | ||
129 | exit ${ret} | |
130 | } | |
131 | ||
1c6a4e30 | 132 | hook_port_attach() { |
828eb94a | 133 | return ${EXIT_NOT_SUPPORTED} |
e5f78859 MT |
134 | } |
135 | ||
1c6a4e30 | 136 | hook_port_detach() { |
828eb94a | 137 | return ${EXIT_NOT_SUPPORTED} |
e5f78859 MT |
138 | } |
139 | ||
1c6a4e30 | 140 | hook_port_edit() { |
828eb94a | 141 | return ${EXIT_NOT_SUPPORTED} |
e5f78859 MT |
142 | } |
143 | ||
1c6a4e30 | 144 | hook_port_status() { |
828eb94a | 145 | return ${EXIT_NOT_SUPPORTED} |
1848564d MT |
146 | } |
147 | ||
1c6a4e30 | 148 | hook_default_port_create() { |
1ba6a2bb MT |
149 | assert [ $# -ge 2 ] |
150 | ||
151 | local zone="${1}" | |
152 | local port="${2}" | |
153 | ||
154 | port_create "${port}" | |
155 | } | |
156 | ||
1c6a4e30 | 157 | hook_port_create() { |
2212045f | 158 | hook_default_port_create "$@" |
1ba6a2bb MT |
159 | } |
160 | ||
1c6a4e30 | 161 | hook_default_port_remove() { |
1ba6a2bb MT |
162 | assert [ $# -ge 2 ] |
163 | ||
164 | local zone="${1}" | |
165 | local port="${2}" | |
166 | ||
167 | port_remove "${port}" | |
168 | } | |
169 | ||
1c6a4e30 | 170 | hook_port_remove() { |
2212045f | 171 | hook_default_port_remove "$@" |
1ba6a2bb MT |
172 | } |
173 | ||
1c6a4e30 | 174 | hook_port_up() { |
828eb94a | 175 | cmd_not_implemented |
1848564d MT |
176 | } |
177 | ||
1c6a4e30 | 178 | hook_port_down() { |
828eb94a | 179 | cmd_not_implemented |
1848564d MT |
180 | } |
181 | ||
1c6a4e30 | 182 | hook_config() { |
2181765d MT |
183 | local zone="${1}" |
184 | assert isset zone | |
185 | ||
186 | local action="${2}" | |
187 | assert isset action | |
1848564d MT |
188 | shift 2 |
189 | ||
190 | local ret | |
1848564d | 191 | case "${action}" in |
2a6b2397 | 192 | new|destroy|edit|show) |
2181765d MT |
193 | hook_config_${action} "${zone}" "$@" |
194 | exit $? | |
1848564d MT |
195 | ;; |
196 | *) | |
197 | error "Unrecognized argument: '${action}'" | |
198 | exit ${EXIT_ERROR} | |
199 | ;; | |
200 | esac | |
1848564d MT |
201 | } |
202 | ||
1c6a4e30 | 203 | hook_config_cmd() { |
2181765d MT |
204 | local cmd="${1}" |
205 | assert isset cmd | |
1848564d | 206 | |
2181765d MT |
207 | local zone="${2}" |
208 | assert isset zone | |
1848564d | 209 | |
2181765d MT |
210 | local hook_config="${3}" |
211 | assert isset hook_config | |
212 | ||
213 | shift 3 | |
214 | ||
215 | local hook_zone="$(zone_get_hook "${zone}")" | |
216 | if ! hook_zone_exists "${hook_zone}"; then | |
217 | log ERROR "Hook '${hook}' does not exist." | |
1848564d MT |
218 | exit ${EXIT_ERROR} |
219 | fi | |
220 | ||
ea699552 MT |
221 | #if ! hook_config_exists "${hook_zone}" "${hook_config}"; then |
222 | # log ERROR "Hook '${hook_config}' is not supported for zone '${zone}'." | |
223 | # exit ${EXIT_ERROR} | |
224 | #fi | |
1848564d | 225 | |
ea699552 | 226 | hook_config_exec "${hook_config}" "${cmd}" "${zone}" "$@" |
1848564d MT |
227 | } |
228 | ||
2a6b2397 | 229 | hook_config_new() { |
51492358 | 230 | assert [ $# -ge 2 ] |
a5ebb169 | 231 | |
2a6b2397 | 232 | hook_config_cmd "new" "$@" |
1848564d MT |
233 | } |
234 | ||
2a6b2397 | 235 | hook_config_destroy() { |
6002aef1 JS |
236 | assert [ $# -eq 2 ] |
237 | local zone=${1} | |
238 | # The id must be the id and not the hid. | |
239 | local id=${2} | |
240 | ||
241 | shift 2 | |
242 | ||
243 | # Check if we get a valid id | |
244 | if ! zone_config_id_is_valid ${zone} ${id}; then | |
245 | log ERROR "ID: ${id} is not a valid id for zone ${zone}" | |
246 | fi | |
247 | ||
248 | local hook=$(zone_config_get_hook_from_id ${zone} ${id}) | |
249 | assert isset hook | |
250 | ||
a6ef0714 JS |
251 | # First we bring the hook down |
252 | hook_config_cmd "down" "${zone}" "${hook}" "${hook}.${id}" | |
253 | ||
254 | # If a hook_destroy function is implemented in the hook this function will be executed. | |
255 | # If not a empty defined in header-config is executed. | |
256 | if ! hook_config_cmd "destroy" "${zone}" "${hook}" "${hook}.${id}" "$@"; then | |
257 | ||
258 | # A better error message should printed inside the hook. | |
259 | # We will not bring the config up because we do not know if it is safe or if some parts are already destroyed. | |
260 | log ERROR "Could not destroy config with the follwoing id: ${id}" | |
261 | return ${EXIT_ERROR} | |
262 | fi | |
263 | ||
264 | # Now we delete the config of the zone | |
265 | zone_config_settings_destroy "${zone}" "${hook}.${id}" | |
1848564d MT |
266 | } |
267 | ||
2a6b2397 | 268 | hook_config_edit() { |
15be9d69 | 269 | assert [ $# -ge 2 ] |
9ee04d82 JS |
270 | local zone=${1} |
271 | # The id must be the id and not the hid. | |
272 | local id=${2} | |
273 | ||
274 | shift 2 | |
275 | ||
276 | # Check if we get a valid id | |
277 | if ! zone_config_id_is_valid ${zone} ${id}; then | |
278 | log ERROR "ID: ${id} is not a valid id for zone ${zone}" | |
279 | fi | |
280 | ||
281 | local hook=$(zone_config_get_hook_from_id ${zone} ${id}) | |
282 | assert isset hook | |
283 | ||
284 | hook_config_cmd "edit" "${zone}" "${hook}" "${hook}.${id}" "$@" | |
1848564d MT |
285 | } |
286 | ||
1c6a4e30 | 287 | hook_config_show() { |
2181765d | 288 | cmd_not_implemented |
1848564d MT |
289 | } |
290 | ||
1c6a4e30 | 291 | hook_ppp_write_config() { |
2181765d | 292 | cmd_not_implemented |
97cb552e MT |
293 | |
294 | # Arguments: <zone> <filename> | |
295 | } | |
296 | ||
1c6a4e30 | 297 | hook_ppp_ip_pre_up() { |
2181765d MT |
298 | local zone="${1}" |
299 | assert isset zone | |
c7ad7801 MT |
300 | shift |
301 | ||
2181765d MT |
302 | if ! zone_exists "${zone}"; then |
303 | log ERROR "Zone '${zone}' does not exist." | |
c7ad7801 MT |
304 | exit ${EXIT_ERROR} |
305 | fi | |
306 | ||
2181765d | 307 | ppp_common_ip_pre_up "${zone}" "$@" |
c7ad7801 | 308 | exit $? |
b4038eca MT |
309 | } |
310 | ||
1c6a4e30 | 311 | hook_ppp_ipv4_up() { |
2181765d MT |
312 | local zone="${1}" |
313 | assert isset zone | |
c7ad7801 MT |
314 | shift |
315 | ||
2181765d MT |
316 | if ! zone_exists "${zone}"; then |
317 | log ERROR "Zone '${zone}' does not exist." | |
c7ad7801 MT |
318 | exit ${EXIT_ERROR} |
319 | fi | |
320 | ||
2181765d | 321 | ppp_common_ipv4_up "${zone}" "$@" |
c7ad7801 | 322 | exit $? |
b4038eca MT |
323 | } |
324 | ||
1c6a4e30 | 325 | hook_ppp_ipv4_down() { |
2181765d MT |
326 | local zone="${1}" |
327 | assert isset zone | |
c7ad7801 MT |
328 | shift |
329 | ||
2181765d MT |
330 | if ! zone_exists "${zone}"; then |
331 | log ERROR "Zone '${zone}' does not exist." | |
c7ad7801 MT |
332 | exit ${EXIT_ERROR} |
333 | fi | |
334 | ||
2181765d | 335 | ppp_common_ipv4_down "${zone}" "$@" |
c7ad7801 | 336 | exit $? |
b4038eca MT |
337 | } |
338 | ||
1c6a4e30 | 339 | hook_ppp_ipv6_up() { |
2181765d MT |
340 | local zone="${1}" |
341 | assert isset zone | |
201b7dff MT |
342 | shift |
343 | ||
2181765d | 344 | if ! zone_exists "${zone}"; then |
201b7dff MT |
345 | error "Zone '${zone}' does not exist." |
346 | exit ${EXIT_ERROR} | |
347 | fi | |
348 | ||
2181765d | 349 | ppp_common_ipv6_up "${zone}" "$@" |
201b7dff MT |
350 | exit $? |
351 | } | |
352 | ||
1c6a4e30 | 353 | hook_ppp_ipv6_down() { |
2181765d MT |
354 | local zone="${1}" |
355 | assert isset zone | |
201b7dff MT |
356 | shift |
357 | ||
2181765d | 358 | if ! zone_exists "${zone}"; then |
201b7dff MT |
359 | error "Zone '${zone}' does not exist." |
360 | exit ${EXIT_ERROR} | |
361 | fi | |
362 | ||
2181765d | 363 | ppp_common_ipv6_down "${zone}" "$@" |
201b7dff MT |
364 | exit $? |
365 | } |