#!/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 . # # # ############################################################################### hook_dir() { local type=${1} if [ -n "${type}" ]; then type="/${type}s" fi echo "${NETWORK_HOOKS_DIR}${type}" } NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)" hook_exists() { local type=${1} local hook=${2} assert isset type assert isset hook # Add the path prefix. hook="$(hook_dir ${type})/${hook}" [ ! -d "${hook}" ] && [ -x "${hook}" ] } hook_exec() { local type="${1}" assert isset type local hook="${2}" assert isset hook local cmd="${3}" assert isset cmd assert hook_exists "${type}" "${hook}" shift 3 # Complete the hook command by prepending "hook_" local hook_cmd="hook_${cmd}" # Check if the hook action is valid. assert hook_valid_command "${type}" "${cmd}" local hook_path="$(hook_dir ${type})/${hook}" # For performance reasons, all hooks are executed # in a subshell and so will inherit the currently # running environment. ( # Set the name of the hook. HOOK=$(basename ${hook}) # Source the code of the hook. source "${hook_path}" # Make sure HOOK is still properly set. assert isset HOOK # Execute the requested command. "${hook_cmd}" "$@" ) local ret=$? case "${ret}" in ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED}) log ERROR "Hook '${hook}' does not implement the method '${cmd}':" log ERROR " arguments: $@" return ${EXIT_COMMAND_NOT_FOUND} ;; ${EXIT_ERROR_ASSERT}) log ERROR "Hook exited with an assertion error." return ${EXIT_ERROR_ASSERT} ;; esac return ${ret} } hook_list() { local type="${1}" local dir="$(hook_dir "${type}")" assert isset dir local hook for hook in $(list_directory "${dir}"); do if hook_exists "${type}" "${hook}"; then echo "${hook}" fi done } # The default help function. hook_help() { # If no man page has been configured, we print an error message. if [ -z "${HOOK_MANPAGE}" ]; then error "There is no help available for hook '${HOOK}'. Exiting." exit ${EXIT_ERROR} fi cli_show_man "${HOOK_MANPAGE}" exit $? } config_get_hook() { local config=${1} assert isset config assert [ -e "${config}" ] ( . ${config} echo "${HOOK}" ) } hook_zone_exists() { hook_exists zone "$@" } hook_zone_exec() { hook_exec zone "$@" } hook_zone_get_all() { hook_list zone } hook_config_exists() { hook_exists config "$@" } hook_config_exec() { hook_exec config "$@" } hook_config_get_all() { hook_list config } hook_valid_command() { local type="${1}" local cmd="${2}" case "${type}" in config) hook_valid_command_config "${cmd}" return ${?} ;; port) hook_valid_command_port "${cmd}" return ${?} ;; zone) hook_valid_command_zone "${cmd}" return ${?} ;; esac return ${EXIT_FALSE} } hook_valid_command_config() { local cmd="${1}" case "${cmd}" in new|destroy|edit|up|down|status|hid) return ${EXIT_TRUE} ;; esac return ${EXIT_FALSE} } hook_valid_command_port() { local cmd="${1}" case "${cmd}" in # Configuration hooks new|edit|destroy) return ${EXIT_TRUE} ;; # Control hooks create|remove|up|down) return ${EXIT_TRUE} ;; # Hotplug hotplug|hotplug_rename) return ${EXIT_TRUE} ;; # Status status|children) return ${EXIT_TRUE} ;; esac return ${EXIT_FALSE} } hook_valid_command_zone() { local cmd="${1}" case "${cmd}" in # Configuration hooks new|edit|destroy) return ${EXIT_TRUE} ;; config_new|config_destroy|config_edit|config_show) return ${EXIT_TRUE} ;; # Control hooks up|down) return ${EXIT_TRUE} ;; # Hotplug hotplug) return ${EXIT_TRUE} ;; # Ports port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down) return ${EXIT_TRUE} ;; # Status status|info|help) return ${EXIT_TRUE} ;; # Discovery discover) return ${EXIT_TRUE} ;; # PPP ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config) return ${EXIT_TRUE} ;; esac return ${EXIT_FALSE} }