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