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