]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.zone
network: Add some initialization handlers.
[people/arne_f/network.git] / functions.zone
CommitLineData
1848564d
MT
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
22function zone_dir() {
23 local zone=${1}
24
711ffac1
MT
25 #assert isset zone
26
d61a01d4 27 echo "${ZONE_DIR}/zones/${zone}"
1848564d
MT
28}
29
30function zone_exists() {
31 local zone=${1}
32
711ffac1
MT
33 assert isset zone
34
1848564d
MT
35 [ -d "$(zone_dir ${zone})" ]
36}
37
38function zone_match() {
39 local match
40
41 local i
42 for i in ${VALID_ZONES}; do
43 match="${match}|${i}[0-9]{1,5}"
44 done
45
46 echo "${match:1:${#match}}"
47}
48
49function zone_name_is_valid() {
50 local zone=${1}
51
711ffac1
MT
52 assert isset zone
53
1848564d
MT
54 [[ ${zone} =~ $(zone_match) ]]
55}
56
57function zone_is_local() {
58 local zone=${1}
59
5e42d659
MT
60 ! zone_is_nonlocal ${zone}
61}
62
63function zone_is_nonlocal() {
64 local zone=${1}
65
711ffac1
MT
66 assert isset zone
67
5e42d659 68 [[ ${zone} =~ ^red[0-9]{1,5} ]]
1848564d
MT
69}
70
71function zone_get_hook() {
72 local zone=${1}
73
711ffac1
MT
74 assert isset zone
75
1848564d
MT
76 config_get_hook $(zone_dir ${zone})/settings
77}
78
79function zone_create() {
80 local zone=${1}
81 local hook=${2}
82 shift 2
83
84 if ! zone_name_is_valid ${zone}; then
85 error "Zone name '${zone}' is not valid."
86 return ${EXIT_ERROR}
87 fi
88
89 if zone_exists ${zone}; then
90 error "Zone '${zone}' does already exist."
91 return ${EXIT_ERROR}
92 fi
93
d61a01d4 94 if ! hook_zone_exists ${hook}; then
1848564d
MT
95 error "Hook '${hook}' does not exist."
96 return ${EXIT_ERROR}
97 fi
98
99 mkdir -p $(zone_dir ${zone})
100
943e3f7e
MT
101 # Create directory for ports
102 mkdir -p $(zone_dir ${zone})/ports
103
d61a01d4 104 hook_zone_exec ${hook} create ${zone} $@
1848564d
MT
105 local ret=$?
106
107 # Maybe the zone create hook did not exit correctly.
108 # If this is the case we remove the created zone immediately.
109 if [ "${ret}" = "${EXIT_ERROR}" ]; then
110 zone_remove ${zone}
111 fi
112}
113
114function zone_edit() {
115 local zone=${1}
116 shift
117
118 if ! zone_exists ${zone}; then
119 error "Zone '${zone}' does not exist."
120 return ${EXIT_ERROR}
121 fi
122
123 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
124
125 if [ -z "${hook}" ]; then
126 error "Config file did not provide any hook."
127 return ${EXIT_ERROR}
128 fi
129
d61a01d4 130 if ! hook_zone_exists ${hook}; then
1848564d
MT
131 error "Hook '${hook}' does not exist."
132 return ${EXIT_ERROR}
133 fi
134
d61a01d4 135 hook_zone_exec ${hook} edit ${zone} $@
1848564d
MT
136}
137
138function zone_remove() {
139 local zone=${1}
140 shift
141
142 if ! zone_exists ${zone}; then
143 error "Zone '${zone}' does not exist."
144 return ${EXIT_ERROR}
145 fi
146
147 # XXX Tear this down here?
148
149 rm -rf $(zone_dir ${zone})
150}
151
152function zone_up() {
153 local zone=${1}
154 shift
155
156 if ! zone_exists ${zone}; then
157 error "Zone '${zone}' does not exist."
158 return ${EXIT_ERROR}
159 fi
160
161 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
162
163 if [ -z "${hook}" ]; then
164 error "Config file did not provide any hook."
165 return ${EXIT_ERROR}
166 fi
167
d61a01d4 168 if ! hook_zone_exists ${hook}; then
1848564d
MT
169 error "Hook '${hook}' does not exist."
170 return ${EXIT_ERROR}
171 fi
172
059469a8
MT
173 zone_db ${zone} starting
174
d61a01d4
MT
175 hook_zone_exec ${hook} up ${zone} $@
176
059469a8 177 zone_db ${zone} started
1848564d
MT
178}
179
180function zone_down() {
181 local zone=${1}
182 shift
183
184 if ! zone_exists ${zone}; then
185 error "Zone '${zone}' does not exist."
186 return ${EXIT_ERROR}
187 fi
188
189 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
190
191 if [ -z "${hook}" ]; then
192 error "Config file did not provide any hook."
193 return ${EXIT_ERROR}
194 fi
195
d61a01d4 196 if ! hook_zone_exists ${hook}; then
1848564d
MT
197 error "Hook '${hook}' does not exist."
198 return ${EXIT_ERROR}
199 fi
200
059469a8
MT
201 zone_db ${zone} stopping
202
d61a01d4 203 hook_zone_exec ${hook} down ${zone} $@
059469a8
MT
204
205 zone_db ${zone} stopped
1848564d
MT
206}
207
208function zone_status() {
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
d61a01d4 224 if ! hook_zone_exists ${hook}; then
1848564d
MT
225 error "Hook '${hook}' does not exist."
226 return ${EXIT_ERROR}
227 fi
228
d61a01d4 229 hook_zone_exec ${hook} status ${zone} $@
1848564d
MT
230}
231
711ffac1
MT
232function zone_port() {
233 local zone=${1}
234 local action=${2}
235 shift 2
236
237 assert isset zone
238 assert isset action
239 assert zone_exists ${zone}
240
943e3f7e 241 # Aliases
711ffac1 242 case "${action}" in
943e3f7e
MT
243 del|delete|remove)
244 action="rem"
711ffac1
MT
245 ;;
246 esac
711ffac1 247
943e3f7e
MT
248 case "${action}" in
249 add|edit|rem)
250 zone_port_${action} ${zone} $@
251 ;;
252 *)
253 error "Unrecognized argument: ${action}"
254 cli_usage root-zone-port-subcommands
255 exit ${EXIT_ERROR}
256 ;;
257 esac
711ffac1
MT
258}
259
260function zone_port_add() {
261 local zone=${1}
262 shift
263
264 assert isset zone
265
266 local hook=$(zone_get_hook ${zone})
267
268 assert isset hook
269
270 hook_zone_exec ${hook} port_add ${zone} $@
271}
272
273function zone_port_edit() {
943e3f7e 274 zone_port_cmd edit $@
711ffac1
MT
275}
276
943e3f7e
MT
277function zone_port_rem() {
278 zone_port_cmd rem $@
711ffac1
MT
279}
280
281function zone_port_cmd() {
282 local cmd=${1}
283 local zone=${2}
284 local port=${3}
285 shift 3
286
287 assert isset zone
288 assert isset port
289
290 local hook_zone=$(zone_get_hook ${zone})
291 local hook_port=$(port_get_hook ${port})
292
293 assert isset hook_zone
294 assert isset hook_port
295
296 assert hook_zone_port_exists ${hook_zone} ${hook_port}
297
298 hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
299}
300
711ffac1
MT
301function zone_port_up() {
302 zone_port_cmd up $@
303}
304
305function zone_port_down() {
306 zone_port_cmd down $@
307}
308
309function zone_get_ports() {
310 local zone=${1}
311
312 assert isset zone
313
314 local port
943e3f7e 315 for port in $(zone_dir ${zone})/ports/*; do
711ffac1 316 port=$(basename ${port})
711ffac1
MT
317
318 if port_exists ${port}; then
319 echo "${port}"
320 fi
321 done
322}
323
1848564d
MT
324function zone_config() {
325 local zone=${1}
326 shift
327
328 if ! zone_exists ${zone}; then
329 error "Zone '${zone}' does not exist."
330 return ${EXIT_ERROR}
331 fi
332
333 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
334
335 if [ -z "${hook}" ]; then
336 error "Config file did not provide any hook."
337 return ${EXIT_ERROR}
338 fi
339
d61a01d4 340 if ! hook_zone_exists ${hook}; then
1848564d
MT
341 error "Hook '${hook}' does not exist."
342 return ${EXIT_ERROR}
343 fi
344
d61a01d4 345 hook_zone_exec ${hook} config ${zone} $@
1848564d
MT
346}
347
348function zone_show() {
349 local zone=${1}
350
351 echo "${zone}"
352 echo " Type: $(zone_get_hook ${zone})"
353 echo
354}
355
356function zones_show() {
357 local zone
358
359 for zone in $(zones_get $@); do
360 zone_show ${zone}
361 done
362}
363
364function zones_get_all() {
365 local zone
d61a01d4 366 for zone in $(zone_dir)/*; do
1848564d
MT
367 zone=$(basename ${zone})
368 zone_exists ${zone} || continue
369
370 echo "${zone}"
371 done | sort
372}
373
374function zones_get_local() {
375 local zone
376 for zone in $(zones_get_all); do
377 zone_is_local ${zone} && echo "${zone}"
378 done
379}
380
381function zones_get_nonlocal() {
382 local zone
383 for zone in $(zones_get_all); do
5e42d659 384 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
385 done
386}
387
388function zones_get() {
389 local local=1
390 local remote=1
391
392 local zones
393
394 while [ $# -gt 0 ]; do
395 case "${1}" in
396 --local-only)
397 local=1
398 remote=0
399 ;;
400 --remote-only)
401 local=0
402 remote=1
403 ;;
404 --all)
405 local=1
406 remote=1
407 ;;
408 *)
409 if zone_name_is_valid ${1}; then
410 zones="${zones} ${1}"
411 else
412 warning "Unrecognized argument '${1}'"
413 fi
414 ;;
415 esac
416 shift
417 done
418
419 if [ -n "${zones}" ]; then
420 local zone
421 for zone in ${zones}; do
422 zone_exists ${zone} && echo "${zone}"
423 done
424 exit ${EXIT_OK}
425 fi
426
427 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
428 zones_get_all
429 elif [ ${local} -eq 1 ]; then
430 zones_get_local
431 elif [ ${remote} -eq 1 ]; then
432 zones_get_nonlocal
433 fi
434}
435
436function zone_ports_list() {
437 local zone=${1}
438
439 local port
440 for port in $(zone_dir ${zone})/port.*; do
441 [ -e "${port}" ] || continue
442
443 echo $(basename ${port})
444 done | sort
445}
446
447function zone_ports_cmd() {
448 local cmd=${1}
449 local zone=${2}
450 shift 2
451
711ffac1
MT
452 assert isset cmd
453 assert isset zone
1848564d 454
711ffac1 455 assert zone_exists ${zone}
1848564d 456
711ffac1
MT
457 local hook=$(zone_get_hook ${zone})
458
459 local port
460 for port in $(zone_get_ports ${zone}); do
461 #zone_port_cmd ${cmd} ${zone} ${port} $@
462 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
1848564d
MT
463 done
464}
465
466function zone_ports_up() {
711ffac1 467 zone_ports_cmd port_up $@
1848564d
MT
468}
469
470function zone_ports_down() {
711ffac1
MT
471 zone_ports_cmd port_down $@
472}
473
474function zone_ports_status() {
475 zone_ports_cmd port_status $@
1848564d
MT
476}
477
478function zone_configs_list() {
479 local zone=${1}
480
481 local config
482 for config in $(zone_dir ${zone})/config.*; do
483 [ -e "${config}" ] || continue
484
485 echo $(basename ${config})
486 done | sort
487}
488
489function zone_configs_cmd() {
490 local cmd=${1}
491 local zone=${2}
492 shift 2
493
494 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
495
496 local hook_config
497 local config
498 for config in $(zone_configs_list ${zone}); do
499 hook_config=$(config_get_hook $(zone_dir ${zone})/${config})
500
d61a01d4 501 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
1848564d
MT
502 done
503}
504
505function zone_configs_up() {
506 zone_configs_cmd up $@
507}
508
509function zone_configs_down() {
510 zone_configs_cmd down $@
511}
512
513function zone_has_ipv4() {
514 device_has_ipv4 $@
515}
516
4231f419
MT
517function zone_has_ipv6() {
518 device_has_ipv6 $@
519}
520
059469a8
MT
521function zone_db() {
522 local zone=${1}
523 local action=${2}
524 shift 2
525
526 case "${action}" in
527 starting|started|stopping|stopped)
528 db_connection_update ${zone} ${action}
529 ;;
530 esac
531}
5e42d659
MT
532
533function zone_is_up() {
534 local zone=${1}
535
536 device_is_up ${zone}
537}
538
539function zone_is_down() {
540 ! zone_is_up $@
541}
711ffac1
MT
542
543function zone_get_supported_hooks() {
544 local zone=${1}
545
546 local hook=$(zone_get_hook ${zone})
547
548 hook_zone_ports_get_all ${hook}
549}
550
551function zone_file() {
552 local zone=${1}
553
554 assert isset zone
555
556 echo "$(zone_dir ${zone})/settings"
557}
558
559function zone_config_read() {
560 local zone=${1}
561
562 assert isset zone
563
564 config_read $(zone_file ${zone})
565}
566
567function zone_config_write() {
568 local zone=${1}
569
570 assert isset zone
571
572 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
573}
574
575function zone_config_set() {
576 local zone=${1}
577 shift
578 local args="$@"
579
580 assert isset zone
581
582 (
583 zone_config_read ${zone}
584
585 for arg in ${args}; do
586 eval "${arg}"
587 done
588
589 zone_config_write ${zone}
590 )
591}