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 # Parse the command line
23 while [ $# -gt 0 ]; do
33 [ -n "${action}" ] && break
36 .
/usr
/lib
/network
/functions
38 function cli_config
() {
39 if cli_help_requested $@
; then
40 cli_show_man network-config
44 if [ -n "${1}" ]; then
52 function cli_device
() {
57 if ! isset device
; then
58 cli_show_man network-device
62 assert device_exists
${device}
66 cli_device_discover
${device} $@
69 cli_device_status
${device}
72 cli_show_man network-device
79 function cli_device_status
() {
81 assert device_exists
${device}
83 # Save the type of the device for later.
84 local type=$
(device_get_type
${device})
86 cli_headline
1 "Device status: ${device}"
87 cli_print_fmt1
1 "Name" "${device}"
89 # Print the device status.
90 device_is_up
${device} &>/dev
/null
95 status
="${COLOUR_GREEN}UP${COLOUR_NORMAL}"
98 status
="${COLOUR_RED}DOWN${COLOUR_NORMAL}"
102 cli_print_fmt1
1 "Status" "${status}"
103 cli_print_fmt1
1 "Type" "${type}"
104 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
107 # Print the link speed for ethernet devices.
110 cli_print_fmt1
1 "Link" \
111 "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex"
115 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
118 # Print device statistics.
119 cli_device_stats
2 ${device}
121 # Print some more information.
122 device_has_carrier
${device} &>/dev
/null
123 cli_print_fmt1
1 "Has carrier?" "$(cli_print_bool $?)"
125 device_is_promisc
${device} &>/dev
/null
126 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
129 # Print all virtual devices.
130 local virtuals
=$
(device_get_virtuals
${device})
131 if [ -n "${virtuals}" ]; then
132 cli_headline
2 "Virtual devices"
135 for virtual
in ${virtuals}; do
136 cli_print
2 "* %-6s - %s" "${virtual}" "$(device_get_address ${virtual})"
143 function cli_device_discover
() {
147 local device_type
=$
(device_get_type
${device})
148 if [ "${device_type}" != "real" ]; then
154 while [ $# -gt 0 ]; do
164 device_is_up
${device} && up
=1
165 device_set_up
${device}
167 enabled raw ||
echo "${device}"
172 for hook
in $
(hook_zone_get_all
); do
173 out
=$
(hook_zone_exec
${hook} discover
${device})
176 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
184 echo "${hook}: ${line}"
189 echo "${hook}: FAILED"
195 echo " ${hook} was successful."
203 echo " ${hook} failed."
211 [ "${up}" = "1" ] || device_set_down
${device}
214 function cli_hostname
() {
215 if cli_help_requested $@
; then
222 if [ -n "${hostname}" ]; then
223 config_hostname
${hostname}
224 log INFO
"Hostname was set to '${hostname}'."
225 log INFO
"Changes do only take affect after reboot."
229 echo "$(config_hostname)"
233 function cli_port
() {
234 if cli_help_requested $@
; then
235 cli_show_man network-port
242 if port_exists
${1}; then
262 port_
${action} ${port} $@
265 error
"Unrecognized argument: ${action}"
278 error
"Unrecognized argument: ${action}"
285 function cli_zone
() {
286 if cli_help_requested $@
; then
287 cli_show_man network-zone
294 if zone_name_is_valid
${1}; then
313 config|down|edit|port|status|up
)
314 zone_
${action} ${zone} $@
317 error
"Unrecognized argument: ${action}"
318 cli_show_man network-zone
334 cli_list_hooks zone $@
337 if [ -n "${action}" ]; then
338 error
"Unrecognized argument: '${action}'"
342 cli_show_man network-zone
349 # Removes a zone either immediately, if it is currently down,
350 # or adds a tag that the removal will be done when the zone
351 # is brought down the next time.
352 function cli_zone_remove
() {
353 if cli_help_requested $@
; then
354 cli_show_man network-zone
359 assert zone_exists
${zone}
361 if zone_is_up
${zone}; then
362 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
365 echo "Removing zone '${zone}' now..."
366 zone_remove_now
${zone}
372 function cli_list_hooks
() {
376 if cli_help_requested $@
; then
377 cli_show_man network-zone
381 local hook_dir
=$
(hook_dir
${type})
384 for hook
in ${hook_dir}/*; do
385 hook
=$
(basename ${hook})
386 if hook_exists
${type} ${hook}; then
392 function cli_start
() {
393 if cli_help_requested $@
; then
398 local zones
=$
(zones_get $@
)
401 for zone
in ${zones}; do
405 wait # until everything is settled
408 function cli_stop
() {
409 if cli_help_requested $@
; then
414 local zones
=$
(zones_get $@
)
417 for zone
in ${zones}; do
421 wait # until everything is settled
424 function cli_restart
() {
425 if cli_help_requested $@
; then
432 # Give the system some time to calm down
433 sleep ${TIMEOUT_RESTART}
438 function cli_status
() {
439 if cli_help_requested $@
; then
444 # When dumping status information, the debug
445 # mode clutters the console which is not what we want.
446 # Logging on the console is disabled for a short time.
447 local log_disable_stdout
=${LOG_DISABLE_STDOUT}
448 LOG_DISABLE_STDOUT
="true"
450 local zones
=$
(zones_get $@
)
453 for zone
in ${zones}; do
458 LOG_DISABLE_STDOUT
=${log_disable_stdout}
461 function cli_reset
() {
462 if cli_help_requested $@
; then
467 warning_log
"Will reset the whole network configuration!!!"
469 # Force mode is disabled by default
472 while [ $# -gt 0 ]; do
481 # If we are not running in force mode, we ask the user if he does know
483 if ! enabled force
; then
484 if ! cli_yesno
"Do you really want to reset the whole network configuration?"; then
490 for zone
in $
(zones_get
--all); do
495 for port
in $
(ports_get
--all); do
499 # Flush all DNS servers.
502 # Re-run the initialization functions
508 # Help function: will show the default man page to the user.
509 # Optionally, there are two arguments taken, the type of hook
510 # and which hook should be shown.
511 function cli_help
() {
515 # Remove unknown types.
516 if ! listmatch
${type} zone port config
; then
520 # If no arguments were given, we will show the default page.
521 if [ -z "${type}" ]; then
526 if ! hook_exists
${type} ${what}; then
527 error
"Hook of type '${type}' and name '${what}' could not be found."
531 hook_exec
${type} ${what} help
535 if cli_help_requested $@
; then
536 cli_show_man network-dns
541 local cmd
=${1}; shift
542 if [ -z "${cmd}" ]; then
543 cli_show_man network-dns
547 # Get the new server to process (if any).
553 __dns_server_println
"SERVER" "PRIORITY"
558 log INFO
"Adding new DNS server: ${server}"
559 dns_server_add
${server} ${priority}
562 log INFO
"Removing DNS server: ${server}"
563 dns_server_remove
${server} ${priority}
566 # Just run the update afterwards.
569 error
"No such command: ${cmd}"
573 # Update the local DNS configuration after changes have been made.
574 dns_generate_resolvconf
580 # Process the given action
586 config|hostname|port|device|zone|start|stop|restart|status|
reset|dns
)
595 error
"Invalid command given: ${action}"
596 cli_usage
"network help"
597 exit ${EXIT_CONF_ERROR}