]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.hook
Use autotools.
[people/ms/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
2181765d
MT
22HOOK_COMMANDS_CONFIG="hook_create hook_down hook_status hook_up"
23
24HOOK_COMMANDS_PORT="hook_create hook_down hook_hotplug hook_hotplug_rename \
25 hook_info hook_status hook_up"
26
2c2aa587 27HOOK_COMMANDS_ZONE="hook_add hook_create hook_discover hook_down hook_edit hook_help \
2181765d
MT
28 hook_info hook_remove hook_status hook_up \
29 \
30 hook_config_create hook_config_edit hook_config_remove hook_config_show \
31 \
32 hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
33 hook_port_status hook_port_up hook_port_down \
34 \
35 hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
36 hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
37
d61a01d4
MT
38function hook_dir() {
39 local type=${1}
40
943e3f7e
MT
41 if [ -n "${type}" ]; then
42 type="/${type}s"
43 fi
44
d2a21d01 45 echo "${NETWORK_HOOKS_DIR}${type}"
d61a01d4 46}
f41fa3d7 47NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
d61a01d4 48
1848564d 49function hook_exists() {
d61a01d4
MT
50 local type=${1}
51 local hook=${2}
1848564d 52
943e3f7e
MT
53 assert isset type
54 assert isset hook
55
f41fa3d7
MT
56 # Add the path prefix.
57 hook="$(hook_dir ${type})/${hook}"
1848564d 58
f41fa3d7 59 [ ! -d "${hook}" ] && [ -x "${hook}" ]
1848564d
MT
60}
61
d61a01d4 62function hook_exec() {
2181765d 63 local type="${1}"
943e3f7e 64 assert isset type
2181765d
MT
65
66 local hook="${2}"
943e3f7e 67 assert isset hook
2181765d
MT
68
69 local cmd="${3}"
f41fa3d7
MT
70 assert isset cmd
71
72 assert hook_exists "${type}" "${hook}"
2181765d
MT
73 shift 3
74
75 # Complete the hook command by prepending "hook_"
76 local hook_cmd="hook_${cmd}"
77
78 # Check if the hook action is valid.
79 local valid_commands
80 case "${type}" in
81 "config")
82 valid_commands="${HOOK_COMMANDS_CONFIG}"
83 ;;
84 "port")
85 valid_commands="${HOOK_COMMANDS_PORT}"
86 ;;
87 "zone")
88 valid_commands="${HOOK_COMMANDS_ZONE}"
89 ;;
90 esac
91 isset valid_commands && assert list_match "${hook_cmd}" ${valid_commands}
92
93 local hook_path="$(hook_dir ${type})/${hook}"
f41fa3d7
MT
94
95 # For performance reasons, all hooks are executed
96 # in a subshell and so will inherit the currently
97 # running environment.
98 (
99 # Set the name of the hook.
100 HOOK=$(basename ${hook})
101
102 # Source the code of the hook.
2181765d 103 source "${hook_path}"
f41fa3d7
MT
104
105 # Make sure HOOK is still properly set.
106 assert isset HOOK
943e3f7e 107
f41fa3d7 108 # Execute the requested command.
2181765d 109 cmd "${hook_cmd}" "$@"
f41fa3d7
MT
110 )
111 local ret=$?
112
2181765d
MT
113 case "${ret}" in
114 ${EXIT_COMMAND_NOT_FOUND})
115 log ERROR "Hook command not implemented: ${hook_command} ($@)"
116 exit ${EXIT_COMMAND_NOT_FOUND}
117 ;;
118 ${EXIT_ERROR_ASSERT})
119 log ERROR "Hook exited with an assertion error."
120 exit ${EXIT_ERROR_ASSERT}
121 ;;
122 esac
d61a01d4 123
f41fa3d7 124 return ${ret}
d61a01d4
MT
125}
126
127function config_get_hook() {
128 local config=${1}
129
943e3f7e 130 assert isset config
a5ebb169 131 assert [ -e "${config}" ]
943e3f7e 132
d61a01d4
MT
133 (
134 . ${config}
135 echo "${HOOK}"
136 )
137}
138
d61a01d4
MT
139function hook_zone_exists() {
140 hook_exists zone $@
141}
142
143function hook_zone_port_exists() {
1848564d
MT
144 local hook_zone=${1}
145 local hook_port=${2}
146
f41fa3d7 147 hook_exists zone "${hook_zone}.ports/${hook_port}"
1848564d
MT
148}
149
d61a01d4 150function hook_zone_config_exists() {
1848564d
MT
151 local hook_zone=${1}
152 local hook_config=${2}
153
f41fa3d7 154 hook_exists zone "${hook_zone}.configs/${hook_config}"
1848564d
MT
155}
156
d61a01d4 157function hook_zone_has_ports() {
1848564d
MT
158 local hook=${1}
159
f41fa3d7 160 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.ports" ]
711ffac1
MT
161}
162
d61a01d4 163function hook_zone_has_configs() {
1848564d
MT
164 local hook=${1}
165
f41fa3d7 166 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ]
1848564d
MT
167}
168
d61a01d4
MT
169function hook_zone_exec() {
170 hook_exec zone $@
1848564d
MT
171}
172
d61a01d4 173function hook_zone_port_exec() {
1848564d
MT
174 local hook_zone=${1}
175 local hook_port=${2}
176 shift 2
177
f41fa3d7 178 hook_zone_exec "${hook_zone}.ports/${hook_port}" $@
1848564d
MT
179}
180
d61a01d4 181function hook_zone_config_exec() {
1848564d 182 local hook_zone=${1}
f41fa3d7 183 local hook_port=${2}
1848564d
MT
184 shift 2
185
f41fa3d7 186 hook_zone_exec "${hook_zone}.configs/${hook_port}" $@
1848564d
MT
187}
188
d61a01d4 189function hook_zone_get_all() {
1848564d
MT
190 local type=${1}
191
192 local hook
d61a01d4 193 for hook in $(hook_dir zone)/*; do
1848564d 194 hook=$(basename ${hook})
d61a01d4 195 hook_zone_exists ${hook} && echo "${hook}"
03170817 196 done
1848564d
MT
197}
198
d61a01d4 199function hook_zone_ports_get_all() {
1848564d
MT
200 local hook=${1}
201
d61a01d4 202 if ! hook_exists zone ${hook}; then
1848564d
MT
203 error "Hook '${hook}' does not exist."
204 return ${EXIT_ERROR}
205 fi
206
a5ebb169
MT
207 # If the zone hook has got no ports we exit silently
208 if ! hook_zone_has_ports ${hook}; then
209 return ${EXIT_OK}
210 fi
211
711ffac1
MT
212 local h
213 for h in $(hook_dir zone)/${hook}.ports/*; do
214 h=$(basename ${h})
a5ebb169
MT
215 if hook_zone_port_exists ${hook} ${h}; then
216 echo "${h}"
217 fi
218 done
219}
220
221function hook_zone_configs_get_all() {
222 local hook=${1}
223
224 if ! hook_exists zone ${hook}; then
225 error "Hook '${hook}' does not exist."
226 return ${EXIT_ERROR}
227 fi
228
229 # If the zone hook has got no configurations we exit silently
230 if ! hook_zone_has_configs ${hook}; then
231 return ${EXIT_OK}
232 fi
233
234 local h
235 for h in $(hook_dir zone)/${hook}.configs/*; do
236 h=$(basename ${h})
237 if hook_zone_config_exists ${hook} ${h}; then
238 echo "${h}"
239 fi
03170817 240 done
a5ebb169
MT
241
242 return ${EXIT_OK}
1848564d 243}