]> git.ipfire.org Git - people/stevee/network.git/blame - functions.zone
network: Change status output of ipv4-static hook.
[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
25 echo "${ZONE_DIR}/${zone}"
26}
27
28function zone_exists() {
29 local zone=${1}
30
31 [ -d "$(zone_dir ${zone})" ]
32}
33
34function zone_match() {
35 local match
36
37 local i
38 for i in ${VALID_ZONES}; do
39 match="${match}|${i}[0-9]{1,5}"
40 done
41
42 echo "${match:1:${#match}}"
43}
44
45function zone_name_is_valid() {
46 local zone=${1}
47
48 [[ ${zone} =~ $(zone_match) ]]
49}
50
51function zone_is_local() {
52 local zone=${1}
53
5e42d659
MT
54 ! zone_is_nonlocal ${zone}
55}
56
57function zone_is_nonlocal() {
58 local zone=${1}
59
60 [[ ${zone} =~ ^red[0-9]{1,5} ]]
1848564d
MT
61}
62
63function zone_get_hook() {
64 local zone=${1}
65
66 config_get_hook $(zone_dir ${zone})/settings
67}
68
69function zone_create() {
70 local zone=${1}
71 local hook=${2}
72 shift 2
73
74 if ! zone_name_is_valid ${zone}; then
75 error "Zone name '${zone}' is not valid."
76 return ${EXIT_ERROR}
77 fi
78
79 if zone_exists ${zone}; then
80 error "Zone '${zone}' does already exist."
81 return ${EXIT_ERROR}
82 fi
83
84 if ! hook_exists ${hook}; then
85 error "Hook '${hook}' does not exist."
86 return ${EXIT_ERROR}
87 fi
88
89 mkdir -p $(zone_dir ${zone})
90
91 hook_exec ${hook} create ${zone} $@
92 local ret=$?
93
94 # Maybe the zone create hook did not exit correctly.
95 # If this is the case we remove the created zone immediately.
96 if [ "${ret}" = "${EXIT_ERROR}" ]; then
97 zone_remove ${zone}
98 fi
99}
100
101function zone_edit() {
102 local zone=${1}
103 shift
104
105 if ! zone_exists ${zone}; then
106 error "Zone '${zone}' does not exist."
107 return ${EXIT_ERROR}
108 fi
109
110 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
111
112 if [ -z "${hook}" ]; then
113 error "Config file did not provide any hook."
114 return ${EXIT_ERROR}
115 fi
116
117 if ! hook_exists ${hook}; then
118 error "Hook '${hook}' does not exist."
119 return ${EXIT_ERROR}
120 fi
121
122 hook_exec ${hook} edit ${zone} $@
123}
124
125function zone_remove() {
126 local zone=${1}
127 shift
128
129 if ! zone_exists ${zone}; then
130 error "Zone '${zone}' does not exist."
131 return ${EXIT_ERROR}
132 fi
133
134 # XXX Tear this down here?
135
136 rm -rf $(zone_dir ${zone})
137}
138
139function zone_up() {
140 local zone=${1}
141 shift
142
143 if ! zone_exists ${zone}; then
144 error "Zone '${zone}' does not exist."
145 return ${EXIT_ERROR}
146 fi
147
148 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
149
150 if [ -z "${hook}" ]; then
151 error "Config file did not provide any hook."
152 return ${EXIT_ERROR}
153 fi
154
155 if ! hook_exists ${hook}; then
156 error "Hook '${hook}' does not exist."
157 return ${EXIT_ERROR}
158 fi
159
059469a8
MT
160 zone_db ${zone} starting
161
1848564d 162 hook_exec ${hook} up ${zone} $@
059469a8
MT
163
164 zone_db ${zone} started
1848564d
MT
165}
166
167function zone_down() {
168 local zone=${1}
169 shift
170
171 if ! zone_exists ${zone}; then
172 error "Zone '${zone}' does not exist."
173 return ${EXIT_ERROR}
174 fi
175
176 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
177
178 if [ -z "${hook}" ]; then
179 error "Config file did not provide any hook."
180 return ${EXIT_ERROR}
181 fi
182
183 if ! hook_exists ${hook}; then
184 error "Hook '${hook}' does not exist."
185 return ${EXIT_ERROR}
186 fi
187
059469a8
MT
188 zone_db ${zone} stopping
189
1848564d 190 hook_exec ${hook} down ${zone} $@
059469a8
MT
191
192 zone_db ${zone} stopped
1848564d
MT
193}
194
195function zone_status() {
196 local zone=${1}
197 shift
198
199 if ! zone_exists ${zone}; then
200 error "Zone '${zone}' does not exist."
201 return ${EXIT_ERROR}
202 fi
203
204 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
205
206 if [ -z "${hook}" ]; then
207 error "Config file did not provide any hook."
208 return ${EXIT_ERROR}
209 fi
210
211 if ! hook_exists ${hook}; then
212 error "Hook '${hook}' does not exist."
213 return ${EXIT_ERROR}
214 fi
215
216 hook_exec ${hook} status ${zone} $@
217}
218
219function zone_port() {
220 local zone=${1}
221 shift
222
223 if ! zone_exists ${zone}; then
224 error "Zone '${zone}' does not exist."
225 return ${EXIT_ERROR}
226 fi
227
228 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
229
230 if [ -z "${hook}" ]; then
231 error "Config file did not provide any hook."
232 return ${EXIT_ERROR}
233 fi
234
235 if ! hook_exists ${hook}; then
236 error "Hook '${hook}' does not exist."
237 return ${EXIT_ERROR}
238 fi
239
240 hook_exec ${hook} port ${zone} $@
241}
242
243function zone_config() {
244 local zone=${1}
245 shift
246
247 if ! zone_exists ${zone}; then
248 error "Zone '${zone}' does not exist."
249 return ${EXIT_ERROR}
250 fi
251
252 local hook=$(config_get_hook $(zone_dir ${zone})/settings)
253
254 if [ -z "${hook}" ]; then
255 error "Config file did not provide any hook."
256 return ${EXIT_ERROR}
257 fi
258
259 if ! hook_exists ${hook}; then
260 error "Hook '${hook}' does not exist."
261 return ${EXIT_ERROR}
262 fi
263
264 hook_exec ${hook} config ${zone} $@
265}
266
267function zone_show() {
268 local zone=${1}
269
270 echo "${zone}"
271 echo " Type: $(zone_get_hook ${zone})"
272 echo
273}
274
275function zones_show() {
276 local zone
277
278 for zone in $(zones_get $@); do
279 zone_show ${zone}
280 done
281}
282
283function zones_get_all() {
284 local zone
285 for zone in ${ZONE_DIR}/*; do
286 zone=$(basename ${zone})
287 zone_exists ${zone} || continue
288
289 echo "${zone}"
290 done | sort
291}
292
293function zones_get_local() {
294 local zone
295 for zone in $(zones_get_all); do
296 zone_is_local ${zone} && echo "${zone}"
297 done
298}
299
300function zones_get_nonlocal() {
301 local zone
302 for zone in $(zones_get_all); do
5e42d659 303 zone_is_nonlocal ${zone} && echo "${zone}"
1848564d
MT
304 done
305}
306
307function zones_get() {
308 local local=1
309 local remote=1
310
311 local zones
312
313 while [ $# -gt 0 ]; do
314 case "${1}" in
315 --local-only)
316 local=1
317 remote=0
318 ;;
319 --remote-only)
320 local=0
321 remote=1
322 ;;
323 --all)
324 local=1
325 remote=1
326 ;;
327 *)
328 if zone_name_is_valid ${1}; then
329 zones="${zones} ${1}"
330 else
331 warning "Unrecognized argument '${1}'"
332 fi
333 ;;
334 esac
335 shift
336 done
337
338 if [ -n "${zones}" ]; then
339 local zone
340 for zone in ${zones}; do
341 zone_exists ${zone} && echo "${zone}"
342 done
343 exit ${EXIT_OK}
344 fi
345
346 if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
347 zones_get_all
348 elif [ ${local} -eq 1 ]; then
349 zones_get_local
350 elif [ ${remote} -eq 1 ]; then
351 zones_get_nonlocal
352 fi
353}
354
355function zone_ports_list() {
356 local zone=${1}
357
358 local port
359 for port in $(zone_dir ${zone})/port.*; do
360 [ -e "${port}" ] || continue
361
362 echo $(basename ${port})
363 done | sort
364}
365
366function zone_ports_cmd() {
367 local cmd=${1}
368 local zone=${2}
369 shift 2
370
371 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
372
373 local hook_port
374 local port
375 for port in $(zone_ports_list ${zone}); do
376 hook_port=$(config_get_hook $(zone_dir ${zone})/${port})
377
378 hook_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
379 done
380}
381
382function zone_ports_up() {
383 zone_ports_cmd up $@
384}
385
386function zone_ports_down() {
387 zone_ports_cmd down $@
388}
389
390function zone_configs_list() {
391 local zone=${1}
392
393 local config
394 for config in $(zone_dir ${zone})/config.*; do
395 [ -e "${config}" ] || continue
396
397 echo $(basename ${config})
398 done | sort
399}
400
401function zone_configs_cmd() {
402 local cmd=${1}
403 local zone=${2}
404 shift 2
405
406 local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)
407
408 local hook_config
409 local config
410 for config in $(zone_configs_list ${zone}); do
411 hook_config=$(config_get_hook $(zone_dir ${zone})/${config})
412
413 hook_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
414 done
415}
416
417function zone_configs_up() {
418 zone_configs_cmd up $@
419}
420
421function zone_configs_down() {
422 zone_configs_cmd down $@
423}
424
425function zone_has_ipv4() {
426 device_has_ipv4 $@
427}
428
059469a8
MT
429function zone_db() {
430 local zone=${1}
431 local action=${2}
432 shift 2
433
434 case "${action}" in
435 starting|started|stopping|stopped)
436 db_connection_update ${zone} ${action}
437 ;;
438 esac
439}
5e42d659
MT
440
441function zone_is_up() {
442 local zone=${1}
443
444 device_is_up ${zone}
445}
446
447function zone_is_down() {
448 ! zone_is_up $@
449}