Project: IPFire
Code Location: git://git.ipfire.org/network.gitmaster
Browse
/
Download File
functions.zone
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#!/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 zone_dir() {
	local zone=${1}

	echo "${NETWORK_ZONE_DIR}/zones/${zone}"
}

function zone_exists() {
	local zone=${1}
	assert isset zone

	[ -d "$(zone_dir ${zone})" ]
}

function zone_match() {
	local match

	local i
	for i in ${VALID_ZONES}; do
		match="${match}|${i}[0-9]{1,5}"
	done

	echo "${match:1:${#match}}"
}

function zone_name_is_valid() {
	local zone=${1}

	# Don't accept empty strings.
	[ -z "${zone}" ] && return ${EXIT_FALSE}

	[[ ${zone} =~ $(zone_match) ]]
}

function zone_is_local() {
	local zone=${1}

	[[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]]
}

function zone_is_nonlocal() {
	local zone=${1}

	[[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]]
}

function zone_get_hook() {
	local zone=${1}
	assert isset zone

	config_get_hook $(zone_dir ${zone})/settings
}

function zone_start() {
	# This function will bring up the zone
	# 'asynchronously' with help of systemd.

	local zone=${1}
	assert zone_exists ${zone}

	service_start "network@${zone}.service"
}

function zone_stop() {
	# This function will bring down the zone
	# 'asynchronously' with help of systemd.

	local zone=${1}
	assert zone_exists ${zone}

	service_stop "network@${zone}.service"
}

function zone_create() {
	local zone=${1}
	local hook=${2}
	shift 2

	if ! zone_name_is_valid ${zone}; then
		error "Zone name '${zone}' is not valid."
		return ${EXIT_ERROR}
	fi

	if zone_exists ${zone}; then
		error "Zone '${zone}' does already exist."
		return ${EXIT_ERROR}
	fi

	if ! hook_zone_exists ${hook}; then
		error "Hook '${hook}' does not exist."
		return ${EXIT_ERROR}
	fi

	mkdir -p $(zone_dir ${zone})

	# Create directories for configs and ports
	mkdir -p $(zone_dir ${zone})/{configs,ports}

	hook_zone_exec ${hook} create ${zone} $@
	local ret=$?

	# Maybe the zone create hook did not exit correctly.
	# If this is the case we remove the created zone immediately.
	if [ "${ret}" = "${EXIT_ERROR}" ]; then
		zone_remove_now ${zone}
	fi
}

function zone_edit() {
	local zone=${1}
	shift

	if ! zone_exists ${zone}; then
		error "Zone '${zone}' does not exist."
		return ${EXIT_ERROR}
	fi

	# Check if the zone is tagged for removal.
	if zone_has_remove_tag ${zone}; then
		error "You cannot edit a zone that is tagged for removal."
		return ${EXIT_ERROR}
	fi

	local hook=$(config_get_hook $(zone_dir ${zone})/settings)

	if [ -z "${hook}" ]; then
		error "Config file did not provide any hook."
		return ${EXIT_ERROR}
	fi

	if ! hook_zone_exists ${hook}; then
		error "Hook '${hook}' does not exist."
		return ${EXIT_ERROR}
	fi

	hook_zone_exec ${hook} edit ${zone} $@
}


function zone_remove() {
	local zone=${1}
	assert zone_exists ${zone}

	# Make the zone for removal.
	touch $(zone_dir ${zone})/.remove

	log INFO "Zone '${zone}' has been tagged for removal."
}

function zone_has_remove_tag() {
	local zone=${1}
	assert zone_exists ${zone}

	[ -e "$(zone_dir ${zone})/.remove" ]
}

# This function will remove the given zone
# RIGHT NOW. Use zone_remove to remove it
# at the next status change.
function zone_remove_now() {
	local zone=${1}
	assert zone_exists ${zone}

	log INFO "Removing zone '${zone}' right now."

	# Force the zone down.
	zone_is_up ${zone} && zone_set_down ${zone}

	rm -rf $(zone_dir ${zone})
}

function zone_up() {
	local zone=${1}
	shift

	if ! zone_exists ${zone}; then
		error "Zone '${zone}' does not exist."
		return ${EXIT_ERROR}
	fi

	# Check if a zone has got the remove tag.
	if zone_has_remove_tag ${zone}; then
		error "Cannot bring up any zone which is to be removed."
		return ${EXIT_ERROR}
	fi

	local hook=$(config_get_hook $(zone_dir ${zone})/settings)

	if [ -z "${hook}" ]; then
		error "Config file did not provide any hook."
		return ${EXIT_ERROR}
	fi

	if ! hook_zone_exists ${hook}; then
		error "Hook '${hook}' does not exist."
		return ${EXIT_ERROR}
	fi

	zone_db ${zone} starting

	hook_zone_exec ${hook} up ${zone} $@

	zone_db ${zone} started
}

