-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcfg_global.pm
More file actions
429 lines (300 loc) · 11.3 KB
/
Copy pathcfg_global.pm
File metadata and controls
429 lines (300 loc) · 11.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
# ****************************************************
# * *
# * C F G G L O B A L *
# * *
# * Object for global features *
# * *
# * Author : Cedric Gustave cgustave@fortinet.com *
# * *
# ****************************************************
#
package cfg_global ;
my $obj = "cfg_global" ;
use Moose ;
use Data::Dumper ;
extends('cfg_rules') ;
use constant NESTED => 1 ;
use constant NOTNESTED => 0 ;
has 'cfg' => (isa => 'cfg_dissector', is => 'rw', required => 1) ;
has 'intfs' => (isa => 'cfg_interfaces', is => 'rw', required => 1) ;
has 'vd' => (isa => 'cfg_vdoms', is => 'rw') ;
has 'debug' => (isa => 'Maybe[Int]', is => 'rw', default => '0') ;
has 'ruledebug' => (isa => 'Str', is => 'rw') ;
has 'splitconfigdir' => (isa => 'Str', is => 'rw', default => '.') ;
# ---
sub BUILD {
my $subn = "BUILD" ;
my $self = shift ;
warn "\n* Entering $obj:$subn with debug=".$self->debug if $self->debug ;
$self->{GLOBAL} = {} ;
$self->{_HA} = {} ;
$self->{'_FORTIGUARD'} = {} ;
}
# ---
sub get {
my $subn = "get" ;
# Returns value stored in global object
my $self = shift ;
my %options = @_ ;
# sanity
die "-key required" if (not(defined($options{'key'}))) ;
return ($self->{GLOBAL}->{$options{'key'}}) ;
}
# ---
sub set {
my $subn = "set" ;
# Returns value stored in global object
my $self = shift ;
my %options = @_ ;
# sanity
die "-key required" if (not(defined($options{'key'}))) ;
die "-value required" if (not(defined($options{'value'}))) ;
$self->{GLOBAL}->{$options{'key'}} = $options{'value'} ;
}
# ---
sub concat {
my $subn = "concat" ;
my $self = shift ;
my %options = @_ ;
# sanity
die "-key is required" if (not(defined($options{'key'}))) ;
die "-value is required" if (not(defined($options{'value'}))) ;
# Create attribute if not defined
if (not(defined($self->{GLOBAL}->{$options{'key'}}))) {
$self->{GLOBAL}->{$options{'key'}} = $options{'value'} ;
}
else {
$self->{GLOBAL}->{$options{'key'}} .= $options{'value'} ;
}
}
# ---
sub global_info {
my $subn = "global_info" ;
my $self = shift ;
my ($key, $value) = undef ;
warn "Entering $obj:$subn" if $self->debug() ;
# Flag special image from config_headers
$self->_flag_special_image() ;
}
# ---
sub parse_resource_limits {
my $subn = "parse_resource_limits" ;
# Inspect configured resource limitation which may impact traffic or deny connections
my $self = shift ;
my @scope = (undef, undef) ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
my %hash_resource_limits = (
'session' => '0',
'dialup-tunnel' => '0',
'sslvpn' => '0',
) ;
$self->cfg->scope_config_and_multiget(\@scope, 'config system resource-limits', \%hash_resource_limits, \$self->{'_RESSOURCE_LIMITS'}) ;
}
# ---
sub parse_admin_users {
my $subn = "parse_admin_users" ;
my $self = shift ;
my @scope = (undef, undef) ;
my @edit_scope = () ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
# Scope for 'config global' if vdoms
if ($self->cfg->vdom_enable) {
$self->cfg->scope_config(\@scope, 'config global') ;
}
my $ok = $self->cfg->scope_config(\@scope, 'config system admin') ;
die "Could not find system admin" if (not $ok) ;
$edit_scope[0] = $scope[0] ;
$edit_scope[1] = $scope[1] ;
my $user = "" ;
my $no_trusthost = 0 ;
my $passwd = 1 ;
$self->{ADMIN_NO_PASSWD} = 0 ;
$self->{ADMIN_ALL_TRUSTHOST} = 1 ;
while ($self->cfg->scope_edit(\@edit_scope, 'edit', \$user)) {
warn "$obj:$subn processing user=$user" if $self->debug() ;
push @{$self->{_USER_LIST}}, $user ;
$self->parse_user_details(\@edit_scope, $user, \$no_trusthost, \$passwd) ;
# set scope for next round
$edit_scope[0] = $edit_scope[1] ;
$edit_scope[1] = $scope[1] ;
# Verify admin account has a passwd and
if (not($passwd)) {
$self->{ADMIN_NO_PASSWD} = 1 ;
}
}
# Verify if 1 admin user has no trushost (this is a security breach)
if ($no_trusthost) {
$self->{ADMIN_ALL_TRUSTHOST} = 0 ;
}
warn "$obj:$subn ADMIN_NO_PASSWD=" . $self->{ADMIN_NO_PASSWD} . " ADMIN_ALL_TRUSTHOST=" . $self->{ADMIN_ALL_TRUSTHOST} if $self->debug() ;
}
# ---
sub get_admin_no_passwd {
my $self = shift ;
return ($self->{ADMIN_NO_PASSWD}) ;
}
# ---
sub get_admin_all_trusthost {
my $self = shift ;
return ($self->{ADMIN_ALL_TRUSTHOST}) ;
}
# ---
sub parse_user_details {
my $subn = "parse_user_details" ;
my $self = shift ;
my $aref_scope = shift ;
my $user = shift ;
my $ref_no_trusthost = shift ;
my $ref_passwd = shift ;
my ($key, $default, $value) = undef ;
my $trusted_host_local = 0 ;
warn "\n* Entering $obj:$subn with user=$user scope=(" . $$aref_scope[0] . "," . $$aref_scope[1] . ")" if $self->debug() ;
my %hash_user = (
'password' => '',
'trusthost1' => '',
'trusthost2' => '',
'trusthost3' => '',
) ;
foreach my $key (keys %hash_user) {
my $value = $self->cfg->get_key($aref_scope, $key, NOTNESTED, '') ;
$key = '' if (not(defined($key))) ;
warn "$obj:$subn user=$user key=$key value=$value" if $self->debug() ;
# passwd
if (($key eq 'password') and $value eq '') {
$$ref_passwd = 1 ;
}
# do we have a trustedhost ?
if (($key =~ /trusthost/) and ($value ne '0.0.0.0 0.0.0.0') and ($value ne '')) {
$trusted_host_local = 1 ;
}
}
if (not($trusted_host_local)) {
warn "$obj:$subn no trusted host for user=$user" if $self->debug() ;
$$ref_no_trusthost = 1 ;
}
}
# ---
sub parse_ha {
my $subn = "parse_ha" ;
my $self = shift ;
my @scope = (undef, undef) ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
my %hash_ha = (
'mode' => 'standalone',
'schedule' => 'roundrobin',
'hbdev' => '',
'ha-mgmt-interface' => 'disable',
'vcluster2' => 'disable',
) ;
$self->cfg->scope_config_and_multiget(\@scope, 'config system ha', \%hash_ha, \$self->{'_HA'}) ;
# Get hbdev interface and flag interface hash
#warn "hbdev=".$hash_ha{'hbdev'};
my ($port1, $port2) = "" ;
my ($prio1, $prio2) = "" ;
#warn "line=".$hash_ha{'hbdev'} ;
# flag HA interfaces with priorities if system is running HA
if (not($hash_ha{'mode'} eq 'standalone')) {
if (($port1, $prio1) = $hash_ha{'hbdev'} =~ /^(\w+)(?:\")(?:\s)+(\d+)$/) {
#warn "single hbdev port=$port1 prio=$prio1" ;
$self->intfs->concat(name => $port1, key => 'interface', value => "[HA $prio1]") ;
}
if (($port1, $prio1, $port2, $prio2) = $hash_ha{'hbdev'} =~ /^(\w+)(?:\")(?:\s)+(\d+)(?:\s)+(?:\")(\w+)(?:\")(?:\s)+(\d+)$/) {
#warn "dual hbdev port1=$port1 prio1=$prio1 port2=$port2 prio2=$prio2" ;
$self->intfs->concat(name => $port1, key => 'interface', value => "[HA $prio1]") ;
$self->intfs->concat(name => $port2, key => 'interface', value => "[HA $prio2]") ;
}
}
$self->set(key => 'ha_mode', value => $self->{'_HA'}->{'mode'}) ;
}
# ---
sub raise_global_warnings {
my $subn = "raise_global_warnings" ;
# Parses values eligible for warning and raise warning flag if needed
my $self = shift ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
$self->{GLOBAL}->{warnings} = "" if (not(defined($self->{GLOBAL}->{warnings}))) ;
$self->warnAdd(warn => 'SP3_PORT', severity => 'low', toolTip => 'NP4 ports are diverted to XH0') if ($self->intfs->has_sp3_port()) ;
}
# ---
sub parse_standalone_session_sync {
my $subn = "parse_standalone_session_synch" ;
# Session sync is configured in global
# we need to look for key 'syncvd' to find out what are the synched vdom and flag them
my $self = shift ;
my @scope = (undef, undef) ;
my @edit_scope = () ;
my $syncvd = undef ;
my $id = undef ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
my $ok = $self->cfg->scope_config(\@scope, 'config system session-sync') ;
$edit_scope[0] = $scope[0] ;
$edit_scope[1] = $scope[1] ;
if ($ok) {
# Go through all edit and identify vdoms selected by "set syncvd"
while ($self->cfg->scope_edit(\@edit_scope, 'edit', \$id)) {
warn "$obj:$subn processing edit id=$id" if $self->debug() ;
$syncvd = $self->cfg->get_key(\@scope, "syncvd") ;
$syncvd =~ s/\"//g ; # Strip "
warn "$obj:$subn syncvd: $syncvd" if $self->debug() ;
$self->vd->set(vdom => $syncvd, key => 'ssync', value => "YES") ;
# Set scope for next id round
$edit_scope[0] = $edit_scope[1] ;
$edit_scope[1] = $scope[1] ;
}
}
}
# ---
sub splitconfig_global {
my $subn = "splitconfig_global" ;
my $self = shift ;
my $fh_out = new FileHandle ;
warn "\n* Entering $obj:$subn" if $self->debug() ;
if ($self->cfg->vdom_enable) {
open $fh_out, '>:encoding(iso-8859-1)', $self->splitconfigdir . "/_global.conf" or die "open: $!" ;
# Write a header
print $fh_out $self->cfg->line(1) ;
print $fh_out $self->cfg->line(2) ;
print $fh_out $self->cfg->line(3) ;
print $fh_out "\n\n" ;
# Get end global index from vdom object
my $end_global_index = $self->cfg->get_end_global_index() ;
for (my $index = 5 ; $index < ($end_global_index - 4) ; $index++) {
warn "$obj:$subn processing global, index=$index" if $self->debug() ;
print $fh_out $self->cfg->line($index) ;
}
close $fh_out ;
}
}
# ---
sub _flag_special_image {
my $subn = "_flag_special_image" ;
my $self = shift ;
my $buildno = undef ;
warn "\n* Entering $subn" if $self->debug() ;
# Detect special image if build number in line 1 does not match the 3rd or 4th buildno=
my $build = $self->cfg->build() ;
if (($buildno) = $self->cfg->line(3) =~ /^(?:#buildno=)(\d{4})/) {
if ($buildno ne $build) {
warn "$obj:$subn special image from header line 3 build=$build buildno=$buildno" if $self->debug() ;
$self->cfg->build_tag($buildno) ;
if ($buildno != $build) {
warn "$obj:$subn Looks like a special image" if $self->debug() ;
$self->warnAdd(warn => 'SPECIAL_IMAGE', severity => 'low', toolTip => 'Firmware is a special image') ;
}
}
}
if (($buildno) = $self->cfg->line(4) =~ /^(?:#buildno=)(\d{4})/) {
if ($buildno ne $build) {
warn "$obj:$subn special image from header line 4 build=$build buildno=$buildno" if $self->debug() ;
$self->cfg->build_tag($buildno) ;
if ($buildno != $build) {
warn "$obj:$subn Looks like a special image" if $self->debug() ;
$self->warnAdd(warn => 'FORTICARRIER_SPECIAL_IMAGE', severity => 'low', toolTip => 'Firmware is a forticarrier image') ;
}
}
}
}
# ---
# ___END_OF_OBJECT___
__PACKAGE__->meta->make_immutable ;
1 ;