]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.zone
network: Handle pppd ip-up and ip-down events sober in hook.
[people/arne_f/network.git] / functions.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 function zone_dir() {
23 local zone=${1}
24
25 echo "${ZONE_DIR}/${zone}"
26 }
27
28 function zone_exists() {
29 local zone=${1}
30
31 [ -d "$(zone_dir ${zone})" ]
32 }
33
34 function zone_match() {
35 local match
36
37 local i
38 for i in ${VALID_ZONES}; do
39 match="${match}|${i}[0-9]{1,5}"
40 done
41
42 echo "${match:1:${#match}}"
43 }
44
45 function zone_name_is_valid() {
46 local zone=${1}
47
48 [[ ${zone} =~ $(zone_match) ]]
49 }
50
51 function zone_is_local() {
52 local zone=${1}
53
54 if [[ ${zone} =~ ^red[0-9]{1,5} ]]; then
55 return ${EXIT_ERROR}
56 fi
57 return ${EXIT_OK}
58 }
59
60 function zone_get_hook() {
61 local zone=${1}
62
63 config_get_hook $(zone_dir ${zone})/settings
64 }
65
66 function zone_create() {
67 local zone=${1}
68 local hook=${2}
69 shift 2
70
71 if ! zone_name_is_valid ${zone}; then
72 error "Zone name '${zone}' is not valid."
73 return ${EXIT_ERROR}
74 fi
75
76 if zone_exists ${zone}; then
77 error "Zone '${zone}' does already exist."
78 return ${EXIT_ERROR}
79 fi
80
81 if ! hook_exists ${hook}; then
82 error "Hook '${hook}' does not exist."
83 return ${EXIT_ERROR}
84 fi
85
86 mkdir -p $(zone_dir ${zone})
87
88 hook_exec ${hook} create ${zone} $@
89 local ret=$?
90
91 # Maybe the zone create hook did not exit correctly.
92 # If this is the case we remove the created zone immediately.
93 if [ "${ret}" = "${EXIT_ERROR}" ]; then
94 zone_remove ${zone}
95 fi
96 }
97
98 function zone_edit() {
99 local zone=${1}
100 shift
101
102 if ! zone_exists ${zone}; then
103 error "Zone '${zone}' does not exist."
104 return ${EXIT_ERROR}
105 fi
106
107 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
108
109 if [ -z "${hook}" ]; then
110 error "Config file did not provide any hook."
111 return ${EXIT_ERROR}
112 fi
113
114 if ! hook_exists ${hook}; then
115 error "Hook '${hook}' does not exist."
116 return ${EXIT_ERROR}
117 fi
118
119 hook_exec ${hook} edit ${zone} $@
120 }
121
122 function zone_remove() {
123 local zone=${1}
124 shift
125
126 if ! zone_exists ${zone}; then
127 error "Zone '${zone}' does not exist."
128 return ${EXIT_ERROR}
129 fi
130
131 # XXX Tear this down here?
132
133 rm -rf $(zone_dir ${zone})
134 }
135
136 function zone_up() {
137 local zone=${1}
138 shift
139
140 if ! zone_exists ${zone}; then
141 error "Zone '${zone}' does not exist."
142 return ${EXIT_ERROR}
143 fi
144
145 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
146
147 if [ -z "${hook}" ]; then
148 error "Config file did not provide any hook."
149 return ${EXIT_ERROR}
150 fi
151
152 if ! hook_exists ${hook}; then
153 error "Hook '${hook}' does not exist."
154 return ${EXIT_ERROR}
155 fi
156
157 hook_exec ${hook} up ${zone} $@
158 }
159
160 function zone_down() {
161 local zone=${1}
162 shift
163
164 if ! zone_exists ${zone}; then
165 error "Zone '${zone}' does not exist."
166 return ${EXIT_ERROR}
167 fi
168
169 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
170
171 if [ -z "${hook}" ]; then
172 error "Config file did not provide any hook."
173 return ${EXIT_ERROR}
174 fi
175
176 if ! hook_exists ${hook}; then
177 error "Hook '${hook}' does not exist."
178 return ${EXIT_ERROR}
179 fi
180
181 hook_exec ${hook} down ${zone} $@
182 }
183
184 function zone_status() {
185 local zone=${1}
186 shift
187
188 if ! zone_exists ${zone}; then
189 error "Zone '${zone}' does not exist."
190 return ${EXIT_ERROR}
191 fi
192
193 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
194
195 if [ -z "${hook}" ]; then
196 error "Config file did not provide any hook."
197 return ${EXIT_ERROR}
198 fi
199
200 if ! hook_exists ${hook}; then
201 error "Hook '${hook}' does not exist."
202 return ${EXIT_ERROR}
203 fi
204
205 hook_exec ${hook} status ${zone} $@
206 }
207
208 function zone_port() {
209 local zone=${1}
210 shift
211
212 if ! zone_exists ${zone}; then
213 error "Zone '${zone}' does not exist."
214 return ${EXIT_ERROR}
215 fi
216
217 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
218
219 if [ -z "${hook}" ]; then
220 error "Config file did not provide any hook."
221 return ${EXIT_ERROR}
222 fi
223
224 if ! hook_exists ${hook}; then
225 error "Hook '${hook}' does not exist."
226 return ${EXIT_ERROR}
227 fi
228
229 hook_exec ${hook} port ${zone} $@
230 }
231
232 function zone_config() {
233 local zone=${1}
234 shift
235
236 if ! zone_exists ${zone}; then
237 error "Zone '${zone}' does not exist."
238 return ${EXIT_ERROR}
239 fi
240
241 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
242
243 if [ -z "${hook}" ]; then
244 error "Config file did not provide any hook."
245 return ${EXIT_ERROR}
246 fi
247
248 if ! hook_exists ${hook}; then
249 error "Hook '${hook}' does not exist."
250 return ${EXIT_ERROR}
251 fi
252
253 hook_exec ${hook} config ${zone} $@
254 }
255
256 function zone_show() {
257 local zone=${1}
258
259 echo "${zone}"
260 echo " Type: $(zone_get_hook ${zone})"
261 echo
262 }
263
264 function zones_show() {
265 local zone
266
267 for zone in $(zones_get $@); do
268 zone_show ${zone}
269 done
270 }
271
272 function zones_get_all() {
273 local zone
274 for zone in ${ZONE_DIR}/*; do
275 zone=$(basename ${zone})
276 zone_exists ${zone} || continue
277
278 echo "${zone}"
279 done | sort
280 }
281
282 function zones_get_local() {
283 local zone
284 for zone in $(zones_get_all); do
285 zone_is_local ${zone} && echo "${zone}"
286 done
287 }
288
289 function zones_get_nonlocal() {
290 local zone
291 for zone in $(zones_get_all); do
292 zone_is_local ${zone} || echo "${zone}"
293 done
294 }
295
296 function zones_get() {
297 local local=1
298 local remote=1
299
300 local zones
301
302 while [ $# -gt 0 ]; do
303 case "${1}" in
304 --local-only)
305 local=1
306 remote=0
307 ;;
308 --remote-only)
309 local=0
310 remote=1
311 ;;
312 --all)
313 local=1
314 remote=1
315 ;;
316 *)
317 if zone_name_is_valid ${1}; then
318 zones="${zones} ${1}"
319 else
320 warning "Unrecognized argument '${1}'"
321 fi
322 ;;
323 esac
324 shift
325 done
326
327 if [ -n "${zones}" ]; then
328 local zone
329 for zone in ${zones}; do
330 zone_exists ${zone} && echo "${zone}"
331 done
332 exit ${EXIT_OK}
333 fi
334
335 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
336 zones_get_all
337 elif [ ${local} -eq 1 ]; then
338 zones_get_local
339 elif [ ${remote} -eq 1 ]; then
340 zones_get_nonlocal
341 fi
342 }
343
344 function zone_ports_list() {
345 local zone=${1}
346
347 local port
348 for port in $(zone_dir ${zone})/port.*; do
349 [ -e "${port}" ] || continue
350
351 echo $(basename ${port})
352 done | sort
353 }
354
355 function zone_ports_cmd() {
356 local cmd=${1}
357 local zone=${2}
358 shift 2
359
360 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
361
362 local hook_port
363 local port
364 for port in $(zone_ports_list ${zone}); do
365 hook_port=$(config_get_hook $(zone_dir ${zone})/${port})
366
367 hook_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
368 done
369 }
370
371 function zone_ports_up() {
372 zone_ports_cmd up $@
373 }
374
375 function zone_ports_down() {
376 zone_ports_cmd down $@
377 }
378
379 function zone_configs_list() {
380 local zone=${1}
381
382 local config
383 for config in $(zone_dir ${zone})/config.*; do
384 [ -e "${config}" ] || continue
385
386 echo $(basename ${config})
387 done | sort
388 }
389
390 function zone_configs_cmd() {
391 local cmd=${1}
392 local zone=${2}
393 shift 2
394
395 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
396
397 local hook_config
398 local config
399 for config in $(zone_configs_list ${zone}); do
400 hook_config=$(config_get_hook $(zone_dir ${zone})/${config})
401
402 hook_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
403 done
404 }
405
406 function zone_configs_up() {
407 zone_configs_cmd up $@
408 }
409
410 function zone_configs_down() {
411 zone_configs_cmd down $@
412 }
413
414 function zone_has_ipv4() {
415 device_has_ipv4 $@
416 }
417