-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathidentity_parsers.go
More file actions
317 lines (272 loc) · 7.91 KB
/
identity_parsers.go
File metadata and controls
317 lines (272 loc) · 7.91 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
/*
File: identity_parsers.go
Description:
String and byte parsing for various hosts and lease formats.
Extracted from identity.go to isolate formatting constraints.
Supports hosts, dnsmasq, ISC DHCP, Kea, and odhcpd lease formats.
*/
package main
import (
"bytes"
"net"
)
// ---------------------------------------------------------------------------
// Parsers
// ---------------------------------------------------------------------------
// parseHostsBytes parses a hosts-file byte slice into a parsedFile.
// ALL hostname tokens per line are processed (fix v1.26.0).
// Inline # comments are stripped (fix v1.26.0).
func parseHostsBytes(data []byte) *parsedFile {
pf := newParsedFile()
for len(data) > 0 {
line, rest := splitLine(data)
data = rest
line = bytes.TrimLeft(line, " \t")
if len(line) == 0 || line[0] == '#' {
continue
}
// Strip inline comment.
if ci := bytes.IndexByte(line, '#'); ci >= 0 {
line = bytes.TrimRight(line[:ci], " \t")
}
if len(line) == 0 {
continue
}
// First token is the IP address.
ipEnd := bytes.IndexAny(line, " \t")
if ipEnd < 0 {
continue
}
ipStr := string(line[:ipEnd])
line = bytes.TrimLeft(line[ipEnd:], " \t")
// All remaining tokens are hostnames/aliases — store every one.
for len(line) > 0 {
nameEnd := bytes.IndexAny(line, " \t")
var name []byte
if nameEnd < 0 {
name = line
line = nil
} else {
name = line[:nameEnd]
line = bytes.TrimLeft(line[nameEnd:], " \t")
}
if len(name) == 0 {
break
}
storeEntry(ipStr, string(name), pf)
}
}
return pf
}
// parseDnsmasqLeasesBytes parses a dnsmasq flat-file lease database.
//
// Format (one record per line, five space-separated fields):
//
// <expiry-epoch> <mac> <ip> <hostname> <client-id>
//
// Hostname '*' means the client didn't supply one — entry is skipped.
func parseDnsmasqLeasesBytes(data []byte) *parsedFile {
pf := newParsedFile()
for len(data) > 0 {
line, rest := splitLine(data)
data = rest
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
// Fields: [0]=expiry [1]=mac [2]=ip [3]=hostname [4]=clientid
fields := bytes.Fields(line)
if len(fields) < 4 {
continue
}
nameBytes := fields[3]
if len(nameBytes) == 0 || bytes.Equal(nameBytes, []byte("*")) {
continue // hostname not provided by DHCP client
}
shortName := storeEntry(string(fields[2]), string(nameBytes), pf)
if shortName == "" {
continue // malformed IP — skip MAC too
}
if mac, err := net.ParseMAC(string(fields[1])); err == nil {
pf.macToName[mac.String()] = shortName
}
}
return pf
}
// parseIscLeasesBytes parses an ISC DHCP block-structured lease database.
//
// Only active and static bindings are imported. Last active block for a given
// IP wins (ISC append semantics). Block format:
//
// lease 192.168.1.x {
// binding state active;
// hardware ethernet aa:bb:cc:dd:ee:ff;
// client-hostname "myhostname";
// }
func parseIscLeasesBytes(data []byte) *parsedFile {
pf := newParsedFile()
var (
inLease bool
leaseIP string
leaseMAC string
leaseName string
leaseActive bool
)
for len(data) > 0 {
line, rest := splitLine(data)
data = rest
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
// "lease 192.168.x.x {" — begins a new block.
if bytes.HasPrefix(line, []byte("lease ")) {
inLease = true
leaseMAC = ""
leaseName = ""
leaseActive = false
trimmed := bytes.TrimPrefix(line, []byte("lease "))
trimmed = bytes.TrimSuffix(bytes.TrimSpace(trimmed), []byte("{"))
leaseIP = string(bytes.TrimSpace(trimmed))
continue
}
if !inLease {
continue
}
// "}" — end of block; commit if active and named.
if bytes.Equal(line, []byte("}")) {
if leaseActive && leaseName != "" {
shortName := storeEntry(leaseIP, leaseName, pf)
if shortName != "" && leaseMAC != "" {
if mac, err := net.ParseMAC(leaseMAC); err == nil {
pf.macToName[mac.String()] = shortName
}
}
}
inLease = false
continue
}
// "binding state active;" / "binding state static;" / etc.
if bytes.HasPrefix(line, []byte("binding state ")) {
state := bytes.TrimSuffix(line[len("binding state "):], []byte(";"))
leaseActive = bytes.Equal(state, []byte("active")) ||
bytes.Equal(state, []byte("static"))
continue
}
// "hardware ethernet aa:bb:cc:dd:ee:ff;"
if bytes.HasPrefix(line, []byte("hardware ethernet ")) {
mac := bytes.TrimSuffix(line[len("hardware ethernet "):], []byte(";"))
leaseMAC = string(bytes.TrimSpace(mac))
continue
}
// `client-hostname "mylaptop";`
if bytes.HasPrefix(line, []byte("client-hostname ")) {
name := line[len("client-hostname "):]
name = bytes.TrimSuffix(name, []byte(";"))
name = bytes.Trim(name, `"`)
leaseName = string(bytes.TrimSpace(name))
continue
}
}
return pf
}
// parseKeaLeasesBytes parses a Kea DHCP4 CSV lease file.
//
// Only state=0 (active) rows are imported. Header row is skipped.
// CSV column layout (0-indexed): 0=address, 1=hwaddr, 8=hostname, 9=state.
// SplitN(11) prevents user_context (col 10) with embedded commas from
// mis-shifting later columns.
func parseKeaLeasesBytes(data []byte) *parsedFile {
pf := newParsedFile()
firstLine := true
for len(data) > 0 {
line, rest := splitLine(data)
data = rest
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
// Skip the mandatory header row (starts with "address,").
if firstLine {
firstLine = false
if bytes.HasPrefix(line, []byte("address,")) {
continue
}
}
fields := bytes.SplitN(line, []byte(","), 11)
if len(fields) < 10 {
continue
}
addrBytes := bytes.TrimSpace(fields[0])
macBytes := bytes.TrimSpace(fields[1])
nameBytes := bytes.TrimSpace(fields[8])
stateBytes := bytes.TrimSpace(fields[9])
if !bytes.Equal(stateBytes, []byte("0")) {
continue // only import active leases
}
if len(nameBytes) == 0 {
continue
}
// Kea sometimes writes FQDNs with a trailing dot — strip it.
nameStr := string(bytes.TrimSuffix(nameBytes, []byte(".")))
shortName := storeEntry(string(addrBytes), nameStr, pf)
if shortName != "" && len(macBytes) > 0 {
if mac, err := net.ParseMAC(string(macBytes)); err == nil {
pf.macToName[mac.String()] = shortName
}
}
}
return pf
}
// parseOdhcpdLeasesBytes parses an odhcpd internal state file.
//
// odhcpd (OpenWrt) uses '#' as the DATA line prefix (not a comment marker).
// Format after the leading '#':
//
// <ifname> <mac/duid> <iaid> <hostname> <remaining-secs> [<ip/plen>...]
//
// Hostname '-' means no hostname was supplied — entry is skipped.
// IP addresses include a prefix-length suffix (/32, /128) that is stripped.
func parseOdhcpdLeasesBytes(data []byte) *parsedFile {
pf := newParsedFile()
for len(data) > 0 {
line, rest := splitLine(data)
data = rest
line = bytes.TrimSpace(line)
// Data lines start with '#'; all other lines are structural — skip.
if len(line) == 0 || line[0] != '#' {
continue
}
// Strip the leading '#' and split remainder into fields.
// [0]=ifname [1]=mac/duid [2]=iaid [3]=hostname [4]=remaining [5..]=ip/plen
fields := bytes.Fields(line[1:])
if len(fields) < 6 {
continue
}
nameBytes := fields[3]
if len(nameBytes) == 0 || bytes.Equal(nameBytes, []byte("-")) {
continue // no hostname provided
}
macOrDUID := string(fields[1])
hostname := string(nameBytes)
var shortName string
for _, addrField := range fields[5:] {
ipStr := string(addrField)
// Strip /prefixlen suffix if present.
if slash := bytes.IndexByte(addrField, '/'); slash >= 0 {
ipStr = string(addrField[:slash])
}
sn := storeEntry(ipStr, hostname, pf)
if sn != "" && shortName == "" {
shortName = sn
}
}
// DHCPv4 MACs parse cleanly; DHCPv6 DUIDs are rejected by ParseMAC (correct).
if shortName != "" {
if mac, err := net.ParseMAC(macOrDUID); err == nil {
pf.macToName[mac.String()] = shortName
}
}
}
return pf
}