function zone_down() {
	local zone=${1}
	shift

	if ! zone_exists ${zone}; then
		error "Zone '${zone}' does not exist."
		return ${EXIT_ERROR}
	fi

	local hook=$(config_get_hook $(zone_dir ${zone})/settings)

	if [ -z "${hook}" ]; then
		error "Config file did not provide any hook."
		return ${EXIT_ERROR}
	fi

	if ! hook_zone_exists ${hook}; then
		error "Hook '${hook}' does not exist."
		return ${EXIT_ERROR}
	fi

	zone_db ${zone} stopping

	hook_zone_exec ${hook} down ${zone} $@

	zone_db ${zone} stopped

	# Remove the zone, if it has got a remove tag.
	if zone_has_remove_tag ${zone}; then
		zone_remove_now ${zone}
	fi
}

function zone_status() {
	local zone=${1}
	shift

	if ! zone_exists ${zone}; then
		error "Zone '${zone}' does not exist."
		return ${EXIT_ERROR}
	fi

	local hook=$(config_get_hook $(zone_dir ${zone})/settings)

	if [ -z "${hook}" ]; then
		error "Config file did not provide any hook."
		return ${EXIT_ERROR}
	fi

	if ! hook_zone_exists ${hook}; then
		error "Hook '${hook}' does not exist."
		return ${EXIT_ERROR}
	fi

	hook_zone_exec ${hook} status ${zone} $@

	# Show that the zone it to be removed soon.
	if zone_has_remove_tag ${zone}; then
		warning "This zone is tagged for removal."
	fi
}

function zone_port() {
	local zone=${1}
	local action=${2}
	shift 2

	assert isset zone
	assert isset action
	assert zone_exists ${zone}

	# Aliases
	case "${action}" in
		del|delete|remove)
			action="rem"
			;;
	esac

	case "${action}" in
		add|edit|rem)
			zone_port_${action} ${zone} $@
			;;
		*)
			error "Unrecognized argument: ${action}"
			cli_usage root-zone-port-subcommands
			exit ${EXIT_ERROR}
			;;		
	esac
}

function zone_port_add() {
	local zone=${1}
	shift

	assert isset zone

	local hook=$(zone_get_hook ${zone})

	assert isset hook

	hook_zone_exec ${hook} port_add ${zone} $@
}

function zone_port_edit() {
	zone_port_cmd edit $@
}

function zone_port_rem() {
	zone_port_cmd rem $@
}

function zone_port_cmd() {
	local cmd=${1}
	local zone=${2}
	local port=${3}
	shift 3

	assert isset zone
	assert isset port

	local hook_zone=$(zone_get_hook ${zone})
	local hook_port=$(port_get_hook ${port})

	assert isset hook_zone
	assert isset hook_port

	hook_zone_port_exec ${hook_zone} ${hook_port} ${cmd} ${zone} ${port} $@
}

function zone_port_up() {
	zone_port_cmd up $@
}

function zone_port_down() {
	zone_port_cmd down $@
}

