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