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