]>
Commit | Line | Data |
---|---|---|
711ffac1 | 1 | #!/bin/bash |
1578dae9 MT |
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 | ############################################################################### | |
711ffac1 MT |
21 | |
22 | function port_dir() { | |
84f3bd05 | 23 | echo "${NETWORK_CONFIG_DIR}/ports" |
711ffac1 MT |
24 | } |
25 | ||
49ec20d8 MT |
26 | function port_get_hook() { |
27 | local port=${1} | |
28 | assert isset port | |
29 | ||
30 | config_get_hook $(port_file ${port}) | |
31 | } | |
32 | ||
33 | function port_config_dir() { | |
34 | local port=${1} | |
35 | ||
36 | print "${RUN_DIR}/ports/${port}" | |
37 | return ${EXIT_OK} | |
38 | } | |
39 | ||
e9df08ad MT |
40 | function port_settings_read() { |
41 | local port="${1}" | |
49ec20d8 | 42 | assert isset port |
e9df08ad | 43 | shift |
49ec20d8 MT |
44 | |
45 | # Save the HOOK variable. | |
46 | local hook="${HOOK}" | |
47 | ||
e9df08ad | 48 | settings_read "$(port_file "${port}")" "$@" |
49ec20d8 MT |
49 | |
50 | # Restore hook. | |
51 | HOOK="${hook}" | |
52 | } | |
53 | ||
e9df08ad MT |
54 | function port_settings_write() { |
55 | local port="${1}" | |
49ec20d8 | 56 | assert isset port |
e9df08ad | 57 | shift |
49ec20d8 | 58 | |
e9df08ad | 59 | settings_write "$(port_file "${port}")" "$@" |
49ec20d8 MT |
60 | } |
61 | ||
8895cf8f MT |
62 | function ports_get_all() { |
63 | local port | |
64 | ||
65 | for port in $(port_dir)/*; do | |
66 | [ -f "${port}" ] || continue | |
67 | ||
68 | basename ${port} | |
69 | done | |
70 | } | |
71 | ||
711ffac1 | 72 | function port_file() { |
e9df08ad | 73 | local port="${1}" |
711ffac1 MT |
74 | assert isset port |
75 | ||
76 | echo "$(port_dir)/${port}" | |
77 | } | |
78 | ||
79 | function port_exists() { | |
80 | local port=${1} | |
81 | ||
84f3bd05 | 82 | [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ] |
711ffac1 MT |
83 | } |
84 | ||
85 | function port_get_hook() { | |
86 | local port=${1} | |
87 | ||
88 | assert isset port | |
89 | ||
90 | config_get_hook $(port_file ${port}) | |
91 | } | |
92 | ||
93 | function port_is_attached() { | |
94 | local port=${1} | |
95 | shift | |
96 | ||
97 | assert isset port | |
98 | ||
99 | local zone | |
100 | for zone in $(zones_get_all); do | |
101 | ||
102 | assert isset zone | |
103 | assert zone_exists ${zone} | |
104 | ||
105 | if listmatch ${port} $(zone_get_ports ${zone}); then | |
106 | echo "${zone}" | |
107 | return ${EXIT_OK} | |
108 | fi | |
109 | done | |
110 | ||
111 | return ${EXIT_ERROR} | |
112 | } | |
113 | ||
114 | function port_create() { | |
115 | #local port=${1} | |
116 | #shift | |
117 | # | |
118 | #if port_exists ${port}; then | |
119 | # error "Port '${port}' does already exist." | |
120 | # return ${EXIT_ERROR} | |
121 | #fi | |
122 | ||
123 | local hook=${1} | |
124 | shift | |
125 | ||
126 | if ! hook_exists port ${hook}; then | |
127 | error "Port hook '${hook}' does not exist." | |
128 | return ${EXIT_ERROR} | |
129 | fi | |
130 | ||
131 | #port_edit ${port} ${hook} $@ | |
132 | # | |
133 | #if [ $? -ne ${EXIT_OK} ]; then | |
134 | # port_destroy ${port} | |
135 | #fi | |
136 | ||
137 | hook_exec port ${hook} create $@ | |
138 | } | |
139 | ||
140 | function port_destroy() { | |
141 | local port=${1} | |
142 | ||
143 | assert isset port | |
144 | ||
145 | port_exists ${port} || return ${EXIT_OK} | |
146 | ||
98f4dae6 MT |
147 | # Check if the port is attached to any zone and don't delete it. |
148 | local ok=${EXIT_OK} | |
711ffac1 | 149 | |
98f4dae6 | 150 | local attached_zone=$(port_is_attached ${port}) |
711ffac1 | 151 | if [ -n "${attached_zone}" ]; then |
98f4dae6 MT |
152 | error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'." |
153 | ok=${EXIT_ERROR} | |
154 | fi | |
155 | ||
156 | # Check if the port is linked to any other port and don't allow the user | |
157 | # to delete it. | |
158 | local other_port | |
159 | for other_port in $(ports_get); do | |
160 | [ "${other_port}" = "${port}" ] && continue | |
161 | ||
162 | if listmatch ${port} $(port_get_parents ${other_port}); then | |
163 | error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'." | |
164 | ok=${EXIT_ERROR} | |
165 | fi | |
166 | ||
167 | if listmatch ${port} $(port_get_children ${other_port}); then | |
168 | error_log "Cannot destroy port '${port}' which is child of port '${other_port}'." | |
169 | ok=${EXIT_ERROR} | |
170 | fi | |
171 | done | |
172 | ||
173 | # If ok says we are not okay --> exit | |
174 | if [ ${ok} -ne ${EXIT_OK} ]; then | |
711ffac1 MT |
175 | return ${EXIT_ERROR} |
176 | fi | |
177 | ||
178 | port_down ${port} | |
179 | ||
180 | rm -f $(port_file ${port}) | |
181 | } | |
182 | ||
f90e550b MT |
183 | function port_remove() { |
184 | port_destroy $@ | |
185 | } | |
186 | ||
711ffac1 MT |
187 | function port_edit() { |
188 | port_cmd edit $@ | |
189 | } | |
190 | ||
191 | # XXX? Compatibility function | |
192 | function port_show() { | |
193 | port_status $@ | |
194 | } | |
195 | ||
196 | function port_up() { | |
197 | port_cmd up $@ | |
198 | } | |
199 | ||
200 | function port_down() { | |
201 | port_cmd down $@ | |
202 | } | |
203 | ||
204 | function port_status() { | |
205 | port_cmd status $@ | |
206 | } | |
207 | ||
98f4dae6 MT |
208 | function port_info() { |
209 | port_cmd info $@ | |
210 | } | |
211 | ||
711ffac1 MT |
212 | function port_cmd() { |
213 | local cmd=${1} | |
214 | local port=${2} | |
215 | shift 2 | |
216 | ||
217 | assert isset cmd | |
218 | assert isset port | |
219 | ||
220 | local hook=$(port_get_hook ${port}) | |
221 | ||
222 | assert isset hook | |
223 | ||
224 | hook_exec port ${hook} ${cmd} ${port} $@ | |
225 | } | |
f90e550b MT |
226 | |
227 | function ports_get() { | |
228 | local port | |
229 | for port in $(port_dir)/*; do | |
230 | port=$(basename ${port}) | |
231 | if port_exists ${port}; then | |
232 | echo "${port}" | |
233 | fi | |
234 | done | |
235 | } | |
2ae0fb8d | 236 | |
d76f5107 MT |
237 | function port_find_free() { |
238 | local pattern=${1} | |
239 | ||
240 | assert isset pattern | |
241 | ||
242 | local port | |
243 | local i=0 | |
244 | ||
245 | while [ ${i} -lt 99 ]; do | |
246 | port=${pattern//N/${i}} | |
247 | if ! port_exists ${port} && ! device_exists ${port}; then | |
248 | echo "${port}" | |
a1a8f0f4 | 249 | return ${EXIT_OK} |
d76f5107 MT |
250 | fi |
251 | i=$(( ${i} + 1 )) | |
252 | done | |
a1a8f0f4 MT |
253 | |
254 | return ${EXIT_ERROR} | |
d76f5107 | 255 | } |
98f4dae6 MT |
256 | |
257 | function port_get_info() { | |
258 | local port=${1} | |
259 | local key=${2} | |
260 | ||
261 | assert isset port | |
262 | assert port_exists ${port} | |
263 | assert isset key | |
264 | ||
265 | ( | |
266 | eval $(port_info ${port}) | |
267 | echo "${!key}" | |
268 | ) | |
269 | } | |
270 | ||
271 | function port_get_parents() { | |
272 | local port=${1} | |
273 | ||
274 | port_get_info ${port} PORT_PARENTS | |
275 | } | |
276 | ||
277 | function port_get_children() { | |
278 | local port=${1} | |
279 | ||
280 | port_get_info ${port} PORT_CHILDREN | |
281 | } | |
3a7fef62 MT |
282 | |
283 | function port_zone() { | |
284 | # Get name of the zones, this port is configured in. | |
285 | local port=${1} | |
286 | shift | |
287 | ||
288 | assert isset port | |
289 | ||
290 | local zone | |
291 | for zone in $(zones_get_all); do | |
292 | if zone_has_port ${zone} ${port}; then | |
293 | echo "${zone}" | |
294 | return ${EXIT_OK} | |
295 | fi | |
296 | done | |
297 | ||
298 | return ${EXIT_OK} | |
299 | } |