]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | function 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 | ||
30 | function zone_exists() { | |
31 | local zone=${1} | |
32 | ||
711ffac1 MT |
33 | assert isset zone |
34 | ||
1848564d MT |
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 | ||
711ffac1 MT |
52 | assert isset zone |
53 | ||
1848564d MT |
54 | [[ ${zone} =~ $(zone_match) ]] |
55 | } | |
56 | ||
57 | function zone_is_local() { | |
58 | local zone=${1} | |
59 | ||
5e42d659 MT |
60 | ! zone_is_nonlocal ${zone} |
61 | } | |
62 | ||
63 | function 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 | ||
71 | function 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 | ||
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 | ||
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 | ||
d61a01d4 | 101 | hook_zone_exec ${hook} create ${zone} $@ |
1848564d MT |
102 | local ret=$? |
103 | ||
104 | # Maybe the zone create hook did not exit correctly. | |
105 | # If this is the case we remove the created zone immediately. | |
106 | if [ "${ret}" = "${EXIT_ERROR}" ]; then | |
107 | zone_remove ${zone} | |
108 | fi | |
109 | } | |
110 | ||
111 | function zone_edit() { | |
112 | local zone=${1} | |
113 | shift | |
114 | ||
115 | if ! zone_exists ${zone}; then | |
116 | error "Zone '${zone}' does not exist." | |
117 | return ${EXIT_ERROR} | |
118 | fi | |
119 | ||
120 | local hook=$(config_get_hook $(zone_dir ${zone})/settings) | |
121 | ||
122 | if [ -z "${hook}" ]; then | |
123 | error "Config file did not provide any hook." | |
124 | return ${EXIT_ERROR} | |
125 | fi | |
126 | ||
d61a01d4 | 127 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
128 | error "Hook '${hook}' does not exist." |
129 | return ${EXIT_ERROR} | |
130 | fi | |
131 | ||
d61a01d4 | 132 | hook_zone_exec ${hook} edit ${zone} $@ |
1848564d MT |
133 | } |
134 | ||
135 | function zone_remove() { | |
136 | local zone=${1} | |
137 | shift | |
138 | ||
139 | if ! zone_exists ${zone}; then | |
140 | error "Zone '${zone}' does not exist." | |
141 | return ${EXIT_ERROR} | |
142 | fi | |
143 | ||
144 | # XXX Tear this down here? | |
145 | ||
146 | rm -rf $(zone_dir ${zone}) | |
147 | } | |
148 | ||
149 | function zone_up() { | |
150 | local zone=${1} | |
151 | shift | |
152 | ||
153 | if ! zone_exists ${zone}; then | |
154 | error "Zone '${zone}' does not exist." | |
155 | return ${EXIT_ERROR} | |
156 | fi | |
157 | ||
158 | local hook=$(config_get_hook $(zone_dir ${zone})/settings) | |
159 | ||
160 | if [ -z "${hook}" ]; then | |
161 | error "Config file did not provide any hook." | |
162 | return ${EXIT_ERROR} | |
163 | fi | |
164 | ||
d61a01d4 | 165 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
166 | error "Hook '${hook}' does not exist." |
167 | return ${EXIT_ERROR} | |
168 | fi | |
169 | ||
059469a8 MT |
170 | zone_db ${zone} starting |
171 | ||
d61a01d4 MT |
172 | hook_zone_exec ${hook} up ${zone} $@ |
173 | ||
059469a8 | 174 | zone_db ${zone} started |
1848564d MT |
175 | } |
176 | ||
177 | function zone_down() { | |
178 | local zone=${1} | |
179 | shift | |
180 | ||
181 | if ! zone_exists ${zone}; then | |
182 | error "Zone '${zone}' does not exist." | |
183 | return ${EXIT_ERROR} | |
184 | fi | |
185 | ||
186 | local hook=$(config_get_hook $(zone_dir ${zone})/settings) | |
187 | ||
188 | if [ -z "${hook}" ]; then | |
189 | error "Config file did not provide any hook." | |
190 | return ${EXIT_ERROR} | |
191 | fi | |
192 | ||
d61a01d4 | 193 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
194 | error "Hook '${hook}' does not exist." |
195 | return ${EXIT_ERROR} | |
196 | fi | |
197 | ||
059469a8 MT |
198 | zone_db ${zone} stopping |
199 | ||
d61a01d4 | 200 | hook_zone_exec ${hook} down ${zone} $@ |
059469a8 MT |
201 | |
202 | zone_db ${zone} stopped | |
1848564d MT |
203 | } |
204 | ||
205 | function zone_status() { | |
206 | local zone=${1} | |
207 | shift | |
208 | ||
209 | if ! zone_exists ${zone}; then | |
210 | error "Zone '${zone}' does not exist." | |
211 | return ${EXIT_ERROR} | |
212 | fi | |
213 | ||
214 | local hook=$(config_get_hook $(zone_dir ${zone})/settings) | |
215 | ||
216 | if [ -z "${hook}" ]; then | |
217 | error "Config file did not provide any hook." | |
218 | return ${EXIT_ERROR} | |
219 | fi | |
220 | ||
d61a01d4 | 221 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
222 | error "Hook '${hook}' does not exist." |
223 | return ${EXIT_ERROR} | |
224 | fi | |
225 | ||
d61a01d4 | 226 | hook_zone_exec ${hook} status ${zone} $@ |
1848564d MT |
227 | } |
228 | ||
711ffac1 | 229 | # XXX deprecated |
1848564d MT |
230 | function zone_port() { |
231 | local zone=${1} | |
232 | shift | |
233 | ||
234 | if ! zone_exists ${zone}; then | |
235 | error "Zone '${zone}' does not exist." | |
236 | return ${EXIT_ERROR} | |
237 | fi | |
238 | ||
239 | local hook=$(config_get_hook $(zone_dir ${zone})/settings) | |
240 | ||
241 | if [ -z "${hook}" ]; then | |
242 | error "Config file did not provide any hook." | |
243 | return ${EXIT_ERROR} | |
244 | fi | |
245 | ||
d61a01d4 | 246 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
247 | error "Hook '${hook}' does not exist." |
248 | return ${EXIT_ERROR} | |
249 | fi | |
250 | ||
d61a01d4 | 251 | hook_zone_exec ${hook} port ${zone} $@ |
1848564d MT |
252 | } |
253 | ||
711ffac1 MT |
254 | function zone_port() { |
255 | local zone=${1} | |
256 | local action=${2} | |
257 | shift 2 | |
258 | ||
259 | assert isset zone | |
260 | assert isset action | |
261 | assert zone_exists ${zone} | |
262 | ||
263 | case "${action}" in | |
264 | add|remove|edit) | |
265 | zone_port_${action} ${zone} $@ | |
266 | ;; | |
267 | esac | |
268 | } | |
269 | ||
270 | function zone_port_add() { | |
271 | local zone=${1} | |
272 | local port=${2} | |
273 | shift 2 | |
274 | ||
275 | assert isset zone | |
276 | assert isset port | |
277 | assert zone_exists ${zone} | |
278 | ||
279 | local hook_port=$(port_get_hook ${port}) | |
280 | ||
281 | assert isset hook_port | |
282 | ||
283 | if ! listmatch ${hook_port} $(zone_get_supported_hooks ${zone}); then | |
284 | error "Zone '${zone}' does not support ports with hook '${hook_port}'." | |
285 | return ${EXIT_ERROR} | |
286 | fi | |
287 | ||
288 | # XXX does this already exist? | |
289 | ||
290 | # XXX I would rather like a relative symlink | |
291 | ln -sf $(port_file ${port}) $(zone_dir ${zone})/port.${port} | |
292 | } | |
293 | ||
294 | function zone_port_add() { | |
295 | local zone=${1} | |
296 | shift | |
297 | ||
298 | assert isset zone | |
299 | ||
300 | local hook=$(zone_get_hook ${zone}) | |
301 | ||
302 | assert isset hook | |
303 | ||
304 | hook_zone_exec ${hook} port_add ${zone} $@ | |
305 | } | |
306 | ||
307 | function zone_port_edit() { | |
308 | local zone=${1} | |
309 | local port=${2} | |
310 | shift 2 | |
311 | ||
312 | assert isset zone | |
313 | assert isset port | |
314 | ||
315 | port_edit ${port} $@ | |
316 | } | |
317 | ||
318 | function zone_port_remove() { | |
319 | local zone=${1} | |
320 | local port=${2} | |
321 | shift 2 | |
322 | ||
323 | assert isset zone | |
324 | assert isset port | |
325 | ||
326 | rm -f $(zone_dir ${zone})/port.${port} | |
327 | } | |
328 | ||
329 | function zone_port_cmd() { | |
330 | local cmd=${1} | |
331 | local zone=${2} | |
332 | local port=${3} | |
333 | shift 3 | |
334 | ||
335 | assert isset zone | |
336 | assert isset port | |
337 | ||
338 | local hook_zone=$(zone_get_hook ${zone}) | |
339 | local hook_port=$(port_get_hook ${port}) | |
340 | ||
341 | assert isset hook_zone | |
342 | assert isset hook_port | |
343 | ||
344 | assert hook_zone_port_exists ${hook_zone} ${hook_port} | |
345 | ||
346 | hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@ | |
347 | } | |
348 | ||
349 | function zone_port_cmd() { | |
350 | error_log "UNSUPPORTED FUNCTION CALLED: zone_port_cmd" | |
351 | backtrace | |
352 | } | |
353 | ||
354 | function zone_port_up() { | |
355 | zone_port_cmd up $@ | |
356 | } | |
357 | ||
358 | function zone_port_down() { | |
359 | zone_port_cmd down $@ | |
360 | } | |
361 | ||
362 | function zone_get_ports() { | |
363 | local zone=${1} | |
364 | ||
365 | assert isset zone | |
366 | ||
367 | local port | |
368 | for port in $(zone_dir ${zone})/port.*; do | |
369 | port=$(basename ${port}) | |
370 | port=${port#port.} | |
371 | ||
372 | if port_exists ${port}; then | |
373 | echo "${port}" | |
374 | fi | |
375 | done | |
376 | } | |
377 | ||
1848564d MT |
378 | function zone_config() { |
379 | local zone=${1} | |
380 | shift | |
381 | ||
382 | if ! zone_exists ${zone}; then | |
383 | error "Zone '${zone}' does not exist." | |
384 | return ${EXIT_ERROR} | |
385 | fi | |
386 | ||
387 | local hook=$(config_get_hook $(zone_dir ${zone})/settings) | |
388 | ||
389 | if [ -z "${hook}" ]; then | |
390 | error "Config file did not provide any hook." | |
391 | return ${EXIT_ERROR} | |
392 | fi | |
393 | ||
d61a01d4 | 394 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
395 | error "Hook '${hook}' does not exist." |
396 | return ${EXIT_ERROR} | |
397 | fi | |
398 | ||
d61a01d4 | 399 | hook_zone_exec ${hook} config ${zone} $@ |
1848564d MT |
400 | } |
401 | ||
402 | function zone_show() { | |
403 | local zone=${1} | |
404 | ||
405 | echo "${zone}" | |
406 | echo " Type: $(zone_get_hook ${zone})" | |
407 | echo | |
408 | } | |
409 | ||
410 | function zones_show() { | |
411 | local zone | |
412 | ||
413 | for zone in $(zones_get $@); do | |
414 | zone_show ${zone} | |
415 | done | |
416 | } | |
417 | ||
418 | function zones_get_all() { | |
419 | local zone | |
d61a01d4 | 420 | for zone in $(zone_dir)/*; do |
1848564d MT |
421 | zone=$(basename ${zone}) |
422 | zone_exists ${zone} || continue | |
423 | ||
424 | echo "${zone}" | |
425 | done | sort | |
426 | } | |
427 | ||
428 | function zones_get_local() { | |
429 | local zone | |
430 | for zone in $(zones_get_all); do | |
431 | zone_is_local ${zone} && echo "${zone}" | |
432 | done | |
433 | } | |
434 | ||
435 | function zones_get_nonlocal() { | |
436 | local zone | |
437 | for zone in $(zones_get_all); do | |
5e42d659 | 438 | zone_is_nonlocal ${zone} && echo "${zone}" |
1848564d MT |
439 | done |
440 | } | |
441 | ||
442 | function zones_get() { | |
443 | local local=1 | |
444 | local remote=1 | |
445 | ||
446 | local zones | |
447 | ||
448 | while [ $# -gt 0 ]; do | |
449 | case "${1}" in | |
450 | --local-only) | |
451 | local=1 | |
452 | remote=0 | |
453 | ;; | |
454 | --remote-only) | |
455 | local=0 | |
456 | remote=1 | |
457 | ;; | |
458 | --all) | |
459 | local=1 | |
460 | remote=1 | |
461 | ;; | |
462 | *) | |
463 | if zone_name_is_valid ${1}; then | |
464 | zones="${zones} ${1}" | |
465 | else | |
466 | warning "Unrecognized argument '${1}'" | |
467 | fi | |
468 | ;; | |
469 | esac | |
470 | shift | |
471 | done | |
472 | ||
473 | if [ -n "${zones}" ]; then | |
474 | local zone | |
475 | for zone in ${zones}; do | |
476 | zone_exists ${zone} && echo "${zone}" | |
477 | done | |
478 | exit ${EXIT_OK} | |
479 | fi | |
480 | ||
481 | if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then | |
482 | zones_get_all | |
483 | elif [ ${local} -eq 1 ]; then | |
484 | zones_get_local | |
485 | elif [ ${remote} -eq 1 ]; then | |
486 | zones_get_nonlocal | |
487 | fi | |
488 | } | |
489 | ||
490 | function zone_ports_list() { | |
491 | local zone=${1} | |
492 | ||
493 | local port | |
494 | for port in $(zone_dir ${zone})/port.*; do | |
495 | [ -e "${port}" ] || continue | |
496 | ||
497 | echo $(basename ${port}) | |
498 | done | sort | |
499 | } | |
500 | ||
501 | function zone_ports_cmd() { | |
502 | local cmd=${1} | |
503 | local zone=${2} | |
504 | shift 2 | |
505 | ||
711ffac1 MT |
506 | assert isset cmd |
507 | assert isset zone | |
1848564d | 508 | |
711ffac1 | 509 | assert zone_exists ${zone} |
1848564d | 510 | |
711ffac1 MT |
511 | local hook=$(zone_get_hook ${zone}) |
512 | ||
513 | local port | |
514 | for port in $(zone_get_ports ${zone}); do | |
515 | #zone_port_cmd ${cmd} ${zone} ${port} $@ | |
516 | hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@ | |
1848564d MT |
517 | done |
518 | } | |
519 | ||
520 | function zone_ports_up() { | |
711ffac1 | 521 | zone_ports_cmd port_up $@ |
1848564d MT |
522 | } |
523 | ||
524 | function zone_ports_down() { | |
711ffac1 MT |
525 | zone_ports_cmd port_down $@ |
526 | } | |
527 | ||
528 | function zone_ports_status() { | |
529 | zone_ports_cmd port_status $@ | |
1848564d MT |
530 | } |
531 | ||
532 | function zone_configs_list() { | |
533 | local zone=${1} | |
534 | ||
535 | local config | |
536 | for config in $(zone_dir ${zone})/config.*; do | |
537 | [ -e "${config}" ] || continue | |
538 | ||
539 | echo $(basename ${config}) | |
540 | done | sort | |
541 | } | |
542 | ||
543 | function zone_configs_cmd() { | |
544 | local cmd=${1} | |
545 | local zone=${2} | |
546 | shift 2 | |
547 | ||
548 | local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings) | |
549 | ||
550 | local hook_config | |
551 | local config | |
552 | for config in $(zone_configs_list ${zone}); do | |
553 | hook_config=$(config_get_hook $(zone_dir ${zone})/${config}) | |
554 | ||
d61a01d4 | 555 | hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@ |
1848564d MT |
556 | done |
557 | } | |
558 | ||
559 | function zone_configs_up() { | |
560 | zone_configs_cmd up $@ | |
561 | } | |
562 | ||
563 | function zone_configs_down() { | |
564 | zone_configs_cmd down $@ | |
565 | } | |
566 | ||
567 | function zone_has_ipv4() { | |
568 | device_has_ipv4 $@ | |
569 | } | |
570 | ||
4231f419 MT |
571 | function zone_has_ipv6() { |
572 | device_has_ipv6 $@ | |
573 | } | |
574 | ||
059469a8 MT |
575 | function zone_db() { |
576 | local zone=${1} | |
577 | local action=${2} | |
578 | shift 2 | |
579 | ||
580 | case "${action}" in | |
581 | starting|started|stopping|stopped) | |
582 | db_connection_update ${zone} ${action} | |
583 | ;; | |
584 | esac | |
585 | } | |
5e42d659 MT |
586 | |
587 | function zone_is_up() { | |
588 | local zone=${1} | |
589 | ||
590 | device_is_up ${zone} | |
591 | } | |
592 | ||
593 | function zone_is_down() { | |
594 | ! zone_is_up $@ | |
595 | } | |
711ffac1 MT |
596 | |
597 | function zone_get_supported_hooks() { | |
598 | local zone=${1} | |
599 | ||
600 | local hook=$(zone_get_hook ${zone}) | |
601 | ||
602 | hook_zone_ports_get_all ${hook} | |
603 | } | |
604 | ||
605 | function zone_file() { | |
606 | local zone=${1} | |
607 | ||
608 | assert isset zone | |
609 | ||
610 | echo "$(zone_dir ${zone})/settings" | |
611 | } | |
612 | ||
613 | function zone_config_read() { | |
614 | local zone=${1} | |
615 | ||
616 | assert isset zone | |
617 | ||
618 | config_read $(zone_file ${zone}) | |
619 | } | |
620 | ||
621 | function zone_config_write() { | |
622 | local zone=${1} | |
623 | ||
624 | assert isset zone | |
625 | ||
626 | config_write $(zone_file ${zone}) ${HOOK_SETTINGS} | |
627 | } | |
628 | ||
629 | function zone_config_set() { | |
630 | local zone=${1} | |
631 | shift | |
632 | local args="$@" | |
633 | ||
634 | assert isset zone | |
635 | ||
636 | ( | |
637 | zone_config_read ${zone} | |
638 | ||
639 | for arg in ${args}; do | |
640 | eval "${arg}" | |
641 | done | |
642 | ||
643 | zone_config_write ${zone} | |
644 | ) | |
645 | } |