forked from cgustave/fgtconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg_fgtconfig.pm
More file actions
472 lines (353 loc) · 13.3 KB
/
cfg_fgtconfig.pm
File metadata and controls
472 lines (353 loc) · 13.3 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
# ****************************************************
# * *
# * F G T C O N F I G *
# * *
# * Parser object for fortigate config style *
# * *
# * Author : Cedric Gustave cgustave@fortinet.com *
# * *
# ****************************************************
package cfg_fgtconfig ;
my $obj = "cfg_fgtconfig" ;
use FileHandle ;
use Moose ;
use Data::Dumper ;
# Uses generic dissector tools
use cfg_dissector ;
use cfg_interfaces ;
use cfg_global ;
use cfg_vdoms ;
use cfg_display ;
use cfg_statistics ;
use constant NESTED => 1 ;
use constant NOTNESTED => 0 ;
# Definition of the colums details for both TP moode and NAT. Some are optional
my $COLOR = 0 ;
# Ansi foreground colors
my $color_red = "\e[31m" ;
my $color_white = "\e[37m" ;
my $color_green = "\e[32m" ;
my $color_yellow = "\e[33m" ;
my $YES_S = "" ;
my $NO_S = "" ;
my $END = "" ;
my %hash_columns = (
'nat',
{
'1', {'label' => 'interface', 'key' => 'name', 'format' => "20", 'required' => '1'},
'2', {'label' => 'alias', 'key' => 'alias', 'format' => "20", 'required' => '0'},
'3', {'label' => 'zone', 'key' => 'zone', 'format' => "20", 'required' => '0'},
'4', {'label' => 'physical', 'key' => 'physical', 'format' => "20", 'required' => '0'},
'5', {'label' => 'mode', 'key' => 'mode', 'format' => "6", 'required' => '0'},
'6', {'label' => 'vlan', 'key' => 'vlan', 'format' => "4", 'required' => '0'},
'7', {'label' => 'ip', 'key' => 'ip', 'format' => "18", 'required' => '1'},
'8', {'label' => 'network', 'key' => 'network', 'format' => "15", 'required' => '1'},
'9', {'label' => 'broadcast', 'key' => 'broadcast', 'format' => "15", 'required' => '1'},
'10', {'label' => 'status', 'key' => 'status', 'format' => "6", 'required' => '1'},
'11', {'label' => 'mtu-override', 'key' => 'mtu-override', 'format' => "6", 'required' => '0'},
'12', {'label' => 'PS', 'key' => 'pingserver', 'format' => "2", 'required' => '0'},
'13', {'label' => 'mtu', 'key' => 'mtu', 'format' => "4", 'required' => '0'},
'14', {'label' => 'speed', 'key' => 'speed', 'format' => "8", 'required' => '0'},
'15', {'label' => 'admin', 'key' => 'admin', 'format' => "16", 'required' => '1'},
},
'tp',
{
'1', {'label' => 'interface', 'key' => 'name', 'format' => "20", 'required' => '1'},
'2', {'label' => 'alias', 'key' => 'alias', 'format' => "20", 'required' => '0'},
'3', {'label' => 'zone', 'key' => 'zone', 'format' => "20", 'required' => '0'},
'4', {'label' => 'physical', 'key' => 'physical', 'format' => "20", 'required' => '0'},
'5', {'label' => 'vlan', 'key' => 'vlan', 'format' => "4", 'required' => '0'},
'6', {'label' => 'fwd', 'key' => 'forward-domain', 'format' => "5", 'required' => '0'},
'7', {'label' => 'status', 'key' => 'status', 'format' => "6", 'required' => '1'},
'8', {'label' => 'speed', 'key' => 'speed', 'format' => "8", 'required' => '0'},
'9', {'label' => 'admin', 'key' => 'admin', 'format' => "16", 'required' => '1'},
},
) ;
has 'splitconfigdir' => (
isa => 'Str',
is => 'rw',
default => '.',
trigger => \&_accessor_debug,
) ;
has 'color' => (
isa => 'Str',
is => 'rw',
default => '0',
trigger => \&_accessor_debug,
) ;
has 'configfile' => (
isa => 'Str',
is => 'rw',
required => 1,
predicate => 'has_configfile',
trigger => \&_accessor_debug,
) ;
# debug level at object creation
has 'debug_level' => (
isa => 'Int',
is => 'rw',
required => 0,
default => 0,
trigger => \&_accessor_debug,
) ;
has 'XMLTrsf' => (is => 'rw') ;
# cfg_dissector object
has 'cfg' => (is => 'rw') ;
# cfg_vdom object
has 'vd' => (is => 'rw') ;
# cfg_interface object
has 'intfs' => (is => 'rw') ;
# cfg_global object
has 'glo' => (is => 'rw') ;
# cfg_statistics object
has 'stat' => (is => 'rw') ;
# cfg_display object
has 'dis' => (is => 'rw') ;
# Ddebug flag for ourself
has 'debug' => (
isa => 'Int',
is => 'rw',
required => 0,
default => 0,
trigger => \&_accessor_debug,
) ;
# ---
sub BUILD {
my $subn = "BUILD" ;
# initiate all objects with debugging
my $self = shift ;
warn "\n* Entering $obj:$subn debug_level=" . $self->debug_level() if $self->debug_level() ;
# object debug masks
# 2 : fgtconfig
# 4 : cfg_dissector
# 8 : cfg_interfaces
# 16 : cfg_global
# 32 : cfg_vdom
# 64 : cfg_display
# 128 : cfg_statistics
# Set debug for outself if & 2
$self->debug(1) if ($self->debug_level & 2) ;
# building the config dissector object
$self->cfg(cfg_dissector->new(configfile => $self->configfile(), debug => ($self->debug_level & 4))) ;
# building the config interfaces object
$self->intfs(cfg_interfaces->new(cfg => $self->cfg, debug => ($self->debug_level & 8))) ;
# building the vdom object (will provide glo later)
$self->vd(cfg_vdoms->new(cfg => $self->cfg, intfs => $self->intfs, debug => ($self->debug_level & 32))) ;
$self->intfs->vd($self->vd) ;
# since vd needs glo and glo needs vd we have to set the attributes after the new
# building the global object (giving vd already created)
$self->glo(cfg_global->new(cfg => $self->cfg, intfs => $self->intfs, vd => $self->vd, debug => ($self->debug_level & 16))) ;
# give glo to vd
$self->vd->glo($self->glo) ;
# building the statistics object
$self->stat(cfg_statistics->new(cfg => $self->cfg, vd => $self->vd, debug => ($self->debug_level & 128))) ;
# give stat to vd so vd can update statistics as well
$self->vd->stat($self->stat) ;
# building the display object
$self->dis(
cfg_display->new(
cfg => $self->cfg,
intfs => $self->intfs,
glo => $self->glo,
vd => $self->vd,
stat => $self->stat,
splitconfigdir => $self->splitconfigdir(),
debug => ($self->debug_level & 64)
)
) ;
# Initialise vdom_link_list
$self->{_IVLINK} = () ;
}
# ---
sub ruledebug {
# Sets object with rule processing with a ruledebug information
# to turn on debugging for a specific rule
my $self = shift ;
my $rule = undef ;
if (@_) {
$rule = shift ;
$self->{_RULEDEBUG} = $rule ;
$self->glo->ruledebug($rule) ;
$self->vd->ruledebug($rule) ;
}
return $self->{_RULEDEBUG} ;
}
# ---
sub parse {
my $subn = "parse" ;
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
# Load config and prepare data
$self->cfg->configfile($self->configfile()) ;
$self->cfg->load() ;
$self->cfg->config_header() ;
# Register all vdoms so we know them and their scopes for rule processsing
$self->cfg->register_vdoms() ;
# Parse interfaces
$self->intfs->parse_sp3_port() ;
$self->intfs->process_interfaces() ;
$self->intfs->parse_zones() ;
}
# ---
sub summary {
my $subn = "summary" ;
# Dump configuration information such as registered vdoms and correspsonding scopes
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug ;
printf "\tNumber of lines : %d\n", $self->cfg->max_lines ;
printf "\tPlateform : %s\n", $self->cfg->plateform ;
printf "\tVersion : %s\n", $self->cfg->version ;
printf "\tType : %s\n", $self->cfg->type ;
printf "\tBuild : %d\n", $self->cfg->build ;
printf "\tvdom enable : %d\n", $self->cfg->vdom_enable ;
printf "\tNumber of vdoms : %d\n", $self->cfg->get_nb_vdoms ;
printf "\tManagement vdom : %s\n", $self->cfg->get_mgmt_vdom ;
printf "\tPorts : " ;
foreach my $int (@{$self->intfs->{_INTERFACE_LIST}}) {
next if ($self->intfs->get(name => $int, key => 'type') ne 'physical') ;
print $int. " " ;
}
print "\n" ;
print "\nVDOMS:\n" ;
foreach my $vdom ($self->cfg->get_vdom_list()) {
printf "\tname:%-25s startindex:%d\tendindex:%d\n",
$vdom,
$self->cfg->vdom_index(vdom => $vdom, type => 'startindex', action => 'get'),
$self->cfg->vdom_index(vdom => $vdom, type => 'endindex', action => 'get') ;
}
print "\n" ;
}
# ---
sub analyse {
my $subn = "analyse" ;
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
# All global warnings and feature we can check generically
# Use XML based global rule files for processing
$self->glo->processRules() ;
# Get other info that can't be set as a rule *config header extraction'
$self->glo->global_info() ;
# Warnings and features for all vdoms
$self->vd->process_vdoms() ;
$self->vd->system_vdom_limitations() ;
# All features requiring a special traitment
$self->glo->parse_resource_limits() ;
$self->glo->parse_admin_users() ;
$self->glo->parse_standalone_session_sync() ;
$self->glo->parse_ha() ;
# Raise warning flags for special traitement features
$self->glo->raise_global_warnings() ;
# $self->_parse_phase1_policy() ; # TO BE DONE
# The following goes through each vdom, they should be grouped and call with vdom argument
# instead of repeating vdom loop for each one.
$self->vd->parse_phase1_ipsec_interface() ;
$self->vd->parse_phase2_ipsec_interface() ;
$self->vd->parse_routes() ;
$self->vd->parse_policies() ;
$self->vd->parse_interface_policies() ;
$self->vd->parse_dos_policies() ;
# Compute stats if needed
if ($self->dis->stats_flag()) {
warn "$obj:$subn Starting object statistics" if $self->debug() ;
$self->stat->object_statistics() ;
}
}
# ---
sub pre_split_processing {
my $subn = "pre_split_processing" ;
my $self = shift ;
my %options = @_ ;
my $nouuid = $options{'nouuid'} ;
warn "*Entering $obj:$subn with nouuid=$nouuid" if $self->debug() ;
my @scope = (1, $self->cfg->max_lines) ;
$self->cfg->delete_all_keys_from_block(aref_scope => \@scope, key => 'uuid') ;
}
# ---
sub splitconfig {
my $subn = "splitconfig" ;
my $self = shift ;
my $config = shift ;
my $fh_out = new FileHandle ;
my $splitconfig = "" ;
warn "*Entering $obj:$subn with config=$config" if $self->debug() ;
# Get splitconfig dir
$splitconfig = $self->splitconfigdir() ;
$splitconfig = "." if $splitconfig eq "-config" ;
warn "$obj:$subn splitconfig: $splitconfig" if $self->debug() ;
# Split all vdoms in files
$self->vd->splitconfigdir($splitconfig) ;
$self->vd->splitconfig_vdoms() ;
# Generated _global.conf file if needed
$self->glo->splitconfigdir($splitconfig) ;
$self->glo->splitconfig_global($splitconfig) ;
# Generate config summary file
$self->dis->splitconfigdir($splitconfig) ;
$self->dis->display('file') ;
}
# ---
sub fullstats {
my $subn = "fullstats" ;
my $self = shift ;
# Parsing configuration using cfg_dissector
$self->parse() ;
my @vdoms = $self->cfg->get_vdom_list();
foreach my $vd (@vdoms) {
warn "processing vd=$vd" if $self->debug;
}
# building a stat object from cfg_statistics
# providing a dissector configuration
$self->stat(cfg_statistics->new(cfg => $self->cfg, vd => $self->vd, debug => ($self->debug_level & 128))) ;
# Run all statistics on the config defined in the cfg_statistics object
$self->stat->object_statistics();
# For testing purpose, dump the statistic object results for each vdom
$self->stat->dump();
}
# ---
sub test {
my $subn = "test" ;
# this method is used for testing purpose
my $self = shift ;
my %options = @_ ;
die "object name is required" if (not(defined($options{'object'}))) ;
die "key is required" if (not(defined($options{'key'}))) ;
if ($options{'object'} eq 'glo') {
return ($self->glo->get(%options)) ;
}
elsif ($options{'object'} eq 'vd') {
return ($self->vd->get(%options)) ;
}
elsif ($options{'object'} eq 'intfs') {
return ($self->intfs->get(%options)) ;
}
elsif ($options{'object'} eq 'stat') {
return ($self->stat->get(%options)) ;
}
elsif ($options{'object'} eq 'cfg') {
return ($self->cfg->get(%options)) ;
}
}
# --------------------------------------------------
#
# Internal functions not exposed outside this object
#
# ---------------------------------------------------
sub _accessor_debug {
my $self = shift ;
my $value = shift ;
my $href_att = shift ;
#my $accessor = $$href_att{'accessor'} ;
#my $isa = $$href_att{'isa'} ;
#if (defined($accessor)) {
#if ($self->debug() or $accessor eq 'debug') {
# warn "* Accessor $accessor : set value=$value (type=$isa)" ;
# }
#}
# Parse all attributes
#foreach my $item (keys %{$href_att}) {
# warn "key=".$item." value=".$$href_att{$item};
# }
}
# ---
# ___END_OF_OBJECT___
__PACKAGE__->meta->make_immutable ;
1 ;