-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip-array_interactive_functions
1382 lines (1343 loc) · 45.8 KB
/
ip-array_interactive_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 INTERACTIVE FUNCTIONS
#
# ------------------------------------------------------------------------- #
rdia() { # run dialog program
"$DIALOG_PROG" "${arr_title[@]:-"$ME interactive mode"}" "$@" 2>&1 > /dev/tty
} # -------------------------------------------------------------------------
dia_ibox() { # input box
rdia --inputbox "$1" $w_height $w_with "$2"
} # -------------------------------------------------------------------------
dia_mbox() { # msg box
rdia --msgbox "$1" $w_height $w_with
}
dia_yn() { # yesno
rdia --yesno "$1" $w_height $w_with
} # -------------------------------------------------------------------------
dia_menu() { # menu
if [[ $1 = --noitem ]]; then
local str_cmd="--noitem"
shift
else
local str_cmd=""
fi
local str="$1"
shift
rdia $str_cmd --menu "$str" $w_height $w_with $l_height "$@"
} # -------------------------------------------------------------------------
dia_clist() { # checklist
if [[ $1 = --noitem ]]; then
local str_cmd="--noitem"
shift
else
local str_cmd=""
fi
local str="$1"
shift
rdia $str_cmd --separate-output --checklist "$str" $w_height $w_with $l_height "$@"
} # -------------------------------------------------------------------------
dia_rlist() { # radiolist
if [[ $1 = --noitem ]]; then
local str_cmd="--noitem"
shift
else
local str_cmd=""
fi
local str="$1"
shift
rdia $str_cmd --radiolist "$str" $w_height $w_with $l_height "$@"
} # -------------------------------------------------------------------------
ia_needopt() {
dia_mbox "The option is mandatory. Quitting and returning to main screen."
} # -------------------------------------------------------------------------
build_wt_dummy_list() { # dialog and whiptail behave differently
while (($#)); do
printf "%s item\n" "$1" # add dummy item entry
shift
done
} # -------------------------------------------------------------------------
_add_rb_line() { # sub function of iactive_ruleblock_builder() - add a line to a ruleblock
local idx=0
arr_line=()
if ! ((${#arr_templ[@]})); then
dia_mbox "Template \`$t_name' is not defined!\nMaybe you need to enable the table in the main config?\nOr you may run parse-xml first?"
return 1
fi
for i in ${!arr_templ[*]}; do
arr_line[idx]=$( dia_ibox "Enter a value for ${arr_templ[i]%%=*} ${arr_templ[i]#*=}" )
(($? == 0)) || return 1
if [[ ${arr_line[idx]} = *[[:blank:]]* ]]; then
arr_line[idx]="\\\"${arr_line[idx]}\\\"" # :"
fi
let idx++
done
} # -------------------------------------------------------------------------
_edit_rb_line() { # sub function of iactive_ruleblock_builder() - edit an existing ruleblock line
if [[ $DIALOG_PROG = *dialog ]] && ((${#arr_rb_tmp[@]} == 1)); then
dia_mbox "Found only one line. Selecting it."
arr_line=( "${arr_rb_tmp[0]//\"/\\\\\"}" )
elif [[ $DIALOG_PROG = *whiptail ]] && ((${#arr_rb_tmp[@]} == 2)); then
dia_mbox "Found only one line. Selecting it."
arr_line=( "${arr_rb_tmp[0]//\"/\\\\\"}" )
else
arr_line=( $(dia_rlist --noitem "Choose a line to edit" "${arr_rb_tmp[@]//\"/\\\\\"}") )
(($? == 0)) || return 1
fi
local -i lnum=$(( ${arr_line[0]%%:*} - 1 )) # real position in array
local -a arr_line_new=()
eval set -- "${arr_rb[lnum]}"
for ((i=0 , x=1; i < $#; i++ , x++)); do # edit existing ruleblock line
arr_line_new[i]=$(dia_ibox "Enter a value for ${arr_tmp[i]#*=}.\nOld value: \`${!x}'" "${!x}")
(($? == 0)) || return 1
if [[ ${arr_line_new[i]} = *[[:blank:]]* ]]; then
arr_line_new[i]="\\\"${arr_line_new[i]}\\\"" # :"
fi
done
arr_rb_tmp[lnum + lnum]="$(($lnum + 1)): ${arr_line_new[@]}"
arr_rb[lnum]="${arr_line_new[@]}"
} # -------------------------------------------------------------------------
_add_conf_var() { # sub function of iactive_config_builder() - add config variable
x=0
arr_tmp=( $(printf "%s\n" "${arr_names[@]}"|run_sort) )
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_tmp=( $(build_wt_dummy_list "${arr_tmp[@]}") )
fi
v_name=$(dia_menu --noitem "Choose a variable" "${arr_tmp[@]}")
if (($? == 0)) && [[ $v_name ]]; then
for i in ${!arr_names[@]}; do
if [[ ${arr_names[i]} = $v_name ]]; then
set -- ${arr_conf[i]}
v_type=$2 # declare name of config entry and its type
break
fi
done
x=0 arr_content=()
if [[ $v_type = @(array|var) ]]; then
v_msg=""
elif [[ $v_type = bool ]]; then
v_msg=" (boolean)"
elif [[ $v_type = int ]]; then
v_msg=" (integer)"
fi
if [[ -s ${HELP_DIR}/conf_vars/${v_name}.txt ]]; then
v_help="\nHelp for ${v_name}:\n$("$CAT" "${HELP_DIR}/conf_vars/${v_name}.txt")\n"
else
v_help=""
fi
if [[ $v_type = array ]]; then
while (($? == 0)); do
arr_content[x++]=$(dia_ibox \
"Enter (more) content for variable $v_name.\nCancel to quit.${v_help}") || {
unset arr_content[x-1]
break
}
done
if ((${#arr_content[*]})); then
if [[ $v_help ]]; then
"$CAT" "${HELP_DIR}/conf_vars/${v_name}.txt" >> "$c_file"
fi
printf '%s=\"\n' "$v_name" >> "$c_file"
printf "%s\n" "${arr_content[@]}" >> "$c_file"
printf '\"\n\n' >> "$c_file"
unset arr_names[i]
fi
else
arr_content[0]=$(dia_ibox \
"Enter content for variable ${v_name}${v_msg}${v_help}")
if (($? == 0)) && [[ ${arr_content[0]} ]]; then
if [[ $v_help ]]; then
"$CAT" "${HELP_DIR}/conf_vars/${v_name}.txt" >> "$c_file"
fi
printf '%s=\"%s\"\n\n' "$v_name" "${arr_content[0]}" >> "$c_file"
unset arr_names[i]
fi
fi
fi
dia_yn "Do you want to add another variable?"
} # -------------------------------------------------------------------------
_remove_dupe_templ_entry() { # sub function of iactive_template_builer - remove dupes from the list
for y in ${!arr_tmp[@]}; do
[[ ${arr_tmp[y]} = m_set ]] && continue # set match is allowed multiple times
if [[ ${arr_tmp[y]} = $1 ]]; then
unset arr_tmp[y]
if [[ $DIALOG_PROG = *whiptail ]]; then
unset arr_tmp[y+1]
fi
fi
done
} # -------------------------------------------------------------------------
_add_single_rule_entries () { # sub function of _ia_add_rules() - Collect entries for <rule> or <optlist>
arr_single_rule=()
x=0 arr_tmp=( "${x_args[@]}" )
while (($? == 0)); do
str_lhs=$(dia_menu --noitem "Choose an iptables argument." "${arr_tmp[@]}")
if (($? == 0)); then
for i in ${!arr_tmp[*]}; do # remove dupes from the list
if [[ ${arr_tmp[i]} = $str_lhs ]]; then
unset arr_tmp[i]
if [[ $DIALOG_PROG = *whiptail ]]; then
unset arr_tmp[i+1]
fi
break
fi
done
str_rhs="$( dia_ibox "Enter a value for $str_lhs")"
if (($? == 0)) && [[ $str_rhs ]]; then
arr_single_rule[x++]="$str_lhs=\"$str_rhs\"" #"
dia_yn "Do you want to add another iptables argument?"
else
return 1
fi
else
return 1
fi
done
} # -------------------------------------------------------------------------
_add_set_element() {
local str_elem
local -a arr_set_element=()
x=0
str_elem="$(dia_ibox "Enter the element")"
if (($? == 0)) && [[ $str_elem ]]; then
arr_set_element[x++]="$str_elem"
else
return
fi
arr_tmp=( comment bytes packets timeout skbmark skbprio skbqueue nomatch )
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_tmp=( $(build_wt_dummy_list "${arr_tmp[@]}") )
fi
while (($? == 0)); do
((${#arr_tmp[@]})) || break
str_lhs=$(dia_menu --noitem "Choose an element option.\nCancel for none." "${arr_tmp[@]}")
if (($? == 0)); then
for i in ${!arr_tmp[*]}; do # remove dupes from the list
if [[ ${arr_tmp[i]} = $str_lhs ]]; then
unset arr_tmp[i]
if [[ $DIALOG_PROG = *whiptail ]]; then
unset arr_tmp[i+1]
fi
break
fi
done
if [[ $str_lhs != nomatch ]]; then
str_rhs="$(dia_ibox "Enter a value for $str_lhs")"
if (($? == 0)) && [[ $str_rhs ]]; then
arr_set_element[x++]="$str_lhs"
if [[ $str_rhs = *+([[:blank:]])* ]]; then
arr_set_element[x++]="\"$str_rhs\"" #"
else
arr_set_element[x++]="$str_rhs"
fi
fi
else
arr_set_element[x++]="$str_lhs"
fi
else
false
fi
done
if ((${#arr_set_element[*]})); then
arr_content[content_idx++]="${str_indent} <element> ${arr_set_element[*]} </element>"
fi
} # -------------------------------------------------------------------------
_ia_add_rules() { # sub function of iactive_rule_builder() - add different rule types
local str_lhs str_rhs str str_opt str_tmp str_set str_type str_header
local -a arr_single_rule arr_attr arr_header arr_types arr_tmp
local -i i x
local str_rule=$(dia_menu "Select the type of rule action" \
"Iptables tag" "iptables argument tag" \
"Rule" "Single iptables rule" \
"Grouped" "Combined tags iptables rule" \
"Template" "Template based iptables rule" \
"Function" "Public function" \
"Set" "Create an ipset set / add elements" \
"Close" "Close last opened tag" \
"Enter comment" "Add a comment" \
"View" "View the created rules"
)
if [[ $str_rule = Template ]]; then
if ((${#arr_templ[*]} == 0)); then
if [[ -r ${TEMPLATE_DIR}/__parsed_TEMPLATE_NAMES ]]; then
while read -t $GLOBAL_READ_TIMEOUT; do
if [[ $REPLY != @(BT|BASE_TEMPLATE)_* ]]; then # don't show base templates
arr_templ[i++]="$REPLY"
fi
done < <( "$CAT" "${TEMPLATE_DIR}"/__parsed_TEMPLATE_NAMES|run_sort )
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_templ=( $(build_wt_dummy_list "${arr_templ[@]}") )
fi
else
dia_mbox "Cannot find \`${TEMPLATE_DIR}/__parsed_TEMPLATE_NAMES'. Templates must first be parsed (parse-xml -pt)."
return 0
fi
fi
if ((${#arr_rb[*]} == 0)); then
if [[ -r ${RULEBLOCK_DIR}/__parsed_RULEBLOCK_NAMES ]]; then
if [[ $DIALOG_PROG = *dialog ]]; then
arr_rb=( $( while read -t $GLOBAL_READ_TIMEOUT; do printf "%s\n" "$REPLY"; done < <( "$CAT" "${RULEBLOCK_DIR}"/__parsed_RULEBLOCK_NAMES|run_sort ) ) )
elif [[ $DIALOG_PROG = *whiptail ]]; then
arr_rb=( $( while read -t $GLOBAL_READ_TIMEOUT; do printf "%s off\n" "$REPLY"; done < <( "$CAT" "${RULEBLOCK_DIR}"/__parsed_RULEBLOCK_NAMES|run_sort ) ) )
fi
else
dia_mbox "Cannot find \`${RULEBLOCK_DIR}/__parsed_RULEBLOCK_NAMES'. Ruleblocks must first be parsed (parse-xml -pb)."
return 0
fi
fi
arr_content[content_idx++]="${str_indent} <template_rule>"
while (($? == 0)); do
if [[ $DIALOG_PROG = *dialog ]] && ((${#arr_templ[@]} == 1)); then
dia_mbox "Only one template has been found: \`${arr_templ[0]}'. Selecting it."
t_name="${arr_templ[0]}"
elif [[ $DIALOG_PROG = *whiptail ]] && ((${#arr_templ[@]} == 2)); then
dia_mbox "Only one template has been found: \`${arr_templ[0]}'. Selecting it."
t_name="${arr_templ[0]}"
else
t_name=$(dia_menu --noitem "Choose a template" "${arr_templ[@]}" ) || {
unset arr_content[--content_idx]
return 0
}
fi
# choose one ruleblock
if [[ $DIALOG_PROG = *dialog ]] && ((${#arr_rb[@]} == 1)); then
dia_mbox "Only one ruleblock has been found: \`${arr_rb[0]}'. Selecting it."
r_name="${arr_rb[0]}"
elif [[ $DIALOG_PROG = *whiptail ]] && ((${#arr_rb[@]} == 2)); then
dia_mbox "Only one ruleblock has been found: \`${arr_rb[0]}'. Selecting it."
r_name="${arr_rb[0]}"
else
r_name=$(dia_menu --noitem "Choose a ruleblock" "${arr_rb[@]}" ) || {
unset arr_content[--content_idx]
return 0
}
fi
str_tmp=$( dia_ibox "Enter a template invocation command.\nLeave it empty, or CANCEL for none.")
if [[ $t_name && $r_name ]]; then
arr_content[content_idx++]="${str_indent} $t_name $r_name $str_tmp"
else
dia_mbox "Template or ruleblock definition is missing."
fi
dia_yn "Do you want to add another template_rule line?"
done
arr_content[content_idx++]="${str_indent} </template_rule>"
elif [[ $str_rule = "Iptables tag" ]]; then
arr_tmp=( "${x_args[@]}" )
while (($? == 0)); do
str_tmp=$(dia_menu --noitem "Choose an iptables argument." "${arr_tmp[@]}")
if (($? == 0)); then
for i in ${!arr_tmp[*]}; do # remove dupes from the list
if [[ ${arr_tmp[i]} = $str_tmp ]]; then
unset arr_tmp[i]
if [[ $DIALOG_PROG = *whiptail ]]; then
unset arr_tmp[i+1]
fi
break
fi
done
arr_tags[tag_idx++]="$str_tmp"
str_indent+=" "
str_tmp="${str_indent}<$str_tmp>"
str_tmp+=" $(dia_ibox "Enter a value for $str_tmp")"
if (($? == 0)); then
arr_content[content_idx++]="$str_tmp"
dia_yn "Do you want to add another iptables argument?"
else
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
unset arr_tags[--tag_idx]
false
fi
else
false
fi
done
elif [[ $str_rule = Rule ]]; then
arr_tmp=( "${x_args[@]}" )
_add_single_rule_entries
if ((${#arr_single_rule[*]})); then
arr_content[content_idx++]="${str_indent} <rule> ${arr_single_rule[*]} </rule>"
else
false
fi
elif [[ $str_rule = Grouped ]]; then
i=0
str_indent+=" "
arr_tmp=(msg "Log message" reverse_mode "Define reverse mode" reverse_condition "Condition to be true for activation of reverse mode")
while (($? == 0)); do
if ((${#arr_tmp[*]})); then
str_lhs=$(dia_menu "Choose a combined rule attribute.\nCancel for none." "${arr_tmp[@]}")
if (($? == 0)); then
for x in ${!arr_tmp[*]}; do
if [[ $str_lhs = ${arr_tmp[x]} ]]; then
unset arr_tmp[x]
if [[ $DIALOG_PROG = *whiptail ]]; then
unset arr_tmp[x+1]
fi
break
fi
done
str_rhs="$(dia_ibox "Enter a value for $str_lhs")"
if (($? == 0)) && [[ $str_rhs ]]; then
arr_attr[i++]="${str_lhs}=\"${str_rhs}\"" #"
else
false
fi
else
false
fi
else
false
fi
done
if ((${#arr_attr[*]})); then
arr_content[content_idx++]="${str_indent}<combined_rule ${arr_attr[*]}>"
else
arr_content[content_idx++]="${str_indent}<combined_rule>"
fi
while (($? == 0)); do
_add_single_rule_entries
if ((${#arr_single_rule[*]})); then
arr_content[content_idx++]="${str_indent} <optlist> ${arr_single_rule[*]} </optlist>"
dia_yn "Do you want to add another option list?"
else
false
fi
done
arr_content[content_idx++]="${str_indent}</combined_rule>"
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
elif [[ $str_rule = Function ]]; then
if ((${#arr_pubfunc[*]} == 0)); then x=0
for i in ${!PUBLIC_FUNCTION_LIST[@]}; do
set -- ${PUBLIC_FUNCTION_LIST[i]}
arr_pubfunc[x++]="$1"
shift
arr_pubfunc[x++]="$*"
done
fi
arr_tmp=()
arr_tmp[0]=$(dia_menu "Choose a public function." "${arr_pubfunc[@]}")
if (($? == 0)); then
arr_tmp[1]=$(dia_ibox "Enter parameters for the function \`${arr_tmp[0]}'. Empty value or CANCEL for none.")
arr_content[content_idx++]="${str_indent} <pub_func> ${arr_tmp[*]} </pub_func>"
fi
elif [[ $str_rule = Set ]]; then
local str_set="" str_type=""
str_rule="$(dia_menu "Select the type of ipset action" \
"Create set" "Create a set and optionally add elements" \
"Add elements" "Add elements to a set" \
"Import set" "Import set(s) from an ipset xml saved file" \
"Import elements" "Import elements from file")"
if [[ $str_rule = "Create set" ]]; then
str_set="$(dia_ibox "Enter the set name")"
if [[ $str_set ]]; then
local -i i=0 x
local arr_types=()
while read -r; do
[[ $REPLY = "Supported set types:"* ]] && ((!i)) && i=1 && continue
((i)) || continue
if [[ $REPLY = *:* ]]; then
set -- $REPLY
if ! lsearch "$1" "${arr_types[@]}"; then
arr_types[${#arr_types[@]}]="$1"
fi
fi
done < <( ( run_ipset help ) 2>/dev/null )
if ((${#arr_types[@]})); then
arr_types=( $(printf "%s\n" "${arr_types[@]}"|run_sort) )
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_types=( $(build_wt_dummy_list "${arr_types[@]}") )
fi
str_type=$(dia_menu --noitem "Choose a set type" "${arr_types[@]}") || return
else
dia_mbox "Unable to retrieve set types!"
return $ER_FAIL
fi
fi
if [[ $str_type ]]; then
dia_yn "Do you want to set header options?"
if (($? == 0)); then
arr_tmp=() str_tmp="" str_opt=""
str_header="counters comment skbinfo timeout"
if [[ $str_type = bitmap:* ]]; then
str_header+=" range"
if [[ $str_type = bitmap:ip ]]; then
str_header+=" netmask"
fi
elif [[ $str_type = hash:* ]]; then
str_header+=" forceadd hashsize maxelem"
if [[ $str_type != hash:mac ]]; then
str_header+=" family"
fi
if [[ $str_type = hash:ip ]]; then
str_header+=" netmask"
fi
if [[ $str_type = hash:ip,mark ]]; then
str_header+=" markmask"
fi
elif [[ $str_type = list:* ]]; then
str_header+=" size"
else
str_header="family forceadd hashsize markmask maxelem netmask range size counters comment skbinfo timeout"
fi
while (($? == 0)); do arr_header=() x=0
if [[ $str_lhs ]]; then
str_header="${str_header/$str_lhs/}"
fi
arr_header=( $str_header )
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_header=( $(build_wt_dummy_list "${arr_header[@]}") )
fi
str_lhs=$(dia_menu --noitem "Choose a header option" ${arr_header[*]}) || break
if [[ $str_lhs = @(hashsize|markmask|maxelem|netmask|range|size|timeout) ]]; then
str_rhs="$( dia_ibox "Enter a value for option $str_lhs")"
if (($? == 0)); then
arr_tmp[${#arr_tmp[@]}]="${str_lhs}=\"${str_rhs}\"" #"
fi
elif [[ $str_lhs = family ]]; then
if [[ $DIALOG_PROG = *dialog ]]; then
str_rhs=$(dia_menu --noitem "Select the family type" inet inet6)
elif [[ $DIALOG_PROG = *whiptail ]]; then
str_rhs=$(dia_menu --noitem "Select the family type" inet "" inet6 "")
fi
if (($? == 0)); then
arr_tmp[${#arr_tmp[@]}]="${str_lhs}=\"${str_rhs}\"" #"
fi
else
str_opt+=" $str_lhs"
fi
dia_yn "Do you want to set another header option?"
done
if [[ $str_opt ]]; then
arr_tmp[${#arr_tmp[@]}]="options=\"${str_opt# }\"" #"
fi
fi
if ((${#arr_tmp[@]})); then
arr_content[content_idx++]="${str_indent} <ipset name=\"$str_set\" type=\"$str_type\" ${arr_tmp[*]}>" #"
else
arr_content[content_idx++]="${str_indent} <ipset name=\"$str_set\" type=\"$str_type\">" #"
fi
dia_yn "Do you want to add an element?"
if (($? == 0)); then
while (($? == 0)); do
_add_set_element
dia_yn "Do you want to add another element?"
done
fi
arr_content[content_idx++]="${str_indent} </ipset>"
fi
elif [[ $str_rule = "Import set" ]]; then
str_tmp="$(dia_ibox "Enter the file name")"
if [[ $str_tmp ]]; then
arr_content[content_idx++]="${str_indent}<ipset import_xml=\"$str_tmp\"/>" #"
fi
elif [[ $str_rule = "Import elements" ]]; then
str_set="$(dia_ibox "Enter the set name")"
if [[ $str_set ]]; then
str_tmp="$(dia_ibox "Enter the file name")"
fi
if [[ $str_tmp ]]; then
arr_content[content_idx++]="${str_indent}<ipset name=\"$str_set\" import_plain=\"$str_tmp\"/>" #"
fi
elif [[ $str_rule = "Add elements" ]]; then
str_set="$(dia_ibox "Enter the set name")"
if [[ $str_set ]]; then
arr_content[content_idx++]="${str_indent} <ipset name=\"$str_set\">" #"
while (($? == 0)); do
_add_set_element
dia_yn "Do you want to add another element?"
done
arr_content[content_idx++]="${str_indent} </ipset>"
fi
fi
elif [[ $str_rule = Close ]]; then
if (($tag_idx > 0)); then
let tag_idx--
arr_content[content_idx++]="${str_indent}</${arr_tags[tag_idx]}>"
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
dia_mbox "Tag \`${arr_tags[tag_idx]}' has been closed."
unset arr_tags[tag_idx]
else
dia_mbox "There are no tags open."
fi
elif [[ $str_rule = "Enter comment" ]]; then
str_tmp="$(dia_ibox "Enter a comment")"
if [[ $str_tmp ]]; then
arr_content[content_idx++]="${str_indent} <!-- $str_tmp -->"
fi
elif [[ $str_rule = View ]]; then
printf "\n"
printf "%s\n" "${arr_content[@]}"
printf "\n"
read -p " Press \`ENTER' to continue"
fi
dia_yn "Do you want to continue working on this rule file?\n\
If you CANCEL, all open tags will be closed and the file will be written."
if (($? != 0)); then
for i in ${!arr_tags[*]}; do
let tag_idx--
arr_content[content_idx++]="${str_indent}</${arr_tags[tag_idx]}>"
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
unset arr_tags[tag_idx]
done
return 1
fi
} # -------------------------------------------------------------------------
iactive_rule_builder () { # interactive rule file builder
local -i i x
local -i template_rule=0 combined_rule=0 single_rule=0 ipt_tag=0
local -i tag_idx=0 content_idx=0
local -a arr_tags arr_tmp arr_templ arr_rb arr_content arr_pubfunc
local t_name r_name str_tmp
local str_indent
arr_title=( --title "$ME rule file builder" )
local r_file="${SAVE_DIR}/${me}-rules_$(run_date +%F_%T)_rnd${RANDOM}.xml"
i=0
for x in ${!ARG_NAMES_LIST[*]}; do
x_args[i++]="${ARG_NAMES_LIST[x]}"
done
if [[ $DIALOG_PROG = *whiptail ]]; then
x_args=( $(build_wt_dummy_list "${x_args[@]}") )
fi
while (($? == 0)); do
_ia_add_rules
done
if (($? <= 1)); then
if ((${#arr_content[@]})); then
mutex_on || bail_out $ER_FAIL
until (set -C; create_file "$r_file") &>/dev/null; do
r_file="$(dia_ibox "File \`$r_file' already exists.\nEnter a new file name (without the prefixing path).")"
(($?)) && return 1
r_file="$SAVE_DIR/${r_file}"
done
printf '<?xml version="1.0"?>\n\n' > "$r_file"
printf '<ip_array_root name="iptables_rules" syntax_version="1.1">\n' >> "$r_file"
printf "%s\n" "${arr_content[@]}" >> "$r_file"
printf '</ip_array_root>\n' >> "$r_file"
cleanup_lock
if [[ -s "$r_file" ]]; then
dia_yn "The resulting file has been saved to: $r_file\nDo you want to view the created rule file?"
if (($? == 0)); then
printf "%s:\n" "$r_file"
printf "\n" # we use cat as --textbox does not handle indention nicely
"$CAT" "$r_file" && printf "\n" && read -p " Press \`ENTER' to continue"
fi
else
run_rm "$r_file"
fi
else
dia_mbox "No rules have been collected."
fi
fi
dia_yn "Do you want to create another rule file?"
} # -------------------------------------------------------------------------
iactive_config_builder() { # interactive config file builder
((${#CONFIG_ENTRY_ARRAY[@]})) || {
log -E "\`CONFIG_ENTRY_ARRAY' is undefined."
return $ER_NODEF
}
local -i i x
local v_name v_type v_msg v_help
local -a arr_content arr_names arr_conf arr_tmp
arr_title=( --title "$ME configuration file builder" )
local c_file="${SAVE_DIR}/${me}-conf_$(run_date +%F_%T)_rnd${RANDOM}.conf"
i=0
for x in ${!CONFIG_ENTRY_ARRAY[@]}; do # use values from the config entry array
set -- ${CONFIG_ENTRY_ARRAY[x]}
arr_names[i]="$1"
arr_conf[i]="$1 $4 $3"
let i++
done
mutex_on || bail_out $ER_FAIL
until (set -C; create_file "$c_file") &>/dev/null; do
c_file="$(dia_ibox "File \`$c_file' already exists.\nEnter a new file name (without the prefixing path).")"
(($?)) && return 1
c_file="$SAVE_DIR/${c_file}"
done
while (($? == 0)); do
_add_conf_var
done
cleanup_lock
if [[ -s "$c_file" ]]; then
dia_yn "The resulting configuration file has been saved to: $c_file\nDo you want to view the created configuration file?"
if (($? == 0)); then
printf "%s:\n" "$c_file"
printf "\n" # we use cat as --textbox does not handle indention nicely
"$CAT" "$c_file" && printf "\n" && read -p " Press \`ENTER' to continue"
fi
else
run_rm "$c_file"
fi
} # -------------------------------------------------------------------------
_ia_add_sysctl() {
local str_choice str_tmp oIFS
local -i i new_index args
local -a arr_tmp
#str_choice=$(dia_menu "Select an option." \
# "Add tag" "Add a sysctl tag" \
# "Close tag" "Close last opened tag" \
# "Enter comment" "Add a comment" \
# "View" "View the created rules")
str_choice=$(dia_menu "Select an option." \
"Add tag" "Add a sysctl tag" \
"Enter comment" "Add a comment" \
"View" "View the created rules")
if [[ $str_choice = "Add tag" ]]; then
while (($? == 0)); do
str_choice=$(dia_menu --noitem "Choose a sysctl setting.\n\
XML code will be automatically generated.\n\
CANCEL to return to the main window." "${arr_sysctl[@]}")
if (($? == 0)); then
for i in ${!arr_sysctl[*]}; do # remove dupes from the list
if [[ ${arr_sysctl[i]} = $str_choice ]]; then
unset arr_sysctl[i]
if [[ $DIALOG_PROG = *whiptail ]]; then
unset arr_sysctl[i+1]
fi
break
fi
done
oIFS="$IFS"
IFS='.'
set -- $str_choice
IFS="$oIFS"
new_index=0 args=$#
if ((${#arr_tags[@]})); then # we have tags open
for ((i=0; i<$((args - 1)); i++)); do # see if the new setting has same path
if [[ ${arr_tags[i]} = $1 ]]; then
new_index=$(($i + 1))
shift
else
new_index=$i
break
fi
done
# close tags if needed
for ((i=$((tag_idx - 1)); i>=new_index; i--)); do let tag_idx--
arr_content[content_idx++]="${str_indent}</${arr_tags[tag_idx]}>"
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
unset arr_tags[i]
done
fi
# open (new) tags
for str_tmp; do
str_indent+=" "
arr_tags[tag_idx++]="$str_tmp"
arr_content[content_idx++]="${str_indent}<$str_tmp>"
done
# leave only last name of path = key name
if (($# > 1)); then
shift $(($# - 1))
fi
str_tmp="$(dia_ibox "Enter a value for: \`$1'")"
if (($? == 0)); then
arr_content[content_idx-1]+=" $str_tmp </$1>"
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
unset arr_tags[--tag_idx]
else
for ((i=$((tag_idx - 1)); i>=new_index; i--)); do
unset arr_content[--content_idx]
unset arr_tags[--tag_idx]
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
done
break
fi
else
break
fi
done
#elif [[ $str_choice = "Close tag" ]]; then
# if (($tag_idx > 0)); then
# let tag_idx--
# arr_content[content_idx++]="${str_indent}</${arr_tags[tag_idx]}>"
# str_indent="${str_indent:0:$((${#str_indent} - 4))}"
# dia_mbox "Tag \`${arr_tags[tag_idx]}' has been closed."
# unset arr_tags[tag_idx]
# else
# dia_mbox "There are no tags open."
# fi
elif [[ $str_choice = "Enter comment" ]]; then
str_choice="$(dia_ibox "Enter a comment")"
if [[ $str_choice ]]; then
arr_content[content_idx++]="${str_indent} <!-- $str_choice -->"
fi
elif [[ $str_choice = "View" ]]; then
printf "\n"
printf "%s\n" "${arr_content[@]}"
printf "\n"
read -p " Press \`ENTER' to continue"
fi
dia_yn "Do you want to continue working on this sysctl rule file?\n\
If you CANCEL, all open tags will be closed and the file will be written."
if (($? != 0)); then
for i in ${!arr_tags[*]}; do
let tag_idx--
arr_content[content_idx++]="${str_indent}</${arr_tags[tag_idx]}>"
str_indent="${str_indent:0:$((${#str_indent} - 4))}"
unset arr_tags[tag_idx]
done
return 1
fi
} # -------------------------------------------------------------------------
iactive_sysctl_builder() { # interactive sysctl rule file builder
local str_indent
local -i tag_idx=0 content_idx=0
local -a arr_tags arr_content arr_sysctl=()
local c_file="${SAVE_DIR}/${me}-sysctl_rules_$(run_date +%F_%T)_rnd${RANDOM}.xml"
arr_title=( --title "$ME sysctl rule file builder" )
while read -r -t $GLOBAL_READ_TIMEOUT; do # retrieve sysctl keys
if [[ $REPLY = net.* && $REPLY != net.ipv6.* ]]; then
if [[ -e /proc/sys/${REPLY//.//} ]]; then
[[ -w /proc/sys/${REPLY//.//} ]] && arr_sysctl[${#arr_sysctl[@]}]="${REPLY#net.}"
else
arr_sysctl[${#arr_sysctl[@]}]="${REPLY#net.}"
fi
fi
done < <(dn2 run_sctl -N -a)
if ((${#arr_sysctl[@]} == 0)); then
dia_mbox "Could not retrieve any sysctl data!"
return $ER_FAIL
fi
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_sysctl=( $(build_wt_dummy_list "${arr_sysctl[@]}") )
fi
while (($? == 0)); do
_ia_add_sysctl
done
if (($? <= 1)); then
if ((${#arr_content[@]})); then
mutex_on || bail_out $ER_FAIL
until (set -C; create_file "$c_file") &>/dev/null; do
c_file="$(dia_ibox "File \`$c_file' already exists.\nEnter a new file name (without the prefixing path).")"
(($?)) && return 1
c_file="$SAVE_DIR/${c_file}"
done
printf '<?xml version="1.0"?>\n\n' > "$c_file"
printf '<ip_array_root name="iptables_rules" syntax_version="1.1">\n' >> "$c_file"
printf '<net>\n' >> "$c_file"
printf "%s\n" "${arr_content[@]}" >> "$c_file"
printf '</net>\n' >> "$c_file"
printf '</ip_array_root>\n' >> "$c_file"
cleanup_lock
else
dia_mbox "No rules have been collected."
fi
fi
if [[ -s "$c_file" ]]; then
dia_yn "The resulting sysctl rule file has been saved to: $c_file\nDo you want to view the created file?"
if (($? == 0)); then
printf "%s:\n" "$c_file"
printf "\n" # we use cat as --textbox does not handle indention nicely
"$CAT" "$c_file" && printf "\n" && read -p " Press \`ENTER' to continue"
fi
else
run_rm "$c_file"
fi
dia_yn "Do you want to create another sysctl rule file?"
} # -------------------------------------------------------------------------
iactive_ruleblock_builder() { # interactive ruleblock builder
arr_title=( --title "$ME ruleblock builder" )
local str_act str_tmp t_name r_name r_file
local -i i x idx
local -a arr_tmp arr_templ arr_rb arr_inf arr_rb_tmp arr_line
if [[ $DIALOG_PROG = *dialog ]]; then
str_act=$(dia_menu --noitem "Select the desired option" "Create a new ruleblock" "Edit an existing ruleblock")
elif [[ $DIALOG_PROG = *whiptail ]]; then
str_act=$(dia_menu --noitem "Select the desired option" "Create a new ruleblock" "" "Edit an existing ruleblock" "")
fi
(($?)) && return 1
if [[ -r ${TEMPLATE_DIR}/__parsed_TEMPLATE_NAMES ]]; then i=0
while read -t $GLOBAL_READ_TIMEOUT; do
if [[ $REPLY != @(BT|BASE_TEMPLATE)_* ]]; then # don't show base templates
arr_tmp[i++]="$REPLY"
fi
done < <( "$CAT" "${TEMPLATE_DIR}"/__parsed_TEMPLATE_NAMES|run_sort )
if ((${#arr_tmp[@]} == 1)); then
dia_mbox "Got only one template: \`${arr_tmp[0]}'. Selecting it."
t_name="${arr_tmp[0]}"
else
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_tmp=( $(build_wt_dummy_list "${arr_tmp[@]}") )
fi
t_name=$(dia_menu --noitem "Choose a template" "${arr_tmp[@]}")
(($? == 0)) || return 1
fi
[[ $t_name ]] || return 1
else
dia_mbox "Templates must first be parsed (parse-xml -pt)"
return 1
fi
str_tmp=""
load_all_templates || return
$($copy_array "$t_name" arr_templ) || return 1
for i in ${!arr_templ[*]}; do
if [[ ${arr_templ[i]} != option_list_* ]]; then
arr_inf[${#arr_inf[*]}]="${arr_templ[i]}"
unset arr_templ[i]
elif [[ ${arr_templ[i]} = option_list_0* ]]; then
str_tmp+="${arr_templ[i]#option_list_0*=} "
fi
done
if [[ $str_tmp ]]; then
arr_inf[${#arr_inf[*]}]="Ruleblock structure:"
arr_inf[${#arr_inf[*]}]="$str_tmp"
fi
if [[ $str_act = "Create a new ruleblock" ]]; then
r_name=$(dia_ibox "Enter a name for the ruleblock (prefix with RB_ or RULEBLOCK_)" RB_)
(($? == 0)) || return 1
[[ $r_name ]] || return 1
x=0
while (($? == 0)); do
dia_yn "Add a line to the ruleblock?" && _add_rb_line && arr_rb[x++]="${arr_line[*]}"
done
elif [[ $str_act = "Edit an existing ruleblock" ]]; then
load_all_ruleblocks || return
# put list of all ruleblocks into an array
if [[ $DIALOG_PROG = *dialog ]]; then
arr_tmp=( $( while read -t $GLOBAL_READ_TIMEOUT; do printf "%s\n" "$REPLY"; done < <( "$CAT" "${RULEBLOCK_DIR}"/__parsed_RULEBLOCK_NAMES|run_sort) ) )
elif [[ $DIALOG_PROG = *whiptail ]]; then
arr_tmp=( $( while read -t $GLOBAL_READ_TIMEOUT; do printf "%s off\n" "$REPLY"; done < <( "$CAT" "${RULEBLOCK_DIR}"/__parsed_RULEBLOCK_NAMES|run_sort) ) )
fi
# choose one ruleblock
if ((${#arr_tmp[@]})); then
if [[ $DIALOG_PROG = *dialog ]] && ((${#arr_tmp[@]} == 1)); then
dia_mbox "Got only one ruleblock: \`${arr_tmp[0]}'. Selecting it."
r_name="${arr_tmp[0]}"
elif [[ $DIALOG_PROG = *whiptail ]] && ((${#arr_tmp[@]} == 2)); then
dia_mbox "Got only one ruleblock: \`${arr_tmp[0]}'. Selecting it."
r_name="${arr_tmp[0]}"
else
r_name=$(dia_menu --noitem "Choose a ruleblock" "${arr_tmp[@]}")
fi
else
dia_mbox "No ruleblocks have been found!"
return 0
fi
(($? == 0)) || return 1
[[ $r_name ]] || return 1
$($copy_array "$r_name" arr_rb) || return 1
x=1 idx=0
if ! ((${#arr_rb[*]})); then
dia_mbox "Ruleblock \`$r_name' is empty or not defined."
return $ER_NODEF
fi
for i in ${!arr_rb[*]}; do # add line numbers
arr_rb_tmp[idx++]="$x: ${arr_rb[i]}"
if [[ $DIALOG_PROG = *whiptail ]]; then
arr_rb_tmp[idx++]="item"
fi
let x++
done
x=0 arr_tmp=()
for i in ${!arr_templ[*]}; do # arr_templ has holes, we need a continous list
if [[ ${arr_templ[i]} = option_list_0* ]]; then
arr_tmp[x++]="${arr_templ[i]}"
fi
done
while (($? == 0)); do # edit line
_edit_rb_line && dia_yn "Do you want to edit another rule?"
done
fi
r_file="$SAVE_DIR/${r_name}.xml"
if ((${#arr_rb[*]})); then
mutex_on || bail_out $ER_FAIL
until (set -C; create_file "$r_file") &>/dev/null; do