]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ports
Rename all "config" to "settings"
[people/stevee/network.git] / src / functions / 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 "${NETWORK_CONFIG_DIR}/ports"
24 }
25
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_settings_read() {
41 local port="${1}"
42 assert isset port
43 shift
44
45 # Save the HOOK variable.
46 local hook="${HOOK}"
47
48 settings_read "$(port_file "${port}")" "$@"
49
50 # Restore hook.
51 HOOK="${hook}"
52 }
53
54 function port_settings_write() {
55 local port="${1}"
56 assert isset port
57 shift
58
59 settings_write "$(port_file "${port}")" "$@"
60 }
61
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
72 function port_file() {
73 local port="${1}"
74 assert isset port
75
76 echo "$(port_dir)/${port}"
77 }
78
79 function port_exists() {
80 local port=${1}
81
82 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
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
147 # Check if the port is attached to any zone and don't delete it.
148 local ok=${EXIT_OK}
149
150 local attached_zone=$(port_is_attached ${port})
151 if [ -n "${attached_zone}" ]; then
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
175 return ${EXIT_ERROR}
176 fi
177
178 port_down ${port}
179
180 rm -f $(port_file ${port})
181 }
182
183 function port_remove() {
184 port_destroy $@
185 }
186
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
208 function port_info() {
209 port_cmd info $@
210 }
211
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 }
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 }
236
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}"
249 return ${EXIT_OK}
250 fi
251 i=$(( ${i} + 1 ))
252 done
253
254 return ${EXIT_ERROR}
255 }
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 }
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 }