]> git.ipfire.org Git - people/stevee/network.git/blob - functions.ports
DNS: Add RDNSS functionality.
[people/stevee/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 "${NETWORK_CONFIG_DIR}/ports"
24 }
25
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
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 "${NETWORK_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
112 # Check if the port is attached to any zone and don't delete it.
113 local ok=${EXIT_OK}
114
115 local attached_zone=$(port_is_attached ${port})
116 if [ -n "${attached_zone}" ]; then
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
140 return ${EXIT_ERROR}
141 fi
142
143 port_down ${port}
144
145 rm -f $(port_file ${port})
146 }
147
148 function port_remove() {
149 port_destroy $@
150 }
151
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
173 function port_info() {
174 port_cmd info $@
175 }
176
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 }
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 }
201
202 function port_find_free() {
203 local pattern=${1}
204
205 assert isset pattern
206
207 local port
208 local i=0
209
210 while [ ${i} -lt 99 ]; do
211 port=${pattern//N/${i}}
212 if ! port_exists ${port} && ! device_exists ${port}; then
213 echo "${port}"
214 return ${EXIT_OK}
215 fi
216 i=$(( ${i} + 1 ))
217 done
218
219 return ${EXIT_ERROR}
220 }
221
222 function port_get_info() {
223 local port=${1}
224 local key=${2}
225
226 assert isset port
227 assert port_exists ${port}
228 assert isset key
229
230 (
231 eval $(port_info ${port})
232 echo "${!key}"
233 )
234 }
235
236 function port_get_parents() {
237 local port=${1}
238
239 port_get_info ${port} PORT_PARENTS
240 }
241
242 function port_get_children() {
243 local port=${1}
244
245 port_get_info ${port} PORT_CHILDREN
246 }
247
248 function port_zone() {
249 # Get name of the zones, this port is configured in.
250 local port=${1}
251 shift
252
253 assert isset port
254
255 local zone
256 for zone in $(zones_get_all); do
257 if zone_has_port ${zone} ${port}; then
258 echo "${zone}"
259 return ${EXIT_OK}
260 fi
261 done
262
263 return ${EXIT_OK}
264 }