]> git.ipfire.org Git - people/ms/network.git/blob - functions.zone
b18d4548fad2c0e47b649fd56410d89eb81bc850
[people/ms/network.git] / functions.zone
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 function zone_dir() {
23 local zone=${1}
24
25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
26 }
27
28 function zone_exists() {
29 local zone=${1}
30 assert isset zone
31
32 [ -d "$(zone_dir ${zone})" ]
33 }
34
35 function zone_match() {
36 local match
37
38 local i
39 for i in ${VALID_ZONES}; do
40 match="${match}|${i}[0-9]{1,5}"
41 done
42
43 echo "${match:1:${#match}}"
44 }
45
46 function zone_name_is_valid() {
47 local zone=${1}
48
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
51
52 [[ ${zone} =~ $(zone_match) ]]
53 }
54
55 function zone_is_local() {
56 local zone=${1}
57
58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
59 }
60
61 function zone_is_nonlocal() {
62 local zone=${1}
63
64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
65 }
66
67 function zone_get_hook() {
68 local zone=${1}
69 assert isset zone
70
71 config_get_hook $(zone_dir ${zone})/settings
72 }
73
74 function zone_start() {
75 # This function will bring up the zone
76 # 'asynchronously' with help of systemd.
77
78 local zone=${1}
79 assert zone_exists ${zone}
80
81 service_start "network@${zone}.service"
82 }
83
84 function zone_stop() {
85 # This function will bring down the zone
86 # 'asynchronously' with help of systemd.
87
88 local zone=${1}
89 assert zone_exists ${zone}
90
91 service_stop "network@${zone}.service"
92 }
93
94 function zone_enable() {
95 # This function will enable the zone
96 # with help of systemd.
97
98 local zone="${1}"
99 assert zone_exists "${zone}"
100
101 # Enable service for the zone
102 service_enable "network@${zone}.service"
103 local ret=$?
104
105 if [ ${ret} -eq ${EXIT_OK} ]; then
106 log INFO "Auto-start enabled for zone ${zone}"
107 return ${EXIT_OK}
108 fi
109
110 log ERROR "Could not enable zone ${zone}: ${ret}"
111 return ${ret}
112 }
113
114 function zone_disable() {
115 # This function will disable the zone
116 # with help of systemd.
117
118 local zone="${1}"
119 assert zone_exists "${zone}"
120
121 # Disable service for the zone
122 service_disable "network@${zone}.service"
123 local ret=$?
124
125 if [ ${ret} -eq ${EXIT_OK} ]; then
126 log INFO "Auto-start disabled for zone ${zone}"
127 return ${EXIT_OK}
128 fi
129
130 log ERROR "Could not disable zone ${zone}: ${ret}"
131 return ${ret}
132 }
133
134 function zone_is_enabled() {
135 local zone="${1}"
136 assert isset zone
137
138 # Ask systemd if the zone is enabled.
139 if service_is_enabled "network@${zone}.service"; then
140 return ${EXIT_TRUE}
141 fi
142
143 return ${EXIT_FALSE}
144 }
145
146 function zone_create() {
147 local zone=${1}
148 local hook=${2}
149 shift 2
150
151 if ! zone_name_is_valid ${zone}; then
152 error "Zone name '${zone}' is not valid."
153 return ${EXIT_ERROR}
154 fi
155
156 if zone_exists ${zone}; then
157 error "Zone '${zone}' does already exist."
158 return ${EXIT_ERROR}
159 fi
160
161 if ! hook_zone_exists ${hook}; then
162 error "Hook '${hook}' does not exist."
163 return ${EXIT_ERROR}
164 fi
165
166 mkdir -p $(zone_dir ${zone})
167
168 # Create directories for configs and ports
169 mkdir -p $(zone_dir ${zone})/{configs,ports}
170
171 hook_zone_exec ${hook} create ${zone} $@
172 local ret=$?
173
174 # Maybe the zone create hook did not exit correctly.
175 # If this is the case we remove the created zone immediately.
176 if [ "${ret}" = "${EXIT_ERROR}" ]; then
177 zone_remove_now ${zone}
178 return ${EXIT_ERROR}
179 fi
180
181 # Automatically enable zone.
182 zone_enable "${zone}"
183 }
184
185 function zone_edit() {
186 local zone=${1}
187 shift
188
189 if ! zone_exists ${zone}; then
190 error "Zone '${zone}' does not exist."
191 return ${EXIT_ERROR}
192 fi
193
194 # Check if the zone is tagged for removal.
195 if zone_has_remove_tag ${zone}; then
196 error "You cannot edit a zone that is tagged for removal."
197 return ${EXIT_ERROR}
198 fi
199
200 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
201
202 if [ -z "${hook}" ]; then
203 error "Config file did not provide any hook."
204 return ${EXIT_ERROR}
205 fi
206
207 if ! hook_zone_exists ${hook}; then
208 error "Hook '${hook}' does not exist."
209 return ${EXIT_ERROR}
210 fi
211
212 hook_zone_exec ${hook} edit ${zone} $@
213 }
214
215
216 function zone_remove() {
217 local zone=${1}
218 assert zone_exists ${zone}
219
220 # Make the zone for removal.
221 touch $(zone_dir ${zone})/.remove
222
223 log INFO "Zone '${zone}' has been tagged for removal."
224 }
225
226 function zone_has_remove_tag() {
227 local zone=${1}
228 assert zone_exists ${zone}
229
230 [ -e "$(zone_dir ${zone})/.remove" ]
231 }
232
233 # This function will remove the given zone
234 # RIGHT NOW. Use zone_remove to remove it
235 # at the next status change.
236 function zone_remove_now() {
237 local zone=${1}
238 assert zone_exists ${zone}
239
240 log INFO "Removing zone '${zone}' right now."
241
242 # Force the zone down.
243 zone_is_up ${zone} && zone_set_down ${zone}
244
245 # Disable zone.
246 zone_disable "${zone}"
247
248 rm -rf $(zone_dir ${zone})
249 }
250
251 function zone_up() {
252 local zone=${1}
253 shift
254
255 if ! zone_exists ${zone}; then
256 error "Zone '${zone}' does not exist."
257 return ${EXIT_ERROR}
258 fi
259
260 # Check if a zone has got the remove tag.
261 if zone_has_remove_tag ${zone}; then
262 error "Cannot bring up any zone which is to be removed."
263 return ${EXIT_ERROR}
264 fi
265
266 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
267
268 if [ -z "${hook}" ]; then
269 error "Config file did not provide any hook."
270 return ${EXIT_ERROR}
271 fi
272
273 if ! hook_zone_exists ${hook}; then
274 error "Hook '${hook}' does not exist."
275 return ${EXIT_ERROR}
276 fi
277
278 zone_db ${zone} starting
279
280 hook_zone_exec ${hook} up ${zone} $@
281
282 zone_db ${zone} started
283 }
284
285 function zone_down() {
286 local zone=${1}
287 shift
288
289 if ! zone_exists ${zone}; then
290 error "Zone '${zone}' does not exist."
291 return ${EXIT_ERROR}
292 fi
293
294 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
295
296 if [ -z "${hook}" ]; then
297 error "Config file did not provide any hook."
298 return ${EXIT_ERROR}
299 fi
300
301 if ! hook_zone_exists ${hook}; then
302 error "Hook '${hook}' does not exist."
303 return ${EXIT_ERROR}
304 fi
305
306 zone_db ${zone} stopping
307
308 hook_zone_exec ${hook} down ${zone} $@
309
310 zone_db ${zone} stopped
311
312 # Remove the zone, if it has got a remove tag.
313 if zone_has_remove_tag ${zone}; then
314 zone_remove_now ${zone}
315 fi
316 }
317
318 function zone_status() {
319 local zone=${1}
320 shift
321
322 if ! zone_exists ${zone}; then
323 error "Zone '${zone}' does not exist."
324 return ${EXIT_ERROR}
325 fi
326
327 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
328
329 if [ -z "${hook}" ]; then
330 error "Config file did not provide any hook."
331 return ${EXIT_ERROR}
332 fi
333
334 if ! hook_zone_exists ${hook}; then
335 error "Hook '${hook}' does not exist."
336 return ${EXIT_ERROR}
337 fi
338
339 hook_zone_exec ${hook} status ${zone} $@
340
341 # Show that the zone it to be removed soon.
342 if zone_has_remove_tag ${zone}; then
343 warning "This zone is tagged for removal."
344 fi
345 }
346
347 function zone_port() {
348 local zone=${1}
349 local action=${2}
350 shift 2
351
352 assert isset zone
353 assert isset action
354 assert zone_exists ${zone}
355
356 # Aliases
357 case "${action}" in
358 del|delete|remove)
359 action="rem"
360 ;;
361 esac
362
363 case "${action}" in
364 add|edit|rem)
365 zone_port_${action} ${zone} $@
366 ;;
367 *)
368 error "Unrecognized argument: ${action}"
369 cli_usage root-zone-port-subcommands
370 exit ${EXIT_ERROR}
371 ;;
372 esac
373 }
374
375 function zone_port_add() {
376 local zone=${1}
377 shift
378
379 assert isset zone
380
381 local hook=$(zone_get_hook ${zone})
382
383 assert isset hook
384
385 hook_zone_exec ${hook} port_add ${zone} $@
386 }
387
388 function zone_port_edit() {
389 zone_port_cmd edit $@
390 }
391
392 function zone_port_rem() {
393 zone_port_cmd rem $@
394 }
395
396 function zone_port_cmd() {
397 local cmd=${1}
398 local zone=${2}
399 local port=${3}
400 shift 3
401
402 assert isset zone
403 assert isset port
404
405 local hook_zone=$(zone_get_hook ${zone})
406 local hook_port=$(port_get_hook ${port})
407
408 assert isset hook_zone
409 assert isset hook_port
410
411 hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
412 }
413
414 function zone_port_up() {
415 zone_port_cmd up $@
416 }
417
418 function zone_port_down() {
419 zone_port_cmd down $@
420 }
421
422 function zone_get_ports() {
423 local zone=${1}
424
425 assert isset zone
426
427 local port
428 for port in $(zone_dir ${zone})/ports/*; do
429 port=$(basename ${port})
430
431 if port_exists ${port}; then
432 echo "${port}"
433 fi
434 done
435 }
436
437 function zone_has_port() {
438 # Check, if the given port is configured
439 # in this zone.
440
441 local zone=${1}
442 local port=${2}
443 shift 2
444
445 assert isset zone
446 assert isset port
447
448 [ -e "$(zone_dir ${zone})/ports/${port}" ]
449 }
450
451 # XXX overwritten some lines below
452 function zone_config() {
453 local zone=${1}
454 shift
455
456 if ! zone_exists ${zone}; then
457 error "Zone '${zone}' does not exist."
458 return ${EXIT_ERROR}
459 fi
460
461 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
462
463 if [ -z "${hook}" ]; then
464 error "Config file did not provide any hook."
465 return ${EXIT_ERROR}
466 fi
467
468 if ! hook_zone_exists ${hook}; then
469 error "Hook '${hook}' does not exist."
470 return ${EXIT_ERROR}
471 fi
472
473 hook_zone_exec ${hook} config ${zone} $@
474 }
475
476 function zone_config() {
477 local zone=${1}
478 local action=${2}
479 shift 2
480
481 assert isset zone
482 assert isset action
483 assert zone_exists ${zone}
484
485 # Aliases
486 case "${action}" in
487 del|delete|remove)
488 action="rem"
489 ;;
490 esac
491
492 case "${action}" in
493 create|edit|rem)
494 zone_config_${action} ${zone} $@
495 ;;
496 *)
497 error "Unrecognized argument: ${action}"
498 cli_usage root-zone-config-subcommands
499 exit ${EXIT_ERROR}
500 ;;
501 esac
502 }
503
504 function zone_config_option() {
505 local zone=${1}
506 local option=${2}
507 local default=${3}
508 shift 2
509
510 assert isset zone
511 assert isset option
512
513 (
514 VALUE="${default}"
515 zone_config_read ${zone}
516
517 VALUE="${!option}"
518 echo "${VALUE}"
519 )
520 }
521
522 function zone_config_create() {
523 local zone=${1}
524 shift
525
526 assert isset zone
527
528 local hook=$(zone_get_hook ${zone})
529
530 assert isset hook
531
532 hook_zone_exec ${hook} config_create ${zone} $@
533 }
534
535 function zone_show() {
536 local zone=${1}
537
538 echo "${zone}"
539 echo " Type: $(zone_get_hook ${zone})"
540 echo
541 }
542
543 function zones_show() {
544 local zone
545
546 for zone in $(zones_get $@); do
547 zone_show ${zone}
548 done
549 }
550
551 function zones_get_all() {
552 local zone
553 for zone in $(zone_dir)/*; do
554 zone=$(basename ${zone})
555 zone_exists ${zone} || continue
556
557 echo "${zone}"
558 done
559 }
560
561 function zones_get_local() {
562 local zone
563 for zone in $(zones_get_all); do
564 zone_is_local ${zone} && echo "${zone}"
565 done
566 }
567
568 function zones_get_nonlocal() {
569 local zone
570 for zone in $(zones_get_all); do
571 zone_is_nonlocal ${zone} && echo "${zone}"
572 done
573 }
574
575 function zones_get() {
576 local local=1
577 local remote=1
578
579 local zones
580
581 while [ $# -gt 0 ]; do
582 case "${1}" in
583 --local-only)
584 local=1
585 remote=0
586 ;;
587 --remote-only)
588 local=0
589 remote=1
590 ;;
591 --all)
592 local=1
593 remote=1
594 ;;
595 *)
596 if zone_name_is_valid ${1}; then
597 zones="${zones} ${1}"
598 else
599 warning "Unrecognized argument '${1}'"
600 fi
601 ;;
602 esac
603 shift
604 done
605
606 if [ -n "${zones}" ]; then
607 local zone
608 for zone in ${zones}; do
609 zone_exists ${zone} && echo "${zone}"
610 done
611 exit ${EXIT_OK}
612 fi
613
614 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
615 zones_get_all
616 elif [ ${local} -eq 1 ]; then
617 zones_get_local
618 elif [ ${remote} -eq 1 ]; then
619 zones_get_nonlocal
620 fi
621 }
622
623 function zone_ports_list() {
624 local zone=${1}
625
626 local port
627 for port in $(zone_dir ${zone})/ports/*; do
628 [ -e "${port}" ] || continue
629
630 echo $(basename ${port})
631 done
632 }
633
634 function zone_ports_cmd() {
635 local cmd=${1}
636 local zone=${2}
637 shift 2
638
639 assert isset cmd
640 assert isset zone
641
642 assert zone_exists ${zone}
643
644 local hook=$(zone_get_hook ${zone})
645
646 local port
647 for port in $(zone_get_ports ${zone}); do
648 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
649 done
650 }
651
652 function zone_ports_up() {
653 zone_ports_cmd port_up $@
654 }
655
656 function zone_ports_down() {
657 zone_ports_cmd port_down $@
658 }
659
660 function zone_ports_status() {
661 zone_ports_cmd port_status $@
662 }
663
664 function zone_configs_list() {
665 local zone=${1}
666
667 local config
668 for config in $(zone_dir ${zone})/configs/*; do
669 [ -e "${config}" ] || continue
670
671 basename ${config}
672 done
673 }
674
675 function zone_configs_cmd() {
676 local cmd=${1}
677 local zone=${2}
678 shift 2
679
680 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
681
682 local hook_config
683 local config
684 for config in $(zone_configs_list ${zone}); do
685 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
686
687 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
688 done
689 }
690
691 function zone_configs_up() {
692 zone_configs_cmd up $@
693 }
694
695 function zone_configs_down() {
696 zone_configs_cmd down $@
697 }
698
699 function zone_configs_status() {
700 zone_configs_cmd config_status $@
701 }
702
703 function zone_has_ip() {
704 device_has_ip $@
705 }
706
707 function zone_db() {
708 local zone=${1}
709 local action=${2}
710 shift 2
711
712 case "${action}" in
713 starting|started|stopping|stopped)
714 db_connection_update ${zone} ${action}
715 ;;
716 esac
717 }
718
719 function zone_is_up() {
720 local zone=${1}
721
722 device_is_up ${zone}
723 }
724
725 function zone_is_down() {
726 ! zone_is_up $@
727 }
728
729 function zone_get_supported_port_hooks() {
730 local zone=${1}
731
732 local hook=$(zone_get_hook ${zone})
733
734 hook_zone_ports_get_all ${hook}
735 }
736
737 function zone_get_supported_config_hooks() {
738 local zone=${1}
739
740 local hook=$(zone_get_hook ${zone})
741
742 hook_zone_configs_get_all ${hook}
743 }
744
745 function zone_file() {
746 local zone=${1}
747
748 assert isset zone
749
750 echo "$(zone_dir ${zone})/settings"
751 }
752
753 function zone_config_read() {
754 local zone=${1}
755
756 assert isset zone
757
758 # Save the HOOK variable.
759 local hook="${HOOK}"
760
761 config_read $(zone_file ${zone})
762
763 # Restore hook.
764 HOOK="${hook}"
765 }
766
767 function zone_config_write() {
768 local zone=${1}
769
770 assert isset zone
771
772 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
773 }
774
775 function zone_config_set() {
776 local zone=${1}
777 shift
778 local args="$@"
779
780 assert isset zone
781
782 (
783 zone_config_read ${zone}
784
785 for arg in ${args}; do
786 eval "${arg}"
787 done
788
789 zone_config_write ${zone}
790 )
791 }
792
793 function zone_config_get() {
794 local zone=${1}
795 local key=${2}
796
797 assert isset zone
798 assert isset key
799
800 (
801 zone_config_read ${zone}
802
803 echo "${!key}"
804 )
805 }