]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.zone
Remove zone_dir and zone_file
[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 4 ]
1101
1102 local zone=${1}
1103 local hook=${2}
1104 local key=${3}
1105 local value=${4}
1106
1107 # The key should be local for this function
1108 local ${key}
1109 local config
1110
1111 for config in $(zone_configs_list ${zone}); do
1112 # Check if the config is from the given hook, when not continue
1113 if [[ $(zone_config_get_hook "${zone}" "${config}") != ${hook} ]]; then
1114 continue
1115 fi
1116 # Get the value of the key for a given function
1117 zone_config_settings_read "${zone}" "${config}" \
1118 --ignore-superfluous-settings "${key}"
1119 # Check if the value of the config and the passed value are eqal
1120 if [[ "${value}" == "${!key}" ]]; then
1121 return ${EXIT_TRUE}
1122 fi
1123 done
1124
1125 return ${EXIT_FALSE}
1126 }
1127
1128 zone_config_get_hook() {
1129 assert [ $# -eq 2 ]
1130
1131 local zone="${1}"
1132 assert isset zone
1133
1134 local config="${2}"
1135 assert isset config
1136
1137 local HOOK
1138 zone_config_settings_read "${zone}" "${config}" \
1139 --ignore-superfluous-settings HOOK
1140
1141 print "${HOOK}"
1142 }
1143
1144 zone_config_hook_is_configured() {
1145 # Checks if a zone has already at least one config with the given hook.
1146 # Returns True when yes and False when no
1147
1148 assert [ $# -eq 2 ]
1149 local zone=${1}
1150 local hook=${2}
1151
1152 local config
1153 for config in $(zone_configs_list "${zone}"); do
1154 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1155 assert isset config_hook
1156 if [[ ${hook} == ${config_hook} ]]; then
1157 return ${EXIT_TRUE}
1158 fi
1159
1160 done
1161
1162 # If we get here the zone has no config with the given hook
1163 return ${EXIT_FALSE}
1164 }
1165
1166 zone_config_id_is_valid() {
1167 # This function checks if a given id is valid for a zone
1168 # Return True when yes and false when no
1169
1170 assert [ $# -eq 2 ]
1171 local zone=${1}
1172 local id=${2}
1173
1174 local zone_path="${NETWORK_ZONES_DIR}/${zone}"
1175
1176 [ -f ${zone_path}/configs/*.${id} ];
1177 }
1178
1179 # This function checks if a given hid is valid for a zone
1180 # Return True when yes and false when no
1181 zone_config_hid_is_valid() {
1182 assert [ $# -eq 2 ]
1183 local zone=${1}
1184 local hid=${2}
1185
1186 local _hid
1187 for _hid in $(zone_config_list_hids ${zone}); do
1188 if [[ ${_hid} == ${hid} ]]; then
1189 return ${EXIT_TRUE}
1190 fi
1191 done
1192
1193 return ${EXIT_FALSE}
1194 }
1195
1196 zone_config_get_hook_from_id() {
1197 # Returns the hook for a given id
1198 assert [ $# -eq 2 ]
1199 local zone=${1}
1200 local id=${2}
1201
1202 local config
1203 for config in $(zone_configs_list "${zone}"); do
1204 if [[ ${config} == *.${id} ]]; then
1205 local config_hook="$(zone_config_get_hook "${zone}" "${config}")"
1206 assert isset config_hook
1207 print "${config_hook}"
1208 return "${EXIT_OK}"
1209 fi
1210 done
1211
1212 # If we get here the zone has no config with the given id
1213 return ${EXIT_ERROR}
1214 }
1215
1216 zone_has_ip() {
1217 device_has_ip "$@"
1218 }
1219
1220 zone_db() {
1221 local zone=${1}
1222 local action=${2}
1223 shift 2
1224
1225 case "${action}" in
1226 starting|started|stopping|stopped)
1227 db_connection_update ${zone} ${action}
1228 ;;
1229 esac
1230 }
1231
1232 zone_is_up() {
1233 local zone=${1}
1234
1235 device_is_up ${zone}
1236 }
1237
1238 zone_is_down() {
1239 ! zone_is_up "$@"
1240 }
1241
1242 zone_get_supported_port_hooks() {
1243 local zone=${1}
1244
1245 local hook=$(zone_get_hook ${zone})
1246
1247 hook_zone_ports_get_all ${hook}
1248 }
1249
1250 zone_get_supported_config_hooks() {
1251 hook_config_get_all
1252 }
1253
1254 zone_settings_read() {
1255 local zone=${1}
1256 assert isset zone
1257 shift
1258
1259 local args
1260 if [ $# -eq 0 ] && [ -n "${HOOK_SETTINGS}" ]; then
1261 list_append args ${HOOK_SETTINGS}
1262 else
1263 list_append args "$@"
1264 fi
1265
1266 # Save the HOOK variable.
1267 local hook="${HOOK}"
1268
1269 settings_read "${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
1270
1271 # Restore hook.
1272 HOOK="${hook}"
1273 }
1274
1275 zone_settings_write() {
1276 local zone="${1}"
1277 assert isset zone
1278
1279 local args
1280 if function_exists "hook_check_settings"; then
1281 list_append args "--check=\"hook_check_settings\""
1282 fi
1283 list_append args ${HOOK_SETTINGS}
1284
1285 settings_write "${NETWORK_ZONES_DIR}/${zone}/settings" ${args}
1286 }
1287
1288 zone_settings_set() {
1289 local zone=${1}
1290 shift
1291 local args="$@"
1292
1293 assert isset zone
1294
1295 (
1296 zone_settings_read ${zone}
1297
1298 for arg in ${args}; do
1299 eval "${arg}"
1300 done
1301
1302 zone_settings_write ${zone}
1303 )
1304 }
1305
1306 zone_settings_get() {
1307 local zone=${1}
1308 local key=${2}
1309
1310 assert isset zone
1311 assert isset key
1312
1313 (
1314 zone_settings_read "${zone}" "${key}" \
1315 --ignore-superfluous-settings
1316
1317 echo "${!key}"
1318 )
1319 }
1320
1321 zone_config_settings_read() {
1322 assert [ $# -ge 2 ]
1323
1324 local zone="${1}"
1325 local config="${2}"
1326 shift 2
1327
1328 local args
1329 if [ $# -eq 0 ] && [ -n "${HOOK_CONFIG_SETTINGS}" ]; then
1330 list_append args ${HOOK_CONFIG_SETTINGS}
1331 else
1332 list_append args "$@"
1333 fi
1334
1335 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
1336 settings_read "${path}" ${args}
1337 }
1338
1339 zone_config_settings_write() {
1340 assert [ $# -ge 2 ]
1341
1342 local zone="${1}"
1343 local hook="${2}"
1344 local id=${3}
1345
1346 if ! isset id; then
1347 id=$(zone_config_get_new_id ${zone})
1348 log DEBUG "ID for the config is: ${id}"
1349 fi
1350
1351 local args
1352 if function_exists "hook_check_config_settings"; then
1353 list_append args "--check=\"hook_check_config_settings\""
1354 fi
1355 list_append args ${HOOK_CONFIG_SETTINGS}
1356
1357 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${hook}.${id}"
1358 settings_write "${path}" ${args}
1359 }
1360
1361 zone_config_settings_destroy() {
1362 # This function deletes the config file for a given zone and config
1363 assert [ $# -ge 2 ]
1364 local zone="${1}"
1365 local config="${2}"
1366
1367 local path="${NETWORK_ZONES_DIR}/${zone}/configs/${config}"
1368
1369 # Check if path is valid
1370 if [ ! -f ${path} ]; then
1371 log ERROR "Path: '${path}' is not valid"
1372 return ${EXIT_ERROR}
1373 fi
1374
1375 log DEBUG "Deleting config file ${path}"
1376 rm -f "${path}"
1377
1378 }
1379 zone_port_settings_read() {
1380 assert [ $# -ge 2 ]
1381
1382 local zone="${1}"
1383 local port="${2}"
1384 shift 2
1385
1386 local args
1387 if [ $# -eq 0 ] && [ -n "${HOOK_PORT_SETTINGS}" ]; then
1388 list_append args ${HOOK_PORT_SETTINGS}
1389 else
1390 list_append args "$@"
1391 fi
1392
1393 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1394 settings_read "${path}" ${args}
1395 }
1396
1397 zone_port_settings_write() {
1398 assert [ $# -ge 2 ]
1399
1400 local zone="${1}"
1401 local port="${2}"
1402 shift 2
1403
1404 local args
1405 if function_exists "hook_check_port_settings"; then
1406 list_append args "--check=\"hook_check_port_settings\""
1407 fi
1408 list_append args ${HOOK_PORT_SETTINGS}
1409
1410 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1411 settings_write "${path}" ${args}
1412 }
1413
1414 zone_port_settings_remove() {
1415 assert [ $# -eq 2 ]
1416
1417 local zone="${1}"
1418 local port="${2}"
1419
1420 local path="${NETWORK_ZONES_DIR}/${zone}/ports/${port}"
1421 settings_remove "${path}"
1422 }
1423
1424 zone_get_color() {
1425 # This function return the color of a zone
1426 assert [ $# -eq 1 ]
1427
1428 local name=${1}
1429 color_read "zone" ${name}
1430 }
1431
1432 zone_get_description_title() {
1433 assert [ $# -eq 1 ]
1434
1435 local name=${1}
1436 description_title_read $(description_format_filename "zone" "${name}")
1437 }