]>
Commit | Line | Data |
---|---|---|
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_info() { | |
23 | echo "HOOK=\"${HOOK}\"" | |
24 | } | |
25 | ||
26 | hook_hotplug() { | |
27 | # If the hook does not handle the hotplug event, it | |
28 | # must return EXIT_NOT_HANDLED. | |
29 | exit ${EXIT_NOT_HANDLED} | |
30 | } | |
31 | ||
32 | hook_new() { | |
33 | local zone="${1}" | |
34 | assert isset zone | |
35 | shift | |
36 | ||
37 | local ${HOOK_SETTINGS[*]} | |
38 | ||
39 | # Import all default variables | |
40 | hook_set_defaults | |
41 | ||
42 | if ! hook_parse_cmdline "$@"; then | |
43 | return ${EXIT_ERROR} | |
44 | fi | |
45 | ||
46 | if ! zone_settings_write "${zone}"; then | |
47 | log ERROR "Could not write settings for zone ${zone}" | |
48 | return ${EXIT_ERROR} | |
49 | fi | |
50 | ||
51 | exit ${EXIT_OK} | |
52 | } | |
53 | ||
54 | hook_edit() { | |
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 | ||
64 | if ! hook_parse_cmdline "$@"; then | |
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 | ||
75 | } | |
76 | ||
77 | hook_remove() { | |
78 | cmd_not_implemented | |
79 | } | |
80 | ||
81 | hook_status() { | |
82 | local zone="${1}" | |
83 | assert isset zone | |
84 | ||
85 | if device_is_up ${zone}; then | |
86 | exit ${STATUS_UP} | |
87 | fi | |
88 | ||
89 | exit ${STATUS_DOWN} | |
90 | } | |
91 | ||
92 | hook_up() { | |
93 | cmd_not_implemented | |
94 | } | |
95 | ||
96 | hook_down() { | |
97 | cmd_not_implemented | |
98 | } | |
99 | ||
100 | hook_discover() { | |
101 | # This hook does not support a discovery | |
102 | exit ${DISCOVER_NOT_SUPPORTED} | |
103 | } | |
104 | ||
105 | # Do nothing | |
106 | hook_parse_cmdline() { | |
107 | return ${EXIT_OK} | |
108 | } | |
109 | ||
110 | hook_port() { | |
111 | local zone="${1}" | |
112 | assert isset zone | |
113 | ||
114 | local action="${2}" | |
115 | shift 2 | |
116 | ||
117 | local ret | |
118 | case "${action}" in | |
119 | add|create|edit|rem|show) | |
120 | hook_port_${action} "${zone}" "$@" | |
121 | ret=$? | |
122 | ;; | |
123 | *) | |
124 | error "Unrecognized argument: '${action}'" | |
125 | exit ${EXIT_ERROR} | |
126 | ;; | |
127 | esac | |
128 | ||
129 | exit ${ret} | |
130 | } | |
131 | ||
132 | hook_port_attach() { | |
133 | return ${EXIT_NOT_SUPPORTED} | |
134 | } | |
135 | ||
136 | hook_port_detach() { | |
137 | return ${EXIT_NOT_SUPPORTED} | |
138 | } | |
139 | ||
140 | hook_port_edit() { | |
141 | return ${EXIT_NOT_SUPPORTED} | |
142 | } | |
143 | ||
144 | hook_port_status() { | |
145 | return ${EXIT_NOT_SUPPORTED} | |
146 | } | |
147 | ||
148 | hook_default_port_create() { | |
149 | assert [ $# -ge 2 ] | |
150 | ||
151 | local zone="${1}" | |
152 | local port="${2}" | |
153 | ||
154 | port_create "${port}" | |
155 | } | |
156 | ||
157 | hook_port_create() { | |
158 | hook_default_port_create "$@" | |
159 | } | |
160 | ||
161 | hook_default_port_remove() { | |
162 | assert [ $# -ge 2 ] | |
163 | ||
164 | local zone="${1}" | |
165 | local port="${2}" | |
166 | ||
167 | port_remove "${port}" | |
168 | } | |
169 | ||
170 | hook_port_remove() { | |
171 | hook_default_port_remove "$@" | |
172 | } | |
173 | ||
174 | hook_port_up() { | |
175 | cmd_not_implemented | |
176 | } | |
177 | ||
178 | hook_port_down() { | |
179 | cmd_not_implemented | |
180 | } | |
181 | ||
182 | hook_config() { | |
183 | local zone="${1}" | |
184 | assert isset zone | |
185 | ||
186 | local action="${2}" | |
187 | assert isset action | |
188 | shift 2 | |
189 | ||
190 | local ret | |
191 | case "${action}" in | |
192 | new|destroy|edit|show) | |
193 | hook_config_${action} "${zone}" "$@" | |
194 | exit $? | |
195 | ;; | |
196 | *) | |
197 | error "Unrecognized argument: '${action}'" | |
198 | exit ${EXIT_ERROR} | |
199 | ;; | |
200 | esac | |
201 | } | |
202 | ||
203 | hook_config_cmd() { | |
204 | local cmd="${1}" | |
205 | assert isset cmd | |
206 | ||
207 | local zone="${2}" | |
208 | assert isset zone | |
209 | ||
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." | |
218 | exit ${EXIT_ERROR} | |
219 | fi | |
220 | ||
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 | |
225 | ||
226 | hook_config_exec "${hook_config}" "${cmd}" "${zone}" "$@" | |
227 | } | |
228 | ||
229 | hook_config_new() { | |
230 | assert [ $# -ge 2 ] | |
231 | ||
232 | hook_config_cmd "new" "$@" | |
233 | } | |
234 | ||
235 | hook_config_destroy() { | |
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 | ||
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}" | |
266 | } | |
267 | ||
268 | hook_config_edit() { | |
269 | assert [ $# -ge 2 ] | |
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}" "$@" | |
285 | } | |
286 | ||
287 | hook_config_show() { | |
288 | cmd_not_implemented | |
289 | } | |
290 | ||
291 | hook_ppp_write_config() { | |
292 | cmd_not_implemented | |
293 | ||
294 | # Arguments: <zone> <filename> | |
295 | } | |
296 | ||
297 | hook_ppp_ip_pre_up() { | |
298 | local zone="${1}" | |
299 | assert isset zone | |
300 | shift | |
301 | ||
302 | if ! zone_exists "${zone}"; then | |
303 | log ERROR "Zone '${zone}' does not exist." | |
304 | exit ${EXIT_ERROR} | |
305 | fi | |
306 | ||
307 | ppp_common_ip_pre_up "${zone}" "$@" | |
308 | exit $? | |
309 | } | |
310 | ||
311 | hook_ppp_ipv4_up() { | |
312 | local zone="${1}" | |
313 | assert isset zone | |
314 | shift | |
315 | ||
316 | if ! zone_exists "${zone}"; then | |
317 | log ERROR "Zone '${zone}' does not exist." | |
318 | exit ${EXIT_ERROR} | |
319 | fi | |
320 | ||
321 | ppp_common_ipv4_up "${zone}" "$@" | |
322 | exit $? | |
323 | } | |
324 | ||
325 | hook_ppp_ipv4_down() { | |
326 | local zone="${1}" | |
327 | assert isset zone | |
328 | shift | |
329 | ||
330 | if ! zone_exists "${zone}"; then | |
331 | log ERROR "Zone '${zone}' does not exist." | |
332 | exit ${EXIT_ERROR} | |
333 | fi | |
334 | ||
335 | ppp_common_ipv4_down "${zone}" "$@" | |
336 | exit $? | |
337 | } | |
338 | ||
339 | hook_ppp_ipv6_up() { | |
340 | local zone="${1}" | |
341 | assert isset zone | |
342 | shift | |
343 | ||
344 | if ! zone_exists "${zone}"; then | |
345 | error "Zone '${zone}' does not exist." | |
346 | exit ${EXIT_ERROR} | |
347 | fi | |
348 | ||
349 | ppp_common_ipv6_up "${zone}" "$@" | |
350 | exit $? | |
351 | } | |
352 | ||
353 | hook_ppp_ipv6_down() { | |
354 | local zone="${1}" | |
355 | assert isset zone | |
356 | shift | |
357 | ||
358 | if ! zone_exists "${zone}"; then | |
359 | error "Zone '${zone}' does not exist." | |
360 | exit ${EXIT_ERROR} | |
361 | fi | |
362 | ||
363 | ppp_common_ipv6_down "${zone}" "$@" | |
364 | exit $? | |
365 | } |