#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# 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        #
# 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 <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

. /usr/lib/network/header-config

HOOK_MANPAGE="network-config-ipv4-static"

HOOK_CONFIG_SETTINGS="HOOK ADDRESS PREFIX GATEWAY"

hook_check_config_settings() {
	assert isset ADDRESS
	assert isinteger PREFIX

	if [ ${PREFIX} -gt 30 ]; then
		error "PREFIX is greater than 30."
		exit ${EXIT_ERROR}
	fi
}

hook_new() {
	local zone="${1}"
	assert zone_exists "${zone}"
	shift

	local arg
	while read -r arg; do
		local key="$(cli_get_key "${arg}")"
		local val="$(cli_get_val "${arg}")"

		case "${key}" in
			address)
				if ! ipv4_is_valid "${val}"; then
					error "Invalid IPv4 address: ${val}"
					exit ${EXIT_CONF_ERROR}
				fi

				ADDRESS="${val}"
				;;

			prefix)
				if ! ipv4_prefix_is_valid "${val}"; then
					error "Invalid IPv4 prefix: ${val}"
					exit ${EXIT_CONF_ERROR}
				fi

				PREFIX="${val}"
				;;

			gateway)
				if ! ipv4_is_valid "${val}"; then
					error "Invalid IPv4 address for gateway: ${val}"
					exit ${EXIT_CONF_ERROR}
				fi

				GATEWAY="${val}"
				;;

			# Compatibility switches
			netmask)
				if ! ipv4_netmask_is_valid "${val}"; then
					error "Invalid netmask: ${val}"
					exit ${EXIT_CONF_ERROR}
				fi

				# The netmask will be converted into a prefix
				PREFIX="$(ipv4_netmask2prefix ${val})"
				;;

			# Unknown switches
			*)
				error "Unhandled argument: ${arg}"
				exit ${EXIT_CONF_ERROR}
				;;
		esac
	done <<< "$(args $@)"

	if ! isset ADDRESS; then
		error "You need to provide an IPv4 address"
		exit ${EXIT_CONF_ERROR}
	fi

	if ! isset PREFIX; then
		error "You need to provide an IPv4 prefix"
		exit ${EXIT_CONF_ERROR}
	fi

	if ! isset GATEWAY && zone_is_nonlocal "${zone}"; then
		warning "You did not configure a gateway for a non-local zone"
	fi

	# XXX maybe we can add some hashing to identify a configuration again
	zone_config_settings_write "${zone}" "${HOOK}.$(uuid)"

	exit ${EXIT_OK}
}

hook_up() {
	local zone=${1}
	local config=${2}
	shift 2

	if ! device_exists ${zone}; then
		error "Zone '${zone}' doesn't exist."
		exit ${EXIT_ERROR}
	fi

	zone_config_settings_read "${zone}" "${config}"

	ip_address_add ${zone} ${ADDRESS}/${PREFIX}

	# Save configuration
	db_set "${zone}/ipv4/type" "${HOOK}"
	db_set "${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}"
	db_set "${zone}/ipv4/remote-ip-address" "${GATEWAY}"
	db_set "${zone}/ipv4/active" 1

	routing_update ${zone} ipv4
	routing_default_update

	exit ${EXIT_OK}
}

hook_down() {
	local zone=${1}
	local config=${2}
	shift 2

	if ! device_exists ${zone}; then
		error "Zone '${zone}' doesn't exist."
		exit ${EXIT_ERROR}
	fi
	
	zone_config_settings_read "${zone}" "${config}"

	ip_address_del ${zone} ${ADDRESS}/${PREFIX}

	# Update routing tables.
	routing_default_update

	exit ${EXIT_OK}
}

hook_status() {
	local zone="${1}"
	assert isset zone

	local config="${2}"
	assert isset config

	shift 2

	if ! device_exists ${zone}; then
		error "Zone '${zone}' doesn't exist."
		exit ${EXIT_ERROR}
	fi

	zone_config_settings_read "${zone}" "${config}"

	local status
	if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
		status=${MSG_HOOK_UP}
	else
		status=${MSG_HOOK_DOWN}
	fi
	cli_statusline 3 "${HOOK}" "${status}"

	cli_print_fmt1 3 "IPv4 address" "${ADDRESS}/${PREFIX}"
	if [ -n "${GATEWAY}" ]; then
		cli_print_fmt1 3 "Gateway" "${GATEWAY}"
	fi
	cli_space

	exit ${EXIT_OK}
}