function zone_get_ports() {
	local zone=${1}

	assert isset zone

	local port
	for port in $(zone_dir ${zone})/ports/*; do
		port=$(basename ${port})

		if port_exists ${port}; then
			echo "${port}"
		fi
	done
}

function zone_has_port() {
	# Check, if the given port is configured
	# in this zone.

	local zone=${1}
	local port=${2}
	shift 2

	assert isset zone
	assert isset port

	[ -e "$(zone_dir ${zone})/ports/${port}" ]
}

# XXX overwritten some lines below
function zone_config() {
	local zone=${1}
	shift

	if ! zone_exists ${zone}; then
		error "Zone '${zone}' does not exist."
		return ${EXIT_ERROR}
	fi

	local hook=$(config_get_hook $(zone_dir ${zone})/settings)

	if [ -z "${hook}" ]; then
		error "Config file did not provide any hook."
		return ${EXIT_ERROR}
	fi

	if ! hook_zone_exists ${hook}; then
		error "Hook '${hook}' does not exist."
		return ${EXIT_ERROR}
	fi

	hook_zone_exec ${hook} config ${zone} $@
}

function zone_config() {
	local zone=${1}
	local action=${2}
	shift 2

	assert isset zone
	assert isset action
	assert zone_exists ${zone}

	# Aliases
	case "${action}" in
		del|delete|remove)
			action="rem"
			;;
	esac

	case "${action}" in
		create|edit|rem)
			zone_config_${action} ${zone} $@
			;;
		*)
			error "Unrecognized argument: ${action}"
			cli_usage root-zone-config-subcommands
			exit ${EXIT_ERROR}
			;;
	esac
}

function zone_config_option() {
	local zone=${1}
	local option=${2}
	local default=${3}
	shift 2

	assert isset zone
	assert isset option

	(
		VALUE="${default}"
		zone_config_read ${zone}

		VALUE="${!option}"
		echo "${VALUE}"
	)
}

function zone_config_create() {
	local zone=${1}
	shift

	assert isset zone

	local hook=$(zone_get_hook ${zone})

	assert isset hook

	hook_zone_exec ${hook} config_create ${zone} $@
}

function zone_show() {
	local zone=${1}

	echo "${zone}"
	echo "  Type: $(zone_get_hook ${zone})"
	echo
}

function zones_show() {
	local zone

	for zone in $(zones_get $@); do
		zone_show ${zone}
	done
}

function zones_get_all() {
	local zone
	for zone in $(zone_dir)/*; do
		zone=$(basename ${zone})
		zone_exists ${zone} || continue

		echo "${zone}"
	done
}

function zones_get_local() {
	local zone
	for zone in $(zones_get_all); do
		zone_is_local ${zone} && echo "${zone}"
	done
}

function zones_get_nonlocal() {
	local zone
	for zone in $(zones_get_all); do
		zone_is_nonlocal ${zone} && echo "${zone}"
	done
}

function zones_get() {
	local local=1
	local remote=1

	local zones

	while [ $# -gt 0 ]; do
		case "${1}" in
			--local-only)
				local=1
				remote=0
				;;
			--remote-only)
				local=0
				remote=1
				;;
			--all)
				local=1
				remote=1
				;;
			*)
				if zone_name_is_valid ${1}; then
					zones="${zones} ${1}"
				else			
					warning "Unrecognized argument '${1}'"
				fi
				;;
		esac
		shift
	done

	if [ -n "${zones}" ]; then
		local zone
		for zone in ${zones}; do
			zone_exists ${zone} && echo "${zone}"
		done
		exit ${EXIT_OK}
	fi

	if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then
		zones_get_all
	elif [ ${local} -eq 1 ]; then
		zones_get_local
	elif [ ${remote} -eq 1 ]; then
		zones_get_nonlocal
	fi
}

function zone_ports_list() {
	local zone=${1}

	local port
	for port in $(zone_dir ${zone})/ports/*; do
		[ -e "${port}" ] || continue

		echo $(basename ${port})
	done
}

function zone_ports_cmd() {
	local cmd=${1}
	local zone=${2}
	shift 2

	assert isset cmd
	assert isset zone

	assert zone_exists ${zone}

	local hook=$(zone_get_hook ${zone})

	local port
	for port in $(zone_get_ports ${zone}); do
		hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@
	done
}

function zone_ports_up() {
	zone_ports_cmd port_up $@
}

function zone_ports_down() {
	zone_ports_cmd port_down $@
}

function zone_ports_status() {
	zone_ports_cmd port_status $@
}

function zone_configs_list() {
	local zone=${1}

	local config
	for config in $(zone_dir ${zone})/configs/*; do
		[ -e "${config}" ] || continue

		basename ${config}
	done
}

function zone_configs_cmd() {
	local cmd=${1}
	local zone=${2}
	shift 2

	local hook_zone=$(config_get_hook $(zone_dir ${zone})/settings)

	local hook_config
	local config
	for config in $(zone_configs_list ${zone}); do
		hook_config=$(config_get_hook $(zone_dir ${zone})/configs/${config})

		hook_zone_config_exec ${hook_zone} ${hook_config} ${cmd} ${zone} ${config} $@
	done
}

function zone_configs_up() {
	zone_configs_cmd up $@
}

function zone_configs_down() {
	zone_configs_cmd down $@
}

function zone_configs_status() {
	zone_configs_cmd config_status $@
}

function zone_has_ip() {
	device_has_ip $@
}

function zone_db() {
	local zone=${1}
	local action=${2}
	shift 2

	case "${action}" in
		starting|started|stopping|stopped)
			db_connection_update ${zone} ${action}
			;;
	esac
}

function zone_is_up() {
	local zone=${1}

	device_is_up ${zone}
}

function zone_is_down() {
	! zone_is_up $@
}

function zone_get_supported_port_hooks() {
	local zone=${1}

	local hook=$(zone_get_hook ${zone})

	hook_zone_ports_get_all ${hook}
}

function zone_get_supported_config_hooks() {
	local zone=${1}

	local hook=$(zone_get_hook ${zone})

	hook_zone_configs_get_all ${hook}
}

function zone_file() {
	local zone=${1}

	assert isset zone

	echo "$(zone_dir ${zone})/settings"
}

function zone_config_read() {
	local zone=${1}

	assert isset zone

	# Save the HOOK variable.
	local hook="${HOOK}"

	config_read $(zone_file ${zone})

	# Restore hook.
	HOOK="${hook}"
}

function zone_config_write() {
	local zone=${1}

	assert isset zone

	config_write $(zone_file ${zone}) ${HOOK_SETTINGS}
}

function zone_config_set() {
	local zone=${1}
	shift
	local args="$@"

	assert isset zone

	(
		zone_config_read ${zone}

		for arg in ${args}; do
			eval "${arg}"
		done
	
		zone_config_write ${zone}
	)
}

function zone_config_get() {
	local zone=${1}
	local key=${2}

	assert isset zone
	assert isset key

	(
		zone_config_read ${zone}

		echo "${!key}"
	)
}