]> git.ipfire.org Git - people/ms/network.git/blame - functions.zone
Remove port initialization on init event.
[people/ms/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
7de0637a 60 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
5e42d659
MT
61}
62
63function zone_is_nonlocal() {
64 local zone=${1}
65
7de0637a 66 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
1848564d
MT
67}
68
69function zone_get_hook() {
70 local zone=${1}
71
711ffac1
MT
72 assert isset zone
73
1848564d
MT
74 config_get_hook $(zone_dir ${zone})/settings
75}
76
77function zone_create() {
78 local zone=${1}
79 local hook=${2}
80 shift 2
81
82 if ! zone_name_is_valid ${zone}; then
83 error "Zone name '${zone}' is not valid."
84 return ${EXIT_ERROR}
85 fi
86
87 if zone_exists ${zone}; then
88 error "Zone '${zone}' does already exist."
89 return ${EXIT_ERROR}
90 fi
91
d61a01d4 92 if ! hook_zone_exists ${hook}; then
1848564d
MT
93 error "Hook '${hook}' does not exist."
94 return ${EXIT_ERROR}
95 fi
96
97 mkdir -p $(zone_dir ${zone})
98
a5ebb169
MT
99 # Create directories for configs and ports
100 mkdir -p $(zone_dir ${zone})/{configs,ports}
943e3f7e 101
d61a01d4 102 hook_zone_exec ${hook} create ${zone} $@
1848564d
MT
103 local ret=$?
104
105 # Maybe the zone create hook did not exit correctly.
106 # If this is the case we remove the created zone immediately.
107 if [ "${ret}" = "${EXIT_ERROR}" ]; then
108 zone_remove ${zone}
109 fi
110}
111
112function zone_edit() {
113 local zone=${1}
114 shift
115
116 if ! zone_exists ${zone}; then
117 error "Zone '${zone}' does not exist."
118 return ${EXIT_ERROR}
119 fi
120
121 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
122
123 if [ -z "${hook}" ]; then
124 error "Config file did not provide any hook."
125 return ${EXIT_ERROR}
126 fi
127
d61a01d4 128 if ! hook_zone_exists ${hook}; then
1848564d
MT
129 error "Hook '${hook}' does not exist."
130 return ${EXIT_ERROR}
131 fi
132
d61a01d4 133 hook_zone_exec ${hook} edit ${zone} $@
1848564d
MT
134}
135
136function zone_remove() {
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 # XXX Tear this down here?
146
147 rm -rf $(zone_dir ${zone})
148}
149
150function zone_up() {
151 local zone=${1}
152 shift
153
154 if ! zone_exists ${zone}; then
155 error "Zone '${zone}' does not exist."
156 return ${EXIT_ERROR}
157 fi
158
159 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
160
161 if [ -z "${hook}" ]; then
162 error "Config file did not provide any hook."
163 return ${EXIT_ERROR}
164 fi
165
d61a01d4 166 if ! hook_zone_exists ${hook}; then
1848564d
MT
167 error "Hook '${hook}' does not exist."
168 return ${EXIT_ERROR}
169 fi
170
059469a8
MT
171 zone_db ${zone} starting
172
d61a01d4
MT
173 hook_zone_exec ${hook} up ${zone} $@
174
059469a8 175 zone_db ${zone} started
1848564d
MT
176}
177
178function zone_down() {
179 local zone=${1}
180 shift
181
182 if ! zone_exists ${zone}; then
183 error "Zone '${zone}' does not exist."
184 return ${EXIT_ERROR}
185 fi
186
187 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
188
189 if [ -z "${hook}" ]; then
190 error "Config file did not provide any hook."
191 return ${EXIT_ERROR}
192 fi
193
d61a01d4 194 if ! hook_zone_exists ${hook}; then
1848564d
MT
195 error "Hook '${hook}' does not exist."
196 return ${EXIT_ERROR}
197 fi
198
059469a8
MT
199 zone_db ${zone} stopping
200
d61a01d4 201 hook_zone_exec ${hook} down ${zone} $@
059469a8
MT
202
203 zone_db ${zone} stopped
1848564d
MT
204}
205
206function zone_status() {
207 local zone=${1}
208 shift
209
210 if ! zone_exists ${zone}; then
211 error "Zone '${zone}' does not exist."
212 return ${EXIT_ERROR}
213 fi
214
215 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
216
217 if [ -z "${hook}" ]; then
218 error "Config file did not provide any hook."
219 return ${EXIT_ERROR}
220 fi
221
d61a01d4 222 if ! hook_zone_exists ${hook}; then
1848564d
MT
223 error "Hook '${hook}' does not exist."
224 return ${EXIT_ERROR}
225 fi
226
d61a01d4 227 hook_zone_exec ${hook} status ${zone} $@
1848564d
MT
228}
229
711ffac1
MT
230function zone_port() {
231 local zone=${1}
232 local action=${2}
233 shift 2
234
235 assert isset zone
236 assert isset action
237 assert zone_exists ${zone}
238
943e3f7e 239 # Aliases
711ffac1 240 case "${action}" in
943e3f7e
MT
241 del|delete|remove)
242 action="rem"
711ffac1
MT
243 ;;
244 esac
711ffac1 245
943e3f7e
MT
246 case "${action}" in
247 add|edit|rem)
248 zone_port_${action} ${zone} $@
249 ;;
250 *)
251 error "Unrecognized argument: ${action}"
252 cli_usage root-zone-port-subcommands
253 exit ${EXIT_ERROR}
254 ;;
255 esac
711ffac1
MT
256}
257
258function zone_port_add() {
259 local zone=${1}
260 shift
261
262 assert isset zone
263
264 local hook=$(zone_get_hook ${zone})
265
266 assert isset hook
267
268 hook_zone_exec ${hook} port_add ${zone} $@
269}
270
271function zone_port_edit() {
943e3f7e 272 zone_port_cmd edit $@
711ffac1
MT
273}
274
943e3f7e
MT
275function zone_port_rem() {
276 zone_port_cmd rem $@
711ffac1
MT
277}
278
279function zone_port_cmd() {
280 local cmd=${1}
281 local zone=${2}
282 local port=${3}
283 shift 3
284
285 assert isset zone
286 assert isset port
287
288 local hook_zone=$(zone_get_hook ${zone})
289 local hook_port=$(port_get_hook ${port})
290
291 assert isset hook_zone
292 assert isset hook_port
293
294 assert hook_zone_port_exists ${hook_zone} ${hook_port}
295
296 hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
297}
298
711ffac1
MT
299function zone_port_up() {
300 zone_port_cmd up $@
301}
302
303function zone_port_down() {
304 zone_port_cmd down $@
305}
306
307function zone_get_ports() {
308 local zone=${1}
309
310 assert isset zone
311
312 local port
943e3f7e 313 for port in $(zone_dir ${zone})/ports/*; do
711ffac1 314 port=$(basename ${port})
711ffac1
MT
315
316 if port_exists ${port}; then
317 echo "${port}"
318 fi
319 done
320}
321
3a7fef62
MT
322function zone_has_port() {
323 # Check, if the given port is configured
324 # in this zone.
325
326 local zone=${1}
327 local port=${2}
328 shift 2
329
330 assert isset zone
331 assert isset port
332
333 [ -e "$(zone_dir ${zone})/ports/${port}" ]
334}
335
a5ebb169 336# XXX overwritten some lines below
1848564d
MT
337function zone_config() {
338 local zone=${1}
339 shift
340
341 if ! zone_exists ${zone}; then
342 error "Zone '${zone}' does not exist."
343 return ${EXIT_ERROR}
344 fi
345
346 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
347
348 if [ -z "${hook}" ]; then
349 error "Config file did not provide any hook."
350 return ${EXIT_ERROR}
351 fi
352
d61a01d4 353 if ! hook_zone_exists ${hook}; then
1848564d
MT
354 error "Hook '${hook}' does not exist."
355 return ${EXIT_ERROR}
356 fi
357
d61a01d4 358 hook_zone_exec ${hook} config ${zone} $@
1848564d
MT
359}
360
a5ebb169
MT
361function zone_config() {
362 local zone=${1}
363 local action=${2}
364 shift 2
365
366 assert isset zone
367 assert isset action
368 assert zone_exists ${zone}
369
370 # Aliases
371 case "${action}" in
372 del|delete|remove)
373 action="rem"
374 ;;
375 esac
376
377 case "${action}" in
378 create|edit|rem)
379 zone_config_${action} ${zone} $@
380 ;;
381 *)
382 error "Unrecognized argument: ${action}"
383 cli_usage root-zone-config-subcommands
384 exit ${EXIT_ERROR}
385 ;;
386 esac
387}
388
3a7fef62
MT
389function zone_config_option() {
390 local zone=${1}
391 local option=${2}
392 local default=${3}
393 shift 2
394
395 assert isset zone
396 assert isset option
397
398 (
399 VALUE="${default}"
400 zone_config_read ${zone}
401
402 VALUE="${!option}"
403 echo "${VALUE}"
404 )
405}
406
a5ebb169
MT
407function zone_config_create() {
408 local zone=${1}
409 shift
410
411 assert isset zone
412
413 local hook=$(zone_get_hook ${zone})
414
415 assert isset hook
416
417 hook_zone_exec ${hook} config_create ${zone} $@
418}
419
1848564d
MT
420function zone_show() {
421 local zone=${1}
422
423 echo "${zone}"
424 echo " Type: $(zone_get_hook ${zone})"
425 echo
426}
427
428function zones_show() {
429 local zone
430
431 for zone in $(zones_get $@); do
432 zone_show ${zone}
433 done
434}
435
436function zones_get_all() {
437 local zone
d61a01d4 438 for zone in $(zone_dir)/*; do
1848564d
MT
439 zone=$(basename ${zone})
440 zone_exists ${zone} || continue
441
442 echo "${zone}"
03170817 443 done
1848564d
MT
444}
445
446function zones_get_local() {
447 local zone
448 for zone in $(zones_get_all); do
449 zone_is_local ${zone} && echo "${zone}"
450 done
451}
452
453function zones_get_nonlocal() {
454 local zone
455 for zone in $(zones_get_all); do
5e42d659 456 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
457 done
458}
459
460function zones_get() {
461 local local=1
462 local remote=1
463
464 local zones
465
466 while [ $# -gt 0 ]; do
467 case "${1}" in
468 --local-only)
469 local=1
470 remote=0
471 ;;
472 --remote-only)
473 local=0
474 remote=1
475 ;;
476 --all)
477 local=1
478 remote=1
479 ;;
480 *)
481 if zone_name_is_valid ${1}; then
482 zones="${zones} ${1}"
483 else
484 warning "Unrecognized argument '${1}'"
485 fi
486 ;;
487 esac
488 shift
489 done
490
491 if [ -n "${zones}" ]; then
492 local zone
493 for zone in ${zones}; do
494 zone_exists ${zone} && echo "${zone}"
495 done
496 exit ${EXIT_OK}
497 fi
498
499 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
500 zones_get_all
501 elif [ ${local} -eq 1 ]; then
502 zones_get_local
503 elif [ ${remote} -eq 1 ]; then
504 zones_get_nonlocal
505 fi
506}
507
508function zone_ports_list() {
509 local zone=${1}
510
511 local port
a5ebb169 512 for port in $(zone_dir ${zone})/ports/*; do
1848564d
MT
513 [ -e "${port}" ] || continue
514
515 echo $(basename ${port})
03170817 516 done
1848564d
MT
517}
518
519function zone_ports_cmd() {
520 local cmd=${1}
521 local zone=${2}
522 shift 2
523
711ffac1
MT
524 assert isset cmd
525 assert isset zone
1848564d 526
711ffac1 527 assert zone_exists ${zone}
1848564d 528
711ffac1
MT
529 local hook=$(zone_get_hook ${zone})
530
531 local port
532 for port in $(zone_get_ports ${zone}); do
533 #zone_port_cmd ${cmd} ${zone} ${port} $@
534 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
1848564d
MT
535 done
536}
537
538function zone_ports_up() {
711ffac1 539 zone_ports_cmd port_up $@
1848564d
MT
540}
541
542function zone_ports_down() {
711ffac1
MT
543 zone_ports_cmd port_down $@
544}
545
546function zone_ports_status() {
547 zone_ports_cmd port_status $@
1848564d
MT
548}
549
550function zone_configs_list() {
551 local zone=${1}
552
553 local config
a5ebb169 554 for config in $(zone_dir ${zone})/configs/*; do
1848564d
MT
555 [ -e "${config}" ] || continue
556
557 echo $(basename ${config})
03170817 558 done
1848564d
MT
559}
560
561function zone_configs_cmd() {
562 local cmd=${1}
563 local zone=${2}
564 shift 2
565
566 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
567
568 local hook_config
569 local config
570 for config in $(zone_configs_list ${zone}); do
a5ebb169 571 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
1848564d 572
d61a01d4 573 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
1848564d
MT
574 done
575}
576
577function zone_configs_up() {
578 zone_configs_cmd up $@
579}
580
581function zone_configs_down() {
582 zone_configs_cmd down $@
583}
584
a5ebb169
MT
585function zone_configs_status() {
586 zone_configs_cmd config_status $@
587}
588
38f61548
MT
589function zone_has_ip() {
590 device_has_ip $@
4231f419
MT
591}
592
059469a8
MT
593function zone_db() {
594 local zone=${1}
595 local action=${2}
596 shift 2
597
598 case "${action}" in
599 starting|started|stopping|stopped)
600 db_connection_update ${zone} ${action}
601 ;;
602 esac
603}
5e42d659
MT
604
605function zone_is_up() {
606 local zone=${1}
607
608 device_is_up ${zone}
609}
610
611function zone_is_down() {
612 ! zone_is_up $@
613}
711ffac1 614
a5ebb169 615function zone_get_supported_port_hooks() {
711ffac1
MT
616 local zone=${1}
617
618 local hook=$(zone_get_hook ${zone})
619
620 hook_zone_ports_get_all ${hook}
621}
622
a5ebb169
MT
623function zone_get_supported_config_hooks() {
624 local zone=${1}
625
626 local hook=$(zone_get_hook ${zone})
627
628 hook_zone_configs_get_all ${hook}
629}
630
711ffac1
MT
631function zone_file() {
632 local zone=${1}
633
634 assert isset zone
635
636 echo "$(zone_dir ${zone})/settings"
637}
638
639function zone_config_read() {
640 local zone=${1}
641
642 assert isset zone
643
644 config_read $(zone_file ${zone})
645}
646
647function zone_config_write() {
648 local zone=${1}
649
650 assert isset zone
651
652 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
653}
654
655function zone_config_set() {
656 local zone=${1}
657 shift
658 local args="$@"
659
660 assert isset zone
661
662 (
663 zone_config_read ${zone}
664
665 for arg in ${args}; do
666 eval "${arg}"
667 done
668
669 zone_config_write ${zone}
670 )
671}
6b3f9c85
MT
672
673function zone_config_get() {
674 local zone=${1}
675 local key=${2}
676
677 assert isset zone
678 assert isset key
679
680 (
681 zone_config_read ${zone}
682
683 echo "${!key}"
684 )
685}