]>
Commit | Line | Data |
---|---|---|
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 | function hook_info() { | |
23 | echo "HOOK=\"${HOOK}\"" | |
24 | } | |
25 | ||
26 | function hook_create() { | |
27 | local zone="${1}" | |
28 | assert isset zone | |
29 | shift | |
30 | ||
31 | config_read $(zone_dir ${zone})/settings | |
32 | ||
33 | hook_parse_cmdline $@ | |
34 | ||
35 | config_write $(zone_dir ${zone})/settings ${HOOK_SETTINGS} | |
36 | ||
37 | exit ${EXIT_OK} | |
38 | } | |
39 | ||
40 | function hook_edit() { | |
41 | hook_create $@ | |
42 | } | |
43 | ||
44 | function hook_remove() { | |
45 | cmd_not_implemented | |
46 | } | |
47 | ||
48 | function hook_status() { | |
49 | local zone="${1}" | |
50 | assert isset zone | |
51 | ||
52 | if device_is_up ${zone}; then | |
53 | exit ${STATUS_UP} | |
54 | fi | |
55 | ||
56 | exit ${STATUS_DOWN} | |
57 | } | |
58 | ||
59 | function hook_up() { | |
60 | cmd_not_implemented | |
61 | } | |
62 | ||
63 | function hook_down() { | |
64 | cmd_not_implemented | |
65 | } | |
66 | ||
67 | function hook_discover() { | |
68 | # This hook does not support a discovery | |
69 | exit ${DISCOVER_NOT_SUPPORTED} | |
70 | } | |
71 | ||
72 | # The default help function. | |
73 | function hook_help() { | |
74 | # If no man page has been configured, we print an error message. | |
75 | if [ -z "${HOOK_MANPAGE}" ]; then | |
76 | error "There is no help available for hook '${HOOK}'. Exiting." | |
77 | exit ${EXIT_ERROR} | |
78 | fi | |
79 | ||
80 | cli_show_man ${HOOK_MANPAGE} | |
81 | } | |
82 | ||
83 | # Do nothing | |
84 | function _parse_cmdline() { | |
85 | return ${EXIT_OK} | |
86 | } | |
87 | ||
88 | function hook_port() { | |
89 | local zone="${1}" | |
90 | assert isset zone | |
91 | ||
92 | local action="${2}" | |
93 | shift 2 | |
94 | ||
95 | local ret | |
96 | case "${action}" in | |
97 | add|create|edit|rem|show) | |
98 | hook_port_${action} "${zone}" $@ | |
99 | ret=$? | |
100 | ;; | |
101 | *) | |
102 | error "Unrecognized argument: '${action}'" | |
103 | exit ${EXIT_ERROR} | |
104 | ;; | |
105 | esac | |
106 | ||
107 | exit ${ret} | |
108 | } | |
109 | ||
110 | function hook_port_add() { | |
111 | hook_port_cmd add "$@" | |
112 | } | |
113 | ||
114 | function hook_port_edit() { | |
115 | _port_cmd edit "$@" | |
116 | } | |
117 | ||
118 | function hook_port_rem() { | |
119 | hook_port_cmd remove "$@" | |
120 | } | |
121 | ||
122 | function hook_port_show() { | |
123 | cmd_not_implemented | |
124 | } | |
125 | ||
126 | function hook_port_status() { | |
127 | hook_port_cmd status "$@" | |
128 | } | |
129 | ||
130 | function hook_port_cmd() { | |
131 | local cmd="${1}" | |
132 | assert isset cmd | |
133 | ||
134 | local zone="${2}" | |
135 | assert isset zone | |
136 | ||
137 | local port="${3}" | |
138 | assert isset port | |
139 | ||
140 | shift 3 | |
141 | ||
142 | local hook_zone="$(zone_get_hook ${zone})" | |
143 | assert isset hook_zone | |
144 | ||
145 | local hook_port="$(port_get_hook ${port})" | |
146 | assert isset hook_port | |
147 | ||
148 | if ! listmatch "${hook_port}" $(zone_get_supported_port_hooks ${zone}); then | |
149 | log ERROR "Zone '${zone}' does not support port of type '${hook_port}'." | |
150 | exit ${EXIT_ERROR} | |
151 | fi | |
152 | ||
153 | hook_zone_port_exec "${hook_zone}" "${hook_port}" "${cmd}" "${zone}" "${port}" "$@" | |
154 | exit $? | |
155 | } | |
156 | ||
157 | function hook_port_up() { | |
158 | hook_port_cmd up "$@" | |
159 | } | |
160 | ||
161 | function hook_port_down() { | |
162 | hook_port_cmd down "$@" | |
163 | } | |
164 | ||
165 | function hook_config() { | |
166 | local zone="${1}" | |
167 | assert isset zone | |
168 | ||
169 | local action="${2}" | |
170 | assert isset action | |
171 | shift 2 | |
172 | ||
173 | local ret | |
174 | case "${action}" in | |
175 | create|edit|rem|show) | |
176 | hook_config_${action} "${zone}" "$@" | |
177 | exit $? | |
178 | ;; | |
179 | *) | |
180 | error "Unrecognized argument: '${action}'" | |
181 | exit ${EXIT_ERROR} | |
182 | ;; | |
183 | esac | |
184 | } | |
185 | ||
186 | function hook_config_cmd() { | |
187 | local cmd="${1}" | |
188 | assert isset cmd | |
189 | ||
190 | local zone="${2}" | |
191 | assert isset zone | |
192 | ||
193 | local hook_config="${3}" | |
194 | assert isset hook_config | |
195 | ||
196 | shift 3 | |
197 | ||
198 | local hook_zone="$(zone_get_hook "${zone}")" | |
199 | if ! hook_zone_exists "${hook_zone}"; then | |
200 | log ERROR "Hook '${hook}' does not exist." | |
201 | exit ${EXIT_ERROR} | |
202 | fi | |
203 | ||
204 | if ! hook_config_exists "${hook_zone}" "${hook_config}"; then | |
205 | log ERROR "Hook '${hook_config}' is not supported for zone '${zone}'." | |
206 | exit ${EXIT_ERROR} | |
207 | fi | |
208 | ||
209 | hook_zone_config_exec "${hook_zone}" "${hook_config}" "${cmd}" "${zone}" "$@" | |
210 | } | |
211 | ||
212 | function hook_config_create() { | |
213 | local zone="${1}" | |
214 | assert isset zone | |
215 | ||
216 | local hook_config="${2}" | |
217 | assert isset hook_config | |
218 | ||
219 | shift 2 | |
220 | ||
221 | if ! listmatch "${hook_config}" $(zone_get_supported_config_hooks ${zone}); then | |
222 | log ERROR "Zone '${zone}' does not support configuration of type '${hook_config}'." | |
223 | exit ${EXIT_ERROR} | |
224 | fi | |
225 | ||
226 | local hook_zone="$(zone_get_hook "${zone}")" | |
227 | assert isset hook_zone | |
228 | ||
229 | hook_zone_config_exec "${hook_zone}" "${hook_config}" create "${zone}" "$@" | |
230 | exit $? | |
231 | } | |
232 | ||
233 | function hook_config_edit() { | |
234 | hook_config_cmd edit "$@" | |
235 | } | |
236 | ||
237 | function hook_config_remove() { | |
238 | cmd_not_implemented | |
239 | } | |
240 | ||
241 | function hook_config_show() { | |
242 | cmd_not_implemented | |
243 | } | |
244 | ||
245 | function hook_ppp_write_config() { | |
246 | cmd_not_implemented | |
247 | ||
248 | # Arguments: <zone> <filename> | |
249 | } | |
250 | ||
251 | function hook_ppp_ip_pre_up() { | |
252 | local zone="${1}" | |
253 | assert isset zone | |
254 | shift | |
255 | ||
256 | if ! zone_exists "${zone}"; then | |
257 | log ERROR "Zone '${zone}' does not exist." | |
258 | exit ${EXIT_ERROR} | |
259 | fi | |
260 | ||
261 | ppp_common_ip_pre_up "${zone}" "$@" | |
262 | exit $? | |
263 | } | |
264 | ||
265 | function hook_ppp_ipv4_up() { | |
266 | local zone="${1}" | |
267 | assert isset zone | |
268 | shift | |
269 | ||
270 | if ! zone_exists "${zone}"; then | |
271 | log ERROR "Zone '${zone}' does not exist." | |
272 | exit ${EXIT_ERROR} | |
273 | fi | |
274 | ||
275 | ppp_common_ipv4_up "${zone}" "$@" | |
276 | exit $? | |
277 | } | |
278 | ||
279 | function hook_ppp_ipv4_down() { | |
280 | local zone="${1}" | |
281 | assert isset zone | |
282 | shift | |
283 | ||
284 | if ! zone_exists "${zone}"; then | |
285 | log ERROR "Zone '${zone}' does not exist." | |
286 | exit ${EXIT_ERROR} | |
287 | fi | |
288 | ||
289 | ppp_common_ipv4_down "${zone}" "$@" | |
290 | exit $? | |
291 | } | |
292 | ||
293 | function hook_ppp_ipv6_up() { | |
294 | local zone="${1}" | |
295 | assert isset zone | |
296 | shift | |
297 | ||
298 | if ! zone_exists "${zone}"; then | |
299 | error "Zone '${zone}' does not exist." | |
300 | exit ${EXIT_ERROR} | |
301 | fi | |
302 | ||
303 | ppp_common_ipv6_up "${zone}" "$@" | |
304 | exit $? | |
305 | } | |
306 | ||
307 | function hook_ppp_ipv6_down() { | |
308 | local zone="${1}" | |
309 | assert isset zone | |
310 | shift | |
311 | ||
312 | if ! zone_exists "${zone}"; then | |
313 | error "Zone '${zone}' does not exist." | |
314 | exit ${EXIT_ERROR} | |
315 | fi | |
316 | ||
317 | ppp_common_ipv6_down "${zone}" "$@" | |
318 | exit $? | |
319 | } |