-
Notifications
You must be signed in to change notification settings - Fork 5
/
PSFortigateParser.psm1
2449 lines (2285 loc) · 98.8 KB
/
PSFortigateParser.psm1
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
#Generated at 2020-05-22 20:35 by Ørnulf Nielsen
#region Class PSFortigateConfig : System.IDisposable
Class PSFortigateConfig : System.IDisposable {
#region Properties
Hidden [System.Boolean]$Disposing = $false
[System.String]$Path
[System.Text.Encoding]$Encoding = [System.Text.Encoding]::Default
Hidden [System.IO.FileStream]$FileStream
Hidden [System.IO.StreamReader]$StreamReader
Hidden [System.Boolean]$BreakTopLevel = $false
Hidden [System.Boolean]$inPolicySection = $false
Hidden [System.Boolean]$inServiceSection = $false
Hidden [System.Int32]$PolicySequence
[System.Collections.Hashtable]$Config
#endregion
#region Constructors
#region Hidden [void]Constructor()
[void] Constructor() {
$this.FileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $this.Path, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
$this.StreamReader = New-Object -TypeName System.IO.StreamReader -ArgumentList $this.FileStream, $this.Encoding
$this.Config = New-Object System.Collections.Hashtable
$this.ReadConfig()
$this.StreamReader.Close()
$this.FileStream.Close()
}
#endregion
#region PSFortigateConfig($Path)
PSFortigateConfig(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfig: Read {0}' -f $Path)
$this.Path = $Path
$this.Constructor()
}
#endregion
#region PSFortigateConfig($Path, $Encoding)
PSFortigateConfig(
[System.String]$Path,
[System.Text.Encoding]$Encoding
) {
Write-Debug ('PSFortigateConfig: Read {0} using {1}' -f $Path, ($Encoding).EncodingName)
$this.Path = $Path
$this.Encoding = $Encoding
$this.Constructor()
}
#endregion
#endregion
#region Destructors
#region Dispose()
[void] Dispose() {
$this.Disposing = $true
$this.Dispose($true)
[System.GC]::SuppressFinalize($this)
}
#endregion
#region Dispose($Disposing)
[void] Dispose(
[System.Boolean]$Disposing
) {
if($Disposing) {
$this.StreamReader.Dispose()
$this.FileStream.Dispose()
}
}
#endregion
#endregion
#region ReadConfig
Hidden ReadConfig() {
while ($this.StreamReader.Peek() -ge 0) {
$this.BreakTopLevel = $false
$sLine = $this.StreamReader.ReadLine()
if ([Environment]::UserInteractive) {
Write-Progress `
-Activity ('Parsing {0}' -f ($this.StreamReader.BaseStream.Name | Split-Path -Leaf)) `
-PercentComplete ([int32]($this.StreamReader.BaseStream.Position * 100 / $this.StreamReader.BaseStream.Length))
}
if ($sLine -match "^\s*config\s+(?<section>.*)\s*$") {
$ConfigSection = $this.ReadConfigSection($Matches.section, 'end')
# Merge sections which occur multiple times on top level in config
if ($this.Config.ContainsKey($Matches.section)) {
# vdom
if ($Matches.section -eq 'vdom') {
foreach ($h in $ConfigSection.GetEnumerator()) {
Write-Debug ('PSFortigateConfig: Found config section vdom {0}' -f $h.Name)
$this.Config[$Matches.section][$h.Name] = $h.Value
}
}
} else {
Write-Debug ('PSFortigateConfig: Found config section {0}' -f $Matches.section)
$this.Config[$Matches.section] = $ConfigSection
}
continue
}
}
}
#endregion
#region ReadConfigSection
Hidden [System.Collections.Hashtable]ReadConfigSection(
[System.String]$CurrentSection,
[System.String]$EndMarker
) {
$Section = New-Object System.Collections.Hashtable
while ($this.StreamReader.Peek() -ge 0) {
# Special handling for vdom - end without next
if ($this.BreakTopLevel) {
Write-Debug 'PSFortigateConfig: end without next'
break
}
$sLine = $this.StreamReader.ReadLine()
# Recurse config and edit statements
if ($sLine -match "^\s*(?<type>config|edit)\s+(?<section>.*)\s*$") {
if ($Matches.type -eq 'config') {
Write-Debug ('PSFortigateConfig: Found config section {0}' -f $Matches.section)
if ($Matches.section -eq "firewall policy" -or $Matches.section -eq "firewall proxy-policy" ) {
$this.inPolicySection = $true
$this.PolicySequence = 0
} elseif ($Matches.section -eq "firewall service custom") {
$this.inServiceSection = $true
}
$Section[$Matches.section] = $this.ReadConfigSection($Matches.section, 'end')
} else {
Write-Debug ('PSFortigateConfig: Found config sub section {0}' -f $Matches.section)
if ($this.inPolicySection) {
$this.PolicySequence++
}
$Section[$Matches.section -replace "`"",""] = $this.ReadConfigSection($Matches.section, 'next')
}
continue
}
# Break on end and next statements
if ($sLine -match ("^\s*(?<EndMarker>next|end)\s*$")) {
# Special handling for vdom - end without next
if ($Matches.EndMarker -eq "end" -and $this.inPolicySection) {
$this.inPolicySection = $false
} elseif ($Matches.EndMarker -eq "end" -and $this.inServiceSection) {
$this.inServiceSection = $false
}
if ($Matches.EndMarker -eq "end" -and $EndMarker -eq "next") {
$this.BreakTopLevel = $true
}
break
}
# Section property
if ($sLine -match "^(\s*)set\s+(?<Key>[^\s]+)\s+(?<Value>.+)\s*$") {
$PropertyKey = $Matches.Key -replace "`"",""
# Inject sequence number for firewall policy
if ($PropertyKey -eq "uuid" -and $this.inPolicySection) {
$Section['sequence'] = $this.PolicySequence
}
# Remove double quotes - use array if multi-valued
if (($this.inServiceSection -and $PropertyKey -like "*-portrange") -or ($this.inPolicySection -and $PropertyKey -like "internet-service-id")) {
$PropertyValue = $Matches.Value -split "\s+"
if ($PropertyValue.Count -eq 1) {
$PropertyValue = $PropertyValue -as [System.String]
}
} else {
$PropertyValue = $Matches.Value -split "`"\s+`""
if ($PropertyValue.Count -gt 1) {
$PropertyValue[0] = $PropertyValue[0] -replace "^`"",""
$PropertyValue[-1] = $PropertyValue[-1] -replace "`"\s*$",""
} else {
$PropertyValue = $PropertyValue -replace "`"","" -as [System.String]
}
}
$Section[$PropertyKey] = $PropertyValue
}
}
return $Section
}
#endregion
}
#endregion
#region Class PSFortigateConfigObject
Class PSFortigateConfigObject : PSFortigateConfig {
#region Properties
Hidden [PSCustomObject]$PolicyTemplate
Hidden [PSCustomObject]$ProxyPolicyTemplate
Hidden [PSCustomObject]$AddressTemplate
Hidden [PSCustomObject]$AddressGroupTemplate
Hidden [PSCustomObject]$ServiceTemplate
Hidden [PSCustomObject]$ServiceGroupTemplate
Hidden [PSCustomObject]$LocalUserTemplate
Hidden [PSCustomObject]$FortitokenTemplate
Hidden [PSCustomObject]$UserGroupTemplate
Hidden [PSCustomObject]$InterfaceTemplate
Hidden [PSCustomObject]$IPsecPhase1Template
Hidden [PSCustomObject]$IPsecPhase2Template
Hidden [PSCustomObject]$SystemZoneTemplate
Hidden [PSCustomObject]$RouterStaticTemplate
#endregion
#region Constructors
#region Hidden [void]Constructor()
Hidden [void] Constructor() {
# Call parent constructor
([PSFortigateConfig]$this).Constructor()
# Setup default templates
$this.setPolicyTemplate()
$this.setProxyPolicyTemplate()
$this.setAddressTemplate()
$this.setAddressGroupTemplate()
$this.setServiceTemplate()
$this.setServiceGroupTemplate()
$this.setLocalUserTemplate()
$this.setFortitokenTemplate()
$this.setUserGroupTemplate()
$this.setInterfaceTemplate()
$this.setIPsecPhase1Template()
$this.setIPsecPhase2Template()
$this.setSystemZoneTemplate()
$this.setRouterStaticTemplate()
}
#endregion
#region PSFortigateConfig($Path)
PSFortigateConfigObject(
[System.String]$Path
) : base($Path) {
$this.Path = $Path
}
#endregion
#region PSFortigateConfigObject($Path, $Encoding)
PSFortigateConfigObject(
[System.String]$Path,
[System.Text.Encoding]$Encoding
) : base($Path, $Encoding) {
$this.Path = $Path
$this.Encoding = $Encoding
}
#endregion
#endregion
#region Hidden [System.String[]]ReadTextFile($Path)
Hidden [System.String[]]ReadTextFile(
[System.String]$Path
) {
$FileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
$StreamReader = New-Object -TypeName System.IO.StreamReader -ArgumentList $FileStream, $this.Encoding
return ($StreamReader.ReadToEnd()).Split([Environment]::NewLine)
$StreamReader.Close()
$StreamReader.Dispose()
$FileStream.Close()
$FileStream.Dispose()
}
#endregion
#region [void]setPolicyTemplate([System.String[]]$Template)
[void]setPolicyTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; sequence = $null ; policyid = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.PolicyTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setPolicyTemplate()
[void]setPolicyTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default policy template'
$Template = @"
edit "deleteme"
set name "deleteme"
set uuid "deleteme"
set srcintf "deleteme"
set dstintf "deleteme"
set srcaddr "deleteme"
set dstaddr "deleteme"
set internet-service "deleteme"
set rtp-nat "deleteme"
set learning-mode "deleteme"
set action "deleteme"
set status "deleteme"
set schedule "deleteme"
set schedule-timeout "deleteme"
set service "deleteme"
set dscp-match "deleteme"
set utm-status "deleteme"
set logtraffic "deleteme"
set logtraffic-start "deleteme"
set capture-packet "deleteme"
set auto-asic-offload "deleteme"
set np-accelation "deleteme"
set wanopt "deleteme"
set webcache "deleteme"
set session-ttl "deleteme"
set vlan-cos-fwd "deleteme"
set vlan-cos-rev "deleteme"
set wccp "deleteme"
set disclaimer "deleteme"
set natip "deleteme"
set diffserv-forward "deleteme"
set diffserv-reverse "deleteme"
set tcp-mss-sender "deleteme"
set tcp-mss-receiver "deleteme"
set comments "deleteme"
set block-notification "deleteme"
set replacemsg-override-group "deleteme"
set srcaddr-negate "deleteme"
set dstaddr-negate "deleteme"
set service-negate "deleteme"
set timeout-send-rst "deleteme"
set captive-portal-exempt "deleteme"
set ssl-mirror "deleteme"
set scan-botnet-connections "deleteme"
set dsri "deleteme"
set radius-mac-auth-bypass "deleteme"
set delay-tcp-npu-session "deleteme"
set traffic-shaper "deleteme"
set traffic-shaper-reverse "deleteme"
set per-ip-shaper "deleteme"
set nat "deleteme"
set match-vip "deleteme"
set global-label "deleteme"
set ips-sensor "deleteme"
set ssl-ssh-profile "deleteme"
set dnsfilter-profile "deleteme"
set profile-protocol-options "deleteme"
set users "deleteme"
set groups "deleteme"
set ippool "deleteme"
set poolname "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setPolicyTemplate($Template)
}
#endregion
#region [void]setPolicyTemplate($Path)
[void]setPolicyTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load policy template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setPolicyTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getPolicy()
[PSCustomObject[]]getPolicy() {
$cPolicies = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['firewall policy'].count -gt 0) {
foreach ($Policy in $vdom.Value['firewall policy'].GetEnumerator()) {
$oPolicy = $this.PolicyTemplate.PsObject.Copy()
$oPolicy.vdom = $vdom.Name
$oPolicy.policyid = $Policy.Name
foreach ($PolicyOption in $Policy.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Policy {1} Option {2}' -f $vdom.Name, $Policy.Name, $PolicyOption.Name)
$oPolicy.($PolicyOption.Name) = $PolicyOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Address {1} Option {2} - option not found in policy template' -f $vdom.Name, $Policy.Name, $PolicyOption.Name)
}
}
$cPolicies.Add($oPolicy)
}
}
}
return $cPolicies
}
elseif ($this.Config['firewall policy'].count -gt 0) {
foreach ($Policy in $this.Config['firewall policy'].GetEnumerator()) {
$oPolicy = $this.PolicyTemplate.PsObject.Copy()
$oPolicy.vdom = ""
$oPolicy.policyid = $Policy.Name
foreach ($PolicyOption in $Policy.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Policy {1} Option {2}' -f "No vDom", $Policy.Name, $PolicyOption.Name)
$oPolicy.($PolicyOption.Name) = $PolicyOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Address {1} Option {2} - option not found in policy template' -f "No vDom", $Policy.Name, $PolicyOption.Name)
}
}
$cPolicies.Add($oPolicy)
}
return $cPolicies
}
Write-Debug ('PSFortigateConfigObject: No firewall policy found')
return $null
}
#endregion
#region [void]setAddressTemplate([System.String[]]$Template)
[void]setAddressTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.AddressTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setAddressTemplate()
[void]setAddressTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default address template'
$Template = @"
edit "deleteme"
set uuid "deleteme"
set type "deleteme"
set comment "deleteme"
set visibility "deleteme"
set associated-interface "deleteme"
set color "deleteme"
set allow-routing "deleteme"
set subnet "deleteme"
set start-ip "deleteme"
set end-ip "deleteme"
set wildcard-fqdn "deleteme"
set fqdn "deleteme"
set cache-ttl "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setAddressTemplate($Template)
}
#endregion
#region [void]setAddressTemplate($Path)
[void]setAddressTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load address template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setAddressTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getAddress()
[PSCustomObject[]]getAddress() {
$cAddresses = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['firewall address'].count -gt 0) {
foreach ($Address in $vdom.Value['firewall address'].GetEnumerator()) {
$oAddress = $this.AddressTemplate.PsObject.Copy()
$oAddress.vdom = $vdom.Name
$oAddress.name = $Address.Name
foreach ($AddressOption in $Address.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Address {1} Option {2}' -f $vdom.Name, $Address.Name, $AddressOption.Name)
$oAddress.($AddressOption.Name) = $AddressOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Address {1} Option {2} - option not found in address template' -f $vdom.Name, $Address.Name, $AddressOption.Name)
}
}
$cAddresses.Add($oAddress)
}
}
}
return $cAddresses
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setAddressGroupTemplate([System.String[]]$Template)
[void]setAddressGroupTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.AddressGroupTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setAddressGroupTemplate()
[void]setAddressGroupTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default address group template'
$Template = @"
edit "deleteme"
set uuid "deleteme"
set member "deleteme"
set comment "deleteme"
set visibility "deleteme"
set color "deleteme"
set allow-routing "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setAddressGroupTemplate($Template)
}
#endregion
#region [void]setAddressGroupTemplate($Path)
[void]setAddressGroupTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load address group template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setAddressGroupTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getAddressGroup()
[PSCustomObject[]]getAddressGroup() {
$cAddressGroups = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['firewall addrgrp'].count -gt 0) {
foreach ($AddressGroup in $vdom.Value['firewall addrgrp'].GetEnumerator()) {
$oAddressGroup = $this.AddressGroupTemplate.PsObject.Copy()
$oAddressGroup.vdom = $vdom.Name
$oAddressGroup.name = $AddressGroup.Name
foreach ($AddressGroupOption in $AddressGroup.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Address Group {1} Option {2}' -f $vdom.Name, $AddressGroup.Name, $AddressGroupOption.Name)
$oAddressGroup.($AddressGroupOption.Name) = $AddressGroupOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Address Group {1} Option {2} - option not found in address group template' -f $vdom.Name, $AddressGroup.Name, $AddressGroupOption.Name)
}
}
$cAddressGroups.Add($oAddressGroup)
}
}
}
return $cAddressGroups
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setServiceTemplate([System.String[]]$Template)
[void]setServiceTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.ServiceTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setServiceTemplate()
[void]setServiceTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default service template'
$Template = @"
edit "deleteme"
set proxy "deleteme"
set category "deleteme"
set protocol "deleteme"
set protocol-number "deleteme"
set helper "deleteme"
set check-reset-range "deleteme"
set comment "deleteme"
set color "deleteme"
set visibility "deleteme"
set app-service-type disable
set iprange "deleteme"
set fqdn "deleteme"
set tcp-portrange "deleteme"
set udp-portrange "deleteme"
set sctp-portrange "deleteme"
set tcp-halfclose-timer "deleteme"
set tcp-halfopen-timer "deleteme"
set tcp-timewait-timer "deleteme"
set udp-idle-timer "deleteme"
set session-ttl "deleteme"
set icmptype "deleteme"
set icmpcode "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setServiceTemplate($Template)
}
#endregion
#region [void]setServiceTemplate($Path)
[void]setServiceTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load service template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setServiceTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getService()
[PSCustomObject[]]getService() {
$cServices = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['firewall service custom'].count -gt 0) {
foreach ($Service in $vdom.Value['firewall service custom'].GetEnumerator()) {
$oService = $this.ServiceTemplate.PsObject.Copy()
$oService.vdom = $vdom.Name
$oService.name = $Service.Name
foreach ($ServiceOption in $Service.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Service {1} Option {2}' -f $vdom.Name, $Service.Name, $ServiceOption.Name)
$oService.($ServiceOption.Name) = $ServiceOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Service {1} Option {2} - option not found in service template' -f $vdom.Name, $Service.Name, $ServiceOption.Name)
}
}
$cServices.Add($oService)
}
}
}
return $cServices
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setServiceGroupTemplate([System.String[]]$Template)
[void]setServiceGroupTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.ServiceGroupTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setServiceGroupTemplate()
[void]setServiceGroupTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default service group template'
$Template = @"
edit "deleteme"
set member "deleteme"
set proxy "deleteme"
set comment "deleteme"
set color "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setServiceGroupTemplate($Template)
}
#endregion
#region [void]setServiceGroupTemplate($Path)
[void]setServiceGroupTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load service group template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setServiceGroupTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getServiceGroup()
[PSCustomObject[]]getServiceGroup() {
$cServiceGroups = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['firewall service group'].count -gt 0) {
foreach ($ServiceGroup in $vdom.Value['firewall service group'].GetEnumerator()) {
$oServiceGroup = $this.ServiceGroupTemplate.PsObject.Copy()
$oServiceGroup.vdom = $vdom.Name
$oServiceGroup.name = $ServiceGroup.Name
foreach ($ServiceGroupOption in $ServiceGroup.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} ServiceGroup {1} Option {2}' -f $vdom.Name, $ServiceGroup.Name, $ServiceGroupOption.Name)
$oServiceGroup.($ServiceGroupOption.Name) = $ServiceGroupOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} ServiceGroup {1} Option {2} - option not found in service group template' -f $vdom.Name, $ServiceGroup.Name, $ServiceGroupOption.Name)
}
}
$cServiceGroups.Add($oServiceGroup)
}
}
}
return $cServiceGroups
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setLocalUserTemplate([System.String[]]$Template)
[void]setLocalUserTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.LocalUserTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setLocalUserTemplate()
[void]setLocalUserTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default local user template'
$Template = @"
edit "deleteme"
set status "deleteme"
set type "deleteme"
set two-factor "deleteme"
set fortitoken "deleteme"
set email-to "deleteme"
set sms-server "deleteme"
set sms-phone "deleteme"
set passwd-policy "deleteme"
set passwd-time "deleteme"
set authtimeout "deleteme"
set auth-concurrent-override "deleteme"
set passwd "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setLocalUserTemplate($Template)
}
#endregion
#region [void]setLocalUserTemplate($Path)
[void]setLocalUserTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load local user template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setLocalUserTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getLocalUser()
[PSCustomObject[]]getLocalUser() {
$cLocalUsers = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['user local'].count -gt 0) {
foreach ($LocalUser in $vdom.Value['user local'].GetEnumerator()) {
$oLocalUser = $this.LocalUserTemplate.PsObject.Copy()
$oLocalUser.vdom = $vdom.Name
$oLocalUser.name = $LocalUser.Name
foreach ($LocalUserOption in $LocalUser.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} LocalUser {1} Option {2}' -f $vdom.Name, $LocalUser.Name, $LocalUserOption.Name)
$oLocalUser.($LocalUserOption.Name) = $LocalUserOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} LocalUser {1} Option {2} - option not found in local user template' -f $vdom.Name, $LocalUser.Name, $LocalUserOption.Name)
}
}
$cLocalUsers.Add($oLocalUser)
}
}
}
return $cLocalUsers
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setFortitokenTemplate([System.String[]]$Template)
[void]setFortitokenTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.FortitokenTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setFortitokenTemplate()
[void]setFortitokenTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default fortitoken template'
$Template = @"
edit "deleteme"
set status "deleteme"
set comments "deleteme"
set license "deleteme"
set activation-code "deleteme"
set activation-expire "deleteme"
set reg-id "deleteme"
set os-ver "deleteme"
set seed "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setFortitokenTemplate($Template)
}
#endregion
#region [void]setFortitokenTemplate($Path)
[void]setFortitokenTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load fortitoken template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setFortitokenTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getFortitoken()
[PSCustomObject[]]getFortitoken() {
$cFortitokens = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['user fortitoken'].count -gt 0) {
foreach ($Fortitoken in $vdom.Value['user fortitoken'].GetEnumerator()) {
$oFortitoken = $this.FortitokenTemplate.PsObject.Copy()
$oFortitoken.vdom = $vdom.Name
$oFortitoken.name = $Fortitoken.Name
foreach ($FortitokenOption in $Fortitoken.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} Fortitoken {1} Option {2}' -f $vdom.Name, $Fortitoken.Name, $FortitokenOption.Name)
$oFortitoken.($FortitokenOption.Name) = $FortitokenOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} Fortitoken {1} Option {2} - option not found in fortitoken template' -f $vdom.Name, $Fortitoken.Name, $FortitokenOption.Name)
}
}
$cFortitokens.Add($oFortitoken)
}
}
}
return $cFortitokens
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setUserGroupTemplate([System.String[]]$Template)
[void]setUserGroupTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ vdom = $null; name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.UserGroupTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setUserGroupTemplate()
[void]setUserGroupTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default user group template'
$Template = @"
edit "deleteme"
set group-type "deleteme"
set authtimeout "deleteme"
set auth-concurrent-override "deleteme"
set http-digest-realm "deleteme"
set user-id "deleteme"
set password "deleteme"
set user-name "deleteme"
set sponsor "deleteme"
set company "deleteme"
set email "deleteme"
set mobile-phone "deleteme"
set expire-type "deleteme"
set expire "deleteme"
set max-accounts "deleteme"
set multiple-guest-add "deleteme"
set member "deleteme"
set sso-attribute-value "deleteme"
next
"@.Split([Environment]::NewLine)
$this.setUserGroupTemplate($Template)
}
#endregion
#region [void]setUserGroupTemplate($Path)
[void]setUserGroupTemplate(
[System.String]$Path
) {
Write-Debug ('PSFortigateConfigObject: Load user group template from {0}' -f $Path)
$Template = $this.ReadTextFile($Path)
$this.setUserGroupTemplate($Template)
}
#endregion
#region [PSCustomObject[]]getUserGroup()
[PSCustomObject[]]getUserGroup() {
$cUserGroups = New-Object System.Collections.ArrayList
if ($this.Config['vdom'].count -gt 0) {
foreach ($vdom in $this.Config['vdom'].GetEnumerator()) {
if ($vdom.Value['user group'].count -gt 0) {
foreach ($UserGroup in $vdom.Value['user group'].GetEnumerator()) {
$oUserGroup = $this.UserGroupTemplate.PsObject.Copy()
$oUserGroup.vdom = $vdom.Name
$oUserGroup.name = $UserGroup.Name
foreach ($UserGroupOption in $UserGroup.Value.GetEnumerator()) {
try {
Write-Debug ('PSFortigateConfigObject: Adding vDom {0} User Group {1} Option {2}' -f $vdom.Name, $UserGroup.Name, $UserGroupOption.Name)
$oUserGroup.($UserGroupOption.Name) = $UserGroupOption.Value
}
catch {
Write-Debug ('PSFortigateConfigObject: Skipping vDom {0} User Group {1} Option {2} - option not found in user group template' -f $vdom.Name, $UserGroup.Name, $UserGroupOption.Name)
}
}
$cUserGroups.Add($oUserGroup)
}
}
}
return $cUserGroups
}
Write-Debug ('PSFortigateConfigObject: No vDom found')
return $null
}
#endregion
#region [void]setInterfaceTemplate([System.String[]]$Template)
[void]setInterfaceTemplate(
[System.String[]]$Template
) {
# Columns are displayed according to order in template
$Options = [Ordered]@{ name = $null }
foreach ($Line in $Template) {
if ($Line -match "^(\s*)set (?<Option>[^\s]+)\s+(?<Value>.*)$") {
$Options.add($Matches.Option, $null)
}
}
$this.InterfaceTemplate = New-Object -TypeName "PSCustomObject" -Property $Options
}
#endregion
#region [void]setInterfaceTemplate()
[void]setInterfaceTemplate() {
Write-Debug 'PSFortigateConfigObject: Set default interface template'
$Template = @"
edit "deleteme"
set vdom "deleteme"
set fortilink "deleteme"
set mode "deleteme"
set distance "deleteme"
set priority "deleteme"
set dhcp-relay-service "deleteme"
set allowaccess "deleteme"
set fail-detect "deleteme"
set arpforward "deleteme"
set broadcast-forward "deleteme"
set bfd "deleteme"
set l2forward "deleteme"
set vlanforward "deleteme"
set stpforward "deleteme"
set ips-sniffer-mode "deleteme"
set ident-accept "deleteme"
set ipmac "deleteme"
set subst "deleteme"
set substitute-dst-mac "deleteme"
set status "deleteme"
set netbios-forward "deleteme"
set wins-ip "deleteme"