-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip-array_xml_functions
1365 lines (1347 loc) · 56.1 KB
/
ip-array_xml_functions
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# ------------------------------------------------------------------------- #
#*# ###### #
# # # # # ##### ##### ## # #
# # # # # # # # # # # # #
# ###### ##### # # # # # # # # #
# # ####### ##### ##### ###### #
# # # # # # # # # # #
### # # # # # # # # # #
# ------------------------------------------------------------------------- #
#
# Copyright (C) 2005-2018 Mart Frauenlob aka AllKind
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ------------------------------------------------------------------------- #
#
# IP-ARRAY XML FUNCTIONS
#
# ------------------------------------------------------------------------- #
_verify_attributes() {
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
for i in ${!ARR_TAG_REF[@]}; do
if [[ ${ARR_TAG_REF[i]%%[[:blank:]]*} = ${tag_name} ]]; then
lsearch "${str_attr_name}" ${ARR_TAG_REF[i]#*[[:blank:]]} || {
log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
}
break
fi
done
done
} # -------------------------------------------------------------------------
#xtract_assignment() {
#local str_w str_v
#IFS='=' read -r str_w str_v < <(printf "%s\n" "$@")
#printf "'%s' '%s'\n" "${str_w}" "${str_v}"
#}
_get_attribute_data() {
for i in ${attr_indicies}; do
# eval 'set -- '$(xtract_assignment "$(subst_var "ARR_ATTR_NO_${tag_idx}[${i}]")")
# printf "%s %s\n" "$1" "$2" # str_attr_name="$1" str_attr_val="$2"
str_attr="$(subst_var "ARR_ATTR_NO_${tag_idx}[${i}]")"
printf "%s %s\n" "${str_attr%%=*}" "${str_attr#*=}"
done
} # -------------------------------------------------------------------------
#_get_attribute_data() {
#for i in ${attr_indicies}; do
# eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
# printf "%s %s\n" "${str_attr%%=*}" "${str_attr#*=}"
#done
#}
# -------------------------------------------------------------------------
xml_interpreter_templates() {
local -i tag_max_depth=3 tag_depth=0 root_found=0 template_open=0 IDX=0
local -i i tag_idx opt_idx m_count attr_members cont_members
local str_root_tag="ip_array_root" str_root_attr="iptables_templates"
local str_act tag_name str_attr str_syntax_version str_attr_name str_attr_val template_name str_attributes attr_indicies str_cont tmp_content str_type
local -a RESULT_ARR
local -a ARR_TAG_REF=(
"template name msg table chain target reverse_condition reverse_mode"
"mandatory_vars"
"table name"
"target name"
"chain name"
"always_use_0 ${ARG_NAMES_LIST[@]}"
"always_use_1 ${ARG_NAMES_LIST[@]}"
"option_list_0 ${ARG_NAMES_LIST[@]}"
"option_list_1 ${ARG_NAMES_LIST[@]}"
"load_template name"
"state_match_arg"
"template_msg name"
"reverse_mode name"
"reverse_condition name"
)
log -e "Done with xml parsing, interpreting data"
for tag_idx in ${!ARR_TAGS[@]}; do
set -- ${ARR_TAGS[tag_idx]}
str_act="$1" tag_name="$2"
case "${str_act}" in
open)
[[ $DEBUG_INFO ]] && {
pr_str "opening tag: \`${tag_name}'"
for str_cont in ARR_COMMENT_NO ARR_ATTR_NO ARR_CONT_NO; do
dbg_arr ${str_cont}_${tag_idx}
done
}
# for i in $(arr_indicies "ARR_COMMENT_NO_${tag_idx}"); do
# eval 'RESULT_ARR[IDX++]="#${ARR_COMMENT_NO_'${tag_idx}'[i]}"'
# done
attr_members=$(arr_members_sum "ARR_ATTR_NO_${tag_idx}")
attr_indicies=$(arr_indicies "ARR_ATTR_NO_${tag_idx}")
cont_members=$(arr_members_sum "ARR_CONT_NO_${tag_idx}")
if ((root_found == 0)); then
[[ ${tag_name} = ${str_root_tag} ]] || {
log -E "Root tag needed"
return 1
}
while read -r str_attr_name str_attr_val; do
case "${str_attr_name}" in
syntax_version)
str_syntax_version="${str_attr_val}"
[[ ${str_syntax_version} = +([[:digit:]]).+([[:digit:]]) ]] || {
log -E "Invalid format for \`syntax_version'."
return 1
}
if ! lsearch "${str_syntax_version}" "${TEMPLATE_SYNTAX_VERSION_ARRAY[@]}"; then
log -E "Incompatible template syntax version \`${str_syntax_version}'."
return 1
fi
;;
name)
str_type="${str_attr_val}"
[[ ${str_type} = ${str_root_attr} ]] || {
log -E "Expected root attribute \`${str_root_attr}'."
return 1
}
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
[[ ${str_syntax_version} ]] || { log -E "Syntax version attribute is required."; return 1; }
[[ ${str_type} ]] || { log -E "Root \`name' attribute is required."; return 1; }
root_found=1 tag_depth=1
else
((++tag_depth <= tag_max_depth)) || { log -E "Max depth is ${tag_max_depth}"; return 1; }
if [[ ${tag_name} != template ]] && ((template_open == 0)); then
log -E "\`${tag_name}' needs an open template."
return 1
fi
case "${tag_name}" in
template)
template_open=1 m_count=0 template_name=""
_verify_attributes || return 1
while read -r str_attr_name str_attr_val; do
if [[ ${str_attr_name} = name ]]; then
template_name="${str_attr_val}"
break
fi
done < <(_get_attribute_data)
[[ ${template_name} ]] || { log -E "No template name provided."; return 1; }
lsearch "${template_name}" "${TEMPLATE_NAMES_ARRAY[@]}" && {
log -E "Duplicate template name \`${template_name}'."
return 1
}
log -w "${PF}reading template: $template_name"
TEMPLATE_NAMES_ARRAY[${#TEMPLATE_NAMES_ARRAY[@]}]="${template_name}"
TEMP_TEMPLATE_NAMES_ARRAY[${#TEMP_TEMPLATE_NAMES_ARRAY[@]}]="${template_name}"
RESULT_ARR[IDX++]="${template_name}=("
_verify_attributes || return 1
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
case "${str_attr_name}" in
target|chain)
opt_idx=0
for tmp_content in ${str_attr_val//,/ }; do
RESULT_ARR[IDX++]="[$((m_count++))]=\"${str_attr_name}[$((opt_idx++))]=\\\"${tmp_content}\\\"\""
# " # just coz syntax highlight messes up
done
;;
table|reverse_mode|reverse_condition|msg)
RESULT_ARR[IDX++]="[$((m_count++))]=\"${str_attr_name}=\\\"${str_attr_val}\\\"\""
;;
esac
done
;;
table|reverse_mode|reverse_condition|msg|template_msg)
((attr_members > 0 && cont_members > 0)) && {
log -E "Either attribute or content is allowed in tag \`${tag_name}', not both."
return 1
}
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
if ((attr_members > 0)); then
_verify_attributes || return 1
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
RESULT_ARR[IDX++]="[$((m_count++))]=\"${str_attr_name}=\\\"${str_attr_val}\\\"\""
# "
done
fi
if ((cont_members > 0)); then
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'[*]}"'
RESULT_ARR[IDX++]="[$((m_count++))]=\"${tag_name}=\\\"${str_cont}\\\"\""
# "
fi
;;
target|chain|load_template|always_use_0|always_use_1|option_list_0|option_list_1|state_match_arg|mandatory_vars)
((attr_members > 0 && cont_members > 0)) && {
log -E "Either attribute or content is allowed in tag \`${tag_name}', not both."
return 1
}
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
if ((attr_members > 0)); then
opt_idx=0
_verify_attributes || return 1
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
case "${tag_name}" in
always_use_0|always_use_1|option_list_0|option_list_1)
if [[ ${str_attr_val} ]]; then
RESULT_ARR[IDX++]="[${m_count}]=\"${tag_name}[${opt_idx}]=${str_attr_name}=${str_attr_val}\""
# "
else
RESULT_ARR[IDX++]="[${m_count}]=\"${tag_name}[${opt_idx}]=${str_attr_name}\""
# "
fi
;;
*) RESULT_ARR[IDX++]="[${m_count}]=\"${tag_name}[${opt_idx}]=${str_attr_name}=\\\"${str_attr_val}\\\"\""
esac
let m_count++ opt_idx++
done
fi
if ((cont_members > 0)); then
opt_idx=0
for i in $(arr_indicies "ARR_CONT_NO_${tag_idx}"); do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
eval 'set -- '"${str_cont}"
for tmp_content in "$@"; do
RESULT_ARR[IDX++]="[$((m_count++))]=\"${tag_name}[$((opt_idx++))]=${tmp_content}\""
# "
done
done
fi
;;
*) log -E "Invalid tag name \`${tag_name}'."
return 1
esac
fi
;;
close)
[[ $DEBUG_INFO ]] && pr_str "closing tag: \`${tag_name}'"
if [[ ${tag_name} = template ]]; then
template_open=0
RESULT_ARR[IDX++]=")"
fi
if ((--tag_depth == 0)); then
root_found=0
str_type="" str_syntax_version=""
fi
# for i in $(arr_indicies "ARR_COMMENT_NO_${tag_idx}"); do
# eval 'RESULT_ARR[IDX++]="#${ARR_COMMENT_NO_'${tag_idx}'[i]}"'
# done
;;
esac
done
# print collected array assignments
if ((${#RESULT_ARR[@]})); then
printf "%s\n" "${RESULT_ARR[@]}" >&7
else
log -W "No templates found."
return 0
fi
}
# -------------------------------------------------------------------------
xml_interpreter_ruleblocks() {
local -i tag_max_depth=2 tag_depth=0 root_found=0 IDX=0 tag_idx i rcount
local str_root_tag="ip_array_root" str_root_attr="ruleblocks" str_valid_tags="ruleblock" str_valid_attr="name"
local str_act tag_name str_attr str_syntax_version str_attr_name str_attr_val attr_indicies str_type
local -a RESULT_ARR
for tag_idx in ${!ARR_TAGS[@]}; do
set -- ${ARR_TAGS[tag_idx]}
str_act="$1" tag_name="$2"
case "${str_act}" in
open)
for i in $(arr_indicies "ARR_COMMENT_NO_${tag_idx}"); do
eval 'RESULT_ARR[IDX++]="# ${ARR_COMMENT_NO_'${tag_idx}'[i]}"'
done
attr_indicies=$(arr_indicies "ARR_ATTR_NO_${tag_idx}")
if ((root_found == 0)); then
[[ ${tag_name} = ${str_root_tag} ]] || {
log -E "Root tag needed"
return 1
}
while read -r str_attr_name str_attr_val; do
case "${str_attr_name}" in
syntax_version)
str_syntax_version="${str_attr_val}"
[[ ${str_syntax_version} = +([[:digit:]]).+([[:digit:]]) ]] || {
log -E "Invalid format for \`syntax_version'."
return 1
}
if ! lsearch "${str_syntax_version}" "${RULEBLOCK_SYNTAX_VERSION_ARRAY[@]}"; then
log -E "Incompatible ruleblock syntax version \`${str_syntax_version}'."
return 1
fi
;;
name)
str_type="${str_attr_val}"
[[ ${str_type} = ${str_root_attr} ]] || {
log -E "Expected root attribute \`${str_root_attr}'."
return 1
}
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
[[ ${str_syntax_version} ]] || { log -E "Syntax version attribute is required."; return 1; }
[[ ${str_type} ]] || { log -E "Root \`name' attribute is required."; return 1; }
root_found=1 tag_depth=1
else
[[ ${tag_name} = ${str_valid_tags} ]] || {
log -E "Tag not valid for \`${str_root_tag}'."
return 1
}
((++tag_depth <= tag_max_depth)) || {
log -E "Max depth is ${tag_max_depth}."
return 1
}
while read -r str_attr_name str_attr_val; do
case "${str_attr_name}" in
"${str_valid_attr}")
ruleblock_name="${str_attr_val}"
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
[[ ${ruleblock_name} ]] || { log -E "No ruleblock name provided."; return 1; }
[[ $DEBUG_INFO ]] && pr_str "Ruleblock: '${ruleblock_name}' opened"
for i in ${!RULEBLOCK_NAMES_ARRAY[@]}; do # check for duplicate ruleblock names
if [[ ${RULEBLOCK_NAMES_ARRAY[i]} = ${ruleblock_name} ]]; then
log -E "Duplicate ruleblock name \`${ruleblock_name}'."
return 1
fi
done
RULEBLOCK_NAMES_ARRAY[${#RULEBLOCK_NAMES_ARRAY[@]}]="${ruleblock_name}"
TEMP_RULEBLOCK_NAMES_ARRAY[${#TEMP_RULEBLOCK_NAMES_ARRAY[@]}]="${ruleblock_name}"
RESULT_ARR[IDX++]="${ruleblock_name}=("
rcount=0
for i in $(arr_indicies "ARR_CONT_NO_${tag_idx}"); do
eval 'RESULT_ARR[IDX++]=['$((rcount++))']="\"${ARR_CONT_NO_'${tag_idx}'['${i}']}\""'
[[ $DEBUG_INFO ]] && eval 'printf "%s\n" "Adding rule: ${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
done
RESULT_ARR[IDX++]=")"
fi
;;
close)
if ((--tag_depth == 0)); then
root_found=0
str_type="" str_syntax_version=""
fi
for i in $(arr_indicies "ARR_COMMENT_NO_${tag_idx}"); do
eval 'RESULT_ARR[IDX++]="# ${ARR_COMMENT_NO_'${tag_idx}'[i]}"'
done
if [[ ${tag_name} == ruleblock ]]; then
[[ $DEBUG_INFO ]] && pr_str "Ruleblock '${ruleblock_name}' closed"
fi
;;
esac
done
# print collected array assignments
if ((${#RESULT_ARR[@]})); then
printf "%s\n" "${RESULT_ARR[@]}" >&7
else
log -W "No ruleblocks found."
return 0
fi
} # -------------------------------------------------------------------------
xml_interpreter_rules() {
local -i root_found=0 tag_depth=0 IDX=0
local str_root_tag="ip_array_root" str_root_attr="iptables_rules"
local str_act tag_name str_attr str_attr_name str_attr_val attr_indicies str_cont tmp_content cont_indicies str_app_opts str_type str_set str_file
local template_name ruleblock_name reverse_mode reverse_condition
local -i tag_idx opt_idx attr_members cont_members opt_list_count opt_count global_opts crule_opts templ_idx rblock_idx c_rule q_rule t_rule pf_rule g_rule set_rule elem_rule
local -a RESULT_ARR ARR_GLOBAL_OPTS ARR_TMP_GLOB_TEMPLATE ARR_GLOB_TMP_RULEBLOCK ARR_CRULE_OPTS arr_set_opts
for tag_idx in ${!ARR_TAGS[@]}; do
set -- ${ARR_TAGS[tag_idx]}
str_act="$1" tag_name="$2"
case "${str_act}" in
open)
for i in $(arr_indicies "ARR_COMMENT_NO_${tag_idx}"); do
eval 'RESULT_ARR[IDX++]="#${ARR_COMMENT_NO_'${tag_idx}'[i]}"'
done
attr_members=$(arr_members_sum "ARR_ATTR_NO_${tag_idx}")
attr_indicies=$(arr_indicies "ARR_ATTR_NO_${tag_idx}")
cont_members=$(arr_members_sum "ARR_CONT_NO_${tag_idx}")
cont_indicies=$(arr_indicies "ARR_CONT_NO_${tag_idx}")
if ((root_found == 0)); then
if [[ $tag_name != $str_root_tag ]]; then
log -E "Root tag \`${str_root_tag}' needed."
return 1
fi
while read -r str_attr_name str_attr_val; do
case "${str_attr_name}" in
syntax_version)
str_syntax_version="${str_attr_val}"
[[ ${str_syntax_version} = +([[:digit:]]).+([[:digit:]]) ]] || {
log -E "Invalid format for \`syntax_version'."
return 1
}
lsearch "${str_syntax_version}" "${RULEFILE_SYNTAX_VERSION_ARRAY[@]}" || {
log -E "Incompatible rule file syntax version \`${str_syntax_version}'."
return 1
}
;;
name)
str_type="${str_attr_val}"
[[ ${str_type} = ${str_root_attr} ]] || {
log -E "Expected root attribute \`${str_root_attr}'."
return 1
}
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
[[ ${str_syntax_version} ]] || { log -E "Syntax version attribute is required."; return 1; }
[[ ${str_type} ]] || { log -E "Root \`name' attribute is required."; return 1; }
root_found=1 tag_depth=1 global_opts=0 opt_list_count=0
else
let ++tag_depth
case "${tag_name}" in
s|set|ipset|ipsets)
((pf_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a public funtion."; return 1; }
((c_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a combined_rule."; return 1; }
((q_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a rule."; return 1; }
((t_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a template_rule."; return 1; }
((global_opts == 0)) || { log -E "Tag \`${tag_name}' is not valid within a global iptables tag."; return 1; }
((set_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within an ipset tag."; return 1; }
((cont_members > 0)) && {
log -E "Only attributes are allowed in tag \`${tag_name}'."
return 1
}
((attr_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
set_rule=1 str_set="" str_stype="" str_file="" arr_set_opts=()
while read -r str_attr_name str_attr_val; do
case "$str_attr_name" in
import_xml)
str_file="$str_attr_val"
if ! [[ -r $str_file ]]; then
log -x "$str_file"
return 1
fi
;;
name)
str_set="$str_attr_val"
;;
type)
str_stype="$str_attr_val"
;;
timeout|maxelem|hashsize|family|netmask|range|size)
arr_set_opts[${#arr_set_opts[@]}]="$str_attr_name $str_attr_val"
;;
opt|opts|options)
arr_set_opts[${#arr_set_opts[@]}]="$str_attr_val"
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
if [[ $str_file ]]; then
( xml_parser 'ipsets' < "$str_file" ) || return 1
else
if [[ -z $str_set ]]; then
log -E "\`name' attribute is needed in an ipset tag."
return 1
elif [[ -z $str_stype ]]; then
if ! check_set_exist "$str_set"; then # assume we want to add elements
log -e "\`type' attribute is empty for set \`$str_set'. Assumuning adding of elements."
fi
else
RESULT_ARR[IDX++]="ipset_create $str_set $str_stype ${arr_set_opts[*]}"
fi
fi
;;
e|elem|element|elements)
((set_rule == 1)) || { log -E "Tag \`${tag_name}' is only valid within an ipset tag."; return 1; }
((elem_rule == 0)) || { log -E "Only one \`${tag_name}' tag is valid within an ipset tag."; return 1; }
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
elem_rule=1
str_regex='.*comment[[:blank:]]+([\"])(.*)([\"]).*' # to match comments
if ((attr_members > 0)); then
while read -r str_attr_name str_attr_val; do
case "$str_attr_name" in
import_plain) # import elements from file - newline separated entries
if ! [[ -r $str_attr_val ]]; then
log -E "File to import set elements from does not exist: \`$str_attr_val'."
return 1
fi
while read -r -t $GLOBAL_READ_TIMEOUT; do
REPLY="${REPLY%%#*}" # remove comments [#]
[[ $REPLY && $REPLY != +([[:blank:]]) ]] || continue
if [[ $REPLY =~ $str_regex ]]; then
# catch comments and escape the string containing spaces
RESULT_ARR[IDX++]="ipset_add $str_set ${REPLY/"${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"/$(printf '%q\n' \""${BASH_REMATCH[2]}\"")}"
else
RESULT_ARR[IDX++]="ipset_add $str_set $REPLY"
fi
done < "$str_attr_val"
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
fi
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
if [[ $str_cont =~ $str_regex ]]; then
# catch comments and escape the string containing spaces
RESULT_ARR[IDX++]="ipset_add $str_set ${str_cont/"${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"/$(printf '%q\n' \""${BASH_REMATCH[2]}\"")}"
else
RESULT_ARR[IDX++]="ipset_add $str_set $str_cont"
fi
done
;;
f|pub_func)
((pf_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a public funtion."; return 1; }
((c_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a combined_rule."; return 1; }
((q_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a rule."; return 1; }
((t_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a template_rule."; return 1; }
((set_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within an ipset tag."; return 1; }
((attr_members > 0 && cont_members > 0)) && {
log -E "Either attribute or content is allowed in tag \`${tag_name}', not both."
return 1
}
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
pf_rule=1
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
lsearch "${str_cont%%+([[:blank:]])*}" $(show_array_column PUBLIC_FUNCTION_LIST) || {
log -E "Unkown public function name \`${str_cont%%+([[:blank:]])*}'."
return 1
}
RESULT_ARR[IDX++]="$(re_normalize str_cont)"
done
if ((attr_members > 0)); then
while read -r str_attr_name str_attr_val; do
case "${str_attr_name}" in
name)
lsearch "${str_attr_val}" $(show_array_column PUBLIC_FUNCTION_LIST) || {
log -E "Unkown public function name \`${str_attr_val}'."
return 1
}
RESULT_ARR[IDX++]="${str_attr_val}"
;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
fi
;;
r|rule)
((pf_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a public funtion."; return 1; }
((c_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a combined_rule."; return 1; }
((t_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a template_rule."; return 1; }
((set_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within an ipset tag."; return 1; }
((attr_members > 0 && cont_members > 0)) && {
log -E "Either attribute or content is allowed in tag \`${tag_name}', not both."
return 1
}
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
q_rule=1
local str_app_opts=""
for i in ${!ARR_GLOBAL_OPTS[@]}; do
str_attr_name="${ARR_GLOBAL_OPTS[i]%%=*}" str_attr_val="${ARR_GLOBAL_OPTS[i]#*=}"
str_app_opts+="${str_attr_name}=\"${str_attr_val}\" "
done
if ((cont_members > 0)); then
tmp_content=""
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
eval 'set -- '"$(re_normalize str_cont)"
str_cont=""
for str_attr in "$@"; do
str_cont+="${str_attr%%=*}=\"${str_attr#*=}\" "
done
tmp_content+="$(re_normalize str_cont)"
done
RESULT_ARR[IDX++]="process_ipt_quickrule ${str_app_opts}${tmp_content%[[:blank:]]}"
fi
if ((attr_members > 0)); then
str_cont=""
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
str_cont+="${str_attr_name}=\"$(re_normalize str_attr_val)\" "
done
RESULT_ARR[IDX++]="process_ipt_quickrule ${str_app_opts}${str_cont%[[:blank:]]}"
fi
;;
tr|template_rule)
((pf_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a public funtion."; return 1; }
((c_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a combined_rule."; return 1; }
((q_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a rule."; return 1; }
((set_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within an ipset tag."; return 1; }
((attr_members > 0 && cont_members > 0)) && {
log -E "Either attribute or content is allowed in tag \`${tag_name}', not both."
return 1
}
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
t_rule=1
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
template_name="${str_cont%%[[:blank:]]*}"
((${#ARR_GLOBAL_OPTS[@]} == 0)) || {
log -W "Global options \`${ARR_GLOBAL_OPTS[@]}' will not be applied to the template \`${template_name}'."
}
RESULT_ARR[IDX++]="process_ipt_ruleblock ${str_cont}"
done
if ((attr_members > 0)); then
template_name="" ruleblock_name=""
while read -r str_attr_name str_attr_val; do
case "${str_attr_name}" in
template) template_name="${str_attr_val}" ;;
ruleblock) ruleblock_name="${str_attr_val}" ;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
[[ ${template_name} || ${ruleblock_name} ]] || {
log -E "The \`${tag_name}' tag requires \`template_name' and \`ruleblock_name' attributes."
return 1
}
((${#ARR_GLOBAL_OPTS[@]} == 0)) || {
log -W "Global options \`${ARR_GLOBAL_OPTS[@]}' will not be applied to the template \`${template_name}'."
}
RESULT_ARR[IDX++]="process_ipt_ruleblock ${template_name} ${ruleblock_name}"
fi
;;
cr|combined_rule)
((pf_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a public funtion."; return 1; }
((q_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a rule."; return 1; }
((t_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a template_rule."; return 1; }
((set_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within an ipset tag."; return 1; }
((cont_members > 0)) && { log -E "Only attributes are allowed in tag \'${tag_name}'."; return 1; }
templ_idx=0 rblock_idx=0 opt_idx=0 c_rule=1 opt_list_count=0 opt_count=0 crule_opts=0 max_optlist=""
reverse_mode="" reverse_condition=""
ARR_TMP_GLOB_TEMPLATE=() ARR_GLOB_TMP_RULEBLOCK=()
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
case "${str_attr_name}" in
msg) RESULT_ARR[IDX++]="log -I \"${str_attr_val}\"" ;;
reverse_mode) reverse_mode="${str_attr_val}" max_optlist=1 ;;
reverse_condition) reverse_condition="${str_attr_val}" ;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done
;;
opt|opts|option|options|optlist)
((pf_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a public funtion."; return 1; }
((q_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a rule."; return 1; }
((t_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within a template_rule."; return 1; }
((c_rule == 1)) || { log -E "Tag \`${tag_name}' is only valid within a \`combined_rule'."; return 1; }
((set_rule == 0)) || { log -E "Tag \`${tag_name}' is not valid within an ipset tag."; return 1; }
((opt_count == 0)) || { log -E "There is already an open option list."; return 1; }
((attr_members > 0 && cont_members > 0)) && {
log -E "Either attribute or content is allowed in tag \`${tag_name}', not both."
return 1
}
((attr_members == 0 && cont_members == 0)) && { log -E "Empty tag \`${tag_name}'."; return 1; }
opt_idx=0
if [[ ${reverse_mode} ]]; then
((opt_list_count <= max_optlist)) || opt_list_count=0
if ((opt_list_count == 0)); then
templ_idx=0 rblock_idx=0
ARR_TMP_GLOB_TEMPLATE=() ARR_GLOB_TMP_RULEBLOCK=()
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="TEMPLATE_TEMP_C_RULE=("
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"reverse_mode=${reverse_mode}\""
[[ ${reverse_condition} ]] && ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"reverse_condition=\\\"${reverse_condition}\\\"\""
# "
ARR_GLOB_TMP_RULEBLOCK[rblock_idx++]="RULEBLOCK_TEMP_C_RULE=("
fi
for i in ${!ARR_GLOBAL_OPTS[@]}; do
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${ARR_GLOBAL_OPTS[i]%%=*}\""
((opt_list_count == 0)) && ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${ARR_GLOBAL_OPTS[i]#*=} "
# "
done
for i in ${!ARR_CRULE_OPTS[@]}; do
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${ARR_CRULE_OPTS[i]%%=*}\""
((opt_list_count == 0)) && ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${ARR_CRULE_OPTS[i]#*=} "
# "
done
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
eval 'set -- '"$(re_normalize str_cont)"
for str_attr in "$@"; do
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
if ! ((opt_list_count == 1)) && [[ $str_attr_name = empty ]]; then
_validate_ipt_option_name "$str_attr_name" || return 1
fi
((opt_list_count == 1)) && [[ ${str_attr_val} = empty ]] && str_attr_name="${str_attr_val}"
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${str_attr_name}\""
# "
((opt_list_count == 0)) && ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${str_attr_val} "
done
((opt_list_count == 0)) && ARR_GLOB_TMP_RULEBLOCK[rblock_idx]="\"${ARR_GLOB_TMP_RULEBLOCK[rblock_idx]%[[:blank:]]}\""
# "
done
if ((attr_members > 0)); then
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
if ! ((opt_list_count == 1)) && [[ $str_attr_name = empty ]]; then
_validate_ipt_option_name "$str_attr_name" || return 1
fi
((opt_list_count == 1)) && [[ ${str_attr_val} = empty ]] && str_attr_name="${str_attr_val}"
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${str_attr_name}\""
((opt_list_count == 0)) && ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${str_attr_val} "
# "
done
((opt_list_count == 0)) && ARR_GLOB_TMP_RULEBLOCK[rblock_idx]="\"${ARR_GLOB_TMP_RULEBLOCK[rblock_idx]%[[:blank:]]}\""
# "
fi
if ((opt_list_count == max_optlist)); then
for i in ${!ARR_TMP_GLOB_TEMPLATE[@]}; do
RESULT_ARR[IDX++]="${ARR_TMP_GLOB_TEMPLATE[i]}"
done
RESULT_ARR[IDX++]=")"
for i in ${!ARR_GLOB_TMP_RULEBLOCK[@]}; do
RESULT_ARR[IDX++]="${ARR_GLOB_TMP_RULEBLOCK[i]}"
done
RESULT_ARR[IDX++]=")"
RESULT_ARR[IDX++]="process_ipt_ruleblock 'TEMPLATE_TEMP_C_RULE' 'RULEBLOCK_TEMP_C_RULE'"
fi
else
templ_idx=0 rblock_idx=0 opt_list_count=0
ARR_TMP_GLOB_TEMPLATE=() ARR_GLOB_TMP_RULEBLOCK=()
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="TEMPLATE_TEMP_C_RULE=("
ARR_GLOB_TMP_RULEBLOCK[rblock_idx++]="RULEBLOCK_TEMP_C_RULE=("
for i in ${!ARR_GLOBAL_OPTS[@]}; do
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${ARR_GLOBAL_OPTS[i]%%=*}\""
# "
if [[ ${ARR_GLOBAL_OPTS[i]#*=} = *[[:blank:]]* ]]; then
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="'${ARR_GLOBAL_OPTS[i]#*=}' "
else
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${ARR_GLOBAL_OPTS[i]#*=} "
fi
done
for i in ${!ARR_CRULE_OPTS[@]}; do
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${ARR_CRULE_OPTS[i]%%=*}\""
# "
if [[ ${ARR_CRULE_OPTS[i]#*=} = *[[:blank:]]* ]]; then
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="'${ARR_CRULE_OPTS[i]#*=}' "
else
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${ARR_CRULE_OPTS[i]#*=} "
fi
done
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
eval 'set -- '"$(re_normalize str_cont)"
for str in "$@"; do
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${str%%=*}\""
# "
if [[ ${str#*=} = *[[:blank:]]* ]]; then
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="'${str#*=}' "
else
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${str#*=} "
fi
done
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]="\"${ARR_GLOB_TMP_RULEBLOCK[rblock_idx]%[[:blank:]]}\""
# "
done
if ((attr_members > 0)); then
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_name="${str_attr%%=*}" str_attr_val="${str_attr#*=}"
ARR_TMP_GLOB_TEMPLATE[templ_idx++]="\"option_list_${opt_list_count}[$((opt_idx++))]=${str_attr_name%%=*}\""
# "
if [[ ${str_attr_val} = *[[:blank:]]* ]]; then
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="\\\"${str_attr_val}\\\" "
# "
else
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]+="${str_attr_val} "
fi
done
ARR_GLOB_TMP_RULEBLOCK[rblock_idx]="\"${ARR_GLOB_TMP_RULEBLOCK[rblock_idx]%[[:blank:]]}\""
# "
fi
for i in ${!ARR_TMP_GLOB_TEMPLATE[@]}; do
RESULT_ARR[IDX++]="${ARR_TMP_GLOB_TEMPLATE[i]}"
done
RESULT_ARR[IDX++]=")"
for i in ${!ARR_GLOB_TMP_RULEBLOCK[@]}; do
RESULT_ARR[IDX++]="${ARR_GLOB_TMP_RULEBLOCK[i]}"
done
RESULT_ARR[IDX++]=")"
RESULT_ARR[IDX++]="process_ipt_ruleblock 'TEMPLATE_TEMP_C_RULE' 'RULEBLOCK_TEMP_C_RULE'"
fi
let ++opt_count ++opt_list_count
;;
*)
# global iptables options (to apply to others, or build rules by their own (on close))
((opt_count == 0)) || { log -E "There is an open option list."; return 1; }
_validate_ipt_option_name "${tag_name}" || return 1
if ((${#attr_indicies} > 1 && ${#cont_indicies} > 1)); then
log -E "Attribute and content in tag #$((tag_idx+1)): \`$tag_name'. Only one of them is valid."
return 1
elif ((${#attr_indicies} > 1)); then
log -E "Too many attributes in tag #$((tag_idx+1)): \`$tag_name'. \
There is only one attribute allowed in a global iptables argument tag. \
The attribute name will ALWAYS BE REPLACED BY THE NAME OF THE TAG!!!"
return 1
elif ((${#cont_indicies} > 1)); then
log -E "Content string out of bounds in tag #$((tag_idx+1)): \`$tag_name'. \
There is only one content string allowed in a global iptables argument tag."
return 1
fi
for i in ${cont_indicies}; do
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'['${i}']}"'
if ((c_rule == 0)); then
ARR_GLOBAL_OPTS[global_opts]="${tag_name}=${str_cont}"
else
ARR_CRULE_OPTS[crule_opts]="${tag_name}=${str_cont}"
fi
done
for i in ${attr_indicies}; do
eval 'str_attr="${ARR_ATTR_NO_'${tag_idx}'['${i}']}"'
str_attr_val="${str_attr#*=}"
if ((c_rule == 0)); then
ARR_GLOBAL_OPTS[global_opts]="${tag_name}=${str_attr_val}"
else
ARR_CRULE_OPTS[crule_opts]="${tag_name}=${str_attr_val}"
fi
done
if ((c_rule == 0)); then
let ++global_opts
g_rule=0 # not a global iptables arguments only rule
else
let ++crule_opts
fi
;;
esac
fi
;;
close)
if ((--tag_depth == 0)); then
root_found=0
str_type="" str_syntax_version=""
else
case "${tag_name}" in
f|pub_func) pf_rule=0 ;;
s|set|ipset) set_rule=0 ;;
e|elem|element|elements) elem_rule=0 ;;
r|rule)
q_rule=0
if ((${#ARR_GLOBAL_OPTS[@]})); then g_rule=1; fi # remember we used the global iptables args
;;
tr|template_rule) t_rule=0 ;;
opt|opts|option|options|optlist) let --opt_count ;;
cr|combined_rule)
if [[ ${reverse_mode} ]]; then
(( $(( $((opt_list_count / 2)) * 2)) == opt_list_count)) || {
log -E "\`reverse_mode' requires 2 option lists."
return 1
}
fi
reverse_mode="" reverse_condition=""
c_rule=0
;;
*)
if ((c_rule == 0)); then
if ((q_rule == 0 && t_rule == 0)); then # not a combined, template or quick rule - must be a global iptables args rule
if ((g_rule == 0)); then # has not been done before
str_app_opts=""
for i in ${!ARR_GLOBAL_OPTS[@]}; do
str_attr_name="${ARR_GLOBAL_OPTS[i]%%=*}" str_attr_val="${ARR_GLOBAL_OPTS[i]#*=}"
str_app_opts+="${str_attr_name}=\"${str_attr_val}\" "
done
RESULT_ARR[IDX++]="process_ipt_quickrule ${str_app_opts}"
g_rule=1 # remember that we already processed that one
fi
fi
unset ARR_GLOBAL_OPTS[--global_opts]
else
unset ARR_CRULE_OPTS[--crule_opts]
fi
esac
fi
for i in $(arr_indicies "ARR_COMMENT_NO_${tag_idx}"); do
eval 'RESULT_ARR[IDX++]="#${ARR_COMMENT_NO_'${tag_idx}'[i]}"'
done
;;
esac
done
if ((${#RESULT_ARR[@]})); then
printf "%s\n" "${RESULT_ARR[@]}" >&7
else
log -W "No rules found."
return 0
fi
} # -------------------------------------------------------------------------
xml_interpreter_ipsets() {
local -i tag_idx ipset_root_found=0 in_header=0 IDX=0
local attr_indicies str_act tag_name str_set str_type
local -a arr_header arr_element RESULT_ARR
for tag_idx in ${!ARR_TAGS[@]}; do
set -- ${ARR_TAGS[tag_idx]}
str_act="$1" tag_name="$2"
case "$str_act" in
open)
if ((ipset_root_found == 0)) && [[ $tag_name != ipsets ]]; then
log -E "Root tag \`ipsets' needed."
return 1
fi
attr_indicies=$(arr_indicies "ARR_ATTR_NO_${tag_idx}")
eval 'str_cont="${ARR_CONT_NO_'${tag_idx}'[*]}"'
case "$tag_name" in
ipsets) ipset_root_found=1
;;
ipset) str_set="" str_type="" str_options="" arr_header=()
while read -r str_attr_name str_attr_val; do
case "$str_attr_name" in
name) str_set="$str_attr_val" ;;
*) log -E "Attribute \`${str_attr_name}' is not valid within the \`${tag_name}' tag."
return 1
esac
done < <(_get_attribute_data)
;;
type)
if [[ -z $str_set ]]; then
log -E "Need \`ipset' tag opened before defining \`$tag_name'."
return 1
fi
str_type="$str_cont"
;;
header)
if [[ -z $str_type ]]; then
log -E "Need \`type' tag before defining \`$tag_name'."
return 1
fi
in_header=1
;;
family|hashsize|maxelem|range|netmask)
if ! ((in_header)); then
log -E "Tag \`$tag_name' is only valid inside a \`header' tag."
return 1
fi
arr_header[${#arr_header[@]}]="$tag_name $str_cont"
;;
counters|skbinfo|forceadd)
if ! ((in_header)); then
log -E "Tag \`$tag_name' is only valid inside a \`header' tag."
return 1
fi
str_options+=" $tag_name"
;;
memsize|references|revision)
: # ignoring those
;;
timeout)
if ! ((in_header || in_member)); then
log -E "Tag \`$tag_name' is only valid inside a \`header' or \`member' tag."
return 1
fi
if ((in_header)); then
arr_header[${#arr_header[@]}]="$tag_name $str_cont"
elif ((in_member)); then
arr_element[${#arr_element[@]}]="$tag_name $str_cont"
fi
;;
member)
if ((in_header || in_member)); then
log -E "Tag \`$tag_name' is only valid inside an \`ipset' tag."
return 1
fi
in_member=1 arr_element=() str_comment=""
;;
elem)
if ! ((in_member)); then
log -E "Tag \`$tag_name' is only valid inside a \`member' tag."
return 1
fi
arr_element[${#arr_element[@]}]="$str_cont"
;;
comment)
if ! ((in_header || in_member)); then