]>
Commit | Line | Data |
---|---|---|
5b20e43a MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
1848564d | 5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # |
5b20e43a MT |
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 | ||
1848564d MT |
22 | # Parse the command line |
23 | while [ $# -gt 0 ]; do | |
24 | case "${1}" in | |
25 | -d|--debug) | |
26 | DEBUG=1 | |
5b20e43a | 27 | ;; |
1848564d MT |
28 | *) |
29 | action=${1} | |
5b20e43a | 30 | ;; |
5b20e43a | 31 | esac |
5b20e43a | 32 | shift |
1848564d | 33 | [ -n "${action}" ] && break |
5b20e43a MT |
34 | done |
35 | ||
3647b19f MT |
36 | . /usr/lib/network/functions |
37 | ||
9111eb72 MT |
38 | function cli_config() { |
39 | if cli_help_requested $@; then | |
40 | cli_show_man network-config | |
41 | exit ${EXIT_OK} | |
42 | fi | |
43 | ||
44 | if [ -n "${1}" ]; then | |
45 | config_set $@ | |
46 | network_config_write | |
47 | else | |
48 | network_config_print | |
49 | fi | |
50 | } | |
51 | ||
52 | function cli_device() { | |
53 | local device=${1} | |
54 | local action=${2} | |
55 | shift 2 | |
56 | ||
57 | assert device_exists ${device} | |
58 | ||
59 | if zone_exists ${device} || port_exists ${device}; then | |
60 | error "The device '${device}' has already been configured." | |
61 | error "You cannot do a device action." | |
62 | return ${EXIT_ERROR} | |
63 | fi | |
64 | ||
65 | case "${action}" in | |
66 | discover) | |
67 | echo "# XXX need to implement --raw here" | |
68 | cli_device_discover ${device} $@ | |
69 | ;; | |
70 | ||
71 | show|"") | |
72 | # XXX device_show needs to be implemented | |
73 | device_show ${device} | |
74 | ;; | |
75 | *) | |
76 | cli_show_man network-device | |
77 | ;; | |
78 | esac | |
79 | } | |
80 | ||
81 | function cli_device_discover() { | |
82 | local device=${1} | |
83 | shift | |
84 | ||
85 | local device_type=$(device_get_type ${device}) | |
86 | if [ "${device_type}" != "real" ]; then | |
87 | return ${EXIT_OK} | |
88 | fi | |
89 | ||
90 | local raw | |
91 | ||
92 | while [ $# -gt 0 ]; do | |
93 | case "${1}" in | |
94 | --raw) | |
95 | raw=1 | |
96 | ;; | |
97 | esac | |
98 | shift | |
99 | done | |
100 | ||
101 | local up | |
102 | device_is_up ${device} && up=1 | |
103 | device_set_up ${device} | |
104 | ||
105 | enabled raw || echo "${device}" | |
106 | ||
107 | local hook | |
108 | local out | |
109 | local ret | |
110 | for hook in $(hook_zone_get_all); do | |
111 | out=$(hook_zone_exec ${hook} discover ${device}) | |
112 | ret=$? | |
113 | ||
114 | [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue | |
115 | ||
116 | if enabled raw; then | |
117 | case "${ret}" in | |
118 | ${DISCOVER_OK}) | |
119 | echo "${hook}: OK" | |
120 | local line | |
121 | while read line; do | |
122 | echo "${hook}: ${line}" | |
123 | done <<<"${out}" | |
124 | ;; | |
125 | ||
126 | ${DISCOVER_ERROR}) | |
127 | echo "${hook}: FAILED" | |
128 | ;; | |
129 | esac | |
130 | else | |
131 | case "${ret}" in | |
132 | ${DISCOVER_OK}) | |
133 | echo " ${hook} was successful." | |
134 | local line | |
135 | while read line; do | |
136 | echo " ${line}" | |
137 | done <<<"${out}" | |
138 | ;; | |
139 | ||
140 | ${DISCOVER_ERROR}) | |
141 | echo " ${hook} failed." | |
142 | ;; | |
143 | esac | |
144 | fi | |
145 | done | |
146 | ||
147 | echo # New line | |
148 | ||
149 | [ "${up}" = "1" ] || device_set_down ${device} | |
150 | } | |
151 | ||
152 | function cli_hostname() { | |
153 | if cli_help_requested $@; then | |
154 | cli_show_man network | |
155 | exit ${EXIT_OK} | |
156 | fi | |
157 | ||
158 | local hostname=${1} | |
159 | ||
160 | if [ -n "${hostname}" ]; then | |
161 | config_hostname ${hostname} | |
162 | log INFO "Hostname was set to '${hostname}'." | |
163 | log INFO "Changes do only take affect after reboot." | |
164 | exit ${EXIT_OK} | |
165 | fi | |
166 | ||
167 | echo "$(config_hostname)" | |
168 | exit ${EXIT_OK} | |
169 | } | |
170 | ||
171 | function cli_port() { | |
172 | if cli_help_requested $@; then | |
173 | cli_show_man network-port | |
174 | exit ${EXIT_OK} | |
175 | fi | |
176 | ||
177 | local action | |
178 | local port | |
179 | ||
180 | if port_exists ${1}; then | |
181 | port=${1} | |
182 | action=${2} | |
183 | shift 2 | |
184 | ||
185 | # Action aliases | |
186 | case "${action}" in | |
187 | start) | |
188 | action="up" | |
189 | ;; | |
190 | stop) | |
191 | action="down" | |
192 | ;; | |
193 | show) | |
194 | action="status" | |
195 | ;; | |
196 | esac | |
197 | ||
198 | case "${action}" in | |
199 | edit|up|down|status) | |
200 | port_${action} ${port} $@ | |
201 | ;; | |
202 | *) | |
203 | error "Unrecognized argument: ${action}" | |
204 | exit ${EXIT_ERROR} | |
205 | ;; | |
206 | esac | |
207 | else | |
208 | action=${1} | |
209 | shift | |
210 | ||
211 | case "${action}" in | |
212 | create|destroy) | |
213 | port_${action} $@ | |
214 | ;; | |
215 | *) | |
216 | error "Unrecognized argument: ${action}" | |
217 | exit ${EXIT_ERROR} | |
218 | ;; | |
219 | esac | |
220 | fi | |
221 | } | |
222 | ||
223 | function cli_zone() { | |
224 | if cli_help_requested $@; then | |
225 | cli_show_man network-zone | |
226 | exit ${EXIT_OK} | |
227 | fi | |
228 | ||
229 | local action | |
230 | local zone | |
231 | ||
232 | if zone_name_is_valid ${1}; then | |
233 | zone=${1} | |
234 | action=${2} | |
235 | shift 2 | |
236 | ||
237 | # Action aliases | |
238 | case "${action}" in | |
239 | start) | |
240 | action="up" | |
241 | ;; | |
242 | stop) | |
243 | action="down" | |
244 | ;; | |
245 | show) | |
246 | action="status" | |
247 | ;; | |
248 | esac | |
249 | ||
250 | case "${action}" in | |
251 | config|down|edit|port|status|up) | |
252 | zone_${action} ${zone} $@ | |
253 | ;; | |
254 | *) | |
255 | error "Unrecognized argument: ${action}" | |
256 | cli_show_man network-zone | |
257 | exit ${EXIT_ERROR} | |
258 | ;; | |
259 | esac | |
260 | else | |
261 | action=${1} | |
262 | shift | |
263 | ||
264 | case "${action}" in | |
265 | create) | |
266 | zone_${action} $@ | |
267 | ;; | |
268 | remove) | |
269 | cli_zone_remove $@ | |
270 | ;; | |
271 | list-hooks) | |
272 | cli_list_hooks zone $@ | |
273 | ;; | |
274 | ""|*) | |
275 | if [ -n "${action}" ]; then | |
276 | error "Unrecognized argument: '${action}'" | |
277 | echo | |
278 | fi | |
279 | ||
280 | cli_show_man network-zone | |
281 | exit ${EXIT_ERROR} | |
282 | ;; | |
283 | esac | |
284 | fi | |
285 | } | |
286 | ||
287 | # Removes a zone either immediately, if it is currently down, | |
288 | # or adds a tag that the removal will be done when the zone | |
289 | # is brought down the next time. | |
290 | function cli_zone_remove() { | |
291 | if cli_help_requested $@; then | |
292 | cli_show_man network-zone | |
293 | exit ${EXIT_OK} | |
294 | fi | |
295 | ||
296 | local zone=${1} | |
297 | assert zone_exists ${zone} | |
298 | ||
299 | if zone_is_up ${zone}; then | |
300 | echo "Zone '${zone}' is up and will be removed when it goes down the next time." | |
301 | zone_remove ${zone} | |
302 | else | |
303 | echo "Removing zone '${zone}' now..." | |
304 | zone_remove_now ${zone} | |
305 | fi | |
306 | ||
307 | exit ${EXIT_OK} | |
308 | } | |
309 | ||
310 | function cli_list_hooks() { | |
311 | local type=${1} | |
312 | shift | |
313 | ||
314 | if cli_help_requested $@; then | |
315 | cli_show_man network-zone | |
316 | exit ${EXIT_OK} | |
317 | fi | |
318 | ||
319 | local hook_dir=$(hook_dir ${type}) | |
320 | local hook | |
321 | ||
322 | for hook in ${hook_dir}/*; do | |
323 | hook=$(basename ${hook}) | |
324 | if hook_exists ${type} ${hook}; then | |
325 | echo "${hook}" | |
326 | fi | |
327 | done | sort -u | |
328 | } | |
329 | ||
330 | function cli_start() { | |
331 | if cli_help_requested $@; then | |
332 | cli_show_man network | |
333 | exit ${EXIT_OK} | |
334 | fi | |
335 | ||
336 | local zones=$(zones_get $@) | |
337 | ||
338 | local zone | |
339 | for zone in ${zones}; do | |
340 | zone_start ${zone} & | |
341 | done | |
342 | ||
343 | wait # until everything is settled | |
344 | } | |
345 | ||
346 | function cli_stop() { | |
347 | if cli_help_requested $@; then | |
348 | cli_show_man network | |
349 | exit ${EXIT_OK} | |
350 | fi | |
351 | ||
352 | local zones=$(zones_get $@) | |
353 | ||
354 | local zone | |
355 | for zone in ${zones}; do | |
356 | zone_stop ${zone} & | |
357 | done | |
358 | ||
359 | wait # until everything is settled | |
360 | } | |
361 | ||
362 | function cli_restart() { | |
363 | if cli_help_requested $@; then | |
364 | cli_show_man network | |
365 | exit ${EXIT_OK} | |
366 | fi | |
367 | ||
368 | cli_stop $@ | |
369 | ||
370 | # Give the system some time to calm down | |
371 | sleep ${TIMEOUT_RESTART} | |
372 | ||
373 | cli_start $@ | |
374 | } | |
375 | ||
376 | function cli_status() { | |
377 | if cli_help_requested $@; then | |
378 | cli_show_man network | |
379 | exit ${EXIT_OK} | |
380 | fi | |
381 | ||
382 | # When dumping status information, the debug | |
383 | # mode clutters the console which is not what we want. | |
384 | # Logging on the console is disabled for a short time. | |
385 | local log_disable_stdout=${LOG_DISABLE_STDOUT} | |
386 | LOG_DISABLE_STDOUT="true" | |
387 | ||
388 | local zones=$(zones_get $@) | |
389 | ||
390 | local zone | |
391 | for zone in ${zones}; do | |
392 | zone_status ${zone} | |
393 | done | |
394 | ||
395 | # Reset logging. | |
396 | LOG_DISABLE_STDOUT=${log_disable_stdout} | |
397 | } | |
398 | ||
399 | function cli_reset() { | |
400 | if cli_help_requested $@; then | |
401 | cli_show_man network | |
402 | exit ${EXIT_OK} | |
403 | fi | |
404 | ||
405 | warning_log "Will reset the whole network configuration!!!" | |
406 | ||
407 | # Force mode is disabled by default | |
408 | local force=0 | |
409 | ||
410 | while [ $# -gt 0 ]; do | |
411 | case "${1}" in | |
412 | --force|-f) | |
413 | force=1 | |
414 | ;; | |
415 | esac | |
416 | shift | |
417 | done | |
418 | ||
419 | # If we are not running in force mode, we ask the user if he does know | |
420 | # what he is doing. | |
421 | if ! enabled force; then | |
422 | if ! cli_yesno "Do you really want to reset the whole network configuration?"; then | |
423 | exit ${EXIT_ERROR} | |
424 | fi | |
425 | fi | |
426 | ||
427 | local zone | |
428 | for zone in $(zones_get --all); do | |
429 | zone_remove ${zone} | |
430 | done | |
431 | ||
432 | local port | |
433 | for port in $(ports_get --all); do | |
434 | port_remove ${port} | |
435 | done | |
436 | ||
437 | # Re-run the initialization functions | |
438 | init_run | |
439 | ||
440 | exit ${EXIT_OK} | |
441 | } | |
442 | ||
1848564d MT |
443 | # Process the given action |
444 | case "${action}" in | |
b8357295 MT |
445 | init) |
446 | init_run | |
447 | ;; | |
448 | ||
de28a630 | 449 | config|hostname|port|device|zone|start|stop|restart|status|reset) |
0a79ea02 | 450 | cli_${action} $@ |
1848564d MT |
451 | ;; |
452 | ||
fe4555b5 | 453 | ""|help|--help|-h) |
de28a630 | 454 | cli_show_man network |
1848564d MT |
455 | exit ${EXIT_OK} |
456 | ;; | |
fe4555b5 | 457 | |
1848564d | 458 | *) |
fe4555b5 | 459 | error "Invalid command given: ${action}" |
de28a630 | 460 | cli_usage "network help" |
1848564d | 461 | exit ${EXIT_CONF_ERROR} |
fe4555b5 | 462 | ;; |
1848564d | 463 | esac |