]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.zone
Merge remote-tracking branch 'jschlag/master'
[people/ms/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 zone_exists() {
23 local zone=${1}
24 assert isset zone
25
26 [ -d "${NETWORK_ZONES_DIR}/${zone}" ]
27 }
28
29 zone_match() {
30 local match
31
32 local i
33 for i in ${VALID_ZONES}; do
34 match="${match}|${i}[0-9]{1,5}"
35 done
36
37 echo "${match:1:${#match}}"
38 }
39
40 zone_name_is_valid() {
41 local zone=${1}
42
43 # Don't accept empty strings.
44 [ -z "${zone}" ] && return ${EXIT_FALSE}
45
46 [[ ${zone} =~ $(zone_match) ]]
47 }
48
49 zone_is_local() {
50 local zone=${1}
51
52 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
53 }
54
55 zone_is_nonlocal() {
56 local zone=${1}
57
58 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
59 }
60
61 zone_get_hook() {
62 local zone=${1}
63 assert isset zone
64
65 config_get_hook "${NETWORK_ZONES_DIR}/${zone}/settings"
66 }
67
68 zone_start() {
69 # This function will bring up the zone
70 # 'asynchronously' with help of systemd.
71
72 local zone=${1}
73 assert zone_exists ${zone}
74
75 service_start "network@${zone}.service"
76 }
77
78 zone_start_auto() {
79 local zone="${1}"
80 assert zone_exists "${zone}"
81
82 # If the zone has already been started, we
83 # will reload it so the current configuration
84 # is re-applied.
85 if zone_is_active "${zone}"; then
86 zone_reload "${zone}"
87 return ${?}
88
89 # If the zone is still down, but in auto-start mode,
90 # we will start it.
91 elif zone_is_enabled "${zone}"; then
92 zone_start "${zone}"
93 return ${?}
94 fi
95
96 # Otherwise, nothing will be done.
97 return ${EXIT_OK}
98 }
99
100 zone_stop() {
101 # This function will bring down the zone
102 # 'asynchronously' with help of systemd.
103
104 local zone=${1}
105 assert zone_exists ${zone}
106
107 service_stop "network@${zone}.service"
108 }
109
110 zone_reload() {
111 local zone="${1}"
112 assert zone_exists "${zone}"
113
114 service_reload "network@${zone}.service"
115 }
116
117 zone_hotplug_event() {
118 local zone="${1}"
119 assert isset zone
120
121 hotplug_assert_in_hotplug_event
122
123 zone_cmd "hotplug" "${zone}"
124 }
125
126 zone_enable() {
127 # This function will enable the zone
128 # with help of systemd.
129
130 local zone="${1}"
131 assert zone_exists "${zone}"
132
133 # Enable service for the zone
134 service_enable "network@${zone}.service"
135 local ret=$?
136
137 if [ ${ret} -eq ${EXIT_OK} ]; then
138 log INFO "Auto-start enabled for zone ${zone}"
139 return ${EXIT_OK}
140 fi
141
142 log ERROR "Could not enable zone ${zone}: ${ret}"
143 return ${ret}
144 }
145
146 zone_disable() {
147 # This function will disable the zone
148 # with help of systemd.
149
150 local zone="${1}"
151 assert zone_exists "${zone}"
152
153 # Disable service for the zone
154 service_disable "network@${zone}.service"
155 local ret=$?
156
157 if [ ${ret} -eq ${EXIT_OK} ]; then
158 log INFO "Auto-start disabled for zone ${zone}"
159 return ${EXIT_OK}
160 fi
161
162 log ERROR "Could not disable zone ${zone}: ${ret}"
163 return ${ret}
164 }
165
166 zone_is_enabled() {
167 local zone="${1}"
168 assert isset zone
169
170 # Ask systemd if the zone is enabled.
171 if service_is_enabled "network@${zone}.service"; then
172 return ${EXIT_TRUE}
173 fi
174
175 return ${EXIT_FALSE}
176 }
177
178 zone_is_active() {
179 local zone="${1}"
180 assert isset zone
181
182 if service_is_active "network@${zone}.service"; then
183 return ${EXIT_TRUE}
184 fi
185
186 return ${EXIT_FALSE}
187 }
188
189 zone_is_enabled_or_active() {
190 local zone="${1}"
191 assert isset zone
192
193 zone_is_enabled "${zone}" || zone_is_active "${zone}"
194 }
195
196 zone_cmd() {
197 local cmd="${1}"
198 local port="${2}"
199 shift 2
200
201 assert isset cmd
202 assert isset zone
203
204 local hook="$(zone_get_hook ${zone})"
205 assert isset hook
206
207 hook_exec zone "${hook}" "${cmd}" "${zone}" "$@"
208 }
209
210 zone_new() {
211 local zone=${1}
212 local hook=${2}
213 shift 2
214
215 if ! zone_name_is_valid ${zone}; then
216 error "Zone name '${zone}' is not valid."
217 return ${EXIT_ERROR}
218 fi
219
220 if zone_exists ${zone}; then
221 error "Zone '${zone}' does already exist."
222 return ${EXIT_ERROR}
223 fi
224
225 if ! hook_zone_exists ${hook}; then
226 error "Hook '${hook}' does not exist."
227 return ${EXIT_ERROR}
228 fi
229
230 # Create directories for configs and ports
231 local what
232 for what in configs ports; do
233 make_directory "${NETWORK_ZONES_DIR}/${zone}/${what}"
234 done
235
236 hook_zone_exec "${hook}" "new" "${zone}" "$@"
237 local ret=$?
238
239 # Maybe the zone new hook did not exit correctly.
240 # If this is the case we remove the created zone immediately.
241 if [ "${ret}" != "${EXIT_OK}" ]; then
242 zone_destroy "${zone}"
243 return ${EXIT_ERROR}
244 fi
245
246 # Automatically enable zone.
247 zone_enable "${zone}"
248
249 # Bring up the zone immediately after
250 zone_start "${zone}"
251 }
252
253 zone_edit() {
254 local zone=${1}
255 shift
256
257 if ! zone_exists ${zone}; then
258 error "Zone '${zone}' does not exist."
259 return ${EXIT_ERROR}
260 fi
261
262 local hook="$(zone_get_hook "${zone}")"
263 if [ -z "${hook}" ]; then
264 error "Config file did not provide any hook."
265 return ${EXIT_ERROR}
266 fi
267
268 if ! hook_zone_exists ${hook}; then
269 error "Hook '${hook}' does not exist."
270 return ${EXIT_ERROR}
271 fi
272
273 hook_zone_exec ${hook} edit ${zone} "$@"
274 }
275
276 zone_rename() {
277 assert [ $# -eq 2 ]
278
279 local zone="${1}"
280 local name="${2}"
281
282 assert zone_exists "${zone}"
283 assert not zone_exists "${name}"
284
285 # The zone must be shut down before, is then renamed and
286 # potentially brought up again
287
288 # Save if the zone is running right now
289 zone_is_active "${zone}"
290 local zone_was_active="${?}"
291
292 # Save if the zone is enabled (i.e. auto-start)
293 zone_is_enabled "${zone}"
294 local zone_was_enabled="${?}"
295
296 # Stop the zone
297 zone_stop "${zone}"
298
299 # Disable the zone
300 zone_disable "${zone}"
301
302 # Rename the configuration files
303 mv -f "${NETWORK_ZONES_DIR}/${zone}" "${NETWORK_ZONES_DIR}/${name}"
304
305 # Enable the zone if it was enabled before
306 [ ${zone_was_enabled} -eq ${EXIT_TRUE} ] && zone_enable "${name}"
307
308 # Start the zone if it was up before
309 [ ${zone_was_active} -eq ${EXIT_TRUE} ] && zone_start "${name}"
310
311 log INFO "Zone ${zone} was renamed to ${name}"
312 return ${EXIT_OK}
313 }
314
315
316 zone_destroy() {
317 local zone="${1}"
318
319 # Cannot delete a zone that does not exist
320 if ! zone_exists "${zone}"; then
321 log ERROR "Zone ${zone} does not exist"
322 return ${EXIT_ERROR}
323 fi
324
325 log INFO "Destroying zone ${zone}"
326
327 # Force the zone down.
328 zone_is_active "${zone}" && zone_stop "${zone}"
329
330 # Disable zone auto-start
331 zone_disable "${zone}"
332
333 rm -rf "${NETWORK_ZONES_DIR}/${zone}"
334 }
335
336 zone_up() {
337 local zone=${1}
338 shift
339
340 if ! zone_exists ${zone}; then
341 error "Zone '${zone}' does not exist."
342 return ${EXIT_ERROR}
343 fi
344
345 local hook="$(zone_get_hook "${zone}")"
346 if [ -z "${hook}" ]; then
347 error "Config file did not provide any hook."
348 return ${EXIT_ERROR}
349 fi
350
351 if ! hook_zone_exists ${hook}; then
352 error "Hook '${hook}' does not exist."
353 return ${EXIT_ERROR}
354 fi
355
356 zone_db ${zone} starting
357
358 hook_zone_exec ${hook} up ${zone} "$@"
359
360 zone_db ${zone} started
361
362 # Execute all triggers after the zone got up
363 triggers_execute_all "up" ZONE="${zone}"
364 }
365
366 zone_down() {
367 local zone=${1}
368 shift
369
370 if ! zone_exists ${zone}; then
371 error "Zone '${zone}' does not exist."
372 return ${EXIT_ERROR}
373 fi
374
375 local hook="$(zone_get_hook "${zone}")"
376 if [ -z "${hook}" ]; then
377 error "Config file did not provide any hook."
378 return ${EXIT_ERROR}
379 fi
380
381 if ! hook_zone_exists ${hook}; then
382 error "Hook '${hook}' does not exist."
383 return ${EXIT_ERROR}
384 fi
385
386 zone_db ${zone} stopping
387
388 hook_zone_exec ${hook} down ${zone} "$@"
389
390 zone_db ${zone} stopped
391
392 # Execute all triggers after the zone went down
393 triggers_execute_all "down" ZONE="${zone}"
394 }
395
396 zone_status() {
397 local zone="${1}"
398 assert isset zone
399 shift
400
401 if ! zone_exists "${zone}"; then
402 error "Zone '${zone}' does not exist."
403 return ${EXIT_ERROR}
404 fi
405
406 local hook="$(zone_get_hook "${zone}")"
407 if [ -z "${hook}" ]; then
408 error "Config file did not provide any hook."
409 return ${EXIT_ERROR}
410 fi
411
412 if ! hook_zone_exists "${hook}"; then
413 error "Hook '${hook}' does not exist."
414 return ${EXIT_ERROR}
415 fi
416
417 hook_zone_exec "${hook}" "status" "${zone}" "$@"
418 }
419
420 zone_identify() {
421 assert [ $# -ge 1 ]
422
423 local zone="${1}"
424 shift
425
426 assert zone_exists "${zone}"
427
428 log INFO "Identifying zone ${zone}"
429 local pids
430
431 local pid
432 local port
433 for port in $(zone_get_ports "${zone}"); do
434 # Identify all the ports
435 port_identify "${port}" --background "$@"
436
437 # Save the PIDs of the subprocesses
438 list_append pids "$(cmd_background_get_pid)"
439 done
440
441 # Wait until all port_identfy processes have finished
442 for pid in ${pids}; do
443 cmd_background_result "${pid}"
444 done
445
446 return ${EXIT_OK}
447 }
448
449 zone_get_ports() {
450 local zone=${1}
451
452 assert isset zone
453
454 local port
455 for port in $(list_directory "${NETWORK_ZONES_DIR}/${zone}/ports"); do
456 if port_exists "${port}"; then
457 echo "${port}"
458 fi
459 done
460 }
461
462 zone_get_ports_num() {
463 local zone="${1}"
464 assert isset zone
465
466 local counter=0
467 local port
468 for port in $(list_directory "${NETWORK_ZONES_DIR}/${zone}/ports"); do
469 if port_exists "${port}"; then
470 counter=$(( ${counter} + 1 ))
471 fi
472 done
473
474 echo "${counter}"
475 return ${EXIT_OK}
476 }
477
478 zone_has_port() {
479 # Check, if the given port is configured
480 # in this zone.
481
482 local zone=${1}
483 local port=${2}
484 shift 2
485
486 assert isset zone
487 assert isset port
488
489 [ -e "${NETWORK_ZONES_DIR}/${zone}/ports/${port}" ]
490 }
491
492 zone_config() {
493 local zone="${1}"
494 local cmd="${2}"
495 shift 2
496
497 assert isset zone
498 assert isset cmd
499 assert zone_exists "${zone}"
500
501 case "${cmd}" in
502 new)
503 zone_config_new "${zone}" "$@"
504 ;;
505 destroy)
506 # usually ${1} is a valid hid
507 local hid=${1}
508 shift 1
509
510 # We convert the hid into an id
511 local id=$(zone_config_convert_hid_to_id ${zone} ${hid})
512
513 # If id isset the hid is valid and we can go on with the id
514 if isset id; then
515 zone_config_destroy "${zone}" "${id}" "$@"
516
517 # If we can't get a valid hid we check if we got a valid id
518 else
519 if zone_config_id_is_valid ${zone} ${hid}; then
520 zone_config_destroy "${zone}" ${hid} "$@"
521 else
522 log ERROR "${id} is not a valid id or hid"
523 fi
524 fi
525 ;;
526 list)
527 zone_config_list "${zone}" "$@"
528 ;;
529 *)
530 # usually ${1} is a valid hid
531 local hid=${cmd}
532 local cmd=${1}
533 shift 1
534
535 local id=$(zone_config_convert_hid_to_id ${zone} ${hid})
536
537 # If id isset the hid is valid and we can go on with the id
538 if isset id && [[ ${cmd} == "edit" ]]; then
539 zone_config_edit "${zone}" "${id}" "$@"
540
541 # If we didn't get a valid hid we check if we got a valid id
542 else
543 if zone_config_id_is_valid ${zone} ${id} && [[ ${cmd} == "edit" ]]; then
544 shift 1
545 zone_config_edit "${zone}" "${id}" "$@"
546 else
547 # in ${hid} is saved the command after network zone ${zone} config
548 error "Unrecognized argument: ${hid}"
549 cli_usage root-zone-config-subcommands
550 exit ${EXIT_ERROR}
551 fi
552 fi
553 ;;
554 esac
555 }
556
557 zone_config_cmd() {
558 assert [ $# -gt 2 ]
559
560 local cmd="${1}"
561 local zone="${2}"
562 shift 2
563
564 local hook="$(zone_get_hook "${zone}")"
565 assert isset hook
566
567 hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@"
568 }
569
570 zone_config_new() {
571 local zone="${1}"
572 shift
573
574 # Create a new configuration, but exit when that was
575 # not successful.
576 zone_config_cmd "new" "${zone}" "$@" || return ${?}
577
578 # If the config could be created, we will try to bring
579 # it up if the zone is up, too.
580 if zone_is_up "${zone}"; then
581 zone_configs_up "${zone}"
582 fi
583 }
584
585 zone_config_destroy() {
586 zone_config_cmd "destroy" "$@"
587 }
588
589 zone_config_edit() {
590 zone_config_cmd "edit" "$@"
591 }
592
593 zone_config_list() {
594 # This function list in an nice way all configs of a zone
595 local zone=${1}
596 assert isset zone
597
598 # Print a nice header
599 local format="%-3s %-20s %-20s"
600 print "${format}" "ID" "HOOK" "HID"
601
602 local config
603 local hook
604 local id
605 local hid
606
607 # Print for all config:
608 # id and hook
609 for config in $(zone_configs_list "${zone}"); do
610 id=${config##*.}
611 hook=$(zone_config_get_hook "${zone}" "${config}")
612 hid=$(zone_config_get_hid "${zone}" "${config}")
613 assert isset hook
614 print "${format}" "${id}" "${hook}" "${hid}"
615 done
616 }
617
618 zone_config_show() {
619 zone_config_cmd "show" "$@"
620 }
621
622 # Returns a list of all used ids for a zone
623 zone_config_list_ids() {
624 assert [ $# -eq 1 ]
625
626 local zone=${1}
627 local config
628 local ids
629
630 for config in $(zone_configs_list ${zone}); do
631 list_append ids "$(config_get_id_from_config ${config})"
632 done
633
634 echo ${ids}
635 }
636
637 # List all hids of a zone
638 zone_config_list_hids() {
639 assert [ $# -eq 1 ]
640
641 local zone=${1}
642
643 local config
644 for config in $(zone_configs_list ${zone}); do
645 zone_config_get_hid "${zone}" "${config}"
646 done
647 }
648
649 # get the hid from a given config
650 zone_config_get_hid() {
651 assert [ $# -eq 2 ]
652
653 local zone=${1}
654 local config=${2}
655
656 local hook="$(zone_config_get_hook "${zone}" "${config}")"
657
658 hook_exec "config" "${hook}" "hid" "${zone}" "${config}"
659 }
660
661 # Checks if a hid is valid for a given zone
662 zone_config_hid_is_valid() {
663 assert [ $# -eq 2]
664
665 local zone=${1}
666 local hid=${2}
667
668 local _hid
669 for _hid in $(zone_config_list_hids "${zone}"); do
670 if [[ ${_hid} = ${hid} ]]; then
671 return ${EXIT_TRUE}
672 fi
673 done
674
675 return ${EXIT_FALSE}
676 }
677
678 # This function converts a hid to a id
679 zone_config_convert_hid_to_id() {
680 assert [ $# -eq 2 ]
681
682 local zone=${1}
683 local hid=${2}
684
685 local config
686 for config in $(zone_configs_list ${zone}); do
687 # Get hook from config
688 local hook="$(zone_config_get_hook "${zone}" "${config}")"
689
690 if [[ "$(hook_exec "config" "${hook}" "hid" "${zone}" "${config}")" == "${hid}" ]]; then
691 config_get_id_from_config "${config}"
692 return ${EXIT_TRUE}
693 fi
694 done
695
696 return ${EXIT_FALSE}
697 }
698
699 zone_show() {
700 local zone=${1}
701
702 echo "${zone}"
703 echo " Type: $(zone_get_hook ${zone})"
704 echo
705 }
706
707 zones_show() {
708 local zone
709
710 for zone in $(zones_get "$@"); do
711 zone_show ${zone}
712 done
713 }
714
715 zones_get_all() {
716 local zone
717 for zone in $(list_directory "${NETWORK_ZONES_DIR}"); do
718 if zone_exists ${zone}; then
719 echo "${zone}"
720 fi
721 done
722 }
723
724 zones_get_next_free() {
725 # This function return the next free zones.
726 # Example net0 upl0 upl1 are configured so the next free zones are:
727 # net1 upl2
728 local i
729 local zone_name
730 for zone_name in ${VALID_ZONES}; do
731 i=0
732
733 while true; do
734 local zone="${zone_name}${i}"
735 if ! zone_exists ${zone}; then
736 echo "${zone}"
737 break
738 fi
739 i=$(( i + 1 ))
740 done
741 done
742 }
743
744 zones_get_local() {
745 local zone
746 for zone in $(zones_get_all); do
747 zone_is_local ${zone} && echo "${zone}"
748 done
749 }
750
751 zones_get_nonlocal() {
752 local zone
753 for zone in $(zones_get_all); do
754 zone_is_nonlocal ${zone} && echo "${zone}"
755 done
756 }
757
758 zones_get() {
759 local local=1
760 local remote=1
761
762 local zones
763
764 while [ $# -gt 0 ]; do
765 case "${1}" in
766 --local-only)
767 local=1
768 remote=0
769 ;;
770 --remote-only)
771 local=0
772 remote=1
773 ;;
774 --all)
775 local=1
776 remote=1
777 ;;
778 *)
779 if zone_name_is_valid ${1}; then
780 zones="${zones} ${1}"
781 else
782 warning "Unrecognized argument '${1}'"
783 fi
784 ;;
785 esac
786 shift
787 done
788
789 if [ -n "${zones}" ]; then
790 local zone
791 for zone in ${zones}; do
792 zone_exists ${zone} && echo "${zone}"
793 done
794 exit ${EXIT_OK}
795 fi
796
797 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
798 zones_get_all
799 elif [ ${local} -eq 1 ]; then
800 zones_get_local
801 elif [ ${remote} -eq 1 ]; then
802 zones_get_nonlocal
803 fi
804 }
805
806 zone_ports_list() {
807 local zone=${1}
808
809 list_directory "${NETWORK_ZONES_DIR}/${zone}/ports"
810 }
811
812 zone_port_attach() {
813 local zone="${1}"
814 assert isset zone
815
816 local port="${2}"
817 assert isset port
818
819 shift 2
820
821 # Check if the port actually exists.
822 if ! port_exists "${port}"; then
823 error "Cannot attach port '${port}' which does not exist"
824 return ${EXIT_ERROR}
825 fi
826
827 # Check if the port is already connected to this or any other zone.
828 local z
829 for z in $(zones_get_all); do
830 if zone_has_port "${z}" "${port}"; then
831 error "Port '${port}' is already attached to zone '${z}'"
832 return ${EXIT_ERROR}
833 fi
834 done
835
836 local hook="$(zone_get_hook "${zone}")"
837 assert isset hook
838
839 # Make the port briefly flash if supported
840 if device_exists ${port}; then
841 port_identify "${port}" --background
842 fi
843
844 hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@"
845 local ret="${?}"
846
847 case "${ret}" in
848 ${EXIT_OK})
849 log INFO "${port} has been attached to ${zone}"
850
851 # Automatically connect the port
852 zone_port_start "${zone}" "${port}"
853 ;;
854 *)
855 log CRITICAL "${port} could not be attached to ${zone}"
856 ;;
857 esac
858
859 return ${ret}
860 }
861
862 zone_port_edit() {
863 local zone="${1}"
864 assert isset zone
865
866 local port="${2}"
867 assert isset port
868
869 shift 2
870
871 # Check if the port actually exists.
872 if ! port_exists "${port}"; then
873 error "Port '${port}' does not exist"
874 return ${EXIT_ERROR}
875 fi
876
877 # Check if the zone actually has this port.
878 if ! zone_has_port "${zone}" "${port}"; then
879 error "Port '${port}' is not attached to zone '${zone}'"
880 return ${EXIT_ERROR}
881 fi
882
883 local hook=$(zone_get_hook "${zone}")
884 assert isset hook
885
886 hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@"
887 }
888
889 zone_port_detach() {
890 local zone="${1}"
891 assert isset zone
892
893 local port="${2}"
894 assert isset port
895
896 shift 2
897
898 # Check if the zone actually has this port.
899 if ! zone_has_port "${zone}" "${port}"; then
900 error "Port '${port}' is not attached to zone '${zone}'"
901 return ${EXIT_ERROR}
902 fi
903
904 local hook=$(zone_get_hook "${zone}")
905 assert isset hook
906
907 # Make the port briefly flash if supported
908 port_identify "${port}" --background
909
910 hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@"
911 local ret="${?}"
912
913 case "${ret}" in
914 ${EXIT_OK})
915 log INFO "${port} has been detached from ${zone}"
916
917 # Bring down the port if needed
918 zone_port_stop "${zone}" "${port}"
919 ;;
920 *)
921 log CRITICAL "${port} could not be detached from ${zone}"
922 ;;
923 esac
924
925 return ${ret}
926 }
927
928 zone_port_cmd() {
929 local cmd="${1}"
930 assert isset cmd
931
932 local zone="${2}"
933 assert isset zone
934
935 local port="${3}"
936 assert isset port
937
938 shift 3
939
940 local hook="$(zone_get_hook "${zone}")"
941 assert isset hook
942
943 # Dispatch command to hook
944 hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" "$@"
945 }
946
947 zone_port_create() {
948 zone_port_cmd "port_create" "$@"
949 }
950
951 zone_port_remove() {
952 zone_port_cmd "port_remove" "$@"
953 }
954
955 zone_port_up() {
956 zone_port_cmd "port_up" "$@"
957 }
958
959 zone_port_down() {
960 zone_port_cmd "port_down" "$@"
961 }
962
963 # The next two functions automagically bring up and down
964 # port that are attached to a bridge or similar.
965 # The problem that is tried to overcome here is that there
966 # are ports which exist all the time (like ethernet ports)
967 # and therefore do not dispatch a hotplug event when
968 # port_create is called.
969
970 zone_port_start() {
971 local zone="${1}"
972 local port="${2}"
973
974 if zone_is_active "${zone}"; then
975 if device_exists "${port}"; then
976 zone_port_up "${zone}" "${port}"
977 return ${?}
978 else
979 zone_port_create "${zone}" "${port}"
980 return ${?}
981 fi
982 fi
983
984 return ${EXIT_OK}
985 }
986
987 zone_port_stop() {
988 local zone="${1}"
989 local port="${2}"
990
991 # Shut down the port if necessary
992 if zone_is_active "${zone}" && port_is_up "${port}"; then
993 zone_port_down "${zone}" "${port}"
994 fi
995
996 # Remove the port
997 zone_port_remove "${zone}" "${port}"
998 }
999
1000 zone_port_status() {
1001 zone_port_cmd "port_status" "$@"
1002 }
1003
1004 zone_ports_cmd() {
1005 local cmd="${1}"
1006 assert isset cmd
1007
1008 local zone="${2}"
1009 assert isset zone
1010
1011 shift 2
1012
1013 local hook="$(zone_get_hook "${zone}")"
1014
1015 local port
1016 for port in $(zone_get_ports ${zone}); do
1017 hook_zone_exec "${hook}" "${cmd}" "${zone}" "${port}" "$@"
1018 done
1019 }
1020
1021 zone_ports_create() {
1022 zone_ports_cmd "port_create" "$@"
1023 }
1024
1025 zone_ports_remove() {
1026 zone_ports_cmd "port_remove" "$@"
1027 }
1028
1029 zone_ports_up() {
1030 zone_ports_cmd "port_up" "$@"
1031 }
1032
1033 zone_ports_down() {
1034 zone_ports_cmd "port_down" "$@"
1035 }
1036
1037 zone_ports_status() {
1038 zone_ports_cmd "port_status" "$@"
1039 }
1040
1041 zone_configs_cmd() {
1042 assert [ $# -ge 2 ]
1043
1044 local cmd="${1}"
1045 local zone="${2}"
1046 shift 2
1047
1048 assert zone_exists "${zone}"
1049
1050 local config
1051 for config in $(zone_configs_list "${zone}"); do
1052 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1053 assert isset config_hook
1054
1055 hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" "$@"
1056 done
1057 }
1058
1059 zone_configs_up() {
1060 zone_configs_cmd "up" "$@"
1061 }
1062
1063 zone_configs_down() {
1064 zone_configs_cmd "down" "$@"
1065 }
1066
1067 zone_configs_status() {
1068 zone_configs_cmd "status" "$@"
1069 }
1070
1071 zone_configs_list() {
1072 local zone=${1}
1073
1074 list_directory "${NETWORK_ZONES_DIR}/${zone}/configs"
1075 }
1076
1077 zone_config_get_new_id() {
1078 # This functions returns the next free id for a zone
1079
1080 assert [ $# -eq 1 ]
1081 local zone=${1}
1082
1083 local zone_path="${NETWORK_ZONES_DIR}/${zone}"
1084 local i=0
1085
1086 while true; do
1087 if [ ! -f ${zone_path}/configs/*.${i} ]; then
1088 echo "${i}"
1089 return ${EXIT_OK}
1090 fi
1091 (( i++ ))
1092 done
1093 }
1094
1095 zone_config_check_same_setting() {
1096 # This functions checks if a config hook
1097 # with the same setting is already configured for this zone.
1098 # Returns True when yes and False when no.
1099
1100 assert [ $# -eq 5 ]
1101
1102 local zone=${1}
1103 local hook=${2}
1104 local id=${3}
1105 local key=${4}
1106 local value=${5}
1107
1108 # The key should be local for this function
1109 local ${key}
1110 local config
1111
1112 for config in $(zone_configs_list ${zone}); do
1113 # Check if the config is eqal with the config we want to edit, when continue
1114 if [[ "${config}" = "${hook}.${id}" ]]; then
1115 continue
1116 fi
1117
1118 # Check if the config is from the given hook, when not continue
1119 if [[ $(zone_config_get_hook "${zone}" "${config}") != ${hook} ]]; then
1120 continue
1121 fi
1122
1123 # Get the value of the key for a given function
1124 zone_config_settings_read "${zone}" "${config}" \
1125 --ignore-superfluous-settings "${key}"
1126 # Check if the value of the config and the passed value are eqal
1127 if [[ "${value}" == "${!key}" ]]; then
1128 return ${EXIT_TRUE}
1129 fi
1130 done
1131
1132 return ${EXIT_FALSE}
1133 }
1134
1135 zone_config_get_hook() {
1136 assert [ $# -eq 2 ]
1137
1138 local zone="${1}"
1139 assert isset zone
1140
1141 local config="${2}"
1142 assert isset config
1143
1144 local HOOK
1145 zone_config_settings_read "${zone}" "${config}" \
1146 --ignore-superfluous-settings HOOK
1147
1148 print "${HOOK}"
1149 }
1150
1151 zone_config_hook_is_configured() {
1152 # Checks if a zone has already at least one config with the given hook.
1153 # Returns True when yes and False when no
1154
1155 assert [ $# -eq 2 ]
1156 local zone=${1}
1157 local hook=${2}
1158
1159 local config
1160 for config in $(zone_configs_list "${zone}"); do
1161 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1162 assert isset config_hook
1163 if [[ ${hook} == ${config_hook} ]]; then
1164 return ${EXIT_TRUE}
1165 fi
1166
1167 done
1168
1169 # If we get here the zone has no config with the given hook
1170 return ${EXIT_FALSE}
1171 }
1172
1173 zone_config_id_is_valid() {
1174 # This function checks if a given id is valid for a zone
1175 # Return True when yes and false when no
1176
1177 assert [ $# -eq 2 ]
1178 local zone=${1}
1179 local id=${2}
1180
1181 local zone_path="${NETWORK_ZONES_DIR}/${zone}"
1182
1183 [ -f ${zone_path}/configs/*.${id} ];
1184 }
1185
1186 # This function checks if a given hid is valid for a zone
1187 # Return True when yes and false when no
1188 zone_config_hid_is_valid() {
1189 assert [ $# -eq 2 ]
1190 local zone=${1}
1191 local hid=${2}
1192
1193 local _hid
1194 for _hid in $(zone_config_list_hids ${zone}); do
1195 if [[ ${_hid} == ${hid} ]]; then
1196 return ${EXIT_TRUE}
1197 fi
1198 done
1199
1200 return ${EXIT_FALSE}
1201 }
1202
1203 zone_config_get_hook_from_id() {
1204 # Returns the hook for a given id
1205 assert [ $# -eq 2 ]
1206 local zone=${1}
1207 local id=${2}
1208
1209 local config
1210 for config in $(zone_configs_list "${zone}"); do
1211 if [[ ${config} == *.${id} ]]; then
1212 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1213 assert isset config_hook
1214 print "${config_hook}"
1215 return "${EXIT_OK}"
1216 fi
1217 done
1218
1219 # If we get here the zone has no config with the given id
1220 return ${EXIT_ERROR}
1221 }
1222
1223 zone_has_ip() {
1224 device_has_ip "$@"
1225 }
1226
1227 zone_db() {
1228 local zone=${1}
1229 local action=${2}
1230 shift 2
1231
1232 case "${action}" in
1233 starting|started|stopping|stopped)
1234 db_connection_update ${zone} ${action}
1235 ;;
1236 esac
1237 }
1238
1239 zone_is_up() {
1240 local zone=${1}
1241
1242 device_is_up ${zone}
1243 }
1244
1245 zone_is_down() {
1246 ! zone_is_up "$@"
1247 }
1248
1249 zone_get_supported_port_hooks() {
1250 local zone=${1}
1251
1252 local hook=$(zone_get_hook ${zone})
1253
1254 hook_zone_ports_get_all ${hook}
1255 }
1256
1257 zone_get_supported_config_hooks() {
1258 hook_config_get_all
1259 }
1260
1261 zone_settings_read() {
1262 local zone=${1}
1263 assert isset zone
1264 shift
1265
1266 local args
1267 if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS}" ]; then
1268 list_append args ${HOOK_SETTINGS}
1269 else
1270 list_append args "$@"
1271 fi
1272
1273 # Save the HOOK variable.
1274 local hook="${HOOK}"
1275
1276 settings_read "${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
1277
1278 # Restore hook.
1279 HOOK="${hook}"
1280 }
1281
1282 zone_settings_write() {
1283 local zone="${1}"
1284 assert isset zone
1285
1286 local args
1287 if function_exists "hook_check_settings"; then
1288 list_append args "--check=\"hook_check_settings\""
1289 fi
1290 list_append args ${HOOK_SETTINGS}
1291
1292 settings_write "${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
1293 }
1294
1295 zone_settings_set() {
1296 local zone=${1}
1297 shift
1298 local args="$@"
1299
1300 assert isset zone
1301
1302 (
1303 zone_settings_read ${zone}
1304
1305 for arg in ${args}; do
1306 eval "${arg}"
1307 done
1308
1309 zone_settings_write ${zone}
1310 )
1311 }
1312
1313 zone_settings_get() {
1314 local zone=${1}
1315 local key=${2}
1316
1317 assert isset zone
1318 assert isset key
1319
1320 (
1321 zone_settings_read "${zone}" "${key}" \
1322 --ignore-superfluous-settings
1323
1324 echo "${!key}"
1325 )
1326 }
1327
1328 zone_config_settings_read() {
1329 assert [ $# -ge 2 ]
1330
1331 local zone="${1}"
1332 local config="${2}"
1333 shift 2
1334
1335 local args
1336 if [ $# -eq 0 ] && [ -n "${HOOK_CONFIG_SETTINGS}" ]; then
1337 list_append args ${HOOK_CONFIG_SETTINGS}
1338 else
1339 list_append args "$@"
1340 fi
1341
1342 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
1343 settings_read "${path}" ${args}
1344 }
1345
1346 zone_config_settings_write() {
1347 assert [ $# -ge 2 ]
1348
1349 local zone="${1}"
1350 local hook="${2}"
1351 local id=${3}
1352
1353 assert isset id
1354
1355 local args
1356 if function_exists "hook_check_config_settings"; then
1357 list_append args "--check=\"hook_check_config_settings\""
1358 fi
1359 list_append args ${HOOK_CONFIG_SETTINGS}
1360
1361 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${hook}.${id}"
1362 settings_write "${path}" ${args}
1363 }
1364
1365 zone_config_settings_destroy() {
1366 # This function deletes the config file for a given zone and config
1367 assert [ $# -ge 2 ]
1368 local zone="${1}"
1369 local config="${2}"
1370
1371 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
1372
1373 # Check if path is valid
1374 if [ ! -f ${path} ]; then
1375 log ERROR "Path: '${path}' is not valid"
1376 return ${EXIT_ERROR}
1377 fi
1378
1379 log DEBUG "Deleting config file ${path}"
1380 rm -f "${path}"
1381
1382 }
1383
1384 zone_config_find_by_hook() {
1385 local zone="${1}"
1386 assert isset zone
1387
1388 local hook="${2}"
1389 assert isset hook
1390
1391 local config
1392 for config in $(zone_configs_list "${zone}"); do
1393 local h="$(zone_config_get_hook "${zone}" "${config}")"
1394
1395 [[ "${hook}" = "${h}" ]] && echo "${config}"
1396 done
1397
1398 return ${EXIT_OK}
1399 }
1400
1401 zone_config_settings_read_by_hook() {
1402 local zone="${1}"
1403 assert isset zone
1404
1405 local hook="${2}"
1406 assert isset hook
1407
1408 local config
1409 for config in $(zone_config_find_by_hook "${zone}" "${hook}"); do
1410 zone_config_settings_read "${zone}" "${config}"
1411 done
1412
1413 return ${EXIT_OK}
1414 }
1415
1416 zone_port_settings_read() {
1417 assert [ $# -ge 2 ]
1418
1419 local zone="${1}"
1420 local port="${2}"
1421 shift 2
1422
1423 local args
1424 if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then
1425 list_append args ${HOOK_PORT_SETTINGS}
1426 else
1427 list_append args "$@"
1428 fi
1429
1430 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1431 settings_read "${path}" ${args}
1432 }
1433
1434 zone_port_settings_write() {
1435 assert [ $# -ge 2 ]
1436
1437 local zone="${1}"
1438 local port="${2}"
1439 shift 2
1440
1441 local args
1442 if function_exists "hook_check_port_settings"; then
1443 list_append args "--check=\"hook_check_port_settings\""
1444 fi
1445 list_append args ${HOOK_PORT_SETTINGS}
1446
1447 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1448 settings_write "${path}" ${args}
1449 }
1450
1451 zone_port_settings_remove() {
1452 assert [ $# -eq 2 ]
1453
1454 local zone="${1}"
1455 local port="${2}"
1456
1457 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1458 settings_remove "${path}"
1459 }
1460
1461 zone_get_color() {
1462 # This function return the color of a zone
1463 assert [ $# -eq 1 ]
1464
1465 local name=${1}
1466 color_read "zone" ${name}
1467 }
1468
1469 zone_get_description_title() {
1470 assert [ $# -eq 1 ]
1471
1472 local name=${1}
1473 description_title_read $(description_format_filename "zone" "${name}")
1474 }