]> git.ipfire.org Git - people/arne_f/network.git/blob - header-zone
4d41feb45e7aa5c0a29cacc5334dd9fcaf0f16d4
[people/arne_f/network.git] / 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 # Notes:
23 # - All functions in this scope must start with an underline (_) to not
24 # conflict with any functions that were defined somewhere else.
25 #
26
27 . /lib/network/functions
28
29 HOOK=$(basename ${0})
30
31 while [ $# -gt 0 ]; do
32 case "${1}" in
33 -*)
34 error "Unrecognized option: ${1}"
35 exit ${EXIT_ERROR}
36 ;;
37 *)
38 action=${1}
39 ;;
40 esac
41 shift
42
43 # If action argument was given, we will exit.
44 [ -n "${action}" ] && break
45 done
46
47 # _notimplemented
48 # Returns a soft error if a function was not implemented, yet.
49 #
50 function _notimplemented() {
51 warning "'$@' was not implemented."
52 exit ${EXIT_CONF_ERROR}
53 }
54
55 function _info() {
56 echo "HOOK=\"${HOOK}\""
57 }
58
59 function _create() {
60 local zone=${1}
61 shift
62
63 config_read $(zone_dir ${zone})/settings
64
65 _parse_cmdline $@
66
67 config_write $(zone_dir ${zone})/settings ${HOOK_SETTINGS}
68
69 exit ${EXIT_OK}
70 }
71
72 function _edit() {
73 _create $@
74 }
75
76 function _rem() {
77 _notimplemented _rem
78 }
79
80 function _status() {
81 local zone=${1}
82
83 if device_is_up ${zone}; then
84 exit ${STATUS_UP}
85 fi
86
87 exit ${STATUS_DOWN}
88 }
89
90 function _up() {
91 _notimplemented _up
92 }
93
94 function _down() {
95 _notimplemented _down
96 }
97
98 function _discover() {
99 # This hook does not support a discovery
100 exit ${DISCOVER_NOT_SUPPORTED}
101 }
102
103 # Do nothing
104 function _parse_cmdline() {
105 return ${EXIT_OK}
106 }
107
108 function _port() {
109 local zone=${1}
110 local action=${2}
111 shift 2
112
113 local ret
114
115 case "${action}" in
116 add|create|edit|rem|show)
117 _port_${action} ${zone} $@
118 ret=$?
119 ;;
120 *)
121 error "Unrecognized argument: '${action}'"
122 exit ${EXIT_ERROR}
123 ;;
124 esac
125
126 exit ${ret}
127 }
128
129 function _port_add() {
130 _port_cmd add $@
131 }
132
133 function _port_edit() {
134 _port_cmd edit $@
135 }
136
137 function _port_rem() {
138 _port_cmd rem $@
139 }
140
141 function _port_show() {
142 _notimplemented _port_show
143 }
144
145 function _port_status() {
146 _port_cmd status $@
147 }
148
149 function _port_cmd() {
150 local cmd=${1}
151 local zone=${2}
152 local port=${3}
153 shift 3
154
155 assert isset cmd
156 assert isset zone
157 assert isset port
158
159 local hook_zone=$(zone_get_hook ${zone})
160 local hook_port=$(port_get_hook ${port})
161
162 assert isset hook_zone
163 assert isset hook_port
164
165 if ! listmatch ${hook_port} $(zone_get_supported_hooks ${zone}); then
166 error_log "Zone '${zone}' does not support port of type '${hook_port}'."
167 exit ${EXIT_ERROR}
168 fi
169
170 hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
171
172 exit $?
173 }
174
175 function _port_up() {
176 _port_cmd up $@
177 }
178
179 function _port_down() {
180 _port_cmd down $@
181 }
182
183 function _config() {
184 local zone=${1}
185 local action=${2}
186 shift 2
187
188 local ret
189
190 case "${action}" in
191 create|edit|rem|show)
192 _config_${action} ${zone} $@
193 ret=$?
194 ;;
195 *)
196 error "Unrecognized argument: '${action}'"
197 exit ${EXIT_ERROR}
198 ;;
199 esac
200
201 exit ${ret}
202 }
203
204 # This function is not a public one
205 function __configcmd() {
206 local cmd=${1}
207 local zone=${2}
208 local hook_config=${3}
209 shift 3
210
211 local hook_zone=$(zone_get_hook ${zone})
212
213 if ! hook_zone_exists ${hook_zone}; then
214 error "Hook '${hook}' does not exist."
215 exit ${EXIT_ERROR}
216 fi
217
218 if ! hook_config_exists ${hook_zone} ${hook_config}; then
219 error "Hook '${hook_config}' is not supported for zone '${zone}'."
220 exit ${EXIT_ERROR}
221 fi
222
223 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} $@
224 }
225
226 function _config_create() {
227 __configcmd create $@
228 }
229
230 function _config_edit() {
231 __configcmd edit $@
232 }
233
234 function _config_rem() {
235 _notimplemented _config_rem
236 }
237
238 function _config_show() {
239 _notimplemented _config_show
240 }
241
242 function _ppp-ip-pre-up() {
243 local zone=${1}
244 shift
245
246 if ! zone_exists ${zone}; then
247 error "Zone '${zone}' does not exist."
248 exit ${EXIT_ERROR}
249 fi
250
251 ppp_common_ip_pre_up ${zone} $@
252
253 exit $?
254 }
255
256 function _ppp-ip-up() {
257 local zone=${1}
258 shift
259
260 if ! zone_exists ${zone}; then
261 error "Zone '${zone}' does not exist."
262 exit ${EXIT_ERROR}
263 fi
264
265 ppp_common_ip_up ${zone} $@
266
267 exit $?
268 }
269
270 function _ppp-ip-down() {
271 local zone=${1}
272 shift
273
274 if ! zone_exists ${zone}; then
275 error "Zone '${zone}' does not exist."
276 exit ${EXIT_ERROR}
277 fi
278
279 ppp_common_ip_down ${zone} $@
280
281 exit $?
282 }
283
284 function run() {
285 # Replace all dashes by an underscore
286 #action=${action//-/_}
287
288 case "${action}" in
289 create|discover|down|edit|info|rem|status|up|port_add|port_rem|port_up|port_down|port_status)
290 _${action} $@
291 ;;
292
293 port)
294 if ! hook_zone_has_ports ${HOOK}; then
295 error "Hook '${HOOK}' does not support ports."
296 exit ${EXIT_ERROR}
297 fi
298
299 _port $@
300 ;;
301
302 config)
303 if ! hook_zone_has_configs ${HOOK}; then
304 error "Hook '${HOOK}' does not support configurations."
305 exit ${EXIT_ERROR}
306 fi
307
308 _config $@
309 ;;
310
311 ppp-ip-pre-up|ppp-ip-up|ppp-ip-down)
312 _${action} $@
313 ;;
314
315 esac
316
317 error "Hook did not exit properly."
318 exit ${EXIT_ERROR}
319 }