]> git.ipfire.org Git - people/stevee/network.git/blame_incremental - src/functions/functions.zone
Rename all "config" to "settings"
[people/stevee/network.git] / src / functions / functions.zone
... / ...
CommitLineData
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
25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
26}
27
28function zone_exists() {
29 local zone=${1}
30 assert isset zone
31
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}
48
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
51
52 [[ ${zone} =~ $(zone_match) ]]
53}
54
55function zone_is_local() {
56 local zone=${1}
57
58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
59}
60
61function zone_is_nonlocal() {
62 local zone=${1}
63
64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
65}
66
67function zone_get_hook() {
68 local zone=${1}
69 assert isset zone
70
71 config_get_hook $(zone_dir ${zone})/settings
72}
73
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
81 service_start "network@${zone}.service"
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
91 service_stop "network@${zone}.service"
92}
93
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
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
161 if ! hook_zone_exists ${hook}; then
162 error "Hook '${hook}' does not exist."
163 return ${EXIT_ERROR}
164 fi
165
166 mkdir -p $(zone_dir ${zone})
167
168 # Create directories for configs and ports
169 mkdir -p $(zone_dir ${zone})/{configs,ports}
170
171 hook_zone_exec ${hook} create ${zone} $@
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
177 zone_remove_now ${zone}
178 return ${EXIT_ERROR}
179 fi
180
181 # Automatically enable zone.
182 zone_enable "${zone}"
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
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
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
207 if ! hook_zone_exists ${hook}; then
208 error "Hook '${hook}' does not exist."
209 return ${EXIT_ERROR}
210 fi
211
212 hook_zone_exec ${hook} edit ${zone} $@
213}
214
215
216function zone_remove() {
217 local zone=${1}
218 assert zone_exists ${zone}
219
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."
241
242 # Force the zone down.
243 zone_is_up ${zone} && zone_set_down ${zone}
244
245 # Disable zone.
246 zone_disable "${zone}"
247
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
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
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
273 if ! hook_zone_exists ${hook}; then
274 error "Hook '${hook}' does not exist."
275 return ${EXIT_ERROR}
276 fi
277
278 zone_db ${zone} starting
279
280 hook_zone_exec ${hook} up ${zone} $@
281
282 zone_db ${zone} started
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
301 if ! hook_zone_exists ${hook}; then
302 error "Hook '${hook}' does not exist."
303 return ${EXIT_ERROR}
304 fi
305
306 zone_db ${zone} stopping
307
308 hook_zone_exec ${hook} down ${zone} $@
309
310 zone_db ${zone} stopped
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
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
334 if ! hook_zone_exists ${hook}; then
335 error "Hook '${hook}' does not exist."
336 return ${EXIT_ERROR}
337 fi
338
339 hook_zone_exec ${hook} status ${zone} $@
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
345}
346
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
357 add|edit|remove)
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
366}
367
368function zone_port_add() {
369 local zone="${1}"
370 assert isset zone
371
372 local port="${2}"
373 assert isset port
374
375 shift 2
376
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}")
393 assert isset hook
394
395 hook_zone_exec "${hook}" "port_add" "${zone}" "${port}" "$@"
396}
397
398function zone_port_edit() {
399 local zone="${1}"
400 assert isset zone
401
402 local port="${2}"
403 assert isset port
404
405 shift 2
406
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
412
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
418
419 local hook=$(zone_get_hook "${zone}")
420 assert isset hook
421
422 hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@"
423}
424
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
442
443 hook_zone_exec "${hook}" "port_remove" "${zone}" "${port}" "$@"
444}
445
446function zone_get_ports() {
447 local zone=${1}
448
449 assert isset zone
450
451 local port
452 for port in $(zone_dir ${zone})/ports/*; do
453 port=$(basename ${port})
454
455 if port_exists ${port}; then
456 echo "${port}"
457 fi
458 done
459}
460
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
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
493# XXX overwritten some lines below
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
510 if ! hook_zone_exists ${hook}; then
511 error "Hook '${hook}' does not exist."
512 return ${EXIT_ERROR}
513 fi
514
515 hook_zone_exec ${hook} config ${zone} $@
516}
517
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
546function zone_config_create() {
547 local zone="${1}"
548 assert isset zone
549 shift
550
551 local hook=$(zone_get_hook "${zone}")
552 assert isset hook
553
554 hook_zone_exec "${hook}" "config_create" "${zone}" "$@"
555}
556
557function zone_show() {
558 local zone=${1}
559
560 echo "${zone}"
561 echo " Type: $(zone_get_hook ${zone})"
562 echo
563}
564
565function zones_show() {
566 local zone
567
568 for zone in $(zones_get $@); do
569 zone_show ${zone}
570 done
571}
572
573function zones_get_all() {
574 local zone
575 for zone in $(zone_dir)/*; do
576 zone=$(basename ${zone})
577 zone_exists ${zone} || continue
578
579 echo "${zone}"
580 done
581}
582
583function zones_get_local() {
584 local zone
585 for zone in $(zones_get_all); do
586 zone_is_local ${zone} && echo "${zone}"
587 done
588}
589
590function zones_get_nonlocal() {
591 local zone
592 for zone in $(zones_get_all); do
593 zone_is_nonlocal ${zone} && echo "${zone}"
594 done
595}
596
597function zones_get() {
598 local local=1
599 local remote=1
600
601 local zones
602
603 while [ $# -gt 0 ]; do
604 case "${1}" in
605 --local-only)
606 local=1
607 remote=0
608 ;;
609 --remote-only)
610 local=0
611 remote=1
612 ;;
613 --all)
614 local=1
615 remote=1
616 ;;
617 *)
618 if zone_name_is_valid ${1}; then
619 zones="${zones} ${1}"
620 else
621 warning "Unrecognized argument '${1}'"
622 fi
623 ;;
624 esac
625 shift
626 done
627
628 if [ -n "${zones}" ]; then
629 local zone
630 for zone in ${zones}; do
631 zone_exists ${zone} && echo "${zone}"
632 done
633 exit ${EXIT_OK}
634 fi
635
636 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
637 zones_get_all
638 elif [ ${local} -eq 1 ]; then
639 zones_get_local
640 elif [ ${remote} -eq 1 ]; then
641 zones_get_nonlocal
642 fi
643}
644
645function zone_ports_list() {
646 local zone=${1}
647
648 local port
649 for port in $(zone_dir ${zone})/ports/*; do
650 [ -e "${port}" ] || continue
651
652 echo $(basename ${port})
653 done
654}
655
656function zone_ports_cmd() {
657 local cmd=${1}
658 local zone=${2}
659 shift 2
660
661 assert isset cmd
662 assert isset zone
663
664 assert zone_exists ${zone}
665
666 local hook=$(zone_get_hook ${zone})
667
668 local port
669 for port in $(zone_get_ports ${zone}); do
670 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
671 done
672}
673
674function zone_ports_up() {
675 zone_ports_cmd port_up $@
676}
677
678function zone_ports_down() {
679 zone_ports_cmd port_down $@
680}
681
682function zone_ports_status() {
683 zone_ports_cmd port_status $@
684}
685
686function zone_configs_list() {
687 local zone=${1}
688
689 local config
690 for config in $(zone_dir ${zone})/configs/*; do
691 [ -e "${config}" ] || continue
692
693 basename ${config}
694 done
695}
696
697function zone_configs_cmd() {
698 local cmd=${1}
699 local zone=${2}
700 shift 2
701
702 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
703
704 local hook_config
705 local config
706 for config in $(zone_configs_list ${zone}); do
707 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
708
709 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
710 done
711}
712
713function zone_configs_up() {
714 zone_configs_cmd up $@
715}
716
717function zone_configs_down() {
718 zone_configs_cmd down $@
719}
720
721function zone_configs_status() {
722 zone_configs_cmd config_status $@
723}
724
725function zone_has_ip() {
726 device_has_ip $@
727}
728
729function zone_db() {
730 local zone=${1}
731 local action=${2}
732 shift 2
733
734 case "${action}" in
735 starting|started|stopping|stopped)
736 db_connection_update ${zone} ${action}
737 ;;
738 esac
739}
740
741function zone_is_up() {
742 local zone=${1}
743
744 device_is_up ${zone}
745}
746
747function zone_is_down() {
748 ! zone_is_up $@
749}
750
751function zone_get_supported_port_hooks() {
752 local zone=${1}
753
754 local hook=$(zone_get_hook ${zone})
755
756 hook_zone_ports_get_all ${hook}
757}
758
759function zone_get_supported_config_hooks() {
760 local zone=${1}
761
762 local hook=$(zone_get_hook ${zone})
763
764 hook_zone_configs_get_all ${hook}
765}
766
767function zone_file() {
768 local zone=${1}
769
770 assert isset zone
771
772 echo "$(zone_dir ${zone})/settings"
773}
774
775function zone_settings_read() {
776 local zone=${1}
777
778 assert isset zone
779
780 # Save the HOOK variable.
781 local hook="${HOOK}"
782
783 settings_read $(zone_file ${zone})
784
785 # Restore hook.
786 HOOK="${hook}"
787}
788
789function zone_settings_write() {
790 local zone=${1}
791
792 assert isset zone
793
794 settings_write $(zone_file ${zone}) ${HOOK_SETTINGS}
795}
796
797function zone_settings_set() {
798 local zone=${1}
799 shift
800 local args="$@"
801
802 assert isset zone
803
804 (
805 zone_settings_read ${zone}
806
807 for arg in ${args}; do
808 eval "${arg}"
809 done
810
811 zone_settings_write ${zone}
812 )
813}
814
815function zone_settings_get() {
816 local zone=${1}
817 local key=${2}
818
819 assert isset zone
820 assert isset key
821
822 (
823 zone_settings_read ${zone}
824
825 echo "${!key}"
826 )
827}
828
829function zone_config_settings_read() {
830 assert [ $# -gt 2 ]
831
832 local zone="${1}"
833 local config="${2}"
834 shift 2
835
836 local path="$(zone_dir "${zone}")/configs/${config}"
837 settings_read "${path}" "$@"
838}
839
840function zone_config_settings_write() {
841 assert [ $# -gt 2 ]
842
843 local zone="${1}"
844 local config="${2}"
845 shift 2
846
847 local path="$(zone_dir "${zone}")/configs/${config}"
848 settings_write "${path}" "$@"
849}
850
851function zone_port_settings_read() {
852 assert [ $# -gt 2 ]
853
854 local zone="${1}"
855 local port="${2}"
856 shift 2
857
858 local path="$(zone_dir "${zone}")/ports/${port}"
859 settings_read "${path}" "$@"
860}
861
862function zone_port_settings_write() {
863 assert [ $# -gt 2 ]
864
865 local zone="${1}"
866 local port="${2}"
867 shift 2
868
869 local path="$(zone_dir "${zone}")/ports/${port}"
870 settings_write "${path}" "$@"
871}
872
873function zone_port_settings_remove() {
874 assert [ $# -eq 2 ]
875
876 local zone="${1}"
877 local port="${2}"
878
879 local path="$(zone_dir "${zone}")/ports/${port}"
880 settings_remove "${path}"
881}