-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdarwin.go
More file actions
325 lines (270 loc) · 10.4 KB
/
darwin.go
File metadata and controls
325 lines (270 loc) · 10.4 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
317
318
319
320
321
322
323
324
325
//go:build darwin
package machineid
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"regexp"
"strings"
)
// Compiled regexes for ioreg output parsing.
var (
ioregUUIDRe = regexp.MustCompile(`"IOPlatformUUID"\s*=\s*"([^"]+)"`)
ioregSerialRe = regexp.MustCompile(`"IOPlatformSerialNumber"\s*=\s*"([^"]+)"`)
)
// nullUUID is the all-zero UUID that some firmware implementations return
// when no real hardware UUID is programmed. It must be rejected so it
// cannot contribute to the machine ID.
const nullUUID = "00000000-0000-0000-0000-000000000000"
// spHardwareDataType represents the JSON output of `system_profiler SPHardwareDataType -json`.
type spHardwareDataType struct {
SPHardwareDataType []spHardwareEntry `json:"SPHardwareDataType"`
}
type spHardwareEntry struct {
PlatformUUID string `json:"platform_UUID"`
SerialNumber string `json:"serial_number"`
ChipType string `json:"chip_type"`
ModelName string `json:"machine_name"`
MachineModel string `json:"machine_model"`
}
// spStorageDataType represents the JSON output of `system_profiler SPStorageDataType -json`.
type spStorageDataType struct {
SPStorageDataType []spStorageEntry `json:"SPStorageDataType"`
}
type spStorageEntry struct {
Name string `json:"_name"`
BSDName string `json:"bsd_name"`
PhysicalDrive spPhysicalDrive `json:"physical_drive"`
VolumeUUID string `json:"volume_uuid"`
}
type spPhysicalDrive struct {
DeviceName string `json:"device_name"`
IsInternal string `json:"is_internal_disk"`
MediaName string `json:"media_name"`
MediumType string `json:"medium_type"`
Protocol string `json:"protocol"`
SmartStatus string `json:"smart_status"`
}
// collectIdentifiers gathers macOS-specific hardware identifiers based on provider config.
func collectIdentifiers(ctx context.Context, p *Provider, diag *DiagnosticInfo) ([]string, error) {
var identifiers []string
logger := p.logger
if p.includeSystemUUID {
identifiers = appendIdentifierIfValid(identifiers, func() (string, error) {
return macOSHardwareUUID(ctx, p.commandExecutor, logger)
}, "uuid:", diag, ComponentSystemUUID, logger)
}
if p.includeMotherboard {
identifiers = appendIdentifierIfValid(identifiers, func() (string, error) {
return macOSSerialNumber(ctx, p.commandExecutor, logger)
}, "serial:", diag, ComponentMotherboard, logger)
}
if p.includeCPU {
identifiers = appendIdentifierIfValid(identifiers, func() (string, error) {
return macOSCPUInfo(ctx, p.commandExecutor, logger)
}, "cpu:", diag, ComponentCPU, logger)
}
if p.includeMAC {
identifiers = appendIdentifiersIfValid(identifiers, func() ([]string, error) {
return collectMACAddresses(p.macFilter, logger)
}, "mac:", diag, ComponentMAC, logger)
}
if p.includeDisk {
identifiers = appendIdentifiersIfValid(identifiers, func() ([]string, error) {
return macOSDiskInfo(ctx, p.commandExecutor, logger)
}, "disk:", diag, ComponentDisk, logger)
}
return identifiers, nil
}
// macOSHardwareUUID retrieves hardware UUID using system_profiler with JSON parsing.
// Null UUIDs (all zeros) are rejected so the fallback path is triggered.
func macOSHardwareUUID(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "system_profiler", "SPHardwareDataType", "-json")
if err == nil {
uuid, parseErr := extractHardwareField(output, func(e spHardwareEntry) string {
return e.PlatformUUID
})
if parseErr == nil {
if uuid == nullUUID {
if logger != nil {
logger.Debug("system_profiler returned null UUID, falling back")
}
} else {
return uuid, nil
}
} else if logger != nil {
logger.Debug("system_profiler UUID parsing failed", "error", parseErr)
}
}
// Fallback to ioreg
if logger != nil {
logger.Info("falling back to ioreg for hardware UUID")
}
return macOSHardwareUUIDViaIOReg(ctx, executor, logger)
}
// macOSHardwareUUIDViaIOReg retrieves hardware UUID using ioreg as fallback.
// Null UUIDs (all zeros) are rejected with ErrNotFound.
func macOSHardwareUUIDViaIOReg(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "ioreg", "-d2", "-c", "IOPlatformExpertDevice")
if err != nil {
return "", err
}
match := ioregUUIDRe.FindStringSubmatch(output)
if len(match) > 1 {
if match[1] == nullUUID {
if logger != nil {
logger.Debug("ioreg returned null UUID")
}
return "", &ParseError{Source: "ioreg output", Err: ErrNotFound}
}
return match[1], nil
}
if logger != nil {
logger.Debug("hardware UUID not found in ioreg output")
}
return "", &ParseError{Source: "ioreg output", Err: ErrNotFound}
}
// macOSSerialNumber retrieves system serial number.
func macOSSerialNumber(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "system_profiler", "SPHardwareDataType", "-json")
if err == nil {
serial, parseErr := extractHardwareField(output, func(e spHardwareEntry) string {
return e.SerialNumber
})
if parseErr == nil {
return serial, nil
}
if logger != nil {
logger.Debug("system_profiler serial parsing failed", "error", parseErr)
}
}
// Fallback to ioreg
if logger != nil {
logger.Info("falling back to ioreg for serial number")
}
return macOSSerialNumberViaIOReg(ctx, executor, logger)
}
// macOSSerialNumberViaIOReg retrieves serial number using ioreg as fallback.
func macOSSerialNumberViaIOReg(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
output, err := executeCommand(ctx, executor, logger, "ioreg", "-d2", "-c", "IOPlatformExpertDevice")
if err != nil {
return "", err
}
match := ioregSerialRe.FindStringSubmatch(output)
if len(match) > 1 {
return match[1], nil
}
if logger != nil {
logger.Debug("serial number not found in ioreg output")
}
return "", &ParseError{Source: "ioreg output", Err: ErrNotFound}
}
// macOSCPUInfo retrieves CPU information.
// Uses sysctl as primary source (consistent with existing machine IDs).
// On Intel: returns brand_string:features.
// On Apple Silicon: sysctl returns brand_string with empty features,
// producing "ChipType:" — this trailing colon is preserved for backward
// compatibility with existing license activations.
// Falls back to system_profiler chip_type only if sysctl fails entirely.
//
// Known quirk: if machdep.cpu.brand_string succeeds but machdep.cpu.features
// errors (e.g. transient syscall failure under sandboxing), the result
// degrades to just cpuBrand instead of "cpuBrand:features". This produces a
// different hash for the same machine across calls. The divergence is
// preserved intentionally — changing it would invalidate every existing
// license activation generated under the current behavior.
func macOSCPUInfo(ctx context.Context, executor CommandExecutor, logger *slog.Logger) (string, error) {
// Primary: sysctl (backward compatible)
output, err := executeCommand(ctx, executor, logger, "sysctl", "-n", "machdep.cpu.brand_string")
if err == nil {
cpuBrand := strings.TrimSpace(output)
if cpuBrand != "" {
// Get CPU features (populated on Intel, empty on Apple Silicon)
featOutput, featErr := executeCommand(ctx, executor, logger, "sysctl", "-n", "machdep.cpu.features")
if featErr == nil {
features := strings.TrimSpace(featOutput)
return fmt.Sprintf("%s:%s", cpuBrand, features), nil
}
return cpuBrand, nil
}
}
// Fallback: system_profiler for Apple Silicon chip type
if logger != nil {
logger.Info("falling back to system_profiler for CPU info")
}
profilerOutput, profilerErr := executeCommand(ctx, executor, logger, "system_profiler", "SPHardwareDataType", "-json")
if profilerErr == nil {
var hw spHardwareDataType
if jsonErr := json.Unmarshal([]byte(profilerOutput), &hw); jsonErr == nil && len(hw.SPHardwareDataType) > 0 {
entry := hw.SPHardwareDataType[0]
if entry.ChipType != "" {
return entry.ChipType, nil
}
if logger != nil {
logger.Debug("system_profiler returned empty chip_type")
}
} else if logger != nil {
logger.Debug("system_profiler CPU JSON parsing failed", "error", jsonErr)
}
}
if logger != nil {
logger.Warn("all CPU info methods failed")
}
return "", ErrAllMethodsFailed
}
// macOSDiskInfo retrieves internal disk device names for stable machine identification.
// It uses system_profiler with JSON output and filters to internal disks only,
// deduplicating across volumes on the same physical disk.
func macOSDiskInfo(ctx context.Context, executor CommandExecutor, logger *slog.Logger) ([]string, error) {
output, err := executeCommand(ctx, executor, logger, "system_profiler", "SPStorageDataType", "-json")
if err != nil {
return nil, err
}
return parseStorageJSON(output)
}
// parseStorageJSON parses system_profiler SPStorageDataType JSON and extracts
// unique internal disk device names.
func parseStorageJSON(jsonOutput string) ([]string, error) {
var storage spStorageDataType
if err := json.Unmarshal([]byte(jsonOutput), &storage); err != nil {
return nil, &ParseError{Source: "system_profiler storage JSON", Err: err}
}
// Use a set to deduplicate — multiple volumes can share the same physical disk.
seen := make(map[string]struct{})
var diskNames []string
for _, entry := range storage.SPStorageDataType {
name := entry.PhysicalDrive.DeviceName
if name == "" {
continue
}
// Only include internal disks for stability.
if entry.PhysicalDrive.IsInternal != "yes" {
continue
}
if _, exists := seen[name]; exists {
continue
}
seen[name] = struct{}{}
diskNames = append(diskNames, name)
}
if len(diskNames) == 0 {
return nil, &ParseError{Source: "system_profiler storage output", Err: ErrNotFound}
}
return diskNames, nil
}
// extractHardwareField extracts a field from system_profiler SPHardwareDataType JSON output.
func extractHardwareField(jsonOutput string, fieldFn func(spHardwareEntry) string) (string, error) {
var hw spHardwareDataType
if err := json.Unmarshal([]byte(jsonOutput), &hw); err != nil {
return "", &ParseError{Source: "system_profiler hardware JSON", Err: err}
}
if len(hw.SPHardwareDataType) == 0 {
return "", &ParseError{Source: "system_profiler hardware JSON", Err: ErrNotFound}
}
value := fieldFn(hw.SPHardwareDataType[0])
if value == "" {
return "", &ParseError{Source: "system_profiler hardware JSON", Err: ErrEmptyValue}
}
return value, nil
}