###############################################################################
# #
# IPFire.org - A linux based firewall #
-# Copyright (C) 2009 Michael Tremer & Christian Schmidt #
+# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# #
###############################################################################
-BOLD="\\033[1;39m"
-NORMAL="\\033[0;39m"
-ERROR="\\033[1;31m"
-
-. /etc/init/functions
-
-if [ -e "/lib/network/functions" ]; then
- . /lib/network/functions
-elif [ -e "lib/functions" ]; then
- HOME_DIR="lib"
- . lib/functions
-else
- echo "Cannot find functions library. Exiting." >&2
- exit 1
-fi
-
-function usage() {
- echo -e "${BOLD}Usage $0${NORMAL}:\n"
- case "$1" in
- main|"")
- echo "This script will help you configuring your network."
- echo
- echo "You should know that there are three different things:"
- echo
- echo " hook: A script to control connections and protocols."
- echo " port: A physical connection to somewhere."
- echo " zone: A group of ports."
- echo
- echo " $0 [global flags] <hook|port|zone> ... or"
- echo " $0 [global flags] <cmd line options...>"
- echo
- echo -e "${BOLD}Global flags:${NORMAL}"
- echo " --verbose -v - Turn on verbose mode."
- echo " --debug -d - Turn on debug mode."
- echo
- echo -e "${BOLD}Command line options:${NORMAL}"
- echo " help - Prints this help message."
- echo " start - Starts the whole network."
- echo " stop - Stops the whole network."
- echo " restart - Restarts the whole network."
- echo " reload - Reloads the whole network."
- echo
- echo " hook - Run \"$0 hook help\" for more information."
- echo " port - Run \"$0 port help\" for more information."
- echo " zone - Run \"$0 zone help\" for more information."
- echo
- ;;
- hook*)
- echo -e "${BOLD}Hook configuration:${NORMAL}"
- echo
- echo " ${0} [global options] hook <command>"
- echo
- echo -e "${BOLD}1st level commands:${NORMAL}"
- echo -e " ${BOLD}list:${NORMAL}"
- echo " Returns a list of all available hooks."
- echo
- echo
- echo " ${0} [global options] hook <hook> <command>"
- echo
- echo -e "${BOLD}2nd level commands:${NORMAL}"
- echo -e " ${BOLD}help:${NORMAL}"
- echo " Displays some help about the given hook."
- echo
- echo " Example: $0 hook ethernet help"
- echo
- ;;
- port)
- echo -e "${BOLD}Port Configuration:${NORMAL}"
- echo
- echo " $0 [global options] port <command> ..."
- echo
- echo -e "${BOLD}Commands:${NORMAL}"
- echo -e " ${BOLD}show:${NORMAL}"
- echo " Displays information about a given port."
- echo
- echo " Requires a \"port\"."
- echo " Example: $0 port show 00:11:22:33:44:55"
- echo " $0 port show port0"
- echo
- ;;
- zone)
- echo -e "${BOLD}Zone Configuration:${NORMAL}"
- echo
- echo " $0 [global options] zone <command> ..."
- echo
- echo -e "${BOLD}Commands:${NORMAL}"
- echo -e " ${BOLD}show:${NORMAL}"
- echo " Displays information about a given zone."
- echo
- echo " Requires a \"zone\"."
- echo " Example: $0 zone show green0"
- echo
- echo -e " ${BOLD}add:${NORMAL}"
- echo " Adds a new zone."
- echo
- echo " Requires a \"zone\"."
- echo " Example: $0 zone add green0"
- echo
- echo -e " ${BOLD}del:${NORMAL}"
- echo " Deletes a zone."
- echo
- echo " Requires a \"zone\"."
- echo " Example: $0 zone del green0"
- echo
- echo -e " ${BOLD}addport:${NORMAL}"
- echo " Adds a port to a zone."
- echo
- echo " Requires a \"zone\" and \"port\"."
- echo " Example: $0 zone addport green0 port0"
- echo
- echo " You may also pass a hook and its parameters:"
- echo " $0 zone addport green0 port0 vlan 10"
- echo
- echo -e " ${BOLD}delport:${NORMAL}"
- echo " Deletes a port from a zone."
- echo
- echo " Requires a \"zone\" and \"port\"."
- echo " Example: $0 zone delport green0"
- echo
- echo " You may also pass a hook and its parameters:"
- echo " $0 zone delport green0 port0 vlan 10"
- echo
+# Parse the command line
+while [ $# -gt 0 ]; do
+ case "${1}" in
+ -d|--debug)
+ DEBUG=1
+ ;;
+ *)
+ action=${1}
+ ;;
esac
- _exit ${2-1}
-}
+ shift
+ [ -n "${action}" ] && break
+done
-function debug() {
- if [ -n "$1" ]; then
- DEBUG=$1
- verbose $1
- return
- else
- if [ "$DEBUG" = "1" ]; then
- return 0
- else
- return 1
- fi
+. /usr/lib/network/functions
+
+# Read network configuration.
+network_config_read
+
+function cli_config() {
+ if cli_help_requested $@; then
+ cli_show_man network-config
+ exit ${EXIT_OK}
fi
-}
-function verbose() {
- if [ -n "$1" ]; then
- VERBOSE=$1
- return
+ if [ -n "${1}" ]; then
+ config_set $@
+ network_config_write
else
- if [ "$VERBOSE" = "1" ]; then
- return 0
- else
- return 1
- fi
+ network_config_print
fi
}
-function decho() {
- debug && echo -e "${ERROR}$@${NORMAL}" >&2
-}
+function cli_device() {
+ if cli_help_requested $@; then
+ cli_show_man network-device
+ exit ${EXIT_OK}
+ fi
+
+ local device=${1}
+ local action=${2}
+ shift 2
+
+ if ! isset device; then
+ cli_show_man network-device
+ return ${EXIT_ERROR}
+ fi
-function vecho() {
- verbose && echo -e "$@" >&2
+ assert device_exists ${device}
+
+ case "${action}" in
+ discover)
+ cli_device_discover ${device} $@
+ ;;
+ status)
+ cli_device_status ${device}
+ ;;
+ unlock)
+ cli_device_serial_unlock ${device} $@
+ ;;
+ *)
+ cli_show_man network-device
+ ;;
+ esac
+
+ return ${EXIT_OK}
}
-function error() {
- echo -e "${ERROR}ERROR${NORMAL}: $@" >&2
- _exit 1
+function cli_device_status() {
+ local device=${1}
+ assert device_exists ${device}
+
+ # Disable debugging output here.
+ local log_disable_stdout=${LOG_DISABLE_STDOUT}
+ LOG_DISABLE_STDOUT="true"
+
+ # Save the type of the device for later.
+ local type=$(device_get_type ${device})
+
+ cli_headline 1 "Device status: ${device}"
+ cli_print_fmt1 1 "Name" "${device}"
+
+ # Handle serial devices.
+ if [ "${type}" = "serial" ]; then
+ cli_device_status_serial ${device}
+ return $?
+ fi
+
+ # Print the device status.
+ device_is_up ${device} &>/dev/null
+ local status=$?
+
+ case "${status}" in
+ ${EXIT_TRUE})
+ status="${CLR_GREEN_B}UP${CLR_RESET}"
+ ;;
+ ${EXIT_FALSE})
+ status="${CLR_RED_B}DOWN${CLR_RESET}"
+ ;;
+ esac
+
+ cli_print_fmt1 1 "Status" "${status}"
+ cli_print_fmt1 1 "Type" "${type}"
+ cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
+ cli_space
+
+ # Print the link speed for ethernet devices.
+ if device_is_up ${device} &>/dev/null; then
+ case "${type}" in
+ ethernet)
+ cli_print_fmt1 1 "Link" \
+ "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex"
+ ;;
+ esac
+ fi
+
+ cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
+ cli_space
+
+ # Print device statistics.
+ cli_device_stats 2 ${device}
+
+ # Print some more information.
+ device_has_carrier ${device} &>/dev/null
+ cli_print_fmt1 1 "Has carrier?" "$(cli_print_bool $?)"
+
+ device_is_promisc ${device} &>/dev/null
+ cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
+ cli_space
+
+ # Print all vlan devices.
+ local vlans=$(device_get_vlans ${device})
+ if [ -n "${vlans}" ]; then
+ cli_headline 2 "VLAN devices"
+
+ local vlan
+ for vlan in ${vlans}; do
+ cli_print 2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})"
+ done
+ cli_space
+ fi
+
+ # Reset the logging level.
+ LOG_DISABLE_STDOUT=${log_disable_stdout}
}
-function _exit() {
- local code
- local reload
+function cli_device_status_serial() {
+ local device=${1}
+ assert device_is_serial ${device}
- while [ $# -gt 0 ]; do
- case "$1" in
- --reload)
- reload=1
+ serial_is_locked ${device} &>/dev/null
+ local locked=$?
+
+ cli_print_fmt1 1 "Locked" "$(cli_print_bool ${locked})"
+ cli_space
+
+ # Cannot go on when the device is locked.
+ [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
+
+ cli_print_fmt1 1 "Manufacturer" \
+ "$(modem_get_manufacturer ${device})"
+ cli_print_fmt1 1 "Model" \
+ "$(modem_get_model ${device})"
+ cli_print_fmt1 1 "Software version" \
+ "$(modem_get_software_version ${device})"
+
+ if modem_is_mobile ${device}; then
+ cli_print_fmt1 1 "IMEI" \
+ "$(modem_get_device_imei ${device})"
+ cli_space
+
+ cli_headline 2 "Network status"
+ modem_sim_status ${device} &>/dev/null
+ local sim_status_code=$?
+
+ local sim_status="unknown"
+ case "${sim_status_code}" in
+ ${EXIT_SIM_READY})
+ sim_status="SIM ready"
;;
- [0-9]*)
- code=$1
+ ${EXIT_SIM_PIN})
+ sim_status="PIN locked"
;;
- *)
- error "Unrecognized argument: $1"
+ ${EXIT_SIM_PUK})
+ sim_status="PUK locked"
+ ;;
+ esac
+ cli_print_fmt1 2 "SIM status" "${sim_status}"
+
+ if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
+ cli_print_fmt1 2 "IMSI" \
+ "$(modem_get_sim_imsi ${device})"
+ cli_print_fmt1 2 "Operator" \
+ "$(modem_get_network_operator ${device})"
+ cli_print_fmt1 2 "Mode" \
+ "$(modem_get_network_mode ${device})"
+ cli_print_fmt1 2 "Signal quality" \
+ "$(modem_get_signal_quality ${device}) dBm"
+
+ local ber=$(modem_get_bit_error_rate ${device})
+ isset ber || ber="unknown"
+ cli_print_fmt1 2 "Bit Error Rate" "${ber}"
+ fi
+ fi
+ cli_space
+}
+
+function cli_device_discover() {
+ local device=${1}
+ shift
+
+ local device_type=$(device_get_type ${device})
+ if [ "${device_type}" != "real" ]; then
+ return ${EXIT_OK}
+ fi
+
+ local raw
+
+ while [ $# -gt 0 ]; do
+ case "${1}" in
+ --raw)
+ raw=1
;;
esac
shift
done
- if [ "${reload}" = "1" ]; then
- # Reloading network to apply changes immediately
- vecho "Reloading network settings..."
- cmd $0 reload
-
- # Reload firewall, too
- firewall=$(which firewall 2>/dev/null)
- if [ -n "${firewall}" ]; then
- vecho "Reloading firewall..."
- cmd ${firewall} reload
+ local up
+ device_is_up ${device} && up=1
+ device_set_up ${device}
+
+ enabled raw || echo "${device}"
+
+ local hook
+ local out
+ local ret
+ for hook in $(hook_zone_get_all); do
+ out=$(hook_zone_exec ${hook} discover ${device})
+ ret=$?
+
+ [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
+
+ if enabled raw; then
+ case "${ret}" in
+ ${DISCOVER_OK})
+ echo "${hook}: OK"
+ local line
+ while read line; do
+ echo "${hook}: ${line}"
+ done <<<"${out}"
+ ;;
+
+ ${DISCOVER_ERROR})
+ echo "${hook}: FAILED"
+ ;;
+ esac
+ else
+ case "${ret}" in
+ ${DISCOVER_OK})
+ echo " ${hook} was successful."
+ local line
+ while read line; do
+ echo " ${line}"
+ done <<<"${out}"
+ ;;
+
+ ${DISCOVER_ERROR})
+ echo " ${hook} failed."
+ ;;
+ esac
fi
- fi
+ done
- decho "Exiting with code ${code}."
- exit ${code}
+ echo # New line
+
+ [ "${up}" = "1" ] || device_set_down ${device}
}
-function cmd() {
- decho "Running command: $@"
- if debug; then
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} $@
- else
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} $@ >/dev/null
+function cli_device_serial_unlock() {
+ if cli_help_requested $@; then
+ cli_show_man network-device
+ exit ${EXIT_OK}
fi
-}
-function size() {
- local size=${1}
-
- local units
- units[0]="Bytes "
- units[1]="kBytes"
- units[2]="MBytes"
- units[3]="GBytes"
- units[4]="TBytes"
-
- local count=${#units}
- while [ ${count} -gt 0 ]; do
- if [ ${size} -lt 1024 ]; then
- break
+ local device=${1}
+ assert isset device
+
+ if ! device_is_serial ${device}; then
+ error "${device} is not a serial device."
+ error "Unlocking is only supported for serial devices."
+ exit ${EXIT_ERROR}
+ fi
+
+ # Read the current state of the SIM card.
+ modem_sim_status ${device} &>/dev/null
+ local sim_status_code=$?
+
+ # If the SIM card is already unlocked, we don't need to do anything.
+ if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
+ print "The SIM card is already unlocked."
+ exit ${EXIT_OK}
+
+ # If the SIM card is in an unknown state, we cannot do anything.
+ elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
+ error "The SIM card is in an unknown state."
+ exit ${EXIT_ERROR}
+ fi
+
+ # Ask for the code.
+ local code=${2}
+ local require_new_pin="false"
+ local new_pin
+
+ while ! isinteger code; do
+ local message
+ case "${sim_status_code}" in
+ ${EXIT_SIM_PIN})
+ message="Please enter PIN:"
+ ;;
+ ${EXIT_SIM_PUK})
+ message="Please enter PUK:"
+ require_new_pin="true"
+ ;;
+ esac
+ assert isset message
+
+ echo -n "${message} "
+ read -s code
+ echo # Print newline.
+
+ if enabled require_new_pin; then
+ local i new_pin2
+ for i in 0 1; do
+ case "${i}" in
+ 0)
+ message="Please enter a new PIN code:"
+ ;;
+ 1)
+ message="Please confirm the new PIN code:"
+ ;;
+ esac
+
+ echo -n "${message} "
+ read -s new_pin2
+ echo # Print newline.
+
+ if [ -n "${new_pin}" ]; then
+ if [ "${new_pin}" != "${new_pin2}" ]; then
+ error "The entered PIN codes did not match."
+ exit ${EXIT_ERROR}
+ fi
+ else
+ new_pin=${new_pin2}
+ fi
+ done
fi
- size=$((${size} / 1024))
- count=$((${count} - 1))
done
- printf "%4d %s\n" "${size}" "${units[$((${#units} - ${count}))]}"
+
+ # Trying to unlock the SIM card.
+ modem_sim_unlock ${device} ${code} ${new_pin}
+
+ exit $?
}
-function port_show() {
- local port
- if [ $# -eq 0 ]; then
- for port in /sys/class/net/*; do
- port=${port##*/}
- device_is_real ${port} || continue
- port_show ${port}
- done
- return
+function cli_hostname() {
+ if cli_help_requested $@; then
+ cli_show_man network
+ exit ${EXIT_OK}
fi
- port=$(devicify $1)
+ local hostname=${1}
+
+ if [ -n "${hostname}" ]; then
+ config_hostname ${hostname}
+ log INFO "Hostname was set to '${hostname}'."
+ log INFO "Changes do only take affect after reboot."
+ exit ${EXIT_OK}
+ fi
- echo "##################################################"
- echo "#"
- echo -e "# Port ${CLR_BOLD_BLU}${port}${NORMAL}"
- echo "# ------------------------------------------------"
+ echo "$(config_hostname)"
+ exit ${EXIT_OK}
+}
- echo -n "# State: "
- if device_is_up ${port}; then
- echo -e "${CLR_BOLD_GRN}up${NORMAL}"
- else
- echo -e "${CLR_BOLD_RED}down${NORMAL}"
+function cli_port() {
+ if cli_help_requested $@; then
+ cli_show_man network-port
+ exit ${EXIT_OK}
fi
- echo -n "# Link : "
- if device_has_carrier ${port}; then
- echo -e "${CLR_BOLD_GRN}yes${NORMAL}"
+ local action
+ local port
+
+ if port_exists ${1}; then
+ port=${1}
+ action=${2}
+ shift 2
+
+ # Action aliases
+ case "${action}" in
+ start)
+ action="up"
+ ;;
+ stop)
+ action="down"
+ ;;
+ show)
+ action="status"
+ ;;
+ esac
+
+ case "${action}" in
+ edit|up|down|status)
+ port_${action} ${port} $@
+ ;;
+ *)
+ error "Unrecognized argument: ${action}"
+ exit ${EXIT_ERROR}
+ ;;
+ esac
else
- echo -e "${CLR_BOLD_RED}no${NORMAL}"
+ action=${1}
+ shift
+
+ case "${action}" in
+ create|destroy)
+ port_${action} $@
+ ;;
+ *)
+ error "Unrecognized argument: ${action}"
+ exit ${EXIT_ERROR}
+ ;;
+ esac
fi
+}
- if device_is_up ${port}; then
- echo "#"
- echo "# Statistics:"
- echo -n "# RX: $(size $(</sys/class/net/${port}/statistics/rx_bytes))"
- echo " ($(</sys/class/net/${port}/statistics/rx_packets) packets)"
- echo -n "# TX: $(size $(</sys/class/net/${port}/statistics/tx_bytes))"
- echo " ($(</sys/class/net/${port}/statistics/tx_packets) packets)"
+function cli_zone() {
+ if cli_help_requested $@; then
+ cli_show_man network-zone
+ exit ${EXIT_OK}
fi
- echo "#"
+ local action
+ local zone
+
+ if zone_name_is_valid ${1}; then
+ zone=${1}
+ action=${2}
+ shift 2
+
+ # Action aliases
+ case "${action}" in
+ start)
+ action="up"
+ ;;
+ stop)
+ action="down"
+ ;;
+ show)
+ action="status"
+ ;;
+ esac
+
+ case "${action}" in
+ config|down|edit|port|status|up)
+ zone_${action} ${zone} $@
+ ;;
+ *)
+ error "Unrecognized argument: ${action}"
+ cli_show_man network-zone
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+ else
+ action=${1}
+ shift
+
+ case "${action}" in
+ create)
+ zone_${action} $@
+ ;;
+ remove)
+ cli_zone_remove $@
+ ;;
+ list-hooks)
+ cli_list_hooks zone $@
+ ;;
+ ""|*)
+ if [ -n "${action}" ]; then
+ error "Unrecognized argument: '${action}'"
+ echo
+ fi
+
+ cli_show_man network-zone
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+ fi
}
-function port_raw() {
- local port
- if [ $# -eq 0 ]; then
- for port in /sys/class/net/*; do
- port=${port##*/}
- device_is_real ${port} || continue
- port_raw ${port}
- done
- return
+# Removes a zone either immediately, if it is currently down,
+# or adds a tag that the removal will be done when the zone
+# is brought down the next time.
+function cli_zone_remove() {
+ if cli_help_requested $@; then
+ cli_show_man network-zone
+ exit ${EXIT_OK}
fi
- port=$(devicify $1)
+ local zone=${1}
+ assert zone_exists ${zone}
- cat <<EOF
-[${port}]
-type=$(device_type ${port})
-mac=$(macify ${port})
-carrier=$(device_has_carrier ${port} && echo "1" || echo "0")
-up=$(device_is_up ${port} && echo "1" || echo "0")
+ if zone_is_up ${zone}; then
+ echo "Zone '${zone}' is up and will be removed when it goes down the next time."
+ zone_remove ${zone}
+ else
+ echo "Removing zone '${zone}' now..."
+ zone_remove_now ${zone}
+ fi
-EOF
+ exit ${EXIT_OK}
}
-function port_add() {
- local zone=${1}
- local hook=${2}
- shift 2
+function cli_list_hooks() {
+ local type=${1}
+ shift
- if ! zone_exists ${zone}; then
- error "Zone ${BOLD}${zone}${NORMAL} does not exist."
- return 1
+ if cli_help_requested $@; then
+ cli_show_man network-zone
+ exit ${EXIT_OK}
fi
- mkdir -p ${CONFIG_PORTS}/${port} 2>/dev/null
- if hook_exists ${hook}; then
- /lib/network/hooks/${hook} --zone=${zone} add $@
- RET=$?
- if [ "$RET" -eq "0" ]; then
- vecho "Successfully added port to ${BOLD}${zone}${NORMAL}."
- else
- error "Hook ${BOLD}${hook}${NORMAL} exited with $RET."
- return $RET
+ local hook_dir=$(hook_dir ${type})
+ local hook
+
+ for hook in ${hook_dir}/*; do
+ hook=$(basename ${hook})
+ if hook_exists ${type} ${hook}; then
+ echo "${hook}"
fi
- else
- error "Hook ${BOLD}${hook}${NORMAL} does not exist or is not executeable."
- return 1
+ done | sort -u
+}
+
+function cli_route() {
+ if cli_help_requested $@; then
+ cli_show_man network-route
+ exit ${EXIT_OK}
fi
+
+ local action=${1}
+ shift
+
+ case "${action}" in
+ # Add a new route.
+ add)
+ route_add $@
+ ;;
+ # Remove an existing route.
+ remove)
+ route_remove $@
+ ;;
+ # List all routes.
+ list)
+ route_list $@
+ return ${EXIT_OK}
+ ;;
+ *)
+ error "Unrecognized action: ${action}"
+ cli_run_help network route
+
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+
+ # Applying all routes.
+ route_apply
+
+ exit ${EXIT_OK}
}
-function port_del() {
- local config
- local hook
- local uuid
+function cli_dhcpd() {
+ local proto=${1}
+ shift
- local zone=${1}
+ if cli_help_requested $@; then
+ cli_show_man network-dhcp
+ exit ${EXIT_OK}
+ fi
+
+ local action=${1}
shift
- if is_uuid ${1}; then
- uuid=${1}
- config="${CONFIG_UUIDS}/${uuid}"
+ case "${action}" in
+ edit)
+ dhcpd_edit ${proto} $@
+ ;;
+ start)
+ dhcpd_start ${proto}
+ ;;
+ stop)
+ dhcpd_stop ${proto}
+ ;;
+ restart|reload)
+ dhcpd_reload ${proto}
+ ;;
+ subnet)
+ cli_dhcpd_subnet ${proto} $@
+ ;;
+ show|"")
+ cli_dhcpd_show ${proto} $@
+ ;;
+ *)
+ error "Unrecognized action: ${action}"
+ cli_run_help network dhcpvN
+
+ exit ${EXIT_ERROR}
+ ;;
+ esac
- if [ -e "${config}" ]; then
- hook=$(config_get_hook ${config})
- else
- error "Given config file does not exist: ${config}."
- return 1
- fi
+ exit ${EXIT_OK}
+}
+
+function cli_dhcpd_show() {
+ local proto=${1}
+ assert isset proto
+
+ local settings=$(dhcpd_settings ${proto})
+ assert isset settings
+
+ local ${settings}
+ dhcpd_global_settings_read ${proto}
+
+ cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}"
+
+ case "${proto}" in
+ ipv6)
+ cli_headline 2 "Lease times"
+ if isinteger VALID_LIFETIME; then
+ cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s"
+ fi
+
+ if isinteger PREFERRED_LIFETIME; then
+ cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s"
+ fi
+
+ cli_space
+ ;;
+ ipv4)
+ cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE)
+ cli_space
+
+ cli_headline 2 "Lease times"
+ cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s"
+ cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s"
+
+ if isset MIN_LEASE_TIME; then
+ cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s"
+ fi
+
+ cli_space
+ ;;
+ esac
+
+ # Read the options.
+ local -A options
+ dhcpd_global_options_read ${proto} ${subnet_id}
+
+ # Print the options if any.
+ if [ ${#options[*]} -gt 0 ]; then
+ cli_headline 2 "Options"
+
+ local option
+ for option in $(dhcpd_options ${proto}); do
+ [ -n "${options[${option}]}" ] || continue
+
+ cli_print_fmt1 2 \
+ "${option}" "${options[${option}]}"
+ done
+ cli_space
fi
- hook_run --config=${config} pre-down
- hook_run --config=${config} post-down
- hook_run --config=${config} rem
+ # Subnets.
+ local subnets=$(dhcpd_subnet_list ${proto})
+ if [ -n "${subnets}" ]; then
+ cli_headline 2 "Subnets"
+ local subnet_id
+ for subnet_id in ${subnets}; do
+ cli_dhcpd_subnet_show ${proto} ${subnet_id} 2
+ done
+ fi
}
-function zone_discover() {
- local zone=${1}
+function cli_dhcpd_subnet() {
+ local proto=${1}
+ shift
- for hook in $(hook_list zone); do
- hook_run ${hook} --zone=${zone} discover
- done
+ if cli_help_requested $@; then
+ cli_show_man network-dhcp-subnet
+ exit ${EXIT_OK}
+ fi
+
+ local action=${1}
+ shift
+
+ case "${action}" in
+ new)
+ dhcpd_subnet_new ${proto} $@
+ ;;
+ remove)
+ dhcpd_subnet_remove ${proto} $@
+ ;;
+ [0-9]*)
+ local subnet_id=${action}
+
+ if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then
+ error "The given subnet with ID ${subnet_id} does not exist."
+ return ${EXIT_ERROR}
+ fi
+
+ # Update the action.
+ action=${1}
+ shift
+
+ case "${action}" in
+ edit)
+ dhcpd_subnet_edit ${proto} ${subnet_id} $@
+ local ret=$?
+
+ if [ ${ret} -eq ${EXIT_OK} ]; then
+ dhcpd_reload ${proto}
+ fi
+ exit ${ret}
+ ;;
+ range)
+ cli_dhcpd_subnet_range ${proto} ${subnet_id} $@
+ exit $?
+ ;;
+ show)
+ cli_dhcpd_subnet_show ${proto} ${subnet_id} $@
+ exit $?
+ ;;
+ options)
+ cli_dhcpd_subnet_options ${proto} ${subnet_id} $@
+ exit $?
+ ;;
+ *)
+ error "Unrecognized action: ${action}"
+ cli_run_help network dhcpvN subnet
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+ ;;
+ show)
+ local subnet_id
+ for subnet_id in $(dhcpd_subnet_list ${proto}); do
+ cli_dhcpd_subnet_show ${proto} ${subnet_id}
+ done
+ ;;
+ *)
+ error "Unrecognized action: ${action}"
+ cli_run_help network dhcpvN subnet
+
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+
+ exit ${EXIT_OK}
}
-function zone_show() {
- local zone
- zone=$1
+function cli_dhcpd_subnet_range() {
+ local proto=${1}
+ assert isset proto
+ shift
+
+ local subnet_id=${1}
+ assert isset subnet_id
+ shift
+
+ local action=${1}
+ shift
+
+ case "${action}" in
+ new)
+ dhcpd_subnet_range_new ${proto} ${subnet_id} $@
+ exit $?
+ ;;
+ remove)
+ dhcpd_subnet_range_remove ${proto} ${subnet_id} $@
+ exit $?
+ ;;
+ *)
+ error "Unrecognized action: ${action}"
+ cli_run_help network dhcpvN subnet range
+ exit ${EXIT_ERROR}
+ ;;
+ esac
+}
+
+function cli_dhcpd_subnet_show() {
+ local proto=${1}
+ assert isset proto
+
+ local subnet_id=${2}
+ assert isset subnet_id
+
+ local level=${3}
+ isset level || level=0
+
+ local $(dhcpd_subnet_settings ${proto})
- if [ -z "$zone" ]; then
- for zone in ${CONFIG_ZONES}/*; do
- zone_show $(basename $zone)
+ # Read in configuration settings.
+ dhcpd_subnet_read ${proto} ${subnet_id}
+
+ cli_headline $(( ${level} + 1 )) \
+ "DHCP${proto/ip/} subnet declaration #${subnet_id}"
+ cli_print_fmt1 $(( ${level} + 1 )) \
+ "Subnet" "${ADDRESS}/${PREFIX}"
+ cli_space
+
+ # Read the options.
+ local -A options
+ dhcpd_subnet_options_read ${proto} ${subnet_id}
+
+ # Print the options if any.
+ if [ ${#options[*]} -gt 0 ]; then
+ cli_headline $(( ${level} + 2 )) "Options"
+
+ local option
+ for option in $(dhcpd_subnet_options ${proto}); do
+ [ -n "${options[${option}]}" ] || continue
+
+ cli_print_fmt1 $(( ${level} + 2 )) \
+ "${option}" "${options[${option}]}"
done
- return
+ cli_space
fi
- if ! zone_exists ${zone}; then
- error "Zone ${BOLD}${zone}${NORMAL} does not exist."
- return 2
- fi
+ # Ranges.
+ cli_headline $(( ${level} + 2 )) "Ranges"
- echo "##################################################"
- echo "#"
- echo -e "# Zone ${CLR_BOLD_BLU}${zone}${NORMAL}"
- echo "# ------------------------------------------------"
+ local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet_id})
+ if isset ranges; then
+ local range_id $(dhcpd_subnet_range_settings ${proto})
+ for range_id in ${ranges}; do
+ dhcpd_subnet_range_read ${proto} ${subnet_id} ${range_id}
- # Up or down?
- if zone_is_up ${zone}; then
- echo -e "# Status: ${CLR_BOLD_GRN}up${NORMAL}"
+ cli_print $(( ${level} + 2 )) \
+ "#%d: %s - %s" ${range_id} ${START} ${END}
+ done
else
- echo -e "# Status: ${CLR_BOLD_RED}down${NORMAL}"
+ cli_print $(( ${level} + 2 )) "No ranges have been defined."
fi
- echo "#"
- # Ports
- echo -e "# ${CLR_BOLD_BLU}Ports:${NORMAL}"
- hooks_run_ports status ${CONFIG_ZONES}/${zone} --zone=${zone}
-
- echo "#"
- echo -e "# ${CLR_BOLD_BLU}Zone configurations:${NORMAL}"
- hooks_run_zones status ${CONFIG_ZONES}/${zone} --zone=${zone}
- echo "#"
+ cli_space
+}
+
+function cli_dhcpd_options() {
+ local proto=${1}
+ assert isset proto
+ shift
+
+ local subnet_id=${1}
+ assert isset subnet_id
+ shift
+
+ local valid_options=$(dhcpd_subnet_options ${proto})
+
+ local key val
+ while [ $# -gt 0 ]; do
+ case "${1}" in
+ *=*)
+ key=$(cli_get_key ${1})
+ val=$(cli_get_val ${1})
+ dhcpd_subnet_option_set ${proto} ${subnet_id} ${key} ${val}
+ esac
+ done
}
-function zone_raw() {
+function cli_start() {
+ if cli_help_requested $@; then
+ cli_show_man network
+ exit ${EXIT_OK}
+ fi
+
+ local zones=$(zones_get $@)
+
local zone
- if [ $# -eq 0 ]; then
- for zone in $(zone_list); do
- zone_raw ${zone##*/}
- done
- return
+ for zone in ${zones}; do
+ zone_start ${zone} &
+ done
+
+ wait # until everything is settled
+}
+
+function cli_stop() {
+ if cli_help_requested $@; then
+ cli_show_man network
+ exit ${EXIT_OK}
fi
- zone=${1}
+ local zones=$(zones_get $@)
-cat <<EOF
-[${zone}]
-up=$(zone_is_up ${zone} && echo "1" || echo "0")
+ local zone
+ for zone in ${zones}; do
+ zone_stop ${zone} &
+ done
-EOF
+ wait # until everything is settled
}
-function zone_add() {
- local zone=$1
+function cli_restart() {
+ if cli_help_requested $@; then
+ cli_show_man network
+ exit ${EXIT_OK}
+ fi
+
+ cli_stop $@
+
+ # Give the system some time to calm down
+ sleep ${TIMEOUT_RESTART}
- if zone_exists ${zone}; then
- error "Zone ${BOLD}${zone}${NORMAL} already exists."
- return 1
+ cli_start $@
+}
+
+function cli_status() {
+ if cli_help_requested $@; then
+ cli_show_man network
+ exit ${EXIT_OK}
fi
-
- if ! zone_valid_name ${zone}; then
- error "The given zone name is not valid."
- return 1
+
+ # When dumping status information, the debug
+ # mode clutters the console which is not what we want.
+ # Logging on the console is disabled for a short time.
+ local log_disable_stdout=${LOG_DISABLE_STDOUT}
+ LOG_DISABLE_STDOUT="true"
+
+ local zones=$(zones_get $@)
+
+ local zone
+ for zone in ${zones}; do
+ zone_status ${zone}
+ done
+
+ # Reset logging.
+ LOG_DISABLE_STDOUT=${log_disable_stdout}
+}
+
+function cli_reset() {
+ if cli_help_requested $@; then
+ cli_show_man network
+ exit ${EXIT_OK}
fi
- mkdir -p ${CONFIG_ZONES}/${zone}
- vecho "Successfully added zone ${BOLD}${zone}${NORMAL}."
+ warning_log "Will reset the whole network configuration!!!"
+
+ # Force mode is disabled by default
+ local force=0
+
+ while [ $# -gt 0 ]; do
+ case "${1}" in
+ --force|-f)
+ force=1
+ ;;
+ esac
+ shift
+ done
+
+ # If we are not running in force mode, we ask the user if he does know
+ # what he is doing.
+ if ! enabled force; then
+ if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
+ exit ${EXIT_ERROR}
+ fi
+ fi
+
+ local zone
+ for zone in $(zones_get --all); do
+ zone_remove ${zone}
+ done
+
+ local port
+ for port in $(ports_get --all); do
+ port_remove ${port}
+ done
+
+ # Flush all DNS servers.
+ dns_server_flush
+
+ # Re-run the initialization functions
+ init_run
+
+ exit ${EXIT_OK}
}
-function zone_del() {
- local zone=$1
+# Help function: will show the default man page to the user.
+# Optionally, there are two arguments taken, the type of hook
+# and which hook should be shown.
+function cli_help() {
+ local type=${1}
+ local what=${2}
+
+ # Remove unknown types.
+ if ! listmatch ${type} zone port config; then
+ type=""
+ fi
+
+ # If no arguments were given, we will show the default page.
+ if [ -z "${type}" ]; then
+ cli_show_man network
+ return ${EXIT_OK}
+ fi
- if ! zone_exists ${zone}; then
- error "Zone ${BOLD}${zone}${NORMAL} does not exist."
- return 1
+ if ! hook_exists ${type} ${what}; then
+ error "Hook of type '${type}' and name '${what}' could not be found."
+ exit "${EXIT_ERROR}"
fi
- cmd /lib/network/zone --zone=${zone} down
- rm -rf ${CONFIG_ZONES}/${zone}
- vecho "Successfully removed zone ${BOLD}${zone}${NORMAL}."
+ hook_exec ${type} ${what} help
}
-# See what to do
-while [ "$#" -gt 0 ]; do
- arg=$1
- shift
+function cli_dns_server() {
+ if cli_help_requested $@; then
+ cli_show_man network-dns-server
+ exit ${EXIT_OK}
+ fi
- case "$arg" in
- --debug|-d)
- debug 1
- decho "Debug mode enabled."
- ;;
- --verbose|-v)
- verbose 1
- vecho "${BOLD}Verbose mode enabled.${NORMAL}"
- ;;
- help|-h|--help)
- usage main 0
- ;;
- start|stop|reload)
- action=${arg}
- for zone in $(zone_list); do
- zone=${zone##*/}
- decho "Running command: ${HOME_DIR}/zone --zone=${zone} ${action}"
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOME_DIR}/zone --zone=${zone} ${action}
- done
- _exit $?
- ;;
- restart)
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} $0 stop $@
- sleep 1
- DEBUG=${DEBUG} VERBOSE=${VERBOSE} $0 start $@
- _exit $?
- ;;
- hook|hooks)
- case "$1" in
- list)
- hook_list
- _exit $?
- ;;
- *)
- if hook_exists ${1}; then
- hook=${1}
- else
- usage hook
- fi
- esac
- shift
- case "$1" in
- help|info)
- if hook_exists ${hook}; then
- hook_run ${hook} ${1}
- _exit $?
- else
- error "Hook ${hook} does not exist or is not executeable."
- _exit 1
- fi
- ;;
- *)
- usage hook
- ;;
- esac
- ;;
- p*)
- arg=$1
- shift
- case "$arg" in
- help)
- usage port 0
- ;;
- show)
- port_show $@
- _exit $?
- ;;
- _raw)
- port_raw $@
- _exit $?
- ;;
- esac
+ # Get the command.
+ local cmd=${1}; shift
+ if [ -z "${cmd}" ]; then
+ cli_show_man network-dns-server
+ exit ${EXIT_ERROR}
+ fi
+
+ # Get the new server to process (if any).
+ local server=${1}
+ local priority=${2}
+
+ case "${cmd}" in
+ list)
+ dns_server_list
+ exit ${EXIT_OK}
;;
- z*)
- arg=$1
- shift
- case "$arg" in
- add)
- zone_add $@
- _exit --reload $?
- ;;
- addport)
- port_add $@
- _exit --reload $?
- ;;
- config)
- zone=$1; hook=$2; shift 2
- if [ -z "${zone}" ] || [ -z "${hook}" ]; then
- usage config
- fi
- hook_run ${hook} --zone=${zone} add $@
- _exit --reload $?
- ;;
- del)
- zone_del $@
- _exit --reload $?
- ;;
- delport)
- port_del $@
- _exit --reload $?
- ;;
- discover)
- zone_discover $@
- _exit $?
- ;;
- help)
- usage zone 0
- ;;
- list)
- zone_list
- _exit $?
- ;;
- show)
- zone_show $@
- _exit $?
- ;;
- start|stop)
- zone=$1; shift
- zone_run --zone=${zone} ${arg} $@
- ;;
- _raw)
- zone_raw $@
- _exit $?
- ;;
- esac
+ add)
+ if dns_server_exists ${server}; then
+ error "DNS server '${server}' already exists!"
+ exit ${EXIT_ERROR}
+ fi
+
+ log INFO "Adding new DNS server: ${server}"
+ dns_server_add ${server} ${priority}
;;
- show)
- arg=${1}
- shift
- case "${arg}" in
- ports)
- port_show $@
- _exit 0
- ;;
- esac
+ remove)
+ if ! dns_server_exists ${server}; then
+ error "DNS server '${server}' does not exist!"
+ exit ${EXIT_ERROR}
+ fi
+
+ log INFO "Removing DNS server: ${server}"
+ dns_server_remove ${server} ${priority}
;;
- -*)
- error "Option \"$arg\" is not known."
+ update)
+ # Just run the update afterwards.
;;
+ *)
+ error "No such command: ${cmd}"
+ exit ${EXIT_ERROR}
esac
-done
-usage main
+ # Update the local DNS configuration after changes have been made.
+ dns_generate_resolvconf
+ radvd_update
+
+ exit ${EXIT_OK}
+}
+
+# Process the given action
+case "${action}" in
+ init)
+ init_run
+ ;;
+
+ config|hostname|port|device|zone|start|stop|restart|status|reset|route)
+ cli_${action} $@
+ ;;
+
+ # DHCP server configuration (automatically detects which protocol to use).
+ dhcpv6|dhcpv4)
+ cli_dhcpd ${action/dhcp/ip} $@
+ ;;
+
+ # DNS server configuration.
+ dns-server)
+ cli_dns_server $@
+ ;;
+
+ ""|help|--help|-h)
+ cli_help $@
+ ;;
+
+ *)
+ error "Invalid command given: ${action}"
+ cli_usage "network help"
+ exit ${EXIT_CONF_ERROR}
+ ;;
+esac
+
+exit ${EXIT_OK}