]> git.ipfire.org Git - network.git/blame - functions.ports
DNS: Add options to configure local DNS servers.
[network.git] / functions.ports
CommitLineData
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
22function port_dir() {
84f3bd05 23 echo "${NETWORK_CONFIG_DIR}/ports"
711ffac1
MT
24}
25
8895cf8f
MT
26function 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
36function port_file() {
37 local port=${1}
38
39 assert isset port
40
41 echo "$(port_dir)/${port}"
42}
43
44function port_exists() {
45 local port=${1}
46
84f3bd05 47 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
711ffac1
MT
48}
49
50function port_get_hook() {
51 local port=${1}
52
53 assert isset port
54
55 config_get_hook $(port_file ${port})
56}
57
58function 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
79function 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
105function 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
148function port_remove() {
149 port_destroy $@
150}
151
711ffac1
MT
152function port_edit() {
153 port_cmd edit $@
154}
155
156# XXX? Compatibility function
157function port_show() {
158 port_status $@
159}
160
161function port_up() {
162 port_cmd up $@
163}
164
165function port_down() {
166 port_cmd down $@
167}
168
169function port_status() {
170 port_cmd status $@
171}
172
98f4dae6
MT
173function port_info() {
174 port_cmd info $@
175}
176
711ffac1
MT
177function 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
192function 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 201
d76f5107
MT
202function 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}"
a1a8f0f4 214 return ${EXIT_OK}
d76f5107
MT
215 fi
216 i=$(( ${i} + 1 ))
217 done
a1a8f0f4
MT
218
219 return ${EXIT_ERROR}
d76f5107 220}
98f4dae6
MT
221
222function 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
236function port_get_parents() {
237 local port=${1}
238
239 port_get_info ${port} PORT_PARENTS
240}
241
242function port_get_children() {
243 local port=${1}
244
245 port_get_info ${port} PORT_CHILDREN
246}
3a7fef62
MT
247
248function 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}