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
36 function cli_device
() {
41 assert device_exists
${device}
43 if zone_exists
${device} || port_exists
${device}; then
44 error
"The device '${device}' has already been configured."
45 error
"You cannot do a device action."
51 echo "# XXX need to implement --raw here"
52 cli_device_discover
${device} $@
56 # XXX device_show needs to be implemented
60 cli_show_man network-device
65 function cli_device_discover
() {
69 local device_type
=$
(device_get_type
${device})
70 if [ "${device_type}" != "real" ]; then
76 while [ $# -gt 0 ]; do
86 device_is_up
${device} && up
=1
87 device_set_up
${device}
89 enabled raw ||
echo "${device}"
94 for hook
in $
(hook_zone_get_all
); do
95 out
=$
(hook_zone_exec
${hook} discover
${device})
98 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
106 echo "${hook}: ${line}"
111 echo "${hook}: FAILED"
117 echo " ${hook} was successful."
125 echo " ${hook} failed."
133 [ "${up}" = "1" ] || device_set_down
${device}
136 function cli_hostname
() {
137 if cli_help_requested $@
; then
144 if [ -n "${hostname}" ]; then
145 config_hostname
${hostname}
146 log INFO
"Hostname was set to '${hostname}'."
147 log INFO
"Changes do only take affect after reboot."
151 echo "$(config_hostname)"
155 function cli_port
() {
156 if cli_help_requested $@
; then
157 cli_show_man network-port
164 if port_exists
${1}; then
184 port_
${action} ${port} $@
187 error
"Unrecognized argument: ${action}"
200 error
"Unrecognized argument: ${action}"
207 function cli_zone
() {
208 if cli_help_requested $@
; then
209 cli_show_man network-zone
216 if zone_name_is_valid
${1}; then
235 config|down|edit|port|status|up
)
236 zone_
${action} ${zone} $@
239 error
"Unrecognized argument: ${action}"
240 cli_show_man network-zone
256 if [ -n "${action}" ]; then
257 error
"Unrecognized argument: '${action}'"
261 cli_show_man network-zone
268 # Removes a zone either immediately, if it is currently down,
269 # or adds a tag that the removal will be done when the zone
270 # is brought down the next time.
271 function cli_zone_remove
() {
272 if cli_help_requested $@
; then
273 cli_show_man network-zone
278 assert zone_exists
${zone}
280 if zone_is_up
${zone}; then
281 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
284 echo "Removing zone '${zone}' now..."
285 zone_remove_now
${zone}
291 function cli_start
() {
292 if cli_help_requested $@
; then
297 local zones
=$
(zones_get $@
)
300 for zone
in ${zones}; do
304 wait # until everything is settled
307 function cli_stop
() {
308 if cli_help_requested $@
; then
313 local zones
=$
(zones_get $@
)
316 for zone
in ${zones}; do
320 wait # until everything is settled
323 function cli_restart
() {
324 if cli_help_requested $@
; then
331 # Give the system some time to calm down
332 sleep ${TIMEOUT_RESTART}
337 function cli_status
() {
338 if cli_help_requested $@
; then
343 # When dumping status information, the debug
344 # mode clutters the console which is not what we want.
345 # Logging on the console is disabled for a short time.
346 local log_disable_stdout
=${LOG_DISABLE_STDOUT}
347 LOG_DISABLE_STDOUT
="true"
349 local zones
=$
(zones_get $@
)
352 for zone
in ${zones}; do
357 LOG_DISABLE_STDOUT
=${log_disable_stdout}
360 function cli_reset
() {
361 if cli_help_requested $@
; then
366 warning_log
"Will reset the whole network configuration!!!"
368 # Force mode is disabled by default
371 while [ $# -gt 0 ]; do
380 # If we are not running in force mode, we ask the user if he does know
382 if ! enabled force
; then
383 if ! cli_yesno
"Do you really want to reset the whole network configuration?"; then
389 for zone
in $
(zones_get
--all); do
394 for port
in $
(ports_get
--all); do
398 # Re-run the initialization functions
404 function cli_help_requested
() {
405 local argument
="${1}"
407 if [ -n "${argument}" ]; then
408 if listmatch
${argument} help -h --help; then
416 function cli_status_headline
() {
419 local state
="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
420 zone_is_up
${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
422 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
425 function cli_headline() {
427 echo -e "${COLOUR_BOLD}$@
${COLOUR_NORMAL}"
430 function cli_yesno() {
431 local message="$@
[y
/N
] "
435 echo -ne "${message}"
438 if listmatch ${yesno} y Y j J yes YES Yes; then
445 function cli_get_key() {
450 function cli_get_val() {
454 function cli_usage() {
456 local basename="$
(basename ${0})"
458 if ! isset command; then
459 command="${basename} help"
462 echo "The given
command was not understood by
${basename}.
" >&2
463 echo "Please run
'${command}' for detailed
help.
" >&2
466 function cli_show_man() {
470 if ! binary_exists man; then
471 error "The man package is not installed on this system.
"
472 error "Please
install 'man' in order to view the
help.
"