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