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