]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.zone
network: Initialize bonding at start.
[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 directory for ports
102 mkdir -p $(zone_dir ${zone})/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 function zone_config() {
325 local zone=${1}
326 shift
327
328 if ! zone_exists ${zone}; then
329 error "Zone '${zone}' does not exist."
330 return ${EXIT_ERROR}
331 fi
332
333 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
334
335 if [ -z "${hook}" ]; then
336 error "Config file did not provide any hook."
337 return ${EXIT_ERROR}
338 fi
339
340 if ! hook_zone_exists ${hook}; then
341 error "Hook '${hook}' does not exist."
342 return ${EXIT_ERROR}
343 fi
344
345 hook_zone_exec ${hook} config ${zone} $@
346 }
347
348 function zone_show() {
349 local zone=${1}
350
351 echo "${zone}"
352 echo " Type: $(zone_get_hook ${zone})"
353 echo
354 }
355
356 function zones_show() {
357 local zone
358
359 for zone in $(zones_get $@); do
360 zone_show ${zone}
361 done
362 }
363
364 function zones_get_all() {
365 local zone
366 for zone in $(zone_dir)/*; do
367 zone=$(basename ${zone})
368 zone_exists ${zone} || continue
369
370 echo "${zone}"
371 done | sort
372 }
373
374 function zones_get_local() {
375 local zone
376 for zone in $(zones_get_all); do
377 zone_is_local ${zone} && echo "${zone}"
378 done
379 }
380
381 function zones_get_nonlocal() {
382 local zone
383 for zone in $(zones_get_all); do
384 zone_is_nonlocal ${zone} && echo "${zone}"
385 done
386 }
387
388 function zones_get() {
389 local local=1
390 local remote=1
391
392 local zones
393
394 while [ $# -gt 0 ]; do
395 case "${1}" in
396 --local-only)
397 local=1
398 remote=0
399 ;;
400 --remote-only)
401 local=0
402 remote=1
403 ;;
404 --all)
405 local=1
406 remote=1
407 ;;
408 *)
409 if zone_name_is_valid ${1}; then
410 zones="${zones} ${1}"
411 else
412 warning "Unrecognized argument '${1}'"
413 fi
414 ;;
415 esac
416 shift
417 done
418
419 if [ -n "${zones}" ]; then
420 local zone
421 for zone in ${zones}; do
422 zone_exists ${zone} && echo "${zone}"
423 done
424 exit ${EXIT_OK}
425 fi
426
427 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
428 zones_get_all
429 elif [ ${local} -eq 1 ]; then
430 zones_get_local
431 elif [ ${remote} -eq 1 ]; then
432 zones_get_nonlocal
433 fi
434 }
435
436 function zone_ports_list() {
437 local zone=${1}
438
439 local port
440 for port in $(zone_dir ${zone})/port.*; do
441 [ -e "${port}" ] || continue
442
443 echo $(basename ${port})
444 done | sort
445 }
446
447 function zone_ports_cmd() {
448 local cmd=${1}
449 local zone=${2}
450 shift 2
451
452 assert isset cmd
453 assert isset zone
454
455 assert zone_exists ${zone}
456
457 local hook=$(zone_get_hook ${zone})
458
459 local port
460 for port in $(zone_get_ports ${zone}); do
461 #zone_port_cmd ${cmd} ${zone} ${port} $@
462 hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
463 done
464 }
465
466 function zone_ports_up() {
467 zone_ports_cmd port_up $@
468 }
469
470 function zone_ports_down() {
471 zone_ports_cmd port_down $@
472 }
473
474 function zone_ports_status() {
475 zone_ports_cmd port_status $@
476 }
477
478 function zone_configs_list() {
479 local zone=${1}
480
481 local config
482 for config in $(zone_dir ${zone})/config.*; do
483 [ -e "${config}" ] || continue
484
485 echo $(basename ${config})
486 done | sort
487 }
488
489 function zone_configs_cmd() {
490 local cmd=${1}
491 local zone=${2}
492 shift 2
493
494 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
495
496 local hook_config
497 local config
498 for config in $(zone_configs_list ${zone}); do
499 hook_config=$(config_get_hook $(zone_dir ${zone})/${config})
500
501 hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
502 done
503 }
504
505 function zone_configs_up() {
506 zone_configs_cmd up $@
507 }
508
509 function zone_configs_down() {
510 zone_configs_cmd down $@
511 }
512
513 function zone_has_ipv4() {
514 device_has_ipv4 $@
515 }
516
517 function zone_has_ipv6() {
518 device_has_ipv6 $@
519 }
520
521 function zone_db() {
522 local zone=${1}
523 local action=${2}
524 shift 2
525
526 case "${action}" in
527 starting|started|stopping|stopped)
528 db_connection_update ${zone} ${action}
529 ;;
530 esac
531 }
532
533 function zone_is_up() {
534 local zone=${1}
535
536 device_is_up ${zone}
537 }
538
539 function zone_is_down() {
540 ! zone_is_up $@
541 }
542
543 function zone_get_supported_hooks() {
544 local zone=${1}
545
546 local hook=$(zone_get_hook ${zone})
547
548 hook_zone_ports_get_all ${hook}
549 }
550
551 function zone_file() {
552 local zone=${1}
553
554 assert isset zone
555
556 echo "$(zone_dir ${zone})/settings"
557 }
558
559 function zone_config_read() {
560 local zone=${1}
561
562 assert isset zone
563
564 config_read $(zone_file ${zone})
565 }
566
567 function zone_config_write() {
568 local zone=${1}
569
570 assert isset zone
571
572 config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
573 }
574
575 function zone_config_set() {
576 local zone=${1}
577 shift
578 local args="$@"
579
580 assert isset zone
581
582 (
583 zone_config_read ${zone}
584
585 for arg in ${args}; do
586 eval "${arg}"
587 done
588
589 zone_config_write ${zone}
590 )
591 }