]> git.ipfire.org Git - people/stevee/network.git/blame - functions.zone
wireless: Add function to find DFS channels.
[people/stevee/network.git] / functions.zone
CommitLineData
1848564d
MT
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
22function zone_dir() {
23 local zone=${1}
24
d2a21d01 25 echo "${NETWORK_ZONE_DIR}/zones/${zone}"
1848564d
MT
26}
27
28function zone_exists() {
29 local zone=${1}
711ffac1
MT
30 assert isset zone
31
1848564d
MT
32 [ -d "$(zone_dir ${zone})" ]
33}
34
35function 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
46function zone_name_is_valid() {
47 local zone=${1}
4fedddef
MT
48
49 # Don't accept empty strings.
50 [ -z "${zone}" ] && return ${EXIT_FALSE}
711ffac1 51
1848564d
MT
52 [[ ${zone} =~ $(zone_match) ]]
53}
54
55function zone_is_local() {
56 local zone=${1}
57
7de0637a 58 [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
5e42d659
MT
59}
60
61function zone_is_nonlocal() {
62 local zone=${1}
63
7de0637a 64 [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
1848564d
MT
65}
66
67function zone_get_hook() {
68 local zone=${1}
711ffac1
MT
69 assert isset zone
70
1848564d
MT
71 config_get_hook $(zone_dir ${zone})/settings
72}
73
5bb2429a
MT
74function 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
1d08b9b3 81 service_start "network@${zone}.service"
5bb2429a
MT
82}
83
84function 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
1d08b9b3 91 service_stop "network@${zone}.service"
5bb2429a
MT
92}
93
5c5b8e36
SS
94function 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
114function 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
134function 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
1848564d
MT
146function 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
d61a01d4 161 if ! hook_zone_exists ${hook}; then
1848564d
MT
162 error "Hook '${hook}' does not exist."
163 return ${EXIT_ERROR}
164 fi
165
166 mkdir -p $(zone_dir ${zone})
167
a5ebb169
MT
168 # Create directories for configs and ports
169 mkdir -p $(zone_dir ${zone})/{configs,ports}
943e3f7e 170
d61a01d4 171 hook_zone_exec ${hook} create ${zone} $@
1848564d
MT
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
69ace22b 177 zone_remove_now ${zone}
5c5b8e36 178 return ${EXIT_ERROR}
1848564d 179 fi
5c5b8e36
SS
180
181 # Automatically enable zone.
182 zone_enable "${zone}"
1848564d
MT
183}
184
185function 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
69ace22b
MT
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
1848564d
MT
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
d61a01d4 207 if ! hook_zone_exists ${hook}; then
1848564d
MT
208 error "Hook '${hook}' does not exist."
209 return ${EXIT_ERROR}
210 fi
211
d61a01d4 212 hook_zone_exec ${hook} edit ${zone} $@
1848564d
MT
213}
214
69ace22b 215
1848564d
MT
216function zone_remove() {
217 local zone=${1}
69ace22b 218 assert zone_exists ${zone}
1848564d 219
69ace22b
MT
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
226function 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.
236function zone_remove_now() {
237 local zone=${1}
238 assert zone_exists ${zone}
239
240 log INFO "Removing zone '${zone}' right now."
1848564d 241
69ace22b
MT
242 # Force the zone down.
243 zone_is_up ${zone} && zone_set_down ${zone}
1848564d 244
5c5b8e36
SS
245 # Disable zone.
246 zone_disable "${zone}"
247
1848564d
MT
248 rm -rf $(zone_dir ${zone})
249}
250
251function 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
69ace22b
MT
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
1848564d
MT
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
d61a01d4 273 if ! hook_zone_exists ${hook}; then
1848564d
MT
274 error "Hook '${hook}' does not exist."
275 return ${EXIT_ERROR}
276 fi
277
059469a8
MT
278 zone_db ${zone} starting
279
d61a01d4
MT
280 hook_zone_exec ${hook} up ${zone} $@
281
059469a8 282 zone_db ${zone} started
1848564d
MT
283}
284
285function 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
d61a01d4 301 if ! hook_zone_exists ${hook}; then
1848564d
MT
302 error "Hook '${hook}' does not exist."
303 return ${EXIT_ERROR}
304 fi
305
059469a8
MT
306 zone_db ${zone} stopping
307
d61a01d4 308 hook_zone_exec ${hook} down ${zone} $@
059469a8
MT
309
310 zone_db ${zone} stopped
69ace22b
MT
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
1848564d
MT
316}
317
318function 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
d61a01d4 334 if ! hook_zone_exists ${hook}; then
1848564d
MT
335 error "Hook '${hook}' does not exist."
336 return ${EXIT_ERROR}
337 fi
338
d61a01d4 339 hook_zone_exec ${hook} status ${zone} $@
69ace22b
MT
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
1848564d
MT
345}
346
711ffac1
MT
347function 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
943e3f7e 356 # Aliases
711ffac1 357 case "${action}" in
943e3f7e
MT
358 del|delete|remove)
359 action="rem"
711ffac1
MT
360 ;;
361 esac
711ffac1 362
943e3f7e
MT
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
711ffac1
MT
373}
374
375function 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
388function zone_port_edit() {
943e3f7e 389 zone_port_cmd edit $@
711ffac1
MT
390}
391
943e3f7e
MT
392function zone_port_rem() {
393 zone_port_cmd rem $@
711ffac1
MT
394}
395
396function 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
711ffac1
MT
411 hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
412}
413
711ffac1
MT
414function zone_port_up() {
415 zone_port_cmd up $@
416}
417
418function zone_port_down() {
419 zone_port_cmd down $@
420}
421
422function zone_get_ports() {
423 local zone=${1}
424
425 assert isset zone
426
427 local port
943e3f7e 428 for port in $(zone_dir ${zone})/ports/*; do
711ffac1 429 port=$(basename ${port})
711ffac1
MT
430
431 if port_exists ${port}; then
432 echo "${port}"
433 fi
434 done
435}
436
3a7fef62
MT
437function 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
a5ebb169 451# XXX overwritten some lines below
1848564d
MT
452function 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
d61a01d4 468 if ! hook_zone_exists ${hook}; then
1848564d
MT
469 error "Hook '${hook}' does not exist."
470 return ${EXIT_ERROR}
471 fi
472
d61a01d4 473 hook_zone_exec ${hook} config ${zone} $@
1848564d
MT
474}
475
a5ebb169
MT
476function 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
3a7fef62
MT
504function 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
a5ebb169
MT
522function 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
1848564d
MT
535function zone_show() {
536 local zone=${1}
537
538 echo "${zone}"
539 echo " Type: $(zone_get_hook ${zone})"
540 echo
541}
542
543function zones_show() {
544 local zone
545
546 for zone in $(zones_get $@); do
547 zone_show ${zone}
548 done
549}
550
551function zones_get_all() {
552 local zone
d61a01d4 553 for zone in $(zone_dir)/*; do
1848564d
MT
554 zone=$(basename ${zone})
555 zone_exists ${zone} || continue
556
557 echo "${zone}"
03170817 558 done
1848564d
MT
559}
560
561function 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
568function zones_get_nonlocal() {
569 local zone
570 for zone in $(zones_get_all); do
5e42d659 571 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
572 done
573}
574
575function 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
623function zone_ports_list() {
624 local zone=${1}
625
626 local port
a5ebb169 627 for port in $(zone_dir ${zone})/ports/*; do
1848564d
MT
628 [ -e "${port}" ] || continue
629
630 echo $(basename ${port})
03170817 631 done
1848564d
MT
632}
633
634function zone_ports_cmd() {
635 local cmd=${1}
636 local zone=${2}
637 shift 2
638
711ffac1
MT
639 assert isset cmd
640 assert isset zone
1848564d 641
711ffac1 642 assert zone_exists ${zone}
1848564d 643
711ffac1
MT
644 local hook=$(zone_get_hook ${zone})
645
646 local port
647 for port in $(zone_get_ports ${zone}); do
711ffac1 648 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
1848564d
MT
649 done
650}
651
652function zone_ports_up() {
711ffac1 653 zone_ports_cmd port_up $@
1848564d
MT
654}
655
656function zone_ports_down() {
711ffac1
MT
657 zone_ports_cmd port_down $@
658}
659
660function zone_ports_status() {
661 zone_ports_cmd port_status $@
1848564d
MT
662}
663
664function zone_configs_list() {
665 local zone=${1}
666
667 local config
a5ebb169 668 for config in $(zone_dir ${zone})/configs/*; do
1848564d
MT
669 [ -e "${config}" ] || continue
670
f41fa3d7 671 basename ${config}
03170817 672 done
1848564d
MT
673}
674
675function 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
a5ebb169 685 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
1848564d 686
d61a01d4 687 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
1848564d
MT
688 done
689}
690
691function zone_configs_up() {
692 zone_configs_cmd up $@
693}
694
695function zone_configs_down() {
696 zone_configs_cmd down $@
697}
698
a5ebb169
MT
699function zone_configs_status() {
700 zone_configs_cmd config_status $@
701}
702
38f61548
MT
703function zone_has_ip() {
704 device_has_ip $@
4231f419
MT
705}
706
059469a8
MT
707function 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}
5e42d659
MT
718
719function zone_is_up() {
720 local zone=${1}
721
722 device_is_up ${zone}
723}
724
725function zone_is_down() {
726 ! zone_is_up $@
727}
711ffac1 728
a5ebb169 729function zone_get_supported_port_hooks() {
711ffac1
MT
730 local zone=${1}
731
732 local hook=$(zone_get_hook ${zone})
733
734 hook_zone_ports_get_all ${hook}
735}
736
a5ebb169
MT
737function 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
711ffac1
MT
745function zone_file() {
746 local zone=${1}
747
748 assert isset zone
749
750 echo "$(zone_dir ${zone})/settings"
751}
752
753function zone_config_read() {
754 local zone=${1}
755
756 assert isset zone
757
bfd6c282
MT
758 # Save the HOOK variable.
759 local hook="${HOOK}"
760
711ffac1 761 config_read $(zone_file ${zone})
bfd6c282
MT
762
763 # Restore hook.
764 HOOK="${hook}"
711ffac1
MT
765}
766
767function zone_config_write() {
768 local zone=${1}
769
770 assert isset zone
771
772 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
773}
774
775function 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}
6b3f9c85
MT
792
793function 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}