-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpnSenseXMLConfig.psm1
124 lines (99 loc) · 3.49 KB
/
OpnSenseXMLConfig.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
<#
.SYNOPSIS
Validate that a file is indeed an OPNsense config file.
.DESCRIPTION
This function does some superficial checks to make sure a recognised
OPNsense configuration file is represented by the supplied DOM.
.OUTPUT
The function throws an exception if the configuration is invalid.
#>
function Validate-OpnSenseXMLConfig {
Param(
# The DOM object representing the OPNsense configuration.
$XMLConfig
)
# This is a very lame attempt at checking this. Check to see if there is a
# schema we can check against (doubtful).
try {
$v = $XMLConfig.opnsense.version
} catch {
throw "This doesn't look like an OPNsense configuration file."
}
switch ($v) {
'11.2' {}
default {
throw "Unrecognised configuration file version: $v"
}
}
}
<#
.SYNOPSIS
Load an OPNsense config file into memory
.DESCRIPTION
The Get-OpnSenseXMLConfig function parses and loads an OPNsense configuration
file. It performs some cursory checks (see Validate-OpnSenseXMLConfig).
.OUTPUT
Returns an xml object if the file was valid, otherwise throws an exception.
.EXAMPLE
$ConfigXML = Get-OpnSenseXMLConfig config.xml
Sets the $ConfigXML variable that can then be piped into or sent as an
argument to other cmdlets in the module.
#>
function Get-OpnSenseXMLConfig {
[CmdletBinding()]
Param(
# The path on the filesystem to look for the config file in.
[string]$FilePath
)
[xml]$XMLConfig = Get-Content $FilePath
Validate-OpnSenseXMLConfig $XMLConfig # Throws an exception if broken
return $XMLConfig
}
<#
.SYNOPSIS
Write an OPNsense config file to disk
.DESCRIPTION
The Out-OpnSenseXMLConfig function writes an an OPNsense configuration
file to disk.
When writing the file, revision data will also be updated.
#>
function Out-OpnSenseXMLConfig {
[CmdletBinding()]
Param(
# The path on the filesystem to write for the config file in.
[Parameter(Mandatory=$True)]
[string]$FilePath,
# The DOM of an OPNsense configuration file. The DOM specified will be
# changed in place as a result of executing the cmdlet.
[Parameter(Mandatory=$True, ValueFromPipeline=$true)]
[xml]$ConfigXML,
# A string describing the latest revision.
[Parameter(Mandatory=$False)]
[string]$Description
)
$r = $ConfigXML.SelectSingleNode('/opnsense/revision')
$r.RemoveAll()
foreach ($elementname in @("time", "description", "username")) {
$child = $ConfigXML.CreateElement($elementname)
$r.AppendChild($child) | Out-Null
}
# Get-Date will format dates according to the current culture. The XML
# file expects numbers in US format. So, we need to temporarilly switch
# the culture for this to work.
$CulturalHeritage = [Threading.Thread]::CurrentThread.CurrentCulture
[Threading.Thread]::CurrentThread.CurrentCulture = "en-US"
[string]$r.time = Get-Date -UFormat "%s"
[Threading.Thread]::CurrentThread.CurrentCulture = $CulturalHeritage
if ($Description) {
$r.description = $Description
} else {
$r.description = "Configuration file generated by PoshOpnSense"
}
$r.username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
# We need to do this load of nonsense because Powershell paths are a royal
# pain in the behind.
$sw = New-Object System.IO.StringWriter
$ConfigXML.Save($sw)
Set-Content -Value $sw.ToString() -Path $FilePath
$sw.Dispose()
}