classes/BindRecordClass.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
using namespace System.Collections.Generic

enum BindRecordType {
    UNKNOWN
    SOA
    NS
    A
    A_AAAA
    AAAA
    AFSDB
    ALL
    ANY
    CNAME
    DHCID
    DNAME
    DNSKEY
    DS
    HINFO
    ISDN
    MB
    MD
    MF
    MG
    MINFO
    MR
    MX
    NSEC
    NSEC3
    NSEC3PARAM
    NULL
    OPT
    PTR
    RP
    RRSIG
    RT
    SRV
    TXT
    WINS
    WKS
    X25
}

class BindRecord:IComparable {

    [string]$HostName

    [ValidateRange( 60, [int]::MaxValue )]
    [System.Nullable[int]]$TimeToLive

    hidden [string]$RecordClass__
    
    [System.Nullable[BindRecordType]]$RecordType
    
    [string]$RecordData
    
    [string]$Comment

    BindRecord () {

        $this.__InitObject()
    }

    BindRecord ( [string]$Record ) {

        $this.__InitObject()

        if ( $Record -match '^(?<HostName>\S+)\s+(?<TimeToLive>\d+)\s+(?<RecordClass>\S+)\s+(?<RecordType>\S+)\s+(?<RecordData>"[^"]*"|[^;]+)\s*;?\s*(?<Comment>.*)$' ) {
        
            $this.__InitRecord( [pscustomobject]$Matches )
        
        } elseif ( $Record -match '^;\s*(.*)$' ) {

            $this.Comment = $Matches[1]

        }
    
    }

    BindRecord ( [pscustomobject]$Record ) {

        $this.__InitObject()

        $this.__InitRecord( $Record )
    
    }

    hidden [void] __InitObject () {

        $this | Add-Member -MemberType ScriptProperty -Name 'RecordClass' -Value {
            
            return $this.RecordClass__
        
        } -SecondValue {
            
            param( [string]$Value )
            
            $this.RecordClass__ = $Value.ToUpper()
        
        }

        [string[]]$DefaultProperties = 'HostName', 'TimeToLive', 'RecordClass', 'RecordType', 'RecordData', 'Comment'

        $DefaultDisplayPropertySet = [System.Management.Automation.PSPropertySet]::new( 'DefaultDisplayPropertySet', $DefaultProperties )

        $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$DefaultDisplayPropertySet

        $this | Add-Member -MemberType MemberSet -Name 'PSStandardMembers' -Value $PSStandardMembers
    
    }

    hidden [void] __InitRecord ( [pscustomobject] $Record ) {

        $ValidProperties = ( $this | Get-Member -MemberType Property, ScriptProperty ).Name

        $Record.
            PSObject.
            Members.
            Where({$_.MemberType -eq 'NoteProperty' -and $_.Name -in $ValidProperties -and -not [string]::IsNullOrEmpty( $_.Value ) }).
            Foreach({
                
                $this.($_.Name) = ([string]$_.Value).Trim()
            
            })

        if ( -not $this.RecordClass ) {
            
            $this.RecordClass = 'IN'
        
        }

    }

    [string] ToString () {
    
        return $this.ToString( '{0} {1} {2} {3} {4}' )
    
    }

    [string] ToString ( [string]$Format ) {

        $FormattedRecord = $Format -f $this.HostName, $this.TimeToLive, $this.RecordClass, $this.RecordType, $this.RecordData

        if ( [string]::IsNullOrWhiteSpace( $FormattedRecord ) -and -not [string]::IsNullOrWhiteSpace( $this.Comment ) ) {
            
            return '; ' + $this.Comment.Trim()
        
        } elseif ( -not [string]::IsNullOrWhiteSpace( $this.Comment ) ) {

            return $FormattedRecord.Trim() + ' ; ' + $this.Comment.Trim()

        } else {

            return $FormattedRecord.Trim()

        }
    
    }

