]>
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 | ||
63 | config_read ${ZONE_DIR}/${zone}/settings | |
64 | ||
65 | _parse_cmdline $@ | |
66 | ||
67 | config_write ${ZONE_DIR}/${zone}/settings ${HOOK_SETTINGS} | |
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 | |
116 | create|edit|rem|show) | |
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 | ||
129 | # This function is not a public one | |
130 | function __portcmd() { | |
131 | local cmd=${1} | |
132 | local zone=${2} | |
133 | local hook_port=${3} | |
134 | shift 3 | |
135 | ||
136 | local hook_zone=$(zone_get_hook ${zone}) | |
137 | ||
138 | if ! hook_exists ${hook_zone}; then | |
139 | error "Hook '${hook}' does not exist." | |
140 | exit ${EXIT_ERROR} | |
141 | fi | |
142 | ||
143 | if ! hook_port_exists ${hook_zone} ${hook_port}; then | |
144 | error "Hook '${hook_port}' is not supported for zone '${zone}'." | |
145 | exit ${EXIT_ERROR} | |
146 | fi | |
147 | ||
148 | hook_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} $@ | |
149 | } | |
150 | ||
151 | function _port_create() { | |
152 | __portcmd create $@ | |
153 | } | |
154 | ||
155 | function _port_edit() { | |
156 | __portcmd edit $@ | |
157 | } | |
158 | ||
159 | function _port_rem() { | |
160 | _notimplemented _port_rem | |
161 | } | |
162 | ||
163 | function _port_show() { | |
164 | _notimplemented _port_show | |
165 | } | |
166 | ||
167 | function _config() { | |
168 | local zone=${1} | |
169 | local action=${2} | |
170 | shift 2 | |
171 | ||
172 | local ret | |
173 | ||
174 | case "${action}" in | |
175 | create|edit|rem|show) | |
176 | _config_${action} ${zone} $@ | |
177 | ret=$? | |
178 | ;; | |
179 | *) | |
180 | error "Unrecognized argument: '${action}'" | |
181 | exit ${EXIT_ERROR} | |
182 | ;; | |
183 | esac | |
184 | ||
185 | exit ${ret} | |
186 | } | |
187 | ||
188 | # This function is not a public one | |
189 | function __configcmd() { | |
190 | local cmd=${1} | |
191 | local zone=${2} | |
192 | local hook_config=${3} | |
193 | shift 3 | |
194 | ||
195 | local hook_zone=$(zone_get_hook ${zone}) | |
196 | ||
197 | if ! hook_exists ${hook_zone}; then | |
198 | error "Hook '${hook}' does not exist." | |
199 | exit ${EXIT_ERROR} | |
200 | fi | |
201 | ||
202 | if ! hook_config_exists ${hook_zone} ${hook_config}; then | |
203 | error "Hook '${hook_config}' is not supported for zone '${zone}'." | |
204 | exit ${EXIT_ERROR} | |
205 | fi | |
206 | ||
207 | hook_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} $@ | |
208 | } | |
209 | ||
210 | function _config_create() { | |
211 | __configcmd create $@ | |
212 | } | |
213 | ||
214 | function _config_edit() { | |
215 | __configcmd edit $@ | |
216 | } | |
217 | ||
218 | function _config_rem() { | |
219 | _notimplemented _config_rem | |
220 | } | |
221 | ||
222 | function _config_show() { | |
223 | _notimplemented _config_show | |
224 | } | |
225 | ||
226 | function run() { | |
227 | case "${action}" in | |
228 | create|discover|down|edit|info|rem|status|up) | |
229 | _${action} $@ | |
230 | ;; | |
231 | ||
232 | port) | |
233 | if ! hook_has_ports ${HOOK}; then | |
234 | error "Hook '${HOOK}' does not support ports." | |
235 | exit ${EXIT_ERROR} | |
236 | fi | |
237 | ||
238 | _port $@ | |
239 | ;; | |
240 | ||
241 | config) | |
242 | if ! hook_has_configs ${HOOK}; then | |
243 | error "Hook '${HOOK}' does not support configurations." | |
244 | exit ${EXIT_ERROR} | |
245 | fi | |
246 | ||
247 | _config $@ | |
248 | ;; | |
249 | ||
250 | esac | |
251 | ||
252 | error "Hook did not exit properly." | |
253 | exit ${EXIT_ERROR} | |
254 | } |