]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.hook
Fix creating new configs
[people/stevee/network.git] / src / functions / functions.hook
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
22 hook_dir() {
23 local type=${1}
24
25 if [ -n "${type}" ]; then
26 type="/${type}s"
27 fi
28
29 echo "${NETWORK_HOOKS_DIR}${type}"
30 }
31 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
32
33 hook_exists() {
34 local type=${1}
35 local hook=${2}
36
37 assert isset type
38 assert isset hook
39
40 # Add the path prefix.
41 hook="$(hook_dir ${type})/${hook}"
42
43 [ ! -d "${hook}" ] && [ -x "${hook}" ]
44 }
45
46 hook_exec() {
47 local type="${1}"
48 assert isset type
49
50 local hook="${2}"
51 assert isset hook
52
53 local cmd="${3}"
54 assert isset cmd
55
56 assert hook_exists "${type}" "${hook}"
57 shift 3
58
59 # Complete the hook command by prepending "hook_"
60 local hook_cmd="hook_${cmd}"
61
62 # Check if the hook action is valid.
63 assert hook_valid_command "${type}" "${cmd}"
64
65 local hook_path="$(hook_dir ${type})/${hook}"
66
67 # For performance reasons, all hooks are executed
68 # in a subshell and so will inherit the currently
69 # running environment.
70 (
71 # Set the name of the hook.
72 HOOK=$(basename ${hook})
73
74 # Source the code of the hook.
75 source "${hook_path}"
76
77 # Make sure HOOK is still properly set.
78 assert isset HOOK
79
80 # Execute the requested command.
81 "${hook_cmd}" "$@"
82 )
83 local ret=$?
84
85 case "${ret}" in
86 ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED})
87 log ERROR "Hook '${hook}' does not implement the method '${cmd}':"
88 log ERROR " arguments: $@"
89 return ${EXIT_COMMAND_NOT_FOUND}
90 ;;
91 ${EXIT_ERROR_ASSERT})
92 log ERROR "Hook exited with an assertion error."
93 return ${EXIT_ERROR_ASSERT}
94 ;;
95 esac
96
97 return ${ret}
98 }
99
100 hook_list() {
101 local type="${1}"
102
103 local dir="$(hook_dir "${type}")"
104 assert isset dir
105
106 local hook
107 for hook in $(list_directory "${dir}"); do
108 if hook_exists "${type}" "${hook}"; then
109 echo "${hook}"
110 fi
111 done
112 }
113
114 # The default help function.
115 hook_help() {
116 # If no man page has been configured, we print an error message.
117 if [ -z "${HOOK_MANPAGE}" ]; then
118 error "There is no help available for hook '${HOOK}'. Exiting."
119 exit ${EXIT_ERROR}
120 fi
121
122 cli_show_man "${HOOK_MANPAGE}"
123
124 exit $?
125 }
126
127 # Dummy functions being overlayed by hooks
128 hook_check_settings() {
129 :
130 }
131
132 hook_check_config_settings() {
133 :
134 }
135
136 hook_check_port_settings() {
137 :
138 }
139
140 # Sets all settings in HOOK_SETTINGS to their DEFAULT_* values
141 hook_set_defaults() {
142 local setting
143 for setting in ${HOOK_SETTINGS[*]}; do
144 local default="DEFAULT_${setting}"
145
146 # Sets the default or empty
147 assign "${setting}" "${!default}"
148 done
149 }
150
151 config_get_hook() {
152 local config=${1}
153
154 assert isset config
155 assert [ -e "${config}" ]
156
157 (
158 . ${config}
159 echo "${HOOK}"
160 )
161 }
162
163 hook_zone_exists() {
164 hook_exists zone "$@"
165 }
166
167 hook_zone_exec() {
168 hook_exec zone "$@"
169 }
170
171 hook_zone_get_all() {
172 hook_list zone
173 }
174
175 hook_config_exists() {
176 hook_exists config "$@"
177 }
178
179 hook_config_exec() {
180 hook_exec config "$@"
181 }
182
183 hook_config_get_all() {
184 hook_list config
185 }
186
187 hook_valid_command() {
188 local type="${1}"
189 local cmd="${2}"
190
191 case "${type}" in
192 config)
193 hook_valid_command_config "${cmd}"
194 return ${?}
195 ;;
196 port)
197 hook_valid_command_port "${cmd}"
198 return ${?}
199 ;;
200 zone)
201 hook_valid_command_zone "${cmd}"
202 return ${?}
203 ;;
204 esac
205
206 return ${EXIT_FALSE}
207 }
208
209 hook_valid_command_config() {
210 local cmd="${1}"
211
212 case "${cmd}" in
213 new|destroy|edit|up|down|status|hid)
214 return ${EXIT_TRUE}
215 ;;
216 esac
217
218 return ${EXIT_FALSE}
219 }
220
221 hook_valid_command_port() {
222 local cmd="${1}"
223
224 case "${cmd}" in
225 # Configuration hooks
226 new|edit|destroy)
227 return ${EXIT_TRUE}
228 ;;
229
230 # Control hooks
231 create|remove|up|down)
232 return ${EXIT_TRUE}
233 ;;
234
235 # Hotplug
236 hotplug|hotplug_rename)
237 return ${EXIT_TRUE}
238 ;;
239
240 # Status
241 status|children)
242 return ${EXIT_TRUE}
243 ;;
244 esac
245
246 return ${EXIT_FALSE}
247 }
248
249 hook_valid_command_zone() {
250 local cmd="${1}"
251
252 case "${cmd}" in
253 # Configuration hooks
254 new|edit|destroy)
255 return ${EXIT_TRUE}
256 ;;
257
258 config_new|config_destroy|config_edit|config_show)
259 return ${EXIT_TRUE}
260 ;;
261
262 # Control hooks
263 up|down)
264 return ${EXIT_TRUE}
265 ;;
266
267 # Hotplug
268 hotplug)
269 return ${EXIT_TRUE}
270 ;;
271
272 # Ports
273 port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down)
274 return ${EXIT_TRUE}
275 ;;
276
277 # Status
278 status|info|help)
279 return ${EXIT_TRUE}
280 ;;
281
282 # Discovery
283 discover)
284 return ${EXIT_TRUE}
285 ;;
286
287 # PPP
288 ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config)
289 return ${EXIT_TRUE}
290 ;;
291 esac
292
293 return ${EXIT_FALSE}
294 }