-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpnSenseInterface.psm1
467 lines (388 loc) · 16.4 KB
/
OpnSenseInterface.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
Import-Module "$PSScriptRoot\Util.psm1"
<#
.SYNOPSIS
Creates a new interface in an OPNsense configuration file.
.DESCRIPTION
The New-OpnSenseInterface function manipulates the DOM of an OPNsense XML
configuration document in order to add a new interface.
In order to use this function, an object of the xml (System.Xml.XmlDocument)
type representing an OPNsense configuration is required. The cmdlet will
mutate the DOM supplied in order to create a new interface configuration with the
specified settings.
.EXAMPLE
$c = Get-OpnSenseXMLConfig config.xml; $c | New-OpnSenseInterface -Name opt1 -Interface em1; $c | Out-OpnSenseXMLConfig config.xml
Edit a configuration file to create the OPNSense interface opt1 connected to
the FreeBSD interface em1.
#>
function New-OpnSenseInterface {
[Cmdletbinding()]
Param(
# 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 representing the OPNsense interface name. Must be wan, lan,
# or opt\d+. If not given, the first available opt interface is used.
[Parameter(Mandatory=$False)]
[ValidatePattern("^(wan|lan|opt\d+)$")]
[string]$Name,
# A string representing the FreeBSD interface name
# (as seen in /sbin/ifconfig)
[Parameter(Mandatory=$True)]
[string]$Interface,
# A string containing a "friendly description" of the interface in question.
# Defaults to
[Parameter(Mandatory=$False)]
[string]$Description
)
if (-not $name) {
# A name was not provided, so we need to find a free opt interface.
$i = 1
do {
$Name = "opt$i"
$i++
} while (Get-OpnSenseInterface $ConfigXML -Name $Name)
} else {
$Name = $Name.ToLower()
# Refuse to create a duplicate
if (Get-OpnSenseInterface $ConfigXML -Name $Name) {
Throw "Interface already exists!"
}
}
if (-not $Description) {
$Description = $Name.ToUpper()
}
$XMLElement = $ConfigXML.CreateElement($Name)
foreach ($elementname in @("descr", "if", "enable", "spoofmac")) {
$child = $ConfigXML.CreateElement($elementname)
$XMLElement.AppendChild($child) | Out-Null
}
$ConfigXML.SelectSingleNode('/opnsense/interfaces').AppendChild($XMLElement) | Out-Null
Get-OpnSenseInterface -XMLElement $XMLElement | Set-OpnSenseInterface -Interface $Interface -Description $Description
}
<#
.SYNOPSIS
Manipulates an existing interface in an OPNsense configuration file.
.DESCRIPTION
The Set-OpnSenseInterface function manipulates the DOM of an OPNsense XML
configuration document in order to set information on interfaces matching
specified criteria.
In order to use this function, an object of the xml (System.Xml.XmlDocument)
type representing an OPNsense configuration is required.
.EXAMPLE
$c = Get-OpnSenseXMLConfig config.xml; $c | Get-OpnSenseInterface -Interface em0 | Set-OpnSenseVLAN -Description "AwesomeNet"; $c | Out-OpnSenseXMLConfig config.xml
Edit a configuration file to add a description to the interface belonging to
the em0 interface.
#>
function Set-OpnSenseInterface {
[Cmdletbinding()]
Param(
# The DOM of an OPNsense configuration file. The DOM specified will be
# changed in place as a result of executing the cmdlet.
[Parameter(ParameterSetName="ByName", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[xml]$ConfigXML,
# A string representing the OPNsense interface name. Must be wan, lan,
# or opt\d+.
[Parameter(ParameterSetName="ByName", Mandatory=$False)]
[ValidatePattern("^(wan|lan|opt\d+)$")]
[string]$Name,
[Parameter(ParameterSetName="ByOpnSenseInterface", Mandatory=$True, ValueFromPipeline=$true)]
[PSCustomObject[]]$OpnSenseInterface,
# A string representing the FreeBSD interface name
# (as seen in /sbin/ifconfig)
[Parameter(Mandatory=$False)]
[string]$Interface,
# A string containing a "friendly description" of the VLAN in question.
[Parameter(Mandatory=$False)]
[string]$Description,
[Parameter(Mandatory=$False)]
[string]$SpoofMac,
[Parameter(Mandatory=$False)]
[ValidateScript({ $_.AddressFamily -eq "InterNetwork" })]
[ipaddress]$IPAddress,
[ValidateScript({ $_.AddressFamily -eq "InterNetworkV6" })]
[Parameter(Mandatory=$False)]
[ipaddress]$IPv6Address,
[ValidateRange(0,32)]
[Parameter(Mandatory=$False)]
[int]$IPPrefixLength,
[ValidateRange(0,128)]
[Parameter(Mandatory=$False)]
[int]$IPv6PrefixLength,
[Parameter(Mandatory=$False)]
[bool]$BlockBogons,
[Parameter(Mandatory=$False)]
[bool]$BlockRFC1918
)
Begin {
if ($PsCmdlet.ParameterSetName -eq "ByName") {
$OpnSenseInterface = Get-OpnSenseInterface $ConfigXML $Name
}
# Don't use $PSBoundParameters here, we don't want to run NormalizeMacAddress on an empty MAC.
if ($SpoofMac) {
$SpoofMac = NormalizeMacAddress($SpoofMac)
}
}
Process {
$OpnSenseInterface | % {
$x = $_.XMLElement
if ($PSBoundParameters.ContainsKey('Interface')) {
$x.if = $Interface
}
if ($PSBoundParameters.ContainsKey('Description')) {
$x.descr = $Description
}
if ($PSBoundParameters.ContainsKey('SpoofMac')) {
$x.spoofmac = $SpoofMac
}
if ($PSBoundParameters.ContainsKey('IPAddress')) {
$x.ipaddr = $IPAddress.ToString()
}
if ($PSBoundParameters.ContainsKey('IPv6Address')) {
$x.ipaddrv6 = $IPv6Address.ToString()
}
if ($PSBoundParameters.ContainsKey('IPPrefixLength')) {
$x.subnet = $IPPrefixLength.ToString()
}
if ($PSBoundParameters.ContainsKey('IPv6PrefixLength')) {
$x.subnetv6 = $IPv6PrefixLength.ToString()
}
if ($PSBoundParameters.ContainsKey('BlockBogons')) {
$n = $x.SelectSingleNode('blockbogons')
if ($BlockBogons) {
if (-not $n) {
$n = $ConfigXML.CreateElement('blockbogons')
$x.AppendChild($n) | Out-Null
}
$x.blockbogons = "1"
} else {
if ($n) {
$x.RemoveChild($n) | Out-Null
}
}
}
if ($PSBoundParameters.ContainsKey('BlockRFC1918')) {
$n = $x.SelectSingleNode('blockpriv')
if ($BlockBogons) {
if (-not $n) {
$n = $ConfigXML.CreateElement('blockpriv')
$x.AppendChild($n) | Out-Null
}
$x.blockpriv = "1"
} else {
if ($n) {
$x.RemoveChild($n) | Out-Null
}
}
}
$_
}
}
}
<#
.SYNOPSIS
Retrieves an interface in an OPNsense configuration file.
.DESCRIPTION
The Get-OpnSenseInterface function reads an OPNsense configuration file in
order to add a new interface.
In order to use this function, an object of the xml (System.Xml.XmlDocument)
type representing an OPNsense configuration is required. The cmdlet will
mutate the DOM supplied in order to create a new interface configuration with
the specified settings.
.OUTPUT
An object of type System.Xml.XmlElement is returned, representing the
requested information. The cmdlet makes no attempt at interpreting the
data, instead opting to present it as is from the configuration DOM.
.EXAMPLE
Get-OpnSenseXMLConfig config.xml | Get-OpnSenseInterface -Interface em0
Name : opt10
Interface : em0
Description : GUEST
IPAddress : 192.0.2.1
IPv6Address : 2001:db8:1561:a::1
Enabled : True
Retrieve information about the interface named em0.
#>
function Get-OpnSenseInterface {
[Cmdletbinding()]
Param(
# The DOM of an OPNsense configuration file.
[Parameter(ParameterSetName="ByValue", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[xml]$ConfigXML,
# A string representing the OPNsense interface name. Must be wan, lan,
# or opt\d+. If not given, the first available opt interface is used.
[Parameter(ParameterSetName="ByValue", Mandatory=$False, Position=2)]
[ValidatePattern("^(wan|lan|opt\d+)$")]
[string]$Name,
[Parameter(ParameterSetName="ByElement", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[System.Xml.XmlElement[]]$XMLElement
)
$defaultProperties = @('Name', 'Interface', 'Description', ’IPAddress', 'IPv6Address', 'Enabled')
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet', [string[]]$defaultProperties)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
if ($PsCmdlet.ParameterSetName -eq "ByValue") {
$xpath = "/opnsense/interfaces/"
if ($Name) {
$xpath += $Name.ToLower()
} else {
$xpath += "*"
}
$XMLElement = $ConfigXML.SelectNodes($xpath)
}
$XMLElement | % {
$if = New-Object PSCustomObject
$if | Add-Member NoteProperty XMLElement $_
$if | Add-Member ScriptProperty Name { $this.XMLElement.Name }
$if | Add-Member ScriptProperty Interface { $this.XMLElement.if }
$if | Add-Member ScriptProperty Description { $this.XMLElement.descr }
$if | Add-Member ScriptProperty Enabled { [bool]$this.XMLElement.enable }
$if | Add-Member ScriptProperty IPAddress { [ipaddress]$this.XMLElement.ipaddr }
$if | Add-Member ScriptProperty IPv6Address { [ipaddress]$this.XMLElement.ipaddrv6 }
$if | Add-Member ScriptProperty IPPrefixLength { [int]$this.XMLElement.subnet }
$if | Add-Member ScriptProperty IPv6PrefixLength { [int]$this.XMLElement.subnetv6 }
$if | Add-Member ScriptProperty BlockBogons { [bool]$this.XMLElement.blockbogons }
$if | Add-Member ScriptProperty BlockRFC1918 { [bool]$this.XMLElement.blockpriv }
$if | Add-Member ScriptProperty SpoofMac { $this.XMLElement.spoofmac }
$if | Add-Member MemberSet PSStandardMembers $PSStandardMembers
$if
}
}
<#
.SYNOPSIS
Removes an interface from an OPNsense configuration file.
.DESCRIPTION
The New-OpnSenseVLAN function manipulates the DOM of an OPNsense XML
configuration document in order to remove an interface.
The interface to be removed can either be specified by value, or providing a
System.Xml.XmlElement object referring to the interface, as provided by the
Get-OpnSenseInterface cmdlet.
.NOTES
Both the Name and Interface arguments are optional. If neither is specified,
and the ConfigXML parameter is used, this will therefore delete all
interfaces.
.EXAMPLE
$c = Get-OpnSenseXMLConfig config.xml; $c | Remove-OpnSenseInterface -Interface em0; $c | Out-OpnSenseXMLConfig config.xml
Edit a configuration file and remove an interface definition with a physical
interface of em0, by specifying the requested interface to be deleted by
value.
.EXAMPLE
$c = Get-OpnSenseXMLConfig config.xml; $c | Get-OpnSenseInterface -Interface em0 | Remove-OpnSenseInterface; $c | Out-OpnSenseXMLConfig config.xml
Edit a configuration file and remove an interface definition with a physical
interface of em0, after retrieving the interface objects using the
Get-OpnSenseInterface cmdlet.
#>
function Remove-OpnSenseInterface {
[Cmdletbinding()]
Param(
# The DOM of an OPNsense configuration file. The DOM specified will be
# changed in place as a result of executing the cmdlet.
[Parameter(ParameterSetName="ByValue", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[xml]$ConfigXML,
# A string representing the OPNsense interface name. Must be wan, lan,
# or opt\d+. If not given, the first available opt interface is used.
[Parameter(ParameterSetName="ByValue", Mandatory=$False)]
[ValidatePattern("^(wan|lan|opt\d+)$")]
[string]$Name,
[Parameter(ParameterSetName="ByOpnSenseInterface", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[PSCustomObject[]]$OpnSenseInterface
)
Begin {
if ($PsCmdlet.ParameterSetName -eq "ByValue") {
$OpnSenseInterface = Get-OpnSenseInterface $ConfigXML $Name
}
}
Process {
if (-not $OpnSenseInterface) {
Throw "Could not find interface to remove!"
}
$OpnSenseInterface | % {
$_.XMLElement.ParentNode.RemoveChild($_.XMLElement) | Out-Null
}
}
}
<#
.SYNOPSIS
Disables an interface in an OPNsense configuration file.
.DESCRIPTION
The Disable-OpnSenseVLAN function manipulates the DOM of an OPNsense XML
configuration document in order to disable an interface.
The interface to be removed can either be specified by value, or providing a
System.Xml.XmlElement object referring to the interface, as provided by the
Get-OpnSenseInterface cmdlet.
.NOTES
Both the Name and Interface arguments are optional. If neither is specified,
and the ConfigXML parameter is used, this will therefore disable all
interfaces.
#>
function Disable-OpnSenseInterface {
[Cmdletbinding()]
Param(
# The DOM of an OPNsense configuration file. The DOM specified will be
# changed in place as a result of executing the cmdlet.
[Parameter(ParameterSetName="ByValue", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[xml]$ConfigXML,
# A string representing the OPNsense interface name. Must be wan, lan,
# or opt\d+. If not given, the first available opt interface is used.
[Parameter(ParameterSetName="ByValue", Mandatory=$False)]
[ValidatePattern("^(wan|lan|opt\d+)$")]
[string]$Name,
[Parameter(ParameterSetName="ByOpnSenseInterface", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[PSCustomObject[]]$OpnSenseInterface
)
Begin {
if ($PsCmdlet.ParameterSetName -eq "ByValue") {
$OpnSenseInterface = Get-OpnSenseInterface $ConfigXML $Name
}
}
Process {
if (-not $OpnSenseInterface) {
Throw "Could not find interface to disable!"
}
$OpnSenseInterface | % {
$_.XMLElement.enable = ""
}
}
}
<#
.SYNOPSIS
Enables an interface in an OPNsense configuration file.
.DESCRIPTION
The Enable-OpnSenseVLAN function manipulates the DOM of an OPNsense XML
configuration document in order to enable an interface.
The interface to be removed can either be specified by value, or providing a
System.Xml.XmlElement object referring to the interface, as provided by the
Get-OpnSenseInterface cmdlet.
.NOTES
Both the Name and Interface arguments are optional. If neither is specified,
and the ConfigXML parameter is used, this will therefore enable all
interfaces.
#>
function Enable-OpnSenseInterface {
[Cmdletbinding()]
Param(
# The DOM of an OPNsense configuration file. The DOM specified will be
# changed in place as a result of executing the cmdlet.
[Parameter(ParameterSetName="ByValue", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[xml]$ConfigXML,
# A string representing the OPNsense interface name. Must be wan, lan,
# or opt\d+. If not given, the first available opt interface is used.
[Parameter(ParameterSetName="ByValue", Mandatory=$False)]
[ValidatePattern("^(wan|lan|opt\d+)$")]
[string]$Name,
[Parameter(ParameterSetName="ByOpnSenseInterface", Mandatory=$True, ValueFromPipeline=$true, Position=1)]
[PSCustomObject[]]$OpnSenseInterface
)
Begin {
if ($PsCmdlet.ParameterSetName -eq "ByValue") {
$OpnSenseInterface = Get-OpnSenseInterface $ConfigXML $Name
}
}
Process {
if (-not $OpnSenseInterface) {
Throw "Could not find interface to remove!"
}
$OpnSenseInterface | % {
$_.XMLElement.enable = "1"
}
}
}