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