forked from cgustave/fgtconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg_dissector.pm
More file actions
1307 lines (973 loc) · 38.5 KB
/
cfg_dissector.pm
File metadata and controls
1307 lines (973 loc) · 38.5 KB
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
# ****************************************************
# * *
# * C F G D I S S E C T O R *
# * *
# * Dissector object for fortinet config style *
# * *
# * Author : Cedric Gustave cgustave@fortinet.com *
# * *
# ****************************************************
#
# This object provides generic tools to dissect a 'Forticonfiguration' file
package cfg_dissector ;
my $obj = "cfg_dissector" ;
use Moose ;
use constant NESTED => 1 ;
use constant NOTNESTED => 0 ;
has 'configfile' => (
isa => 'Str',
is => 'rw',
# default => 'fgt_system.conf',
predicate => 'has_configfile',
trigger => \&_accessor_debug,
) ;
has 'version' => (isa => 'Str', is => 'rw') ;
has 'build' => (isa => 'Str', is => 'rw') ;
has 'plateform' => (isa => 'Str', is => 'rw') ;
has 'type' => (isa => 'Str', is => 'rw') ;
has 'fos_carrier' => (isa => 'Str', is => 'rw') ;
has 'build_tag' => (isa => 'Str', is => 'rw', default => '') ;
has 'vdom_enable' => (isa => 'Int', is => 'rw', default => 0) ;
has 'debug' => (
isa => 'Int',
is => 'rw',
default => 0,
trigger => \&_accessor_debug,
) ;
# --- PRIVATE ATTRIBUTES ---
# $self->{_CONFIG} = () ; # Array of config lines starting a 1 to match config line
# $self->{_CONFIG_LINES} = 0 ; # nb of lines in the config
# $self->{_FEEDBACK} = {} ; # Href for feedback on search
sub BUILD {
my $subn = "BUILD" ;
my $self = shift ;
warn "\n* Entering $obj:$subn with debug=" . $self->debug if $self->debug ;
$self->{_FEEDBACK} = {} ;
$self->{VDOM_LIST} = () ; # array of list of vdom
$self->{VDOM} = {} ; # Only to store startindex and endindex
$self->{_CACHE_MGMT_VDOM} ;
}
# ---
sub load {
my $subn = "load" ;
# Load file in memory
# Index starts at 1 instead of 0 so it matches the line number displayed in text editor when editing the config
my $self = shift ;
$self->{_CONFIG} = () ;
my $index = 1 ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
open(CONFIG, '<', $self->configfile()) or die "Cannot open config file " . $self->configfile() . " for reading (-config )" ;
while (<CONFIG>) {
s/\r\n/\n/ ; # standardize DOS format to normal format...
$self->{_CONFIG}[$index] = $_ ;
$index++ ;
}
close(CONFIG) ;
$self->{_CONFIG_LINES} = $index - 1 ;
warn "$obj:$subn Loaded $index lines" if $self->debug() ;
}
# ---
sub config_header {
my $subn = "config_header" ;
my $self = shift ;
my ($plateform, $version, $type, $build) = undef ;
warn "Entering $obj:$subn" if $self->debug() ;
# Inspect first header line
# ex: #config-version=FG3K8A-4.00-FW-build099-090407:opmode=0:vdom=1:user=admin
# ex #config-version=FG3K9B-5.00-FW-build292-140801:opmode=0:vdom=1:user=fwmd_fortigate
# ex: #config-version=FGT3KB-4.00-FW-build513-120130:opmode=0:vdom=1:user=admin
# ex: #config-version=FG1K2D-5.04-FW-build1011-151221:opmode=0:vdom=0:user=cgustave
# ex: #config-version=FGT1KD-5.6.2-FW-build1486-170816:opmode=0:vdom=1:user=ravindrab
# ex: #config-version=FGVMK6-6.0.0-FW-build0009-171026:opmode=0:vdom=0:user=admin
# do not start RE with ^ as it seems some invisible control chars may sneak in at beginning of the line
($plateform, $version, $type, $build) = $self->line(1) =~ /
(?:\#config-version=)
(\S{6})
(?:-)
(\d\.\d{1,2}\.?\d{1,2}?)
(?:\-)(FW|FOC)(?:-build)(\w{3,4})(?:-)
/x ;
if (defined($plateform)) {
warn "$obj:$subn plateform=$plateform version=$version type=$type build=$build" if $self->debug() ;
$self->plateform($plateform) ;
$self->version($version) ;
$self->type($type) ;
$self->build($build) ;
}
else {
warn "$subn: Is first config line ok ? can't recognise the plateform: line=" . $self->line(1) ;
}
# FortiOS Carrier has a typical 2md line header:
#config-version=FG5A01-3.00-FW-build730-090317:opmode=0:vdom=1:user=admin
#version=FortiOS-Carrier-5001A 3.00,build8102,090317
#conf_file_ver=15993214564799777768
#buildno=8102
if ($self->line(2) =~ /(#version=FortiOS-Carrier-)|(#version=FortiCarrier)/) {
warn "$obj:$subn FortiOS Carrier" if $self->debug() ;
$self->fos_carrier("CARRIER") ;
}
else { $self->fos_carrier("") }
}
# ---
sub config_version {
my $subn = "config_version" ;
# Return the major and minor version (3.0, 4.1, 4.2, 4.3, 5.0, 5.2)
# from version and build number
# returns "" for special image (high build number)
my $self = shift ;
my $return = "" ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
# Use cache if possible
return ($self->{_CFG}->{_CACHE_BASE_BRANCH})
if (defined($self->{_CFG}->{_CACHE_BASE_BRANCH})) ;
# sanity
die "undefined version" if (not(defined($self->version()))) ;
die "undefined build" if (not(defined($self->build()))) ;
my ($version, $build) = undef ;
$version = $self->version() ;
$build = $self->build() ;
warn "$obj:$subn version=$version build=$build" if $self->debug ;
if ($version eq "3.00") {
$return = "3.0" ;
$return = "3.1" if ($build >= 247) ;
$return = "3.2" if ($build >= 318) ;
$return = "3.3" if ($build >= 400) ;
$return = "3.5" if ($build >= 405) ;
$return = "3.6" if ($build >= 406) ;
$return = "3.7" if ($build >= 410) ;
$return = "3.8" if ($build >= 411) ;
$return = "3.9" if ($build >= 413) ;
$return = "3.10" if ($build >= 415) ;
$return = "3.13" if ($build >= 417) ;
$return = "3.14" if ($build >= 418) ;
}
elsif ($version eq "4.00") {
$return = "4.0" ;
$return = "4.1" if ($build >= 178) ;
$return = "4.2" if ($build >= 272) ;
$return = "4.3" if ($build >= 441) ;
}
elsif ($version eq "5.00") {
$return = "5.0" ;
}
elsif (($version eq "5.02") or ($version eq "5.2")) {
$return = "5.2" ;
}
elsif (($version eq "5.04") or ($version eq "5.4")) {
$return = "5.4" ;
}
# Now, there is no need to guess based on build
else {
$return = $version ;
}
# fill cache
$self->{_CFG}->{_CACHE_BASE_BRANCH} = $return ;
return $return ;
}
# ---
sub line {
my $subn = "line" ;
# Returns config line from the line index provided (starting from 1)
my $self = shift ;
my $line = shift ;
warn "\n* Entering $obj:$subn with line=$line" if $self->debug() ;
# Sanity
die "line index required" if (not(defined($line))) ;
return ($self->{_CONFIG}[$line]) ;
}
# ---
sub get_line {
my $subn = "get_line" ;
my $self = shift ;
my %options = @_ ;
my $index = $options{'index'} ;
warn "\n* Entering $obj:$subn with index=$index" if $self->debug() ;
# sanity
die "index is required" if (not(defined($index))) ;
die "index should be numeric" if ($index !~ /\d+/) ;
return ($self->{_CONFIG}[$index]) ;
}
# ---
sub set_line {
my $subn = "set_line" ;
my $self = shift ;
my %options = @_ ;
my $index = $options{'index'} ;
my $content = $options{'content'} ;
warn "\n* Entering $obj:$subn with index=$index content=$content" if $self->debug() ;
# sanity
die "index is required" if (not(defined($index))) ;
die "index should be numeric" if ($index !~ /\d+/) ;
$self->{_CONFIG}[$index] = $content ;
}
# ---
sub max_lines {
my $subn = "max_lines" ;
my $self = shift ;
warn "\n* Entering $obj:$subn max_line=" . $self->{_CONFIG_LINES} if $self->debug() ;
return ($self->{_CONFIG_LINES}) ;
}
# ---
sub insert {
my $subn = "insert" ;
# Inserts a line "content" at provided index
# config grows by one line and shift to the bottom
my $self = shift ;
my %options = @_ ;
my $index = $options{'index'} ;
my $content = $options{'content'} ;
$content = "" if (not(defined($content))) ;
warn "\n* Entering $obj:$subn with index=$index and content=$content" if $self->debug ;
# sanity
die "index is required" if (not(defined($index))) ;
die "index should be an integer" if (not($index =~ /\d+/)) ;
# shift config from bottom up until our index
for (my $i = $self->{_CONFIG_LINES} ; $i >= $index ; $i--) {
warn "$obj:$subn shift line ($i+1) to line $i" if $self->debug ;
$self->{_CONFIG}[$i + 1] = $self->{_CONFIG}[$i] ;
}
# put content line with new line
warn "$obj:$subn put content=$content at line $index" if $self->debug ;
$self->{_CONFIG}[$index] = $content . "\n" ;
# Increase config lines
$self->{_CONFIG_LINES}++ ;
}
# ---
sub delete {
my $subn = "delete" ;
# Deletes line at provided index
# config shriks by one line and shift up
my $self = shift ;
my %options = @_ ;
my $index = $options{'index'} ;
my $action = $options{'action'} ;
$action = "blank" if (not(defined($action)) or ($action eq "")) ;
warn "\n* Entering $obj:$subn with index=$index action=$action" if $self->debug ;
# sanity
die "index is required" if (not(defined($index))) ;
die "index should be an integer" if (not($index =~ /\d+/)) ;
if ($action eq "shrink") {
# Shrink config
for (my $i = $index ; $i < $self->{_CONFIG_LINES} ; $i++) {
warn "$obj:$subn shift line ($i+1) to line $i" if $self->debug ;
$self->{_CONFIG}[$i] = $self->{_CONFIG}[$i + 1] ;
}
# Erase previous last line
$self->{_CONFIG}[$self->{_CONFIG_LINES}] = "" ;
# Decrease config lines
$self->{_CONFIG_LINES}-- ;
}
elsif ($action eq "blank") {
$self->{_CONFIG}[$index] = "\n" ;
}
}
# ---
sub replace {
my $subn = "replace" ;
# Replaces a line in the configuration at a defined index
my $self = shift ;
my %options = @_ ;
my $index = $options{'index'} ;
my $content = $options{'content'} ;
warn "\n* Entering $obj:$subn with index=$index, content=$content" if $self->debug ;
# Insert our line
$self->set_line(index => $index, content => $content) ;
}
# ---
sub delete_block {
my $subn = "delete_block" ;
# Deletes and entire block of config delimimited by scopes
my $self = shift ;
my %options = @_ ;
my $startindex = $options{'startindex'} ;
my $endindex = $options{'endindex'} ;
warn "\n* Entering $obj:$subn with startindex=$startindex endindex=$endindex" if $self->debug ;
# Sanity
die "startindex is required" if (not(defined($startindex)) or ($startindex eq "")) ;
die "endindex is required" if (not(defined($endindex)) or ($endindex eq "")) ;
die "startindex should be numeric" if ($startindex !~ /\d+/) ;
die "endindex should be numeric" if ($endindex !~ /\d+/) ;
# Copy config just after the block at our startindex
my $diff = $endindex - $startindex ;
for (my $i = $startindex ; $i <= $self->max_lines - $diff ; $i++) {
$self->{_CONFIG}[$i] = $self->{_CONFIG}[$i + $diff + 1] ;
}
# Wipe config bottom
for (my $i = $self->max_lines - $diff ; $i < $self->max_lines ; $i++) {
$self->{_CONFIG}[$i] = "" ;
}
# Reduce max_line
$self->{_CONFIG_LINES} = $self->{_CONFIG_LINES} - $diff - 1 ;
$self->register_vdoms() ;
}
# ---
sub delete_all_keys_from_block {
my $subn = "delete_all_keys_from_block" ;
# Blank any lines from the block that need to be deleted
# Do not delete the lines as this would shift all vdoms index
# deletion of empty lines may be done at a later stage
my $self = shift ;
my %options = @_ ;
my $key = $options{'key'} ;
my $aref_scope = $options{'aref_scope'} ;
warn "\n* Entering $obj:$subn with key=$key scope start=" . $$aref_scope[0] . " end=" . $$aref_scope[1] if $self->debug ;
# Sanity
die "key is required" if (not(defined($key))) ;
die "aref_scope is required" if (not(defined($aref_scope))) ;
# Mark likes for deletion
for (my $i = $$aref_scope[0] ; $i <= $$aref_scope[1] ; $i++) {
if ($self->{_CONFIG}[$i] =~ /^(?:\s|\t)*(?:set\s)$key(?:\s|\t)/) {
warn "$obj:$subn delete index=$i line=" . ($self->{_CONFIG}[$i]) if $self->debug ;
$self->{_CONFIG}[$i] = "\n" ;
}
}
}
# ---
sub get_end_global_index {
# needed for splitconfig in cfg_global
my $self = shift ;
return ($self->{_endglobalindex}) ;
}
# ---
sub register_vdoms {
my $subn = "register_vdoms" ;
# Registers all vdom
# we only register a vdom when entering the vdom for config so we ignore the first 'config vdom'
# in the config which are immediately followed-up with an 'end' (or a next since 5.0.4)
my $self = shift ;
my $vdom = undef ;
my $previous_vdom = undef ;
my $flag = 0 ;
my $index = 0 ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
# Clear out list so we can be called multiple times
$self->{VDOM_LIST} = () ;
$self->{VDOM} = {} ;
# Parses all config and registers vdom, marking the index start and end
foreach my $line (@{$self->{_CONFIG}}) {
if ($flag == 1) { # flag==1 means line is "config vdom"
# Get vdom name
($vdom) = $line =~ /edit\s((\w|-)*)/ ;
if (defined($vdom)) {
$flag = 2 ; # got vdom name
}
else { die "config error" }
}
elsif ($flag == 2) {
if (not($line =~ /^(end|next)/)) { # Avoid the first vdom definition that need to be ignored
$self->vdom_index(vdom => $vdom, action => 'set', type => 'startindex', value => $index) ;
# This is needed for splitconfig
if (not(defined($self->{_endglobalindex}))) {
$self->{_endglobalindex} = $index ;
}
# register vdom
push @{$self->{VDOM_LIST}}, $vdom ;
warn "$obj:$subn registering vdom $vdom starting line $index" if $self->debug() ;
# Terminates previous vdom if there was one (4 lines before)
if (defined($previous_vdom)) {
my $previous_vdom_end_index = ($index - 4) ;
$self->vdom_index(vdom => $previous_vdom, type => 'endindex', action => 'set', value => $previous_vdom_end_index) ;
warn "$obj:$subn updating vdom $previous_vdom ending line $previous_vdom_end_index" if $self->debug() ;
}
$previous_vdom = $vdom ;
}
$flag = 0 ;
}
# reset flags
if ($flag == 1) { $flag = 0 }
# Set flag = 1 when reaching config vdom
if (defined($line)) {
if ($line =~ /config vdom/) {
warn "$subn : found config vdom" if $self->debug() ;
$flag = 1 ;
}
}
$index++ ;
}
# Case where there is no vdoms (only the default root vdom)
if (not(defined($vdom))) {
warn "$obj:$subn no vdoms" if $self->debug() ;
$vdom = 'root' ;
$self->vdom_index(vdom => $vdom, type => 'startindex', action => 'set', value => '4') ;
$self->vdom_enable(0) ;
push @{$self->{VDOM_LIST}}, 'root' ;
}
else {
$self->vdom_enable(1) ;
}
# Terminates the last vdom (need to remove 1 line_
$self->vdom_index(vdom => $vdom, type => 'endindex', action => 'set', value => ($index - 1)) ;
}
# ---
sub vdom_index {
my $subn = "vdom_index" ;
# Set or get startindex and endindex
my $self = shift ;
my %options = @_ ;
my $vdom = $options{'vdom'} ;
my $type = $options{'type'} ;
my $action = $options{'action'} ;
my $value = $options{'value'} ;
warn "$obj:$subn with vdom=$vdom type=$type action=$action value=$value" if $self->debug ;
# Sanity
die "vdom is required" if (not(defined($vdom))) ;
die "type is required" if (not(defined($type))) ;
die "type can only be startindex|endindex" if ($type !~ /startindex|endindex/) ;
die "action is required" if (not(defined($action))) ;
die "action can only be get|set" if ($action !~ /get|set/) ;
die "set value cannot be undef" if (($action eq 'set') and not(defined($value))) ;
if (defined($value) and ($action eq "set")) {
$self->{VDOM}->{$vdom}->{$type} = $value ;
}
if (not(defined($value)) and ($action eq "get")) {
return $self->{VDOM}->{$vdom}->{$type} ;
}
}
# ---
sub get_nb_vdoms {
my $subn = "get_nb_vdoms" ;
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
return (scalar @{$self->{VDOM_LIST}}) ;
}
# ---
sub get_vdom_list {
my $subn = "get_vdom_list" ;
# returns the vdom list sorted alphabetically with management vdom on top
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
my @sortedList = () ;
# Get and push management vdom first
my $mgmt = $self->get_mgmt_vdom() ;
# build a list of vdoms without the management vdom
my @listNoMgmt = () ;
foreach my $element (@{$self->{VDOM_LIST}}) {
if ($element ne $mgmt) {
push @listNoMgmt, $element ;
}
}
# sort our list and add management vdom at the begining
@sortedList = sort { $a cmp $b } (@listNoMgmt) ;
unshift @sortedList, $mgmt ;
return @sortedList ;
}
# --
sub get_vdom_opmode {
my $subn = "get_vdom_opmode" ;
my $self = shift ;
my $vdom = shift ;
warn "\n* Entering $obj:$subn with vdom=$vdom" if $self->debug ;
# sanity
die "register_vdom first" if (not($self->{VDOM}) or not($self->{VDOM_LIST})) ;
if ($self->{VDOM}->{$vdom}->{'opmode'}) {
warn "$obj:$subn from cache vdom=$vdom opmode=" . $self->{VDOM}->{$vdom}->{'opmode'} if $self->debug ;
return ($self->{VDOM}->{$vdom}->{'opmode'}) ;
}
else {
my @scope = $self->scope_vdom($vdom) ;
if ($self->scope_config(\@scope, 'config system settings')) {
my $opmode = $self->get_key(\@scope, 'opmode', NOTNESTED, 'nat') ;
warn "$obj:$subn vdom=$vdom opmode=$opmode" if $self->debug ;
# put in cache
$self->{VDOM}->{$vdom}->{'opmode'} = $opmode ;
return ($opmode) ;
}
else {
warn "$obj:$subn could not find config system settings to get vdom opmode for vdom $vdom, assuming nat-route" if $self->debug;
return ('nat');
}
}
}
# ---
sub get_mgmt_vdom {
my $subn = "get_mgmt_vdom" ;
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug ;
if (not($self->{_CACHE_MGMT_VDOM})) {
# Init scop
my @scope = (1, $self->max_lines) ;
$self->scope_config(\@scope, 'config system global') ;
if ($self->feedback('found')) {
my $mgmt_vdom = $self->get_key(\@scope, 'management-vdom', NOTNESTED, "root") ;
warn "$obj:$subn management vdom=$mgmt_vdom" if $self->debug ;
$self->{_CACHE_MGMT_VDOM} = $mgmt_vdom ;
return ($mgmt_vdom) ;
}
else {
warn "$obj:$subn default management vdom=root" if $self->debug ;
$self->{_CACHE_MGMT_VDOM} = 'root' ;
return ('root') ;
}
}
else {
warn "$obj:$subn from cache=" . $self->{_CACHE_MGMT_VDOM} if $self->debug ;
return ($self->{_CACHE_MGMT_VDOM}) ;
}
}
# ---
sub print_config {
# For debugging purpose
my $self = shift ;
my $start = shift ;
my $stop = shift ;
$stop = $self->max_lines if (not(defined($stop))) ;
print "\nConfig Extract from start=$start and stop=$stop\n" ;
for (my $i = $start ; $i <= $stop ; $i++) {
print "[ $i ]: " . $self->line($i) ;
}
}
# ---
sub scope_vdom {
my $subn = "scope_vdom" ;
# Returns the scope for a vdom
# Requires first register_vdoms
my $self = shift ;
my $vdom = shift ;
my @return = () ;
warn "\n* Entering $obj:$subn with vdom=$vdom" if $self->debug() ;
my $value = $self->vdom_index(type => 'startindex', vdom => $vdom, action => 'get') ;
warn "$obj:$subn start=$value" if $self->debug() ;
push(@return, $value) ;
$value = $self->vdom_index(type => 'endindex', vdom => $vdom, action => 'get') ;
warn "$obj:$subn stop=$value" if $self->debug() ;
push(@return, $value) ;
return (@return) ;
}
# ---
sub scope_config {
my $subn = "scope_config" ;
# Withing a start/end index (like the vdom), specify a config statement
# arguments :
# ref of scope (array of 2 values startindex, stopindex)
# returns 1 if found else 0
my $self = shift ;
my $aref_scope = shift ;
my $config_statement = shift ;
my $partial_flag = shift || 0 ;
warn "\n* Entering $obj:$subn with scope=("
. $$aref_scope[0] . ","
. $$aref_scope[1]
. ") statement ->$config_statement<- partial_flag=$partial_flag "
if $self->debug() ;
# clear feedback
$self->{_FEEDBACK} = {} ;
$self->{_FEEDBACK}->{'found'} = 0 ;
# remove the result line if still here
my @return = $self->_config_seek($$aref_scope[0], $$aref_scope[1], $config_statement, 'end', 'config', $partial_flag) ;
$$aref_scope[0] = $return[1] ;
$$aref_scope[1] = $return[2] ;
# Report feedback
$self->{_FEEDBACK}->{'found'} = $return[0] ;
$self->{_FEEDBACK}->{'startindex'} = $return[1] ;
$self->{_FEEDBACK}->{'endindex'} = $return[2] ;
return $return[0] ;
}
# ---
sub scope_edit {
my $subn = "_scope_edit" ;
# Within a start/end index (like the vdom), specify a config statement
# Return a key if asked
my $self = shift ;
my $aref_scope = shift ;
my $edit_statement = shift ;
my $ref_key = shift ;
my $partial_flag = undef ;
if (defined($ref_key)) {
$partial_flag = 1 ;
}
else {
$partial_flag = 0 ;
}
warn "\n* Entering $obj:$subn with scope=("
. $$aref_scope[0] . ","
. $$aref_scope[1]
. ") statement ->$edit_statement<- partial_flag=$partial_flag"
if $self->debug() ;
# clear feedback
$self->{_FEEDBACK} = {} ;
$self->{_FEEDBACK}->{'found'} = 0 ;
my @return = $self->_config_seek($$aref_scope[0], $$aref_scope[1], $edit_statement, 'next', 'edit', $partial_flag) ;
$$aref_scope[0] = $return[1] ;
$$aref_scope[1] = $return[2] ;
if (defined($ref_key)) {
$$ref_key = $return[3] ;
}
# Report feedback
$self->{_FEEDBACK}->{'found'} = $return[0] ;
$self->{_FEEDBACK}->{'startindex'} = $return[1] ;
$self->{_FEEDBACK}->{'endindex'} = $return[2] ;
return $return[0] ;
}
# ---
sub get_key {
my $subn = "get_key" ;
# Withing the define scope, retrieve the value after the statement
# don't search the statement inside a nested config/end or edit/next
# ex: get_key(\@scope,'hostname', NESTED|NOTNESTED, 'default_value') ;
my $self = shift ;
my $aref_scope = shift ;
my $key = shift ;
my $nested = shift ; # use NESTED|NOTNESTED (decalred as constant)
my $default = shift ; # default value to return if get is not found
my $get_value = shift ;
$get_value = 1 if (not(defined($get_value))) ;
my $flag_found = 0 ;
my $nested_count = 0 ;
my ($nested_key, $line, $return, $value) = undef ;
# Initialise
$$aref_scope[0] = 1 if (not(defined($$aref_scope[0]))) ;
$$aref_scope[1] = 1000000 if (not(defined($$aref_scope[1]))) ;
$nested = 0 if (not(defined($nested))) ;
$self->{_FEEDBACK} = {} ;
$self->{_FEEDBACK}->{'found'} = 0 ;
warn "\n* Entering $obj:$subn with scope=(" . $$aref_scope[0] . "," . $$aref_scope[1] . ") key=$key nested=$nested get_value=$get_value"
if $self->debug() ;
my $index = $$aref_scope[0] ;
$index++ ; # to avoid the initial config/edit is counted as a nested block
while (
($index <= $self->{_CONFIG_LINES})
and ($index <= $$aref_scope[1]) # Stricly to avoid the last end/next is counted as a nested block
and (not($flag_found))
)
{
$line = $self->{_CONFIG}[$index] ;
if (not(defined($line))) {
$index++ ;
next ;
}
# Detect start of netsted config/end or edit/next
if (($nested_key) = $line =~ /^(?:\s|\t)*(config|edit)/) {
warn "$obj:$subn found start of nested block $nested_key line=$index" if $self->debug() ;
$nested_count++ ;
}
# Detect end of nested config/end or edit/next
if (($nested_key) = $line =~ /^(?:\s|\t)*(end|next)$/) {
warn "$obj:$subn found end of nested block $nested_key line=$index" if $self->debug() ;
$nested_count-- ;
}
# Did we found the statement ?
if ( (($nested_count == 0) and (not($nested)))
or ($nested))
{
if ($get_value) {
# Trying to extract values (no list with brackets possible here)
if (($value) = $line =~ /^(?:\s|\t)*(?:set\s)$key(?:\s|\t)(?:")?((.)*)(?:")?(?:(\s|\t)*)(?:\n)?$/) {
if (defined($value)) { $value =~ s/(\s|\t|\")*$// } # remove trailing spaces
warn "$obj:$subn found key at line=$index config line:->$line<- value=$value" if $self->debug() ;
$flag_found = 1 ;
$self->{_FEEDBACK}->{'found'} = $flag_found ;
$self->{_FEEDBACK}->{'index'} = $index ;
$self->{_FEEDBACK}->{'key'} = $key ;
$self->{_FEEDBACK}->{'value'} = $value ;
return ($value) ;
}
}
else {
warn "no value key=$key line=$line" if $self->debug ;
# Don't extract value, only need the index
if ($line =~ /^(?:\s|\t)*(?:set\s)$key(?:\s|\t)/) {
warn "$obj:$subn found key at line=$index config line:->$line<- (novalue)" if $self->debug() ;
$flag_found = 1 ;
$self->{_FEEDBACK}->{'found'} = $flag_found ;
$self->{_FEEDBACK}->{'index'} = $index ;
return () ;
}
}
}
$index++ ;
}
# Use default if needed
if (defined($default) and (not($flag_found))) {
warn "$obj:$subn using provided default value default=$default for key=$key" if $self->debug() ;
$value = $default ;
}
warn "$obj:$subn returning value=$value for key=$key" if $self->debug() ;
return ($value) ;
}
# ---
sub unset_key {
my $subn = "unset_key" ;
# remove the line with the key if found in the given scope
# if not found, no action is taken
my $self = shift ;
my %options = @_ ;
my $aref_scope = $options{'aref_scope'} ;
my $key = $options{'key'} ;
my $nested = defined($options{'nested'}) ? $options{'nested'} : NOTNESTED ;
warn "\n* Entering $obj:$subn with scope, key=$key, nested=$nested scope=[" . $$aref_scope[0] . "-" . $$aref_scope[1] . "]" if $self->debug ;
# Sanity
die "key is required" if (not(defined($key))) ;
# See if the key exists, if so, get its index (we don't provide any default value here)
my $return = $self->get_key($aref_scope, $key, $nested, '', 0) ; # Don't want any value, only see existance of key
my $found = $self->feedback('found') ;
if ($found) {
my $index = $self->feedback('index') ;
warn "$obj:$subn key $key found => deleting (blanking) line at index=$index" if $self->debug ;
$self->delete(index => $index) ;
}
else {
warn "$obj:$subn key $key was not found, do nothing" if $self->debug ;
}
}
# ---
sub set_key {
my $subn = "set_key" ;
# Inserts or update the configuration key
# The key is first search on the given scope, if found the key is replaced (no new line added)
# If not found, a new line with the key is added in the configuration
my $self = shift ;
my %options = @_ ;
my $aref_scope = $options{'aref_scope'} ;
my $key = $options{'key'} ;
my $value = $options{'value'} ;
my $index_increment = defined($options{'index_increment'}) ? $options{'index_increment'} : 1 ; # increment of position to add from scope start
my $nb_spaces = defined($options{'nb_spaces'}) ? $options{'nb_spaces'} : 0 ; # number of spaces before the "set"
my $nested = defined($options{'nested'}) ? $options{'nested'} : NOTNESTED ; # use NESTED|NOTNESTED (declared as constant)
warn "\n* Entering $obj:$subn with scope, key=$key, value=$value, index_increment=$index_increment, nb_spaces=$nb_spaces index_nested=$nested"
if $self->debug ;
# See if the key exists, if so, get its index (we don't provide any default value here
$self->get_key($aref_scope, $key, $nested, '') ;
my $found = $self->feedback('found') ;
my $content = " " x $nb_spaces . "set $key $value\n" ;
if ($found) {
my $old_value = $self->feedback('value') ;
my $index = $self->feedback('index') ;
warn "$obj:$subn key $key found => replacing existing value \"$old_value\" with \"$value\" at index=$index" if $self->debug ;
$self->set_line(index => $index, content => $content) ;
}
else {
# Get index where to add the statement, index is at scope start + index increment
my $scope_start = $$aref_scope[0] ;
my $index = $scope_start + $index_increment ;
warn "$obj:$subn key $key not found => adding a new key statement with value \"$value\" at index=$index" if $self->debug ;
chomp($content) ;
$self->insert(index => $index, content => $content) ;