2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 function cli_config
() {
23 if cli_help_requested $@
; then
28 if [ -n "${1}" ]; then
35 function cli_device
() {
40 assert device_exists
${device}
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."
50 echo "# XXX need to implement --raw here"
51 cli_device_discover
${device} $@
55 # XXX device_show needs to be implemented
64 function cli_device_discover
() {
68 local device_type
=$
(device_get_type
${device})
69 if [ "${device_type}" != "real" ]; then
75 while [ $# -gt 0 ]; do
85 device_is_up
${device} && up
=1
86 device_set_up
${device}
88 enabled raw ||
echo "${device}"
93 for hook
in $
(hook_zone_get_all
); do
94 out
=$
(hook_zone_exec
${hook} discover
${device})
97 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
105 echo "${hook}: ${line}"
110 echo "${hook}: FAILED"
116 echo " ${hook} was successful."
124 echo " ${hook} failed."
132 [ "${up}" = "1" ] || device_set_down
${device}
135 function cli_port
() {
136 if cli_help_requested $@
; then
144 if port_exists
${1}; then
164 port_
${action} ${port} $@
167 error
"Unrecognized argument: ${action}"
180 error
"Unrecognized argument: ${action}"
187 function cli_zone
() {
188 if cli_help_requested $@
; then
196 if zone_name_is_valid
${1}; then
215 config|down|edit|port|status|up
)
216 zone_
${action} ${zone} $@
219 error
"Unrecognized argument: ${action}"
220 cli_usage root-zone-subcommands
233 if [ -n "${action}" ]; then
234 error
"Unrecognized argument: '${action}'"
245 function cli_start
() {
246 if cli_help_requested $@
; then
251 local zones
=$
(zones_get $@
)
254 for zone
in ${zones}; do
259 function cli_stop
() {
260 if cli_help_requested $@
; then
265 local zones
=$
(zones_get $@
)
268 for zone
in ${zones}; do
273 function cli_restart
() {
274 if cli_help_requested $@
; then
275 cli_usage root-restart
281 # Give the system some time to calm down
282 sleep ${TIMEOUT_RESTART}
287 function cli_status
() {
288 if cli_help_requested $@
; then
289 cli_usage root-status
293 local zones
=$
(zones_get $@
)
296 for zone
in ${zones}; do
301 function cli_reset
() {
302 if cli_help_requested $@
; then
307 warning_log
"Will reset the whole network configuration!!!"
309 # Force mode is disabled by default
312 while [ $# -gt 0 ]; do
321 # If we are not running in force mode, we ask the user if he does know
323 if ! enabled force
; then
324 if ! cli_yesno
"Do you really want to reset the whole network configuration?"; then
330 for zone
in $
(zones_get
--all); do
335 for port
in $
(ports_get
--all); do
339 # Re-run the initialization functions
345 function cli_help_requested
() {
346 local argument
="${1}"
348 if [ -n "${argument}" ]; then
349 if listmatch
${argument} help -h --help; then
357 function cli_usage
() {
362 echo "${0}: [command] <options ...>"
366 echo " restart - ..."
376 echo "${0}: ${what#root-} [KEY=VAL, ...]"
378 echo " This command allows setting of global configuration parameters."
380 echo " If no additional arguments are passed it will list the current configuration."
382 echo " You can overwrite the settings like the following:"
384 echo " ${0} ${what#root-} DEBUG=1 ..."
388 echo "${0}: ${what#root-} [--force | -f]"
390 echo " This command resets the network configuration."
392 echo " Will delete all zones and ports."
394 echo -e " ${COLOUR_RED}USE WITH CAUTION!${COLOUR_NORMAL}"
397 root-start|root-stop|root-restart
)
398 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
400 echo " This commands ${what#root-}s all zones by default."
401 echo " One can pass several parameters to only process a subset of all"
402 echo " available zones:"
404 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
405 echo " Process all local zones which includes every zone without red."
407 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
408 echo " Process all remote zones which means only the red ones."
410 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
411 echo " Process all zones. This is the default parameter."
413 echo " Additionally, you can pass one or more zone names which will"
414 echo " be processed."
418 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
420 echo " This commands shows status information of all zones by default."
421 echo " One can pass several parameters to only process a subset of all"
422 echo " available zones:"
424 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
425 echo " Process all local zones which includes every zone without red."
427 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
428 echo " Process all remote zones which means only the red ones."
430 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
431 echo " Process all zones. This is the default parameter."
433 echo " Additionally, you can pass one or more zone names which will"
434 echo " be processed."
438 echo "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]"
440 echo " Create or remove a zone."
442 echo -e " ${COLOUR_BOLD}create <zone> <type> <options>${COLOUR_NORMAL}"
443 echo " Create a new zone of type <type> where <zone> is an allowed"
446 echo -e " ${COLOUR_BOLD}remove <zone>${COLOUR_NORMAL}"
447 echo " Remove the zone <zone>."
449 echo " You may also edit the configuration of the zones."
451 echo -e " ${COLOUR_BOLD}<zone> ...${COLOUR_NORMAL}"
452 echo " Edit the zone <zone>."
457 echo " Run '${0} help' to get information how to use this tool."
461 error
"No help available for this command '${what}'."
466 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
469 function cli_status_headline
() {
472 local state
="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
473 zone_is_up
${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
475 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
478 function cli_headline() {
480 echo -e "${COLOUR_BOLD}$@
${COLOUR_NORMAL}"
483 function cli_yesno() {
484 local message="$@
[y
/N
] "
488 echo -ne "${message}"
491 if listmatch ${yesno} y Y j J yes YES Yes; then
498 function cli_get_key() {
503 function cli_get_val() {