]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.zone
51351172d3b6d742d54da4a5ff4c7ef446f55650
[people/stevee/network.git] / src / functions / functions.zone
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
22 function zone_dir() {
23 local zone=${1}
24
25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
26 }
27
28 function zone_exists() {
29 local zone=${1}
30 assert isset zone
31
32 [ -d "$(zone_dir ${zone})" ]
33 }
34
35 function 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
46 function 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
55 function zone_is_local() {
56 local zone=${1}
57
58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
59 }
60
61 function zone_is_nonlocal() {
62 local zone=${1}
63
64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
65 }
66
67 function zone_get_hook() {
68 local zone=${1}
69 assert isset zone
70
71 config_get_hook $(zone_dir ${zone})/settings
72 }
73
74 function 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
84 function 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
94 function 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
114 function 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
134 function 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
146 function 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
185 function 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
216 function 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
226 function 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.
236 function 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
251 function 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
285 function 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
318 function 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
347 function 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
368 function 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
398 function 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
425 function 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
446 function 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
461 function 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
479 function 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
494 function 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
518 function 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
546 function 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
564 function 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
577 function zone_show() {
578 local zone=${1}
579
580 echo "${zone}"
581 echo " Type: $(zone_get_hook ${zone})"
582 echo
583 }
584
585 function zones_show() {
586 local zone
587
588 for zone in $(zones_get $@); do
589 zone_show ${zone}
590 done
591 }
592
593 function zones_get_all() {
594 local zone
595 for zone in $(zone_dir)/*; do
596 zone=$(basename ${zone})
597 zone_exists ${zone} || continue
598
599 echo "${zone}"
600 done
601 }
602
603 function 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
610 function zones_get_nonlocal() {
611 local zone
612 for zone in $(zones_get_all); do
613 zone_is_nonlocal ${zone} && echo "${zone}"
614 done
615 }
616
617 function 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
665 function zone_ports_list() {
666 local zone=${1}
667
668 local port
669 for port in $(zone_dir ${zone})/ports/*; do
670 [ -e "${port}" ] || continue
671
672 echo $(basename ${port})
673 done
674 }
675
676 function zone_ports_cmd() {
677 local cmd=${1}
678 local zone=${2}
679 shift 2
680
681 assert isset cmd
682 assert isset zone
683
684 assert zone_exists ${zone}
685
686 local hook=$(zone_get_hook ${zone})
687
688 local port
689 for port in $(zone_get_ports ${zone}); do
690 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
691 done
692 }
693
694 function zone_ports_up() {
695 zone_ports_cmd port_up $@
696 }
697
698 function zone_ports_down() {
699 zone_ports_cmd port_down $@
700 }
701
702 function zone_ports_status() {
703 zone_ports_cmd port_status $@
704 }
705
706 function zone_configs_list() {
707 local zone=${1}
708
709 local config
710 for config in $(zone_dir ${zone})/configs/*; do
711 [ -e "${config}" ] || continue
712
713 basename ${config}
714 done
715 }
716
717 function 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
727 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
728
729 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
730 done
731 }
732
733 function zone_configs_up() {
734 zone_configs_cmd up $@
735 }
736
737 function zone_configs_down() {
738 zone_configs_cmd down $@
739 }
740
741 function zone_configs_status() {
742 zone_configs_cmd config_status $@
743 }
744
745 function zone_has_ip() {
746 device_has_ip $@
747 }
748
749 function 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 }
760
761 function zone_is_up() {
762 local zone=${1}
763
764 device_is_up ${zone}
765 }
766
767 function zone_is_down() {
768 ! zone_is_up $@
769 }
770
771 function zone_get_supported_port_hooks() {
772 local zone=${1}
773
774 local hook=$(zone_get_hook ${zone})
775
776 hook_zone_ports_get_all ${hook}
777 }
778
779 function 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
787 function zone_file() {
788 local zone=${1}
789
790 assert isset zone
791
792 echo "$(zone_dir ${zone})/settings"
793 }
794
795 function zone_config_read() {
796 local zone=${1}
797
798 assert isset zone
799
800 # Save the HOOK variable.
801 local hook="${HOOK}"
802
803 config_read $(zone_file ${zone})
804
805 # Restore hook.
806 HOOK="${hook}"
807 }
808
809 function zone_config_write() {
810 local zone=${1}
811
812 assert isset zone
813
814 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
815 }
816
817 function 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 }
834
835 function 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 }