#!/bin/bash
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2009 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 #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
###############################################################################
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] ... or"
echo " $0 [global flags] "
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 "
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 "
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 ..."
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 ..."
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
esac
_exit ${2-1}
}
function debug() {
if [ -n "$1" ]; then
DEBUG=$1
verbose $1
return
else
if [ "$DEBUG" = "1" ]; then
return 0
else
return 1
fi
fi
}
function verbose() {
if [ -n "$1" ]; then
VERBOSE=$1
return
else
if [ "$VERBOSE" = "1" ]; then
return 0
else
return 1
fi
fi
}
function decho() {
debug && echo -e "${ERROR}$@${NORMAL}" >&2
}
function vecho() {
verbose && echo -e "$@" >&2
}
function error() {
echo -e "${ERROR}ERROR${NORMAL}: $@" >&2
_exit 1
}
function _exit() {
local code
local reload
while [ $# -gt 0 ]; do
case "$1" in
--reload)
reload=1
;;
[0-9]*)
code=$1
;;
*)
error "Unrecognized argument: $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
fi
fi
decho "Exiting with code ${code}."
exit ${code}
}
function cmd() {
decho "Running command: $@"
if debug; then
DEBUG=${DEBUG} VERBOSE=${VERBOSE} $@
else
DEBUG=${DEBUG} VERBOSE=${VERBOSE} $@ >/dev/null
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
fi
size=$((${size} / 1024))
count=$((${count} - 1))
done
printf "%4d %s\n" "${size}" "${units[$((${#units} - ${count}))]}"
}
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
fi
port=$(devicify $1)
echo "##################################################"
echo "#"
echo -e "# Port ${CLR_BOLD_BLU}${port}${NORMAL}"
echo "# ------------------------------------------------"
echo -n "# State: "
if device_is_up ${port}; then
echo -e "${CLR_BOLD_GRN}up${NORMAL}"
else
echo -e "${CLR_BOLD_RED}down${NORMAL}"
fi
echo -n "# Link : "
if device_has_carrier ${port}; then
echo -e "${CLR_BOLD_GRN}yes${NORMAL}"
else
echo -e "${CLR_BOLD_RED}no${NORMAL}"
fi
if device_is_up ${port}; then
echo "#"
echo "# Statistics:"
echo -n "# RX: $(size $(/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
fi
else
error "Hook ${BOLD}${hook}${NORMAL} does not exist or is not executeable."
return 1
fi
}
function port_del() {
local config
local hook
local uuid
local zone=${1}
shift
if is_uuid ${1}; then
uuid=${1}
config="${CONFIG_UUIDS}/${uuid}"
if [ -e "${config}" ]; then
hook=$(config_get_hook ${config})
else
error "Given config file does not exist: ${config}."
return 1
fi
fi
hook_run --config=${config} pre-down
hook_run --config=${config} post-down
hook_run --config=${config} rem
}
function zone_discover() {
local zone=${1}
for hook in $(hook_list zone); do
hook_run ${hook} --zone=${zone} discover
done
}
function zone_show() {
local zone
zone=$1
if [ -z "$zone" ]; then
for zone in ${CONFIG_ZONES}/*; do
zone_show $(basename $zone)
done
return
fi
if ! zone_exists ${zone}; then
error "Zone ${BOLD}${zone}${NORMAL} does not exist."
return 2
fi
echo "##################################################"
echo "#"
echo -e "# Zone ${CLR_BOLD_BLU}${zone}${NORMAL}"
echo "# ------------------------------------------------"
# Up or down?
if zone_is_up ${zone}; then
echo -e "# Status: ${CLR_BOLD_GRN}up${NORMAL}"
else
echo -e "# Status: ${CLR_BOLD_RED}down${NORMAL}"
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 "#"
}
function zone_raw() {
local zone
if [ $# -eq 0 ]; then
for zone in $(zone_list); do
zone_raw ${zone##*/}
done
return
fi
zone=${1}
cat <