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