]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.zone
Move config hooks into a separate directory
[people/stevee/network.git] / src / functions / 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
d2a21d01 25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
1848564d
MT
26}
27
28function zone_exists() {
29 local zone=${1}
711ffac1
MT
30 assert isset zone
31
1848564d
MT
32 [ -d "$(zone_dir ${zone})" ]
33}
34
35function zone_match() {
36 local match
37
38 local i
39 for i in ${VALID_ZONES}; do
40 match="${match}|${i}[0-9]{1,5}"
41 done
42
43 echo "${match:1:${#match}}"
44}
45
46function zone_name_is_valid() {
47 local zone=${1}
4fedddef
MT
48
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
711ffac1 51
1848564d
MT
52 [[ ${zone} =~ $(zone_match) ]]
53}
54
55function zone_is_local() {
56 local zone=${1}
57
7de0637a 58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
5e42d659
MT
59}
60
61function zone_is_nonlocal() {
62 local zone=${1}
63
7de0637a 64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
1848564d
MT
65}
66
67function zone_get_hook() {
68 local zone=${1}
711ffac1
MT
69 assert isset zone
70
1848564d
MT
71 config_get_hook $(zone_dir ${zone})/settings
72}
73
5bb2429a
MT
74function zone_start() {
75 # This function will bring up the zone
76 # 'asynchronously' with help of systemd.
77
78 local zone=${1}
79 assert zone_exists ${zone}
80
1d08b9b3 81 service_start "network@${zone}.service"
5bb2429a
MT
82}
83
84function zone_stop() {
85 # This function will bring down the zone
86 # 'asynchronously' with help of systemd.
87
88 local zone=${1}
89 assert zone_exists ${zone}
90
1d08b9b3 91 service_stop "network@${zone}.service"
5bb2429a
MT
92}
93
5c5b8e36
SS
94function zone_enable() {
95 # This function will enable the zone
96 # with help of systemd.
97
98 local zone="${1}"
99 assert zone_exists "${zone}"
100
101 # Enable service for the zone
102 service_enable "network@${zone}.service"
103 local ret=$?
104
105 if [ ${ret} -eq ${EXIT_OK} ]; then
106 log INFO "Auto-start enabled for zone ${zone}"
107 return ${EXIT_OK}
108 fi
109
110 log ERROR "Could not enable zone ${zone}: ${ret}"
111 return ${ret}
112}
113
114function zone_disable() {
115 # This function will disable the zone
116 # with help of systemd.
117
118 local zone="${1}"
119 assert zone_exists "${zone}"
120
121 # Disable service for the zone
122 service_disable "network@${zone}.service"
123 local ret=$?
124
125 if [ ${ret} -eq ${EXIT_OK} ]; then
126 log INFO "Auto-start disabled for zone ${zone}"
127 return ${EXIT_OK}
128 fi
129
130 log ERROR "Could not disable zone ${zone}: ${ret}"
131 return ${ret}
132}
133
134function zone_is_enabled() {
135 local zone="${1}"
136 assert isset zone
137
138 # Ask systemd if the zone is enabled.
139 if service_is_enabled "network@${zone}.service"; then
140 return ${EXIT_TRUE}
141 fi
142
143 return ${EXIT_FALSE}
144}
145
1848564d
MT
146function zone_create() {
147 local zone=${1}
148 local hook=${2}
149 shift 2
150
151 if ! zone_name_is_valid ${zone}; then
152 error "Zone name '${zone}' is not valid."
153 return ${EXIT_ERROR}
154 fi
155
156 if zone_exists ${zone}; then
157 error "Zone '${zone}' does already exist."
158 return ${EXIT_ERROR}
159 fi
160
d61a01d4 161 if ! hook_zone_exists ${hook}; then
1848564d
MT
162 error "Hook '${hook}' does not exist."
163 return ${EXIT_ERROR}
164 fi
165
166 mkdir -p $(zone_dir ${zone})
167
a5ebb169
MT
168 # Create directories for configs and ports
169 mkdir -p $(zone_dir ${zone})/{configs,ports}
943e3f7e 170
d61a01d4 171 hook_zone_exec ${hook} create ${zone} $@
1848564d
MT
172 local ret=$?
173
174 # Maybe the zone create hook did not exit correctly.
175 # If this is the case we remove the created zone immediately.
176 if [ "${ret}" = "${EXIT_ERROR}" ]; then
69ace22b 177 zone_remove_now ${zone}
5c5b8e36 178 return ${EXIT_ERROR}
1848564d 179 fi
5c5b8e36
SS
180
181 # Automatically enable zone.
182 zone_enable "${zone}"
1848564d
MT
183}
184
185function zone_edit() {
186 local zone=${1}
187 shift
188
189 if ! zone_exists ${zone}; then
190 error "Zone '${zone}' does not exist."
191 return ${EXIT_ERROR}
192 fi
193
69ace22b
MT
194 # Check if the zone is tagged for removal.
195 if zone_has_remove_tag ${zone}; then
196 error "You cannot edit a zone that is tagged for removal."
197 return ${EXIT_ERROR}
198 fi
199
1848564d
MT
200 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
201
202 if [ -z "${hook}" ]; then
203 error "Config file did not provide any hook."
204 return ${EXIT_ERROR}
205 fi
206
d61a01d4 207 if ! hook_zone_exists ${hook}; then
1848564d
MT
208 error "Hook '${hook}' does not exist."
209 return ${EXIT_ERROR}
210 fi
211
d61a01d4 212 hook_zone_exec ${hook} edit ${zone} $@
1848564d
MT
213}
214
69ace22b 215
1848564d
MT
216function zone_remove() {
217 local zone=${1}
69ace22b 218 assert zone_exists ${zone}
1848564d 219
69ace22b
MT
220 # Make the zone for removal.
221 touch $(zone_dir ${zone})/.remove
222
223 log INFO "Zone '${zone}' has been tagged for removal."
224}
225
226function zone_has_remove_tag() {
227 local zone=${1}
228 assert zone_exists ${zone}
229
230 [ -e "$(zone_dir ${zone})/.remove" ]
231}
232
233# This function will remove the given zone
234# RIGHT NOW. Use zone_remove to remove it
235# at the next status change.
236function zone_remove_now() {
237 local zone=${1}
238 assert zone_exists ${zone}
239
240 log INFO "Removing zone '${zone}' right now."
1848564d 241
69ace22b
MT
242 # Force the zone down.
243 zone_is_up ${zone} && zone_set_down ${zone}
1848564d 244
5c5b8e36
SS
245 # Disable zone.
246 zone_disable "${zone}"
247
1848564d
MT
248 rm -rf $(zone_dir ${zone})
249}
250
251function zone_up() {
252 local zone=${1}
253 shift
254
255 if ! zone_exists ${zone}; then
256 error "Zone '${zone}' does not exist."
257 return ${EXIT_ERROR}
258 fi
259
69ace22b
MT
260 # Check if a zone has got the remove tag.
261 if zone_has_remove_tag ${zone}; then
262 error "Cannot bring up any zone which is to be removed."
263 return ${EXIT_ERROR}
264 fi
265
1848564d
MT
266 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
267
268 if [ -z "${hook}" ]; then
269 error "Config file did not provide any hook."
270 return ${EXIT_ERROR}
271 fi
272
d61a01d4 273 if ! hook_zone_exists ${hook}; then
1848564d
MT
274 error "Hook '${hook}' does not exist."
275 return ${EXIT_ERROR}
276 fi
277
059469a8
MT
278 zone_db ${zone} starting
279
d61a01d4
MT
280 hook_zone_exec ${hook} up ${zone} $@
281
059469a8 282 zone_db ${zone} started
1848564d
MT
283}
284
285function zone_down() {
286 local zone=${1}
287 shift
288
289 if ! zone_exists ${zone}; then
290 error "Zone '${zone}' does not exist."
291 return ${EXIT_ERROR}
292 fi
293
294 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
295
296 if [ -z "${hook}" ]; then
297 error "Config file did not provide any hook."
298 return ${EXIT_ERROR}
299 fi
300
d61a01d4 301 if ! hook_zone_exists ${hook}; then
1848564d
MT
302 error "Hook '${hook}' does not exist."
303 return ${EXIT_ERROR}
304 fi
305
059469a8
MT
306 zone_db ${zone} stopping
307
d61a01d4 308 hook_zone_exec ${hook} down ${zone} $@
059469a8
MT
309
310 zone_db ${zone} stopped
69ace22b
MT
311
312 # Remove the zone, if it has got a remove tag.
313 if zone_has_remove_tag ${zone}; then
314 zone_remove_now ${zone}
315 fi
1848564d
MT
316}
317
318function zone_status() {
319 local zone=${1}
320 shift
321
322 if ! zone_exists ${zone}; then
323 error "Zone '${zone}' does not exist."
324 return ${EXIT_ERROR}
325 fi
326
327 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
328
329 if [ -z "${hook}" ]; then
330 error "Config file did not provide any hook."
331 return ${EXIT_ERROR}
332 fi
333
d61a01d4 334 if ! hook_zone_exists ${hook}; then
1848564d
MT
335 error "Hook '${hook}' does not exist."
336 return ${EXIT_ERROR}
337 fi
338
d61a01d4 339 hook_zone_exec ${hook} status ${zone} $@
69ace22b
MT
340
341 # Show that the zone it to be removed soon.
342 if zone_has_remove_tag ${zone}; then
343 warning "This zone is tagged for removal."
344 fi
1848564d
MT
345}
346
711ffac1
MT
347function zone_port() {
348 local zone=${1}
349 local action=${2}
350 shift 2
351
352 assert isset zone
353 assert isset action
354 assert zone_exists ${zone}
355
356 case "${action}" in
828eb94a 357 add|edit|remove)
943e3f7e
MT
358 zone_port_${action} ${zone} $@
359 ;;
360 *)
361 error "Unrecognized argument: ${action}"
362 cli_usage root-zone-port-subcommands
363 exit ${EXIT_ERROR}
364 ;;
365 esac
711ffac1
MT
366}
367
368function zone_port_add() {
828eb94a 369 local zone="${1}"
711ffac1
MT
370 assert isset zone
371
828eb94a
MT
372 local port="${2}"
373 assert isset port
374
375 shift 2
711ffac1 376
828eb94a
MT
377 # Check if the port actually exists.
378 if ! port_exists "${port}"; then
379 error "Cannot add port '${port}' which does not exist"
380 return ${EXIT_ERROR}
381 fi
382
383 # Check if the port is already connected to this or any other zone.
384 local z
385 for z in $(zones_get_all); do
386 if zone_has_port "${z}" "${port}"; then
387 error "Port '${port}' is already assigned to zone '${z}'"
388 return ${EXIT_ERROR}
389 fi
390 done
391
392 local hook=$(zone_get_hook "${zone}")
711ffac1
MT
393 assert isset hook
394
828eb94a 395 hook_zone_exec "${hook}" "port_add" "${zone}" "${port}" "$@"
711ffac1
MT
396}
397
398function zone_port_edit() {
828eb94a
MT
399 local zone="${1}"
400 assert isset zone
711ffac1 401
828eb94a
MT
402 local port="${2}"
403 assert isset port
711ffac1 404
828eb94a 405 shift 2
711ffac1 406
828eb94a
MT
407 # Check if the port actually exists.
408 if ! port_exists "${port}"; then
409 error "Port '${port}' does not exist"
410 return ${EXIT_ERROR}
411 fi
711ffac1 412
828eb94a
MT
413 # Check if the zone actually has this port.
414 if ! zone_has_port "${zone}" "${port}"; then
415 error "Port '${port}' is not attached to zone '${zone}'"
416 return ${EXIT_ERROR}
417 fi
711ffac1 418
828eb94a
MT
419 local hook=$(zone_get_hook "${zone}")
420 assert isset hook
711ffac1 421
828eb94a 422 hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@"
711ffac1
MT
423}
424
828eb94a
MT
425function zone_port_remove() {
426 local zone="${1}"
427 assert isset zone
428
429 local port="${2}"
430 assert isset port
431
432 shift 2
433
434 # Check if the zone actually has this port.
435 if ! zone_has_port "${zone}" "${port}"; then
436 error "Port '${port}' is not attached to zone '${zone}'"
437 return ${EXIT_ERROR}
438 fi
439
440 local hook=$(zone_get_hook "${zone}")
441 assert isset hook
711ffac1 442
828eb94a 443 hook_zone_exec "${hook}" "port_remove" "${zone}" "${port}" "$@"
711ffac1
MT
444}
445
446function zone_get_ports() {
447 local zone=${1}
448
449 assert isset zone
450
451 local port
943e3f7e 452 for port in $(zone_dir ${zone})/ports/*; do
711ffac1 453 port=$(basename ${port})
711ffac1
MT
454
455 if port_exists ${port}; then
456 echo "${port}"
457 fi
458 done
459}
460
529141df
MT
461function zone_get_ports_num() {
462 local zone="${1}"
463 assert isset zone
464
465 local counter=0
466 local port
467 for port in $(zone_dir "${zone}")/ports/*; do
468 port="$(basename "${port}")"
469
470 if port_exists "${port}"; then
471 counter=$(( ${counter} + 1 ))
472 fi
473 done
474
475 echo "${counter}"
476 return ${EXIT_OK}
477}
478
3a7fef62
MT
479function zone_has_port() {
480 # Check, if the given port is configured
481 # in this zone.
482
483 local zone=${1}
484 local port=${2}
485 shift 2
486
487 assert isset zone
488 assert isset port
489
490 [ -e "$(zone_dir ${zone})/ports/${port}" ]
491}
492
a5ebb169 493# XXX overwritten some lines below
1848564d
MT
494function zone_config() {
495 local zone=${1}
496 shift
497
498 if ! zone_exists ${zone}; then
499 error "Zone '${zone}' does not exist."
500 return ${EXIT_ERROR}
501 fi
502
503 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
504
505 if [ -z "${hook}" ]; then
506 error "Config file did not provide any hook."
507 return ${EXIT_ERROR}
508 fi
509
d61a01d4 510 if ! hook_zone_exists ${hook}; then
1848564d
MT
511 error "Hook '${hook}' does not exist."
512 return ${EXIT_ERROR}
513 fi
514
d61a01d4 515 hook_zone_exec ${hook} config ${zone} $@
1848564d
MT
516}
517
a5ebb169
MT
518function zone_config() {
519 local zone=${1}
520 local action=${2}
521 shift 2
522
523 assert isset zone
524 assert isset action
525 assert zone_exists ${zone}
526
527 # Aliases
528 case "${action}" in
529 del|delete|remove)
530 action="rem"
531 ;;
532 esac
533
534 case "${action}" in
535 create|edit|rem)
536 zone_config_${action} ${zone} $@
537 ;;
538 *)
539 error "Unrecognized argument: ${action}"
540 cli_usage root-zone-config-subcommands
541 exit ${EXIT_ERROR}
542 ;;
543 esac
544}
545
3a7fef62
MT
546function zone_config_option() {
547 local zone=${1}
548 local option=${2}
549 local default=${3}
550 shift 2
551
552 assert isset zone
553 assert isset option
554
555 (
556 VALUE="${default}"
557 zone_config_read ${zone}
558
559 VALUE="${!option}"
560 echo "${VALUE}"
561 )
562}
563
a5ebb169
MT
564function zone_config_create() {
565 local zone=${1}
566 shift
567
568 assert isset zone
569
570 local hook=$(zone_get_hook ${zone})
571
572 assert isset hook
573
574 hook_zone_exec ${hook} config_create ${zone} $@
575}
576
1848564d
MT
577function zone_show() {
578 local zone=${1}
579
580 echo "${zone}"
581 echo " Type: $(zone_get_hook ${zone})"
582 echo
583}
584
585function zones_show() {
586 local zone
587
588 for zone in $(zones_get $@); do
589 zone_show ${zone}
590 done
591}
592
593function zones_get_all() {
594 local zone
d61a01d4 595 for zone in $(zone_dir)/*; do
1848564d
MT
596 zone=$(basename ${zone})
597 zone_exists ${zone} || continue
598
599 echo "${zone}"
03170817 600 done
1848564d
MT
601}
602
603function zones_get_local() {
604 local zone
605 for zone in $(zones_get_all); do
606 zone_is_local ${zone} && echo "${zone}"
607 done
608}
609
610function zones_get_nonlocal() {
611 local zone
612 for zone in $(zones_get_all); do
5e42d659 613 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
614 done
615}
616
617function zones_get() {
618 local local=1
619 local remote=1
620
621 local zones
622
623 while [ $# -gt 0 ]; do
624 case "${1}" in
625 --local-only)
626 local=1
627 remote=0
628 ;;
629 --remote-only)
630 local=0
631 remote=1
632 ;;
633 --all)
634 local=1
635 remote=1
636 ;;
637 *)
638 if zone_name_is_valid ${1}; then
639 zones="${zones} ${1}"
640 else
641 warning "Unrecognized argument '${1}'"
642 fi
643 ;;
644 esac
645 shift
646 done
647
648 if [ -n "${zones}" ]; then
649 local zone
650 for zone in ${zones}; do
651 zone_exists ${zone} && echo "${zone}"
652 done
653 exit ${EXIT_OK}
654 fi
655
656 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
657 zones_get_all
658 elif [ ${local} -eq 1 ]; then
659 zones_get_local
660 elif [ ${remote} -eq 1 ]; then
661 zones_get_nonlocal
662 fi
663}
664
665function zone_ports_list() {
666 local zone=${1}
667
668 local port
a5ebb169 669 for port in $(zone_dir ${zone})/ports/*; do
1848564d
MT
670 [ -e "${port}" ] || continue
671
672 echo $(basename ${port})
03170817 673 done
1848564d
MT
674}
675
676function zone_ports_cmd() {
677 local cmd=${1}
678 local zone=${2}
679 shift 2
680
711ffac1
MT
681 assert isset cmd
682 assert isset zone
1848564d 683
711ffac1 684 assert zone_exists ${zone}
1848564d 685
711ffac1
MT
686 local hook=$(zone_get_hook ${zone})
687
688 local port
689 for port in $(zone_get_ports ${zone}); do
711ffac1 690 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
1848564d
MT
691 done
692}
693
694function zone_ports_up() {
711ffac1 695 zone_ports_cmd port_up $@
1848564d
MT
696}
697
698function zone_ports_down() {
711ffac1
MT
699 zone_ports_cmd port_down $@
700}
701
702function zone_ports_status() {
703 zone_ports_cmd port_status $@
1848564d
MT
704}
705
706function zone_configs_list() {
707 local zone=${1}
708
709 local config
a5ebb169 710 for config in $(zone_dir ${zone})/configs/*; do
1848564d
MT
711 [ -e "${config}" ] || continue
712
f41fa3d7 713 basename ${config}
03170817 714 done
1848564d
MT
715}
716
717function zone_configs_cmd() {
718 local cmd=${1}
719 local zone=${2}
720 shift 2
721
722 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
723
724 local hook_config
725 local config
726 for config in $(zone_configs_list ${zone}); do
a5ebb169 727 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
1848564d 728
d61a01d4 729 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
1848564d
MT
730 done
731}
732
733function zone_configs_up() {
734 zone_configs_cmd up $@
735}
736
737function zone_configs_down() {
738 zone_configs_cmd down $@
739}
740
a5ebb169
MT
741function zone_configs_status() {
742 zone_configs_cmd config_status $@
743}
744
38f61548
MT
745function zone_has_ip() {
746 device_has_ip $@
4231f419
MT
747}
748
059469a8
MT
749function zone_db() {
750 local zone=${1}
751 local action=${2}
752 shift 2
753
754 case "${action}" in
755 starting|started|stopping|stopped)
756 db_connection_update ${zone} ${action}
757 ;;
758 esac
759}
5e42d659
MT
760
761function zone_is_up() {
762 local zone=${1}
763
764 device_is_up ${zone}
765}
766
767function zone_is_down() {
768 ! zone_is_up $@
769}
711ffac1 770
a5ebb169 771function zone_get_supported_port_hooks() {
711ffac1
MT
772 local zone=${1}
773
774 local hook=$(zone_get_hook ${zone})
775
776 hook_zone_ports_get_all ${hook}
777}
778
a5ebb169
MT
779function zone_get_supported_config_hooks() {
780 local zone=${1}
781
782 local hook=$(zone_get_hook ${zone})
783
784 hook_zone_configs_get_all ${hook}
785}
786
711ffac1
MT
787function zone_file() {
788 local zone=${1}
789
790 assert isset zone
791
792 echo "$(zone_dir ${zone})/settings"
793}
794
795function zone_config_read() {
796 local zone=${1}
797
798 assert isset zone
799
bfd6c282
MT
800 # Save the HOOK variable.
801 local hook="${HOOK}"
802
711ffac1 803 config_read $(zone_file ${zone})
bfd6c282
MT
804
805 # Restore hook.
806 HOOK="${hook}"
711ffac1
MT
807}
808
809function zone_config_write() {
810 local zone=${1}
811
812 assert isset zone
813
814 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
815}
816
817function zone_config_set() {
818 local zone=${1}
819 shift
820 local args="$@"
821
822 assert isset zone
823
824 (
825 zone_config_read ${zone}
826
827 for arg in ${args}; do
828 eval "${arg}"
829 done
830
831 zone_config_write ${zone}
832 )
833}
6b3f9c85
MT
834
835function zone_config_get() {
836 local zone=${1}
837 local key=${2}
838
839 assert isset zone
840 assert isset key
841
842 (
843 zone_config_read ${zone}
844
845 echo "${!key}"
846 )
847}