Project:
IPFire
Code Location:
git://git.ipfire.org/network.gitmaster
/
functions.ports
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2010 Michael Tremer & Christian Schmidt # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # # # ############################################################################### function port_dir() { echo "${NETWORK_CONFIG_DIR}/ports" } function port_get_hook() { local port=${1} assert isset port config_get_hook $(port_file ${port}) } function port_config_dir() { local port=${1} print "${RUN_DIR}/ports/${port}" return ${EXIT_OK} } function port_config_read() { local port=${1} assert isset port # Save the HOOK variable. local hook="${HOOK}" config_read $(port_file ${port}) # Restore hook. HOOK="${hook}" } function port_config_write() { local port=${1} assert isset port config_write $(port_file ${port}) } function ports_get_all() { local port for port in $(port_dir)/*; do [ -f "${port}" ] || continue basename ${port} done } function port_file() { local port=${1} assert isset port echo "$(port_dir)/${port}" } function port_exists() { local port=${1} [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ] } function port_get_hook() { local port=${1} assert isset port config_get_hook $(port_file ${port}) } function port_is_attached() { local port=${1} shift assert isset port local zone for zone in $(zones_get_all); do assert isset zone assert zone_exists ${zone} if listmatch ${port} $(zone_get_ports ${zone}); then echo "${zone}" return ${EXIT_OK} fi done return ${EXIT_ERROR} } function port_create() { #local port=${1} #shift # #if port_exists ${port}; then # error "Port '${port}' does already exist." # return ${EXIT_ERROR} #fi local hook=${1} shift if ! hook_exists port ${hook}; then error "Port hook '${hook}' does not exist." return ${EXIT_ERROR} fi #port_edit ${port} ${hook} $@ # #if [ $? -ne ${EXIT_OK} ]; then # port_destroy ${port} #fi hook_exec port ${hook} create $@ } function port_destroy() { local port=${1} assert isset port port_exists ${port} || return ${EXIT_OK} # Check if the port is attached to any zone and don't delete it. local ok=${EXIT_OK} local attached_zone=$(port_is_attached ${port}) if [ -n "${attached_zone}" ]; then error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'." ok=${EXIT_ERROR} fi # Check if the port is linked to any other port and don't allow the user # to delete it. local other_port for other_port in $(ports_get); do [ "${other_port}" = "${port}" ] && continue if listmatch ${port} $(port_get_parents ${other_port}); then error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'." ok=${EXIT_ERROR} fi if listmatch ${port} $(port_get_children ${other_port}); then error_log "Cannot destroy port '${port}' which is child of port '${other_port}'." ok=${EXIT_ERROR} fi done # If ok says we are not okay --> exit if [ ${ok} -ne ${EXIT_OK} ]; then return ${EXIT_ERROR} fi port_down ${port} rm -f $(port_file ${port}) } function port_remove() { port_destroy $@ } function port_edit() { port_cmd edit $@ } # XXX? Compatibility function function port_show() { port_status $@ } function port_up() { port_cmd up $@ } function port_down() { port_cmd down $@ } function port_status() { port_cmd status $@ } function port_info() { port_cmd info $@ } function port_cmd() { local cmd=${1} local port=${2} shift 2 assert isset cmd assert isset port local hook=$(port_get_hook ${port}) assert isset hook hook_exec port ${hook} ${cmd} ${port} $@ } function ports_get() { local port for port in $(port_dir)/*; do port=$(basename ${port}) if port_exists ${port}; then echo "${port}" fi done } function port_find_free() { local pattern=${1} assert isset pattern local port local i=0 while [ ${i} -lt 99 ]; do port=${pattern//N/${i}} if ! port_exists ${port} && ! device_exists ${port}; then echo "${port}" return ${EXIT_OK} fi i=$(( ${i} + 1 )) done return ${EXIT_ERROR} } function port_get_info() { local port=${1} local key=${2} assert isset port assert port_exists ${port} assert isset key ( eval $(port_info ${port}) echo "${!key}" ) } function port_get_parents() { local port=${1} port_get_info ${port} PORT_PARENTS } function port_get_children() { local port=${1} port_get_info ${port} PORT_CHILDREN } function port_zone() { # Get name of the zones, this port is configured in. local port=${1} shift assert isset port local zone for zone in $(zones_get_all); do if zone_has_port ${zone} ${port}; then echo "${zone}" return ${EXIT_OK} fi done return ${EXIT_OK} }
