ipv4-static: Update hook
[people/stevee/network.git] / src / functions / functions.hook
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
ccbc0dd4
MT
22HOOK_COMMANDS_CONFIG="hook_create hook_down hook_edit hook_status hook_remove"
23HOOK_COMMANDS_CONFIG="${HOOK_COMMANDS_CONFIG} hook_up"
2181765d 24
08c5b789
MT
25HOOK_COMMANDS_PORT="hook_create hook_down hook_edit hook_hotplug \
26 hook_hotplug_rename hook_info hook_status hook_up"
2181765d 27
2c2aa587 28HOOK_COMMANDS_ZONE="hook_add hook_create hook_discover hook_down hook_edit hook_help \
2181765d
MT
29 hook_info hook_remove hook_status hook_up \
30 \
31 hook_config_create hook_config_edit hook_config_remove hook_config_show \
32 \
33 hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
34 hook_port_status hook_port_up hook_port_down \
35 \
36 hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
37 hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
38
d61a01d4
MT
39function hook_dir() {
40 local type=${1}
41
943e3f7e
MT
42 if [ -n "${type}" ]; then
43 type="/${type}s"
44 fi
45
d2a21d01 46 echo "${NETWORK_HOOKS_DIR}${type}"
d61a01d4 47}
f41fa3d7 48NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
d61a01d4 49
1848564d 50function hook_exists() {
d61a01d4
MT
51 local type=${1}
52 local hook=${2}
1848564d 53
943e3f7e
MT
54 assert isset type
55 assert isset hook
56
f41fa3d7
MT
57 # Add the path prefix.
58 hook="$(hook_dir ${type})/${hook}"
1848564d 59
f41fa3d7 60 [ ! -d "${hook}" ] && [ -x "${hook}" ]
1848564d
MT
61}
62
d61a01d4 63function hook_exec() {
2181765d 64 local type="${1}"
943e3f7e 65 assert isset type
2181765d
MT
66
67 local hook="${2}"
943e3f7e 68 assert isset hook
2181765d
MT
69
70 local cmd="${3}"
f41fa3d7
MT
71 assert isset cmd
72
73 assert hook_exists "${type}" "${hook}"
2181765d
MT
74 shift 3
75
76 # Complete the hook command by prepending "hook_"
77 local hook_cmd="hook_${cmd}"
78
79 # Check if the hook action is valid.
80 local valid_commands
81 case "${type}" in
82 "config")
83 valid_commands="${HOOK_COMMANDS_CONFIG}"
84 ;;
85 "port")
86 valid_commands="${HOOK_COMMANDS_PORT}"
87 ;;
88 "zone")
89 valid_commands="${HOOK_COMMANDS_ZONE}"
90 ;;
91 esac
92 isset valid_commands && assert list_match "${hook_cmd}" ${valid_commands}
93
94 local hook_path="$(hook_dir ${type})/${hook}"
f41fa3d7
MT
95
96 # For performance reasons, all hooks are executed
97 # in a subshell and so will inherit the currently
98 # running environment.
99 (
100 # Set the name of the hook.
101 HOOK=$(basename ${hook})
102
103 # Source the code of the hook.
2181765d 104 source "${hook_path}"
f41fa3d7
MT
105
106 # Make sure HOOK is still properly set.
107 assert isset HOOK
943e3f7e 108
f41fa3d7 109 # Execute the requested command.
2181765d 110 cmd "${hook_cmd}" "$@"
f41fa3d7
MT
111 )
112 local ret=$?
113
2181765d 114 case "${ret}" in
828eb94a
MT
115 ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED})
116 log ERROR "Hook '${hook}' does not implement the method '${cmd}':"
117 log ERROR " arguments: $@"
2181765d
MT
118 exit ${EXIT_COMMAND_NOT_FOUND}
119 ;;
120 ${EXIT_ERROR_ASSERT})
121 log ERROR "Hook exited with an assertion error."
122 exit ${EXIT_ERROR_ASSERT}
123 ;;
124 esac
d61a01d4 125
f41fa3d7 126 return ${ret}
d61a01d4
MT
127}
128
ea699552
MT
129function hook_list() {
130 local type="${1}"
131
132 local dir="$(hook_dir "${type}")"
133 assert isset dir
134
135 local hook
136 for hook in ${dir}/*; do
137 hook="$(basename "${hook}")"
138
139 if hook_exists "${type}" "${hook}"; then
140 echo "${hook}"
141 fi
142 done
143}
144
ccbc0dd4
MT
145# The default help function.
146function hook_help() {
147 # If no man page has been configured, we print an error message.
148 if [ -z "${HOOK_MANPAGE}" ]; then
149 error "There is no help available for hook '${HOOK}'. Exiting."
150 exit ${EXIT_ERROR}
151 fi
152
153 cli_show_man "${HOOK_MANPAGE}"
154
155 exit $?
156}
157
d61a01d4
MT
158function config_get_hook() {
159 local config=${1}
160
943e3f7e 161 assert isset config
a5ebb169 162 assert [ -e "${config}" ]
943e3f7e 163
d61a01d4
MT
164 (
165 . ${config}
166 echo "${HOOK}"
167 )
168}
169
d61a01d4
MT
170function hook_zone_exists() {
171 hook_exists zone $@
172}
173
d61a01d4
MT
174function hook_zone_exec() {
175 hook_exec zone $@
1848564d
MT
176}
177
d61a01d4 178function hook_zone_get_all() {
ea699552 179 hook_list zone
1848564d
MT
180}
181
ea699552
MT
182function hook_config_exists() {
183 hook_exists config $@
184}
a5ebb169 185
ea699552
MT
186function hook_config_exec() {
187 hook_exec config $@
188}
a5ebb169 189
ea699552
MT
190function hook_config_get_all() {
191 hook_list config
1848564d 192}