mirror of
https://github.com/tailscale/tailscale.git
synced 2025-09-21 05:31:36 +02:00
This is step 2 of ~4, breaking up #14720 into reviewable chunks, with the aim to make syspolicy be a build-time configurable feature. Step 1 was #16984. In this second step, the util/syspolicy/policyclient package is added with the policyclient.Client interface. This is the interface that's always present (regardless of build tags), and is what code around the tree uses to ask syspolicy/MDM questions. There are two implementations of policyclient.Client for now: 1) NoPolicyClient, which only returns default values. 2) the unexported, temporary 'globalSyspolicy', which is implemented in terms of the global functions we wish to later eliminate. This then starts to plumb around the policyclient.Client to most callers. Future changes will plumb it more. When the last of the global func callers are gone, then we can unexport the global functions and make a proper policyclient.Client type and constructor in the syspolicy package, removing the globalSyspolicy impl out of tsd. The final change will sprinkle build tags in a few more places and lock it in with dependency tests to make sure the dependencies don't later creep back in. Updates #16998 Updates #12614 Change-Id: Ib2c93d15c15c1f2b981464099177cd492d50391c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build cgo && darwin && !ios
|
|
|
|
package posture
|
|
|
|
// #cgo LDFLAGS: -framework CoreFoundation -framework IOKit
|
|
// #include <CoreFoundation/CoreFoundation.h>
|
|
// #include <IOKit/IOKitLib.h>
|
|
//
|
|
// #if __MAC_OS_X_VERSION_MIN_REQUIRED < 120000
|
|
// #define kIOMainPortDefault kIOMasterPortDefault
|
|
// #endif
|
|
//
|
|
// const char *
|
|
// getSerialNumber()
|
|
// {
|
|
// CFMutableDictionaryRef matching = IOServiceMatching("IOPlatformExpertDevice");
|
|
// if (!matching) {
|
|
// return "err: failed to create dictionary to match IOServices";
|
|
// }
|
|
//
|
|
// io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault, matching);
|
|
// if (!service) {
|
|
// return "err: failed to look up registered IOService objects that match a matching dictionary";
|
|
// }
|
|
//
|
|
// CFStringRef serialNumberRef = IORegistryEntryCreateCFProperty(
|
|
// service,
|
|
// CFSTR("IOPlatformSerialNumber"),
|
|
// kCFAllocatorDefault,
|
|
// 0
|
|
// );
|
|
// if (!serialNumberRef) {
|
|
// return "err: failed to look up serial number in IORegistry";
|
|
// }
|
|
//
|
|
// CFIndex length = CFStringGetLength(serialNumberRef);
|
|
// CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
|
|
// char *serialNumberBuf = (char *)malloc(max_size);
|
|
//
|
|
// bool result = CFStringGetCString(serialNumberRef, serialNumberBuf, max_size, kCFStringEncodingUTF8);
|
|
//
|
|
// CFRelease(serialNumberRef);
|
|
// IOObjectRelease(service);
|
|
//
|
|
// if (!result) {
|
|
// free(serialNumberBuf);
|
|
//
|
|
// return "err: failed to convert serial number reference to string";
|
|
// }
|
|
//
|
|
// return serialNumberBuf;
|
|
// }
|
|
import "C"
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/syspolicy/policyclient"
|
|
)
|
|
|
|
// GetSerialNumber returns the platform serial sumber as reported by IOKit.
|
|
func GetSerialNumbers(policyclient.Client, logger.Logf) ([]string, error) {
|
|
csn := C.getSerialNumber()
|
|
serialNumber := C.GoString(csn)
|
|
|
|
if err, ok := strings.CutPrefix(serialNumber, "err: "); ok {
|
|
return nil, fmt.Errorf("failed to get serial number from IOKit: %s", err)
|
|
}
|
|
|
|
return []string{serialNumber}, nil
|
|
}
|