]>
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 cli_config() { | |
fe688aa4 MT |
23 | if cli_help_requested $@; then |
24 | cli_usage root-config | |
25 | exit ${EXIT_OK} | |
26 | fi | |
27 | ||
1848564d MT |
28 | if [ -n "${1}" ]; then |
29 | network_config_set $@ | |
30 | else | |
31 | network_config_print | |
32 | fi | |
33 | } | |
34 | ||
35 | function cli_device() { | |
8a4ccede MT |
36 | local device=${1} |
37 | local action=${2} | |
38 | shift 2 | |
1848564d | 39 | |
8a4ccede | 40 | assert device_exists ${device} |
1848564d | 41 | |
8a4ccede MT |
42 | if zone_exists ${device} || port_exists ${device}; then |
43 | error "The device '${device}' has already been configured." | |
44 | error "You cannot do a device action." | |
45 | return ${EXIT_ERROR} | |
46 | fi | |
1848564d | 47 | |
8a4ccede MT |
48 | case "${action}" in |
49 | discover) | |
50 | echo "# XXX need to implement --raw here" | |
51 | cli_device_discover ${device} $@ | |
52 | ;; | |
1848564d | 53 | |
8a4ccede MT |
54 | show|"") |
55 | # XXX device_show needs to be implemented | |
56 | device_show ${device} | |
57 | ;; | |
58 | *) | |
59 | cli_usage device | |
60 | ;; | |
61 | esac | |
1848564d MT |
62 | } |
63 | ||
64 | function cli_device_discover() { | |
65 | local device=${1} | |
66 | shift | |
67 | ||
68 | local device_type=$(device_get_type ${device}) | |
69 | if [ "${device_type}" != "real" ]; then | |
70 | return ${EXIT_OK} | |
71 | fi | |
72 | ||
73 | local raw | |
74 | ||
75 | while [ $# -gt 0 ]; do | |
76 | case "${1}" in | |
77 | --raw) | |
78 | raw=1 | |
79 | ;; | |
80 | esac | |
81 | shift | |
82 | done | |
83 | ||
84 | local up | |
85 | device_is_up ${device} && up=1 | |
86 | device_set_up ${device} | |
87 | ||
88 | enabled raw || echo "${device}" | |
89 | ||
90 | local hook | |
91 | local out | |
92 | local ret | |
d61a01d4 MT |
93 | for hook in $(hook_zone_get_all); do |
94 | out=$(hook_zone_exec ${hook} discover ${device}) | |
1848564d MT |
95 | ret=$? |
96 | ||
97 | [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue | |
98 | ||
99 | if enabled raw; then | |
100 | case "${ret}" in | |
101 | ${DISCOVER_OK}) | |
102 | echo "${hook}: OK" | |
103 | local line | |
104 | while read line; do | |
105 | echo "${hook}: ${line}" | |
106 | done <<<"${out}" | |
107 | ;; | |
108 | ||
109 | ${DISCOVER_ERROR}) | |
110 | echo "${hook}: FAILED" | |
111 | ;; | |
112 | esac | |
113 | else | |
114 | case "${ret}" in | |
115 | ${DISCOVER_OK}) | |
116 | echo " ${hook} was successful." | |
117 | local line | |
118 | while read line; do | |
119 | echo " ${line}" | |
120 | done <<<"${out}" | |
121 | ;; | |
122 | ||
123 | ${DISCOVER_ERROR}) | |
124 | echo " ${hook} failed." | |
125 | ;; | |
126 | esac | |
127 | fi | |
128 | done | |
129 | ||
130 | echo # New line | |
131 | ||
132 | [ "${up}" = "1" ] || device_set_down ${device} | |
133 | } | |
134 | ||
31e59f2b MT |
135 | function cli_hostname() { |
136 | if cli_help_requested $@; then | |
137 | cli_usage hostname | |
138 | exit ${EXIT_OK} | |
139 | fi | |
140 | ||
141 | local hostname=${1} | |
142 | ||
143 | if [ -n "${hostname}" ]; then | |
144 | config_hostname ${hostname} | |
145 | log INFO "Hostname was set to '${hostname}'." | |
146 | log INFO "Changes do only take affect after reboot." | |
147 | exit ${EXIT_OK} | |
148 | fi | |
149 | ||
150 | echo "$(config_hostname)" | |
151 | exit ${EXIT_OK} | |
152 | } | |
153 | ||
8895cf8f MT |
154 | function cli_hotplug() { |
155 | if cli_help_requested $@; then | |
156 | cli_usage root-hotplug | |
157 | exit ${EXIT_OK} | |
158 | fi | |
159 | ||
160 | local command=${1} | |
161 | shift | |
162 | ||
163 | case "${command}" in | |
164 | device) | |
165 | device_hotplug $@ | |
166 | exit $? | |
167 | ;; | |
168 | *) | |
169 | cli_usage root-hotplug | |
170 | exit ${EXIT_OK} | |
171 | ;; | |
172 | esac | |
173 | } | |
174 | ||
711ffac1 MT |
175 | function cli_port() { |
176 | if cli_help_requested $@; then | |
177 | cli_usage root-port | |
178 | exit ${EXIT_OK} | |
179 | fi | |
180 | ||
181 | local action | |
182 | local port | |
183 | ||
184 | if port_exists ${1}; then | |
185 | port=${1} | |
186 | action=${2} | |
187 | shift 2 | |
188 | ||
189 | # Action aliases | |
190 | case "${action}" in | |
191 | start) | |
192 | action="up" | |
193 | ;; | |
194 | stop) | |
195 | action="down" | |
196 | ;; | |
197 | show) | |
198 | action="status" | |
199 | ;; | |
200 | esac | |
201 | ||
202 | case "${action}" in | |
203 | edit|up|down|status) | |
204 | port_${action} ${port} $@ | |
205 | ;; | |
206 | *) | |
207 | error "Unrecognized argument: ${action}" | |
208 | exit ${EXIT_ERROR} | |
209 | ;; | |
210 | esac | |
211 | else | |
212 | action=${1} | |
213 | shift | |
214 | ||
215 | case "${action}" in | |
216 | create|destroy) | |
217 | port_${action} $@ | |
218 | ;; | |
219 | *) | |
220 | error "Unrecognized argument: ${action}" | |
221 | exit ${EXIT_ERROR} | |
222 | ;; | |
223 | esac | |
224 | fi | |
225 | } | |
226 | ||
1848564d | 227 | function cli_zone() { |
8db9698f MT |
228 | if cli_help_requested $@; then |
229 | cli_usage root-zone | |
230 | exit ${EXIT_OK} | |
231 | fi | |
232 | ||
1848564d MT |
233 | local action |
234 | local zone | |
235 | ||
236 | if zone_name_is_valid ${1}; then | |
237 | zone=${1} | |
238 | action=${2} | |
239 | shift 2 | |
240 | ||
711ffac1 MT |
241 | # Action aliases |
242 | case "${action}" in | |
243 | start) | |
244 | action="up" | |
245 | ;; | |
246 | stop) | |
247 | action="down" | |
248 | ;; | |
249 | show) | |
250 | action="status" | |
251 | ;; | |
252 | esac | |
253 | ||
1848564d | 254 | case "${action}" in |
711ffac1 | 255 | config|down|edit|port|status|up) |
1848564d MT |
256 | zone_${action} ${zone} $@ |
257 | ;; | |
8db9698f MT |
258 | *) |
259 | error "Unrecognized argument: ${action}" | |
260 | cli_usage root-zone-subcommands | |
261 | exit ${EXIT_ERROR} | |
262 | ;; | |
1848564d MT |
263 | esac |
264 | else | |
265 | action=${1} | |
266 | shift | |
267 | ||
268 | case "${action}" in | |
269 | create|remove) | |
270 | zone_${action} $@ | |
271 | ;; | |
8db9698f MT |
272 | ""|*) |
273 | if [ -n "${action}" ]; then | |
274 | error "Unrecognized argument: '${action}'" | |
275 | echo | |
276 | fi | |
277 | ||
278 | cli_usage root-zone | |
279 | exit ${EXIT_ERROR} | |
1848564d MT |
280 | ;; |
281 | esac | |
282 | fi | |
283 | } | |
284 | ||
285 | function cli_start() { | |
1d7bc4f3 MT |
286 | if cli_help_requested $@; then |
287 | cli_usage root-start | |
288 | exit ${EXIT_OK} | |
289 | fi | |
290 | ||
1848564d MT |
291 | local zones=$(zones_get $@) |
292 | ||
293 | local zone | |
294 | for zone in ${zones}; do | |
295 | zone_up ${zone} | |
296 | done | |
297 | } | |
298 | ||
299 | function cli_stop() { | |
1d7bc4f3 MT |
300 | if cli_help_requested $@; then |
301 | cli_usage root-stop | |
302 | exit ${EXIT_OK} | |
303 | fi | |
304 | ||
1848564d MT |
305 | local zones=$(zones_get $@) |
306 | ||
307 | local zone | |
308 | for zone in ${zones}; do | |
309 | zone_down ${zone} | |
310 | done | |
311 | } | |
312 | ||
bcef495d MT |
313 | function cli_restart() { |
314 | if cli_help_requested $@; then | |
315 | cli_usage root-restart | |
316 | exit ${EXIT_OK} | |
317 | fi | |
318 | ||
319 | cli_stop $@ | |
320 | ||
321 | # Give the system some time to calm down | |
2ae4f579 | 322 | sleep ${TIMEOUT_RESTART} |
bcef495d MT |
323 | |
324 | cli_start $@ | |
325 | } | |
326 | ||
4c5857b2 MT |
327 | function cli_status() { |
328 | if cli_help_requested $@; then | |
329 | cli_usage root-status | |
330 | exit ${EXIT_OK} | |
331 | fi | |
332 | ||
333 | local zones=$(zones_get $@) | |
334 | ||
335 | local zone | |
336 | for zone in ${zones}; do | |
337 | zone_status ${zone} | |
338 | done | |
339 | } | |
340 | ||
f90e550b MT |
341 | function cli_reset() { |
342 | if cli_help_requested $@; then | |
343 | cli_usage root-reset | |
344 | exit ${EXIT_OK} | |
345 | fi | |
346 | ||
347 | warning_log "Will reset the whole network configuration!!!" | |
348 | ||
349 | # Force mode is disabled by default | |
350 | local force=0 | |
351 | ||
352 | while [ $# -gt 0 ]; do | |
353 | case "${1}" in | |
354 | --force|-f) | |
355 | force=1 | |
356 | ;; | |
357 | esac | |
358 | shift | |
359 | done | |
360 | ||
361 | # If we are not running in force mode, we ask the user if he does know | |
362 | # what he is doing. | |
363 | if ! enabled force; then | |
364 | if ! cli_yesno "Do you really want to reset the whole network configuration?"; then | |
365 | exit ${EXIT_ERROR} | |
366 | fi | |
367 | fi | |
368 | ||
369 | local zone | |
370 | for zone in $(zones_get --all); do | |
371 | zone_remove ${zone} | |
372 | done | |
373 | ||
374 | local port | |
375 | for port in $(ports_get --all); do | |
376 | port_remove ${port} | |
377 | done | |
378 | ||
2ae0fb8d MT |
379 | # Re-run the initialization functions |
380 | init_run | |
f90e550b MT |
381 | |
382 | exit ${EXIT_OK} | |
383 | } | |
384 | ||
1d7bc4f3 | 385 | function cli_help_requested() { |
866de228 MT |
386 | local argument="${1}" |
387 | ||
388 | if [ -n "${argument}" ]; then | |
389 | if listmatch ${argument} help -h --help; then | |
1d7bc4f3 MT |
390 | return ${EXIT_OK} |
391 | fi | |
866de228 | 392 | fi |
1d7bc4f3 MT |
393 | |
394 | return ${EXIT_ERROR} | |
395 | } | |
396 | ||
1848564d MT |
397 | function cli_usage() { |
398 | local what=${1} | |
399 | ||
400 | case "${what}" in | |
401 | root) | |
402 | echo "${0}: [command] <options ...>" | |
403 | echo | |
404 | echo " start - ..." | |
405 | echo " stop - ..." | |
bcef495d | 406 | echo " restart - ..." |
4c5857b2 | 407 | echo " status - ..." |
1848564d MT |
408 | echo |
409 | echo " config - ..." | |
410 | echo | |
411 | echo " device - ..." | |
1848564d MT |
412 | echo " zone - ..." |
413 | echo | |
414 | ;; | |
fe688aa4 MT |
415 | root-config) |
416 | echo "${0}: ${what#root-} [KEY=VAL, ...]" | |
417 | echo | |
418 | echo " This command allows setting of global configuration parameters." | |
419 | echo | |
420 | echo " If no additional arguments are passed it will list the current configuration." | |
421 | echo | |
422 | echo " You can overwrite the settings like the following:" | |
423 | echo | |
424 | echo " ${0} ${what#root-} DEBUG=1 ..." | |
425 | echo | |
426 | ;; | |
f90e550b MT |
427 | root-reset) |
428 | echo "${0}: ${what#root-} [--force | -f]" | |
429 | echo | |
430 | echo " This command resets the network configuration." | |
431 | echo | |
432 | echo " Will delete all zones and ports." | |
433 | echo | |
434 | echo -e " ${COLOUR_RED}USE WITH CAUTION!${COLOUR_NORMAL}" | |
435 | echo | |
436 | ;; | |
bcef495d | 437 | root-start|root-stop|root-restart) |
1d7bc4f3 MT |
438 | echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]" |
439 | echo | |
440 | echo " This commands ${what#root-}s all zones by default." | |
441 | echo " One can pass several parameters to only process a subset of all" | |
442 | echo " available zones:" | |
443 | echo | |
2ab7f50f | 444 | echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}" |
1d7bc4f3 MT |
445 | echo " Process all local zones which includes every zone without red." |
446 | echo | |
2ab7f50f | 447 | echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}" |
1d7bc4f3 MT |
448 | echo " Process all remote zones which means only the red ones." |
449 | echo | |
2ab7f50f | 450 | echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}" |
1d7bc4f3 MT |
451 | echo " Process all zones. This is the default parameter." |
452 | echo | |
453 | echo " Additionally, you can pass one or more zone names which will" | |
454 | echo " be processed." | |
455 | echo | |
456 | ;; | |
4c5857b2 MT |
457 | root-status) |
458 | echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]" | |
459 | echo | |
460 | echo " This commands shows status information of all zones by default." | |
461 | echo " One can pass several parameters to only process a subset of all" | |
462 | echo " available zones:" | |
463 | echo | |
2ab7f50f | 464 | echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}" |
4c5857b2 MT |
465 | echo " Process all local zones which includes every zone without red." |
466 | echo | |
2ab7f50f | 467 | echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}" |
4c5857b2 MT |
468 | echo " Process all remote zones which means only the red ones." |
469 | echo | |
2ab7f50f | 470 | echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}" |
4c5857b2 MT |
471 | echo " Process all zones. This is the default parameter." |
472 | echo | |
473 | echo " Additionally, you can pass one or more zone names which will" | |
474 | echo " be processed." | |
475 | echo | |
476 | ;; | |
8db9698f MT |
477 | root-zone) |
478 | echo "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]" | |
479 | echo | |
480 | echo " Create or remove a zone." | |
481 | echo | |
2ab7f50f | 482 | echo -e " ${COLOUR_BOLD}create <zone> <type> <options>${COLOUR_NORMAL}" |
8db9698f MT |
483 | echo " Create a new zone of type <type> where <zone> is an allowed" |
484 | echo " zone name." | |
485 | echo | |
2ab7f50f | 486 | echo -e " ${COLOUR_BOLD}remove <zone>${COLOUR_NORMAL}" |
8db9698f MT |
487 | echo " Remove the zone <zone>." |
488 | echo | |
489 | echo " You may also edit the configuration of the zones." | |
490 | echo | |
2ab7f50f | 491 | echo -e " ${COLOUR_BOLD}<zone> ...${COLOUR_NORMAL}" |
8db9698f MT |
492 | echo " Edit the zone <zone>." |
493 | echo | |
494 | ;; | |
1848564d MT |
495 | usage) |
496 | echo | |
497 | echo " Run '${0} help' to get information how to use this tool." | |
498 | echo | |
499 | ;; | |
500 | *) | |
501 | error "No help available for this command '${what}'." | |
1d7bc4f3 | 502 | echo |
1848564d MT |
503 | ;; |
504 | esac | |
1d7bc4f3 MT |
505 | |
506 | echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>." | |
1848564d | 507 | } |
9178284d MT |
508 | |
509 | function cli_status_headline() { | |
510 | local zone=${1} | |
511 | ||
512 | local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}" | |
513 | zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}" | |
514 | ||
515 | echo -e "${zone} - ${state} - $(zone_get_hook ${zone})" | |
516 | } | |
517 | ||
518 | function cli_headline() { | |
519 | echo | |
520 | echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}" | |
521 | } | |
f90e550b MT |
522 | |
523 | function cli_yesno() { | |
524 | local message="$@ [y/N] " | |
525 | local yesno | |
526 | ||
527 | echo | |
528 | echo -ne "${message}" | |
529 | read yesno | |
530 | ||
531 | if listmatch ${yesno} y Y j J yes YES Yes; then | |
532 | return ${EXIT_OK} | |
533 | fi | |
534 | ||
535 | return ${EXIT_ERROR} | |
536 | } | |
d76f5107 MT |
537 | |
538 | function cli_get_key() { | |
539 | local key="${1%%=*}" | |
540 | echo "${key/--/}" | |
541 | } | |
542 | ||
543 | function cli_get_val() { | |
544 | echo "${1##*=}" | |
545 | } |