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