    [bool] Equals ( $that ) {

        $IsEqual = $true

        [BindRecord]$that = $that

        $this.
            PSObject.
            Members.
            Where({ $_.MemberType -eq 'Property' -and $_.Value -ne $that.($_.Name) }).
            ForEach({ $IsEqual = $false })

        return $IsEqual

    }

    [int] CompareTo ( $that ) {

        [BindRecord]$that = $that

        # SOA records are always before
        if ( $this.RecordType -eq 'SOA' -and $that.RecordType -ne 'SOA' ) { return -1 }
        if ( $this.RecordType -ne 'SOA' -and $that.RecordType -eq 'SOA' ) { return 1 }

        # now we sort by host name
        if ( $this.HostName -lt $that.HostName ) { return -1 }
        if ( $this.HostName -gt $that.HostName ) { return 1 }

        # now we sort record types based on the ENUM values
        if ( $this.RecordType -lt $that.RecordType ) { return -1 }
        if ( $this.RecordType -gt $that.RecordType ) { return 1 }

        # now we sort on record data
        if ( $this.RecordData -lt $that.RecordData ) { return -1 }
        if ( $this.RecordData -gt $that.RecordData ) { return 1 }

        # finally we sort on TTL
        if ( $this.TimeToLive -lt $that.TimeToLive ) { return -1 }
        if ( $this.TimeToLive -gt $that.TimeToLive ) { return 1 }

        # if everything is equal then the records are equal
        # we are ignoring comments for the sort
        return 0
    
    }

    [List[string]] GetErrors() {

        [List[string]]$Errors = @()

        # if all attributes are blank except a comment, then it's a valid comment
        if ( [string]::IsNullOrEmpty( $this.HostName ) -and 
             $null -eq $this.TimeToLive -and 
             [string]::IsNullOrEmpty( $this.RecordClass ) -and 
             $null -eq $this.RecordType -and 
             [string]::IsNullOrEmpty( $this.RecordData ) -and
             -not [string]::IsNullOrEmpty( $this.Comment ) ) {

                return $Errors
            
            }

        # if the hostname is not defined then it's invalid
        if ( [string]::IsNullOrEmpty( $this.HostName ) ) {

            $Errors.Add( 'HostName is missing' )

        }

        # if the TTL is not defined or less than 60 then it's invalid
        if ( [string]::IsNullOrEmpty( $this.TimeToLive ) -or [int]$this.TimeToLive -lt 60 ) {

            $Errors.Add( 'TimeToLive is missing or invalid, value must not be less than 60' )

        }

        # if the RecordClass is empty it's invalid
        if ( [string]::IsNullOrEmpty( $this.RecordClass ) ) {

            $Errors.Add( 'RecordClass is missing' )

        }

        # if the RecordType is empty it's invalid
        if ( [string]::IsNullOrEmpty( $this.RecordType ) ) {

            $Errors.Add( 'RecordType is missing' )

        # if the RecordType is UNKNOWN it's invalid
        } elseif ( $this.RecordType -eq 'UNKNOWN' -or $this.RecordType -notin [BindRecordType].GetEnumValues() ) {

            $Errors.Add( 'RecordType is invalid' )

        }

        # if the RecordData is empty it's invalid
        if ( [string]::IsNullOrEmpty( $this.RecordData ) ) {

            $Errors.Add( 'RecordData is missing' )

        }

        # if the RecordType is TXT and the RecordData is not enclosed in double quotes
        # then the value is invalid
        if ( $this.RecordType -eq 'TXT' -and $this.RecordData -notmatch '^".*"$' ) {

            $Errors.Add( 'RecordType is TXT, RecordData needs to be enclosed in double quotes' )

        }

        # if RecordData is enclosed in quotes, and contains unescaped quotes the data is invalid
        if ( $this.RecordData -match '^".*"$' -and $this.RecordData -replace '^"(.*)"$', '$1' -match '(?<!\\)"' ) {

            $Errors.Add( 'RecordData is enclosed in quotes and contains unescaped quotes' )

        }
        
        return $Errors
    
    }

}