]>
Commit | Line | Data |
---|---|---|
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 | # | |
22 | # Notes: | |
23 | # - All functions in this scope must start with an underline (_) to not | |
24 | # conflict with any functions that were defined somewhere else. | |
25 | # | |
26 | ||
27 | . /lib/network/functions | |
28 | ||
29 | HOOK=$(basename ${0}) | |
30 | ||
31 | while [ $# -gt 0 ]; do | |
32 | case "${1}" in | |
33 | -*) | |
34 | error "Unrecognized option: ${1}" | |
35 | exit ${EXIT_ERROR} | |
36 | ;; | |
37 | *) | |
38 | action=${1} | |
39 | ;; | |
40 | esac | |
41 | shift | |
42 | ||
43 | # If action argument was given, we will exit. | |
44 | [ -n "${action}" ] && break | |
45 | done | |
46 | ||
47 | # _notimplemented | |
48 | # Returns a soft error if a function was not implemented, yet. | |
49 | # | |
50 | function _notimplemented() { | |
51 | warning "'$@' was not implemented." | |
52 | exit ${EXIT_CONF_ERROR} | |
53 | } | |
54 | ||
55 | function _info() { | |
56 | echo "HOOK=\"${HOOK}\"" | |
57 | } | |
58 | ||
59 | function _create() { | |
60 | local zone=${1} | |
61 | shift | |
62 | ||
e5f78859 | 63 | config_read $(zone_dir ${zone})/settings |
1848564d MT |
64 | |
65 | _parse_cmdline $@ | |
66 | ||
e5f78859 | 67 | config_write $(zone_dir ${zone})/settings ${HOOK_SETTINGS} |
1848564d MT |
68 | |
69 | exit ${EXIT_OK} | |
70 | } | |
71 | ||
72 | function _edit() { | |
73 | _create $@ | |
74 | } | |
75 | ||
76 | function _rem() { | |
77 | _notimplemented _rem | |
78 | } | |
79 | ||
80 | function _status() { | |
81 | local zone=${1} | |
82 | ||
83 | if device_is_up ${zone}; then | |
84 | exit ${STATUS_UP} | |
85 | fi | |
86 | ||
87 | exit ${STATUS_DOWN} | |
88 | } | |
89 | ||
90 | function _up() { | |
91 | _notimplemented _up | |
92 | } | |
93 | ||
94 | function _down() { | |
95 | _notimplemented _down | |
96 | } | |
97 | ||
98 | function _discover() { | |
99 | # This hook does not support a discovery | |
100 | exit ${DISCOVER_NOT_SUPPORTED} | |
101 | } | |
102 | ||
103 | # Do nothing | |
104 | function _parse_cmdline() { | |
105 | return ${EXIT_OK} | |
106 | } | |
107 | ||
108 | function _port() { | |
109 | local zone=${1} | |
110 | local action=${2} | |
111 | shift 2 | |
112 | ||
113 | local ret | |
114 | ||
115 | case "${action}" in | |
e5f78859 | 116 | add|create|edit|rem|show) |
1848564d MT |
117 | _port_${action} ${zone} $@ |
118 | ret=$? | |
119 | ;; | |
120 | *) | |
121 | error "Unrecognized argument: '${action}'" | |
122 | exit ${EXIT_ERROR} | |
123 | ;; | |
124 | esac | |
125 | ||
126 | exit ${ret} | |
127 | } | |
128 | ||
e5f78859 MT |
129 | function _port_add() { |
130 | _port_cmd add $@ | |
131 | } | |
132 | ||
133 | function _port_edit() { | |
134 | _port_cmd edit $@ | |
135 | } | |
136 | ||
137 | function _port_rem() { | |
138 | _port_cmd rem $@ | |
139 | } | |
140 | ||
141 | function _port_show() { | |
142 | _notimplemented _port_show | |
143 | } | |
144 | ||
145 | function _port_status() { | |
146 | _port_cmd status $@ | |
147 | } | |
148 | ||
149 | function _port_cmd() { | |
1848564d MT |
150 | local cmd=${1} |
151 | local zone=${2} | |
e5f78859 | 152 | local port=${3} |
1848564d MT |
153 | shift 3 |
154 | ||
e5f78859 MT |
155 | assert isset cmd |
156 | assert isset zone | |
157 | assert isset port | |
158 | ||
1848564d | 159 | local hook_zone=$(zone_get_hook ${zone}) |
e5f78859 | 160 | local hook_port=$(port_get_hook ${port}) |
1848564d | 161 | |
e5f78859 MT |
162 | assert isset hook_zone |
163 | assert isset hook_port | |
1848564d | 164 | |
a5ebb169 | 165 | if ! listmatch ${hook_port} $(zone_get_supported_port_hooks ${zone}); then |
e5f78859 | 166 | error_log "Zone '${zone}' does not support port of type '${hook_port}'." |
1848564d MT |
167 | exit ${EXIT_ERROR} |
168 | fi | |
169 | ||
e5f78859 | 170 | hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@ |
1848564d | 171 | |
e5f78859 | 172 | exit $? |
1848564d MT |
173 | } |
174 | ||
e5f78859 MT |
175 | function _port_up() { |
176 | _port_cmd up $@ | |
1848564d MT |
177 | } |
178 | ||
e5f78859 MT |
179 | function _port_down() { |
180 | _port_cmd down $@ | |
1848564d MT |
181 | } |
182 | ||
183 | function _config() { | |
184 | local zone=${1} | |
185 | local action=${2} | |
186 | shift 2 | |
187 | ||
188 | local ret | |
189 | ||
190 | case "${action}" in | |
191 | create|edit|rem|show) | |
192 | _config_${action} ${zone} $@ | |
193 | ret=$? | |
194 | ;; | |
195 | *) | |
196 | error "Unrecognized argument: '${action}'" | |
197 | exit ${EXIT_ERROR} | |
198 | ;; | |
199 | esac | |
200 | ||
201 | exit ${ret} | |
202 | } | |
203 | ||
204 | # This function is not a public one | |
205 | function __configcmd() { | |
206 | local cmd=${1} | |
207 | local zone=${2} | |
208 | local hook_config=${3} | |
209 | shift 3 | |
210 | ||
211 | local hook_zone=$(zone_get_hook ${zone}) | |
212 | ||
d61a01d4 | 213 | if ! hook_zone_exists ${hook_zone}; then |
1848564d MT |
214 | error "Hook '${hook}' does not exist." |
215 | exit ${EXIT_ERROR} | |
216 | fi | |
217 | ||
218 | if ! hook_config_exists ${hook_zone} ${hook_config}; then | |
219 | error "Hook '${hook_config}' is not supported for zone '${zone}'." | |
220 | exit ${EXIT_ERROR} | |
221 | fi | |
222 | ||
d61a01d4 | 223 | hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} $@ |
1848564d MT |
224 | } |
225 | ||
226 | function _config_create() { | |
a5ebb169 MT |
227 | local zone=${1} |
228 | local hook_config=${2} | |
229 | shift 2 | |
230 | ||
231 | assert isset zone | |
232 | assert isset hook_config | |
233 | assert zone_exists ${zone} | |
234 | ||
235 | if ! listmatch ${hook_config} $(zone_get_supported_config_hooks ${zone}); then | |
236 | error_log "Zone '${zone}' does not support configuration of type '${hook_config}'." | |
237 | exit ${EXIT_ERROR} | |
238 | fi | |
239 | ||
240 | local hook_zone=$(zone_get_hook ${zone}) | |
241 | assert isset hook_zone | |
242 | ||
243 | hook_zone_config_exec ${hook_zone} ${hook_config} create ${zone} $@ | |
244 | ||
245 | exit $? | |
1848564d MT |
246 | } |
247 | ||
248 | function _config_edit() { | |
249 | __configcmd edit $@ | |
250 | } | |
251 | ||
252 | function _config_rem() { | |
253 | _notimplemented _config_rem | |
254 | } | |
255 | ||
256 | function _config_show() { | |
257 | _notimplemented _config_show | |
258 | } | |
259 | ||
b4038eca | 260 | function _ppp-ip-pre-up() { |
c7ad7801 MT |
261 | local zone=${1} |
262 | shift | |
263 | ||
264 | if ! zone_exists ${zone}; then | |
265 | error "Zone '${zone}' does not exist." | |
266 | exit ${EXIT_ERROR} | |
267 | fi | |
268 | ||
269 | ppp_common_ip_pre_up ${zone} $@ | |
270 | ||
271 | exit $? | |
b4038eca MT |
272 | } |
273 | ||
274 | function _ppp-ip-up() { | |
c7ad7801 MT |
275 | local zone=${1} |
276 | shift | |
277 | ||
278 | if ! zone_exists ${zone}; then | |
279 | error "Zone '${zone}' does not exist." | |
280 | exit ${EXIT_ERROR} | |
281 | fi | |
282 | ||
283 | ppp_common_ip_up ${zone} $@ | |
284 | ||
285 | exit $? | |
b4038eca MT |
286 | } |
287 | ||
288 | function _ppp-ip-down() { | |
c7ad7801 MT |
289 | local zone=${1} |
290 | shift | |
291 | ||
292 | if ! zone_exists ${zone}; then | |
293 | error "Zone '${zone}' does not exist." | |
294 | exit ${EXIT_ERROR} | |
295 | fi | |
296 | ||
297 | ppp_common_ip_down ${zone} $@ | |
298 | ||
299 | exit $? | |
b4038eca MT |
300 | } |
301 | ||
1848564d | 302 | function run() { |
e5f78859 MT |
303 | # Replace all dashes by an underscore |
304 | #action=${action//-/_} | |
305 | ||
1848564d | 306 | case "${action}" in |
a5ebb169 MT |
307 | # Main functions |
308 | create|discover|down|edit|info|rem|status|up) | |
1848564d MT |
309 | _${action} $@ |
310 | ;; | |
311 | ||
a5ebb169 MT |
312 | # Port callbacks |
313 | port_add|port_rem|port_up|port_down|port_status) | |
314 | _${action} $@ | |
1848564d MT |
315 | ;; |
316 | ||
a5ebb169 MT |
317 | # Configuration callbacks |
318 | config_create) | |
319 | _${action} $@ | |
1848564d MT |
320 | ;; |
321 | ||
a5ebb169 | 322 | # ppp daemon callbacks |
b4038eca MT |
323 | ppp-ip-pre-up|ppp-ip-up|ppp-ip-down) |
324 | _${action} $@ | |
325 | ;; | |
326 | ||
a5ebb169 MT |
327 | *) |
328 | error "Unknown action: ${action}" | |
329 | ;; | |
1848564d MT |
330 | esac |
331 | ||
332 | error "Hook did not exit properly." | |
333 | exit ${EXIT_ERROR} | |
334 | } |