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