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