]> git.ipfire.org Git - people/stevee/network.git/blame - functions.zone
Don't use connection tracking for loopback traffic.
[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
1848564d
MT
94function zone_create() {
95 local zone=${1}
96 local hook=${2}
97 shift 2
98
99 if ! zone_name_is_valid ${zone}; then
100 error "Zone name '${zone}' is not valid."
101 return ${EXIT_ERROR}
102 fi
103
104 if zone_exists ${zone}; then
105 error "Zone '${zone}' does already exist."
106 return ${EXIT_ERROR}
107 fi
108
d61a01d4 109 if ! hook_zone_exists ${hook}; then
1848564d
MT
110 error "Hook '${hook}' does not exist."
111 return ${EXIT_ERROR}
112 fi
113
114 mkdir -p $(zone_dir ${zone})
115
a5ebb169
MT
116 # Create directories for configs and ports
117 mkdir -p $(zone_dir ${zone})/{configs,ports}
943e3f7e 118
d61a01d4 119 hook_zone_exec ${hook} create ${zone} $@
1848564d
MT
120 local ret=$?
121
122 # Maybe the zone create hook did not exit correctly.
123 # If this is the case we remove the created zone immediately.
124 if [ "${ret}" = "${EXIT_ERROR}" ]; then
69ace22b 125 zone_remove_now ${zone}
1848564d
MT
126 fi
127}
128
129function zone_edit() {
130 local zone=${1}
131 shift
132
133 if ! zone_exists ${zone}; then
134 error "Zone '${zone}' does not exist."
135 return ${EXIT_ERROR}
136 fi
137
69ace22b
MT
138 # Check if the zone is tagged for removal.
139 if zone_has_remove_tag ${zone}; then
140 error "You cannot edit a zone that is tagged for removal."
141 return ${EXIT_ERROR}
142 fi
143
1848564d
MT
144 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
145
146 if [ -z "${hook}" ]; then
147 error "Config file did not provide any hook."
148 return ${EXIT_ERROR}
149 fi
150
d61a01d4 151 if ! hook_zone_exists ${hook}; then
1848564d
MT
152 error "Hook '${hook}' does not exist."
153 return ${EXIT_ERROR}
154 fi
155
d61a01d4 156 hook_zone_exec ${hook} edit ${zone} $@
1848564d
MT
157}
158
69ace22b 159
1848564d
MT
160function zone_remove() {
161 local zone=${1}
69ace22b 162 assert zone_exists ${zone}
1848564d 163
69ace22b
MT
164 # Make the zone for removal.
165 touch $(zone_dir ${zone})/.remove
166
167 log INFO "Zone '${zone}' has been tagged for removal."
168}
169
170function zone_has_remove_tag() {
171 local zone=${1}
172 assert zone_exists ${zone}
173
174 [ -e "$(zone_dir ${zone})/.remove" ]
175}
176
177# This function will remove the given zone
178# RIGHT NOW. Use zone_remove to remove it
179# at the next status change.
180function zone_remove_now() {
181 local zone=${1}
182 assert zone_exists ${zone}
183
184 log INFO "Removing zone '${zone}' right now."
1848564d 185
69ace22b
MT
186 # Force the zone down.
187 zone_is_up ${zone} && zone_set_down ${zone}
1848564d
MT
188
189 rm -rf $(zone_dir ${zone})
190}
191
192function zone_up() {
193 local zone=${1}
194 shift
195
196 if ! zone_exists ${zone}; then
197 error "Zone '${zone}' does not exist."
198 return ${EXIT_ERROR}
199 fi
200
69ace22b
MT
201 # Check if a zone has got the remove tag.
202 if zone_has_remove_tag ${zone}; then
203 error "Cannot bring up any zone which is to be removed."
204 return ${EXIT_ERROR}
205 fi
206
1848564d
MT
207 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
208
209 if [ -z "${hook}" ]; then
210 error "Config file did not provide any hook."
211 return ${EXIT_ERROR}
212 fi
213
d61a01d4 214 if ! hook_zone_exists ${hook}; then
1848564d
MT
215 error "Hook '${hook}' does not exist."
216 return ${EXIT_ERROR}
217 fi
218
059469a8
MT
219 zone_db ${zone} starting
220
d61a01d4
MT
221 hook_zone_exec ${hook} up ${zone} $@
222
059469a8 223 zone_db ${zone} started
1848564d
MT
224}
225
226function zone_down() {
227 local zone=${1}
228 shift
229
230 if ! zone_exists ${zone}; then
231 error "Zone '${zone}' does not exist."
232 return ${EXIT_ERROR}
233 fi
234
235 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
236
237 if [ -z "${hook}" ]; then
238 error "Config file did not provide any hook."
239 return ${EXIT_ERROR}
240 fi
241
d61a01d4 242 if ! hook_zone_exists ${hook}; then
1848564d
MT
243 error "Hook '${hook}' does not exist."
244 return ${EXIT_ERROR}
245 fi
246
059469a8
MT
247 zone_db ${zone} stopping
248
d61a01d4 249 hook_zone_exec ${hook} down ${zone} $@
059469a8
MT
250
251 zone_db ${zone} stopped
69ace22b
MT
252
253 # Remove the zone, if it has got a remove tag.
254 if zone_has_remove_tag ${zone}; then
255 zone_remove_now ${zone}
256 fi
1848564d
MT
257}
258
259function zone_status() {
260 local zone=${1}
261 shift
262
263 if ! zone_exists ${zone}; then
264 error "Zone '${zone}' does not exist."
265 return ${EXIT_ERROR}
266 fi
267
268 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
269
270 if [ -z "${hook}" ]; then
271 error "Config file did not provide any hook."
272 return ${EXIT_ERROR}
273 fi
274
d61a01d4 275 if ! hook_zone_exists ${hook}; then
1848564d
MT
276 error "Hook '${hook}' does not exist."
277 return ${EXIT_ERROR}
278 fi
279
d61a01d4 280 hook_zone_exec ${hook} status ${zone} $@
69ace22b
MT
281
282 # Show that the zone it to be removed soon.
283 if zone_has_remove_tag ${zone}; then
284 warning "This zone is tagged for removal."
285 fi
1848564d
MT
286}
287
711ffac1
MT
288function zone_port() {
289 local zone=${1}
290 local action=${2}
291 shift 2
292
293 assert isset zone
294 assert isset action
295 assert zone_exists ${zone}
296
943e3f7e 297 # Aliases
711ffac1 298 case "${action}" in
943e3f7e
MT
299 del|delete|remove)
300 action="rem"
711ffac1
MT
301 ;;
302 esac
711ffac1 303
943e3f7e
MT
304 case "${action}" in
305 add|edit|rem)
306 zone_port_${action} ${zone} $@
307 ;;
308 *)
309 error "Unrecognized argument: ${action}"
310 cli_usage root-zone-port-subcommands
311 exit ${EXIT_ERROR}
312 ;;
313 esac
711ffac1
MT
314}
315
316function zone_port_add() {
317 local zone=${1}
318 shift
319
320 assert isset zone
321
322 local hook=$(zone_get_hook ${zone})
323
324 assert isset hook
325
326 hook_zone_exec ${hook} port_add ${zone} $@
327}
328
329function zone_port_edit() {
943e3f7e 330 zone_port_cmd edit $@
711ffac1
MT
331}
332
943e3f7e
MT
333function zone_port_rem() {
334 zone_port_cmd rem $@
711ffac1
MT
335}
336
337function zone_port_cmd() {
338 local cmd=${1}
339 local zone=${2}
340 local port=${3}
341 shift 3
342
343 assert isset zone
344 assert isset port
345
346 local hook_zone=$(zone_get_hook ${zone})
347 local hook_port=$(port_get_hook ${port})
348
349 assert isset hook_zone
350 assert isset hook_port
351
711ffac1
MT
352 hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
353}
354
711ffac1
MT
355function zone_port_up() {
356 zone_port_cmd up $@
357}
358
359function zone_port_down() {
360 zone_port_cmd down $@
361}
362
363function zone_get_ports() {
364 local zone=${1}
365
366 assert isset zone
367
368 local port
943e3f7e 369 for port in $(zone_dir ${zone})/ports/*; do
711ffac1 370 port=$(basename ${port})
711ffac1
MT
371
372 if port_exists ${port}; then
373 echo "${port}"
374 fi
375 done
376}
377
3a7fef62
MT
378function zone_has_port() {
379 # Check, if the given port is configured
380 # in this zone.
381
382 local zone=${1}
383 local port=${2}
384 shift 2
385
386 assert isset zone
387 assert isset port
388
389 [ -e "$(zone_dir ${zone})/ports/${port}" ]
390}
391
a5ebb169 392# XXX overwritten some lines below
1848564d
MT
393function zone_config() {
394 local zone=${1}
395 shift
396
397 if ! zone_exists ${zone}; then
398 error "Zone '${zone}' does not exist."
399 return ${EXIT_ERROR}
400 fi
401
402 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
403
404 if [ -z "${hook}" ]; then
405 error "Config file did not provide any hook."
406 return ${EXIT_ERROR}
407 fi
408
d61a01d4 409 if ! hook_zone_exists ${hook}; then
1848564d
MT
410 error "Hook '${hook}' does not exist."
411 return ${EXIT_ERROR}
412 fi
413
d61a01d4 414 hook_zone_exec ${hook} config ${zone} $@
1848564d
MT
415}
416
a5ebb169
MT
417function zone_config() {
418 local zone=${1}
419 local action=${2}
420 shift 2
421
422 assert isset zone
423 assert isset action
424 assert zone_exists ${zone}
425
426 # Aliases
427 case "${action}" in
428 del|delete|remove)
429 action="rem"
430 ;;
431 esac
432
433 case "${action}" in
434 create|edit|rem)
435 zone_config_${action} ${zone} $@
436 ;;
437 *)
438 error "Unrecognized argument: ${action}"
439 cli_usage root-zone-config-subcommands
440 exit ${EXIT_ERROR}
441 ;;
442 esac
443}
444
3a7fef62
MT
445function zone_config_option() {
446 local zone=${1}
447 local option=${2}
448 local default=${3}
449 shift 2
450
451 assert isset zone
452 assert isset option
453
454 (
455 VALUE="${default}"
456 zone_config_read ${zone}
457
458 VALUE="${!option}"
459 echo "${VALUE}"
460 )
461}
462
a5ebb169
MT
463function zone_config_create() {
464 local zone=${1}
465 shift
466
467 assert isset zone
468
469 local hook=$(zone_get_hook ${zone})
470
471 assert isset hook
472
473 hook_zone_exec ${hook} config_create ${zone} $@
474}
475
1848564d
MT
476function zone_show() {
477 local zone=${1}
478
479 echo "${zone}"
480 echo " Type: $(zone_get_hook ${zone})"
481 echo
482}
483
484function zones_show() {
485 local zone
486
487 for zone in $(zones_get $@); do
488 zone_show ${zone}
489 done
490}
491
492function zones_get_all() {
493 local zone
d61a01d4 494 for zone in $(zone_dir)/*; do
1848564d
MT
495 zone=$(basename ${zone})
496 zone_exists ${zone} || continue
497
498 echo "${zone}"
03170817 499 done
1848564d
MT
500}
501
502function zones_get_local() {
503 local zone
504 for zone in $(zones_get_all); do
505 zone_is_local ${zone} && echo "${zone}"
506 done
507}
508
509function zones_get_nonlocal() {
510 local zone
511 for zone in $(zones_get_all); do
5e42d659 512 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
513 done
514}
515
516function zones_get() {
517 local local=1
518 local remote=1
519
520 local zones
521
522 while [ $# -gt 0 ]; do
523 case "${1}" in
524 --local-only)
525 local=1
526 remote=0
527 ;;
528 --remote-only)
529 local=0
530 remote=1
531 ;;
532 --all)
533 local=1
534 remote=1
535 ;;
536 *)
537 if zone_name_is_valid ${1}; then
538 zones="${zones} ${1}"
539 else
540 warning "Unrecognized argument '${1}'"
541 fi
542 ;;
543 esac
544 shift
545 done
546
547 if [ -n "${zones}" ]; then
548 local zone
549 for zone in ${zones}; do
550 zone_exists ${zone} && echo "${zone}"
551 done
552 exit ${EXIT_OK}
553 fi
554
555 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
556 zones_get_all
557 elif [ ${local} -eq 1 ]; then
558 zones_get_local
559 elif [ ${remote} -eq 1 ]; then
560 zones_get_nonlocal
561 fi
562}
563
564function zone_ports_list() {
565 local zone=${1}
566
567 local port
a5ebb169 568 for port in $(zone_dir ${zone})/ports/*; do
1848564d
MT
569 [ -e "${port}" ] || continue
570
571 echo $(basename ${port})
03170817 572 done
1848564d
MT
573}
574
575function zone_ports_cmd() {
576 local cmd=${1}
577 local zone=${2}
578 shift 2
579
711ffac1
MT
580 assert isset cmd
581 assert isset zone
1848564d 582
711ffac1 583 assert zone_exists ${zone}
1848564d 584
711ffac1
MT
585 local hook=$(zone_get_hook ${zone})
586
587 local port
588 for port in $(zone_get_ports ${zone}); do
711ffac1 589 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
1848564d
MT
590 done
591}
592
593function zone_ports_up() {
711ffac1 594 zone_ports_cmd port_up $@
1848564d
MT
595}
596
597function zone_ports_down() {
711ffac1
MT
598 zone_ports_cmd port_down $@
599}
600
601function zone_ports_status() {
602 zone_ports_cmd port_status $@
1848564d
MT
603}
604
605function zone_configs_list() {
606 local zone=${1}
607
608 local config
a5ebb169 609 for config in $(zone_dir ${zone})/configs/*; do
1848564d
MT
610 [ -e "${config}" ] || continue
611
f41fa3d7 612 basename ${config}
03170817 613 done
1848564d
MT
614}
615
616function zone_configs_cmd() {
617 local cmd=${1}
618 local zone=${2}
619 shift 2
620
621 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
622
623 local hook_config
624 local config
625 for config in $(zone_configs_list ${zone}); do
a5ebb169 626 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
1848564d 627
d61a01d4 628 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
1848564d
MT
629 done
630}
631
632function zone_configs_up() {
633 zone_configs_cmd up $@
634}
635
636function zone_configs_down() {
637 zone_configs_cmd down $@
638}
639
a5ebb169
MT
640function zone_configs_status() {
641 zone_configs_cmd config_status $@
642}
643
38f61548
MT
644function zone_has_ip() {
645 device_has_ip $@
4231f419
MT
646}
647
059469a8
MT
648function zone_db() {
649 local zone=${1}
650 local action=${2}
651 shift 2
652
653 case "${action}" in
654 starting|started|stopping|stopped)
655 db_connection_update ${zone} ${action}
656 ;;
657 esac
658}
5e42d659
MT
659
660function zone_is_up() {
661 local zone=${1}
662
663 device_is_up ${zone}
664}
665
666function zone_is_down() {
667 ! zone_is_up $@
668}
711ffac1 669
a5ebb169 670function zone_get_supported_port_hooks() {
711ffac1
MT
671 local zone=${1}
672
673 local hook=$(zone_get_hook ${zone})
674
675 hook_zone_ports_get_all ${hook}
676}
677
a5ebb169
MT
678function zone_get_supported_config_hooks() {
679 local zone=${1}
680
681 local hook=$(zone_get_hook ${zone})
682
683 hook_zone_configs_get_all ${hook}
684}
685
711ffac1
MT
686function zone_file() {
687 local zone=${1}
688
689 assert isset zone
690
691 echo "$(zone_dir ${zone})/settings"
692}
693
694function zone_config_read() {
695 local zone=${1}
696
697 assert isset zone
698
bfd6c282
MT
699 # Save the HOOK variable.
700 local hook="${HOOK}"
701
711ffac1 702 config_read $(zone_file ${zone})
bfd6c282
MT
703
704 # Restore hook.
705 HOOK="${hook}"
711ffac1
MT
706}
707
708function zone_config_write() {
709 local zone=${1}
710
711 assert isset zone
712
713 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
714}
715
716function zone_config_set() {
717 local zone=${1}
718 shift
719 local args="$@"
720
721 assert isset zone
722
723 (
724 zone_config_read ${zone}
725
726 for arg in ${args}; do
727 eval "${arg}"
728 done
729
730 zone_config_write ${zone}
731 )
732}
6b3f9c85
MT
733
734function zone_config_get() {
735 local zone=${1}
736 local key=${2}
737
738 assert isset zone
739 assert isset key
740
741 (
742 zone_config_read ${zone}
743
744 echo "${!key}"
745 )
746}