]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.zone
network: STP: Make protocol version configureable.
[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
a5ebb169
MT
101 # Create directories for configs and ports
102 mkdir -p $(zone_dir ${zone})/{configs,ports}
943e3f7e 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
a5ebb169 324# XXX overwritten some lines below
1848564d
MT
325function zone_config() {
326 local zone=${1}
327 shift
328
329 if ! zone_exists ${zone}; then
330 error "Zone '${zone}' does not exist."
331 return ${EXIT_ERROR}
332 fi
333
334 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
335
336 if [ -z "${hook}" ]; then
337 error "Config file did not provide any hook."
338 return ${EXIT_ERROR}
339 fi
340
d61a01d4 341 if ! hook_zone_exists ${hook}; then
1848564d
MT
342 error "Hook '${hook}' does not exist."
343 return ${EXIT_ERROR}
344 fi
345
d61a01d4 346 hook_zone_exec ${hook} config ${zone} $@
1848564d
MT
347}
348
a5ebb169
MT
349function zone_config() {
350 local zone=${1}
351 local action=${2}
352 shift 2
353
354 assert isset zone
355 assert isset action
356 assert zone_exists ${zone}
357
358 # Aliases
359 case "${action}" in
360 del|delete|remove)
361 action="rem"
362 ;;
363 esac
364
365 case "${action}" in
366 create|edit|rem)
367 zone_config_${action} ${zone} $@
368 ;;
369 *)
370 error "Unrecognized argument: ${action}"
371 cli_usage root-zone-config-subcommands
372 exit ${EXIT_ERROR}
373 ;;
374 esac
375}
376
377function zone_config_create() {
378 local zone=${1}
379 shift
380
381 assert isset zone
382
383 local hook=$(zone_get_hook ${zone})
384
385 assert isset hook
386
387 hook_zone_exec ${hook} config_create ${zone} $@
388}
389
1848564d
MT
390function zone_show() {
391 local zone=${1}
392
393 echo "${zone}"
394 echo " Type: $(zone_get_hook ${zone})"
395 echo
396}
397
398function zones_show() {
399 local zone
400
401 for zone in $(zones_get $@); do
402 zone_show ${zone}
403 done
404}
405
406function zones_get_all() {
407 local zone
d61a01d4 408 for zone in $(zone_dir)/*; do
1848564d
MT
409 zone=$(basename ${zone})
410 zone_exists ${zone} || continue
411
412 echo "${zone}"
03170817 413 done
1848564d
MT
414}
415
416function zones_get_local() {
417 local zone
418 for zone in $(zones_get_all); do
419 zone_is_local ${zone} && echo "${zone}"
420 done
421}
422
423function zones_get_nonlocal() {
424 local zone
425 for zone in $(zones_get_all); do
5e42d659 426 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
427 done
428}
429
430function zones_get() {
431 local local=1
432 local remote=1
433
434 local zones
435
436 while [ $# -gt 0 ]; do
437 case "${1}" in
438 --local-only)
439 local=1
440 remote=0
441 ;;
442 --remote-only)
443 local=0
444 remote=1
445 ;;
446 --all)
447 local=1
448 remote=1
449 ;;
450 *)
451 if zone_name_is_valid ${1}; then
452 zones="${zones} ${1}"
453 else
454 warning "Unrecognized argument '${1}'"
455 fi
456 ;;
457 esac
458 shift
459 done
460
461 if [ -n "${zones}" ]; then
462 local zone
463 for zone in ${zones}; do
464 zone_exists ${zone} && echo "${zone}"
465 done
466 exit ${EXIT_OK}
467 fi
468
469 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
470 zones_get_all
471 elif [ ${local} -eq 1 ]; then
472 zones_get_local
473 elif [ ${remote} -eq 1 ]; then
474 zones_get_nonlocal
475 fi
476}
477
478function zone_ports_list() {
479 local zone=${1}
480
481 local port
a5ebb169 482 for port in $(zone_dir ${zone})/ports/*; do
1848564d
MT
483 [ -e "${port}" ] || continue
484
485 echo $(basename ${port})
03170817 486 done
1848564d
MT
487}
488
489function zone_ports_cmd() {
490 local cmd=${1}
491 local zone=${2}
492 shift 2
493
711ffac1
MT
494 assert isset cmd
495 assert isset zone
1848564d 496
711ffac1 497 assert zone_exists ${zone}
1848564d 498
711ffac1
MT
499 local hook=$(zone_get_hook ${zone})
500
501 local port
502 for port in $(zone_get_ports ${zone}); do
503 #zone_port_cmd ${cmd} ${zone} ${port} $@
504 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
1848564d
MT
505 done
506}
507
508function zone_ports_up() {
711ffac1 509 zone_ports_cmd port_up $@
1848564d
MT
510}
511
512function zone_ports_down() {
711ffac1
MT
513 zone_ports_cmd port_down $@
514}
515
516function zone_ports_status() {
517 zone_ports_cmd port_status $@
1848564d
MT
518}
519
520function zone_configs_list() {
521 local zone=${1}
522
523 local config
a5ebb169 524 for config in $(zone_dir ${zone})/configs/*; do
1848564d
MT
525 [ -e "${config}" ] || continue
526
527 echo $(basename ${config})
03170817 528 done
1848564d
MT
529}
530
531function zone_configs_cmd() {
532 local cmd=${1}
533 local zone=${2}
534 shift 2
535
536 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
537
538 local hook_config
539 local config
540 for config in $(zone_configs_list ${zone}); do
a5ebb169 541 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
1848564d 542
d61a01d4 543 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
1848564d
MT
544 done
545}
546
547function zone_configs_up() {
548 zone_configs_cmd up $@
549}
550
551function zone_configs_down() {
552 zone_configs_cmd down $@
553}
554
a5ebb169
MT
555function zone_configs_status() {
556 zone_configs_cmd config_status $@
557}
558
1848564d
MT
559function zone_has_ipv4() {
560 device_has_ipv4 $@
561}
562
4231f419
MT
563function zone_has_ipv6() {
564 device_has_ipv6 $@
565}
566
059469a8
MT
567function zone_db() {
568 local zone=${1}
569 local action=${2}
570 shift 2
571
572 case "${action}" in
573 starting|started|stopping|stopped)
574 db_connection_update ${zone} ${action}
575 ;;
576 esac
577}
5e42d659
MT
578
579function zone_is_up() {
580 local zone=${1}
581
582 device_is_up ${zone}
583}
584
585function zone_is_down() {
586 ! zone_is_up $@
587}
711ffac1 588
a5ebb169 589function zone_get_supported_port_hooks() {
711ffac1
MT
590 local zone=${1}
591
592 local hook=$(zone_get_hook ${zone})
593
594 hook_zone_ports_get_all ${hook}
595}
596
a5ebb169
MT
597function zone_get_supported_config_hooks() {
598 local zone=${1}
599
600 local hook=$(zone_get_hook ${zone})
601
602 hook_zone_configs_get_all ${hook}
603}
604
711ffac1
MT
605function zone_file() {
606 local zone=${1}
607
608 assert isset zone
609
610 echo "$(zone_dir ${zone})/settings"
611}
612
613function zone_config_read() {
614 local zone=${1}
615
616 assert isset zone
617
618 config_read $(zone_file ${zone})
619}
620
621function zone_config_write() {
622 local zone=${1}
623
624 assert isset zone
625
626 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
627}
628
629function zone_config_set() {
630 local zone=${1}
631 shift
632 local args="$@"
633
634 assert isset zone
635
636 (
637 zone_config_read ${zone}
638
639 for arg in ${args}; do
640 eval "${arg}"
641 done
642
643 zone_config_write ${zone}
644 )
645}
6b3f9c85
MT
646
647function zone_config_get() {
648 local zone=${1}
649 local key=${2}
650
651 assert isset zone
652 assert isset key
653
654 (
655 zone_config_read ${zone}
656
657 echo "${!key}"
658 )
659}