]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.zone
Fix weird device CLI command.
[people/arne_f/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_is_nonlocal ${zone}
61 }
62
63 function zone_is_nonlocal() {
64 local zone=${1}
65
66 assert isset zone
67
68 [[ ${zone} =~ ^red[0-9]{1,5} ]]
69 }
70
71 function zone_get_hook() {
72 local zone=${1}
73
74 assert isset zone
75
76 config_get_hook $(zone_dir ${zone})/settings
77 }
78
79 function 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
94 if ! hook_zone_exists ${hook}; then
95 error "Hook '${hook}' does not exist."
96 return ${EXIT_ERROR}
97 fi
98
99 mkdir -p $(zone_dir ${zone})
100
101 # Create directories for configs and ports
102 mkdir -p $(zone_dir ${zone})/{configs,ports}
103
104 hook_zone_exec ${hook} create ${zone} $@
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
114 function 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
130 if ! hook_zone_exists ${hook}; then
131 error "Hook '${hook}' does not exist."
132 return ${EXIT_ERROR}
133 fi
134
135 hook_zone_exec ${hook} edit ${zone} $@
136 }
137
138 function 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
152 function 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
168 if ! hook_zone_exists ${hook}; then
169 error "Hook '${hook}' does not exist."
170 return ${EXIT_ERROR}
171 fi
172
173 zone_db ${zone} starting
174
175 hook_zone_exec ${hook} up ${zone} $@
176
177 zone_db ${zone} started
178 }
179
180 function 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
196 if ! hook_zone_exists ${hook}; then
197 error "Hook '${hook}' does not exist."
198 return ${EXIT_ERROR}
199 fi
200
201 zone_db ${zone} stopping
202
203 hook_zone_exec ${hook} down ${zone} $@
204
205 zone_db ${zone} stopped
206 }
207
208 function 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
224 if ! hook_zone_exists ${hook}; then
225 error "Hook '${hook}' does not exist."
226 return ${EXIT_ERROR}
227 fi
228
229 hook_zone_exec ${hook} status ${zone} $@
230 }
231
232 function 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
241 # Aliases
242 case "${action}" in
243 del|delete|remove)
244 action="rem"
245 ;;
246 esac
247
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
258 }
259
260 function 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
273 function zone_port_edit() {
274 zone_port_cmd edit $@
275 }
276
277 function zone_port_rem() {
278 zone_port_cmd rem $@
279 }
280
281 function 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
301 function zone_port_up() {
302 zone_port_cmd up $@
303 }
304
305 function zone_port_down() {
306 zone_port_cmd down $@
307 }
308
309 function zone_get_ports() {
310 local zone=${1}
311
312 assert isset zone
313
314 local port
315 for port in $(zone_dir ${zone})/ports/*; do
316 port=$(basename ${port})
317
318 if port_exists ${port}; then
319 echo "${port}"
320 fi
321 done
322 }
323
324 # XXX overwritten some lines below
325 function zone_config() {
326 local zone=${1}
327 shift
328
329 if ! zone_exists ${zone}; then
330 error "Zone '${zone}' does not exist."
331 return ${EXIT_ERROR}
332 fi
333
334 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
335
336 if [ -z "${hook}" ]; then
337 error "Config file did not provide any hook."
338 return ${EXIT_ERROR}
339 fi
340
341 if ! hook_zone_exists ${hook}; then
342 error "Hook '${hook}' does not exist."
343 return ${EXIT_ERROR}
344 fi
345
346 hook_zone_exec ${hook} config ${zone} $@
347 }
348
349 function zone_config() {
350 local zone=${1}
351 local action=${2}
352 shift 2
353
354 assert isset zone
355 assert isset action
356 assert zone_exists ${zone}
357
358 # Aliases
359 case "${action}" in
360 del|delete|remove)
361 action="rem"
362 ;;
363 esac
364
365 case "${action}" in
366 create|edit|rem)
367 zone_config_${action} ${zone} $@
368 ;;
369 *)
370 error "Unrecognized argument: ${action}"
371 cli_usage root-zone-config-subcommands
372 exit ${EXIT_ERROR}
373 ;;
374 esac
375 }
376
377 function zone_config_create() {
378 local zone=${1}
379 shift
380
381 assert isset zone
382
383 local hook=$(zone_get_hook ${zone})
384
385 assert isset hook
386
387 hook_zone_exec ${hook} config_create ${zone} $@
388 }
389
390 function zone_show() {
391 local zone=${1}
392
393 echo "${zone}"
394 echo " Type: $(zone_get_hook ${zone})"
395 echo
396 }
397
398 function zones_show() {
399 local zone
400
401 for zone in $(zones_get $@); do
402 zone_show ${zone}
403 done
404 }
405
406 function zones_get_all() {
407 local zone
408 for zone in $(zone_dir)/*; do
409 zone=$(basename ${zone})
410 zone_exists ${zone} || continue
411
412 echo "${zone}"
413 done
414 }
415
416 function zones_get_local() {
417 local zone
418 for zone in $(zones_get_all); do
419 zone_is_local ${zone} && echo "${zone}"
420 done
421 }
422
423 function zones_get_nonlocal() {
424 local zone
425 for zone in $(zones_get_all); do
426 zone_is_nonlocal ${zone} && echo "${zone}"
427 done
428 }
429
430 function zones_get() {
431 local local=1
432 local remote=1
433
434 local zones
435
436 while [ $# -gt 0 ]; do
437 case "${1}" in
438 --local-only)
439 local=1
440 remote=0
441 ;;
442 --remote-only)
443 local=0
444 remote=1
445 ;;
446 --all)
447 local=1
448 remote=1
449 ;;
450 *)
451 if zone_name_is_valid ${1}; then
452 zones="${zones} ${1}"
453 else
454 warning "Unrecognized argument '${1}'"
455 fi
456 ;;
457 esac
458 shift
459 done
460
461 if [ -n "${zones}" ]; then
462 local zone
463 for zone in ${zones}; do
464 zone_exists ${zone} && echo "${zone}"
465 done
466 exit ${EXIT_OK}
467 fi
468
469 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
470 zones_get_all
471 elif [ ${local} -eq 1 ]; then
472 zones_get_local
473 elif [ ${remote} -eq 1 ]; then
474 zones_get_nonlocal
475 fi
476 }
477
478 function zone_ports_list() {
479 local zone=${1}
480
481 local port
482 for port in $(zone_dir ${zone})/ports/*; do
483 [ -e "${port}" ] || continue
484
485 echo $(basename ${port})
486 done
487 }
488
489 function zone_ports_cmd() {
490 local cmd=${1}
491 local zone=${2}
492 shift 2
493
494 assert isset cmd
495 assert isset zone
496
497 assert zone_exists ${zone}
498
499 local hook=$(zone_get_hook ${zone})
500
501 local port
502 for port in $(zone_get_ports ${zone}); do
503 #zone_port_cmd ${cmd} ${zone} ${port} $@
504 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
505 done
506 }
507
508 function zone_ports_up() {
509 zone_ports_cmd port_up $@
510 }
511
512 function zone_ports_down() {
513 zone_ports_cmd port_down $@
514 }
515
516 function zone_ports_status() {
517 zone_ports_cmd port_status $@
518 }
519
520 function zone_configs_list() {
521 local zone=${1}
522
523 local config
524 for config in $(zone_dir ${zone})/configs/*; do
525 [ -e "${config}" ] || continue
526
527 echo $(basename ${config})
528 done
529 }
530
531 function zone_configs_cmd() {
532 local cmd=${1}
533 local zone=${2}
534 shift 2
535
536 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
537
538 local hook_config
539 local config
540 for config in $(zone_configs_list ${zone}); do
541 hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})
542
543 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
544 done
545 }
546
547 function zone_configs_up() {
548 zone_configs_cmd up $@
549 }
550
551 function zone_configs_down() {
552 zone_configs_cmd down $@
553 }
554
555 function zone_configs_status() {
556 zone_configs_cmd config_status $@
557 }
558
559 function zone_has_ip() {
560 device_has_ip $@
561 }
562
563 function zone_db() {
564 local zone=${1}
565 local action=${2}
566 shift 2
567
568 case "${action}" in
569 starting|started|stopping|stopped)
570 db_connection_update ${zone} ${action}
571 ;;
572 esac
573 }
574
575 function zone_is_up() {
576 local zone=${1}
577
578 device_is_up ${zone}
579 }
580
581 function zone_is_down() {
582 ! zone_is_up $@
583 }
584
585 function zone_get_supported_port_hooks() {
586 local zone=${1}
587
588 local hook=$(zone_get_hook ${zone})
589
590 hook_zone_ports_get_all ${hook}
591 }
592
593 function zone_get_supported_config_hooks() {
594 local zone=${1}
595
596 local hook=$(zone_get_hook ${zone})
597
598 hook_zone_configs_get_all ${hook}
599 }
600
601 function zone_file() {
602 local zone=${1}
603
604 assert isset zone
605
606 echo "$(zone_dir ${zone})/settings"
607 }
608
609 function zone_config_read() {
610 local zone=${1}
611
612 assert isset zone
613
614 config_read $(zone_file ${zone})
615 }
616
617 function zone_config_write() {
618 local zone=${1}
619
620 assert isset zone
621
622 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
623 }
624
625 function zone_config_set() {
626 local zone=${1}
627 shift
628 local args="$@"
629
630 assert isset zone
631
632 (
633 zone_config_read ${zone}
634
635 for arg in ${args}; do
636 eval "${arg}"
637 done
638
639 zone_config_write ${zone}
640 )
641 }
642
643 function zone_config_get() {
644 local zone=${1}
645 local key=${2}
646
647 assert isset zone
648 assert isset key
649
650 (
651 zone_config_read ${zone}
652
653 echo "${!key}"
654 )
655 }