summaryrefslogtreecommitdiffstats
path: root/src/detection/host/host_apple.c
blob: a177bf40533fe994520e4a2eee0eaba6d0d3f683 (plain) (blame)
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
#include "host.h"
#include "common/sysctl.h"
#include "common/apple/cf_helpers.h"

#include <IOKit/IOKitLib.h>

const char* getProductNameWithIokit(FFstrbuf* result) {
    FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t registryEntry = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/product");
    if (!registryEntry) {
        return "IOServiceGetMatchingService() failed";
    }

    FF_CFTYPE_AUTO_RELEASE CFStringRef productName = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("product-name"), kCFAllocatorDefault, kNilOptions);
    if (!productName) {
        return "IORegistryEntryCreateCFProperty() failed";
    }

    return ffCfStrGetString(productName, result);
}

const char* getOthersByIokit(FFHostResult* host) {
    FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t registryEntry = IOServiceGetMatchingService(MACH_PORT_NULL, IOServiceMatching("IOPlatformExpertDevice"));
    if (!registryEntry) {
        return "IOServiceGetMatchingService() failed";
    }

    FF_CFTYPE_AUTO_RELEASE CFStringRef serialNumber = IORegistryEntryCreateCFProperty(registryEntry, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, kNilOptions);
    if (serialNumber) {
        ffCfStrGetString(serialNumber, &host->serial);
    }

    FF_CFTYPE_AUTO_RELEASE CFStringRef uuid = IORegistryEntryCreateCFProperty(registryEntry, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, kNilOptions);
    if (uuid) {
        ffCfStrGetString(uuid, &host->uuid);
    }

    FF_CFTYPE_AUTO_RELEASE CFStringRef manufacturer = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("manufacturer"), kCFAllocatorDefault, kNilOptions);
    if (manufacturer) {
        ffCfStrGetString(manufacturer, &host->vendor);
    }

    FF_CFTYPE_AUTO_RELEASE CFStringRef version = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("version"), kCFAllocatorDefault, kNilOptions);
    if (version) {
        ffCfStrGetString(version, &host->version);
    }

    return NULL;
}

const char* ffDetectHost(FFHostResult* host) {
    const char* error = ffSysctlGetString("hw.product", &host->family);
    if (error) {
        error = ffSysctlGetString("hw.model", &host->family);
    }
    if (error) {
        return error;
    }

    ffStrbufSetStatic(&host->name, ffHostGetMacProductNameWithHwModel(&host->family));
    if (host->name.length == 0) {
        getProductNameWithIokit(&host->name);
    }
    if (host->name.length == 0) {
        ffStrbufSet(&host->name, &host->family);
    }
    getOthersByIokit(host);
    return NULL;
}