#include <Security/Security.h>
#include <Security/SecAssessment.h>
#include <getopt.h>
#include <syslog.h>
#include <libgen.h>
#include <notify.h>
#include <xpc/private.h>
#include <security_utilities/cfutilities.h>
#include <security_utilities/logging.h>
static const char serviceName[] = "com.apple.security.syspolicy";
static const char rearmActivityName[] = "com.apple.security.syspolicy.rearm";
static const CFTimeInterval rearmPeriod = 30*24*60*60;
static void usage();
void doAssess(xpc_object_t msg, xpc_object_t reply);
void doUpdate(xpc_object_t msg, xpc_object_t reply);
void doRecord(xpc_object_t msg, xpc_object_t reply, xpc_connection_t connection);
void doCancel(xpc_object_t msg);
void doRearmCheck();
void sendProgress(xpc_connection_t connection, uint64_t ref, std::string token, CFDictionaryRef info);
int main (int argc, char * const argv[])
{
const char *name = serviceName;
if (const char *s = getenv("SYSPOLICYNAME"))
name = s;
extern int optind;
int arg;
while ((arg = getopt(argc, argv, "v")) != -1)
switch (arg) {
case 'v':
break;
case '?':
usage();
}
if (optind < argc)
usage();
xpc_activity_register(rearmActivityName, XPC_ACTIVITY_CHECK_IN, ^(xpc_activity_t activity) {
if (xpc_activity_get_state(activity) == XPC_ACTIVITY_STATE_RUN) {
doRearmCheck();
}
});
dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0); xpc_connection_t service = xpc_connection_create_mach_service(name, queue, XPC_CONNECTION_MACH_SERVICE_LISTENER );
xpc_connection_set_event_handler(service, ^(xpc_object_t cmsg) {
if (xpc_get_type(cmsg) == XPC_TYPE_CONNECTION) {
xpc_connection_t connection = xpc_connection_t(cmsg);
syslog(LOG_DEBUG, "Connection from pid %d", xpc_connection_get_pid(connection));
xpc_connection_set_event_handler(connection, ^(xpc_object_t msg) {
if (xpc_get_type(msg) == XPC_TYPE_DICTIONARY) {
xpc_retain(msg);
dispatch_async(queue, ^{ const char *function = xpc_dictionary_get_string(msg, "function");
syslog(LOG_DEBUG, "pid %d requested %s", xpc_connection_get_pid(connection), function);
xpc_object_t reply = xpc_dictionary_create_reply(msg);
try {
if (function == NULL) {
xpc_dictionary_set_int64(reply, "error", errSecCSInternalError);
} else if (!strcmp(function, "assess")) {
doAssess(msg, reply);
} else if (!strcmp(function, "update")) {
doUpdate(msg, reply);
} else if (!strcmp(function, "record")) {
doRecord(msg, reply, connection);
} else if (!strcmp(function, "cancel")) {
doCancel(msg);
} else {
xpc_dictionary_set_int64(reply, "error", errSecCSInternalError);
}
} catch (...) {
xpc_dictionary_set_int64(reply, "error", errSecCSInternalError);
}
xpc_release(msg);
if (reply) {
xpc_connection_send_message(connection, reply);
xpc_release(reply);
}
});
}
});
xpc_connection_resume(connection);
} else {
const char *s = xpc_copy_description(cmsg);
printf("Incoming message - %s\n", s);
free((char*)s);
}
});
xpc_connection_resume(service);
dispatch_main();
return 1;
}
static void usage()
{
fprintf(stderr, "Usage: spd\n");
exit(2);
}
std::set<std::string> cancellationMailbox;
Mutex cancellationLock;
void doAssess(xpc_object_t msg, xpc_object_t reply)
{
const char *path = xpc_dictionary_get_string(msg, "path");
uint64_t flags = xpc_dictionary_get_int64(msg, "flags");
CFErrorRef errors = NULL;
size_t contextLength;
const void *contextData = xpc_dictionary_get_data(msg, "context", &contextLength);
CFRef<CFDictionaryRef> context = makeCFDictionaryFrom(contextData, contextLength);
CFTypeRef cfprogress = CFDictionaryGetValue(context, kSecAssessmentContextKeyFeedback);
CFNumberRef progress = NULL; __block string progressToken; if (cfprogress && CFGetTypeID(cfprogress) == CFNumberGetTypeID())
progress = CFNumberRef(cfprogress);
if (progress) {
CFRef<CFUUIDRef> uuid = CFUUIDCreate(NULL);
CFRef<CFStringRef> uuids = CFUUIDCreateString(NULL, uuid);
progressToken = cfString(uuids);
uint64_t progressRef = cfNumber<uint64_t>(progress); CFRef<CFMutableDictionaryRef> ctx = makeCFMutableDictionary(context.get());
CFDictionarySetValue(ctx, kSecAssessmentContextKeyFeedback, ^Boolean(CFStringRef type, CFDictionaryRef info) {
{
StLock<Mutex> _(cancellationLock);
if (cancellationMailbox.find(progressToken) != cancellationMailbox.end()) {
return false;
}
}
xpc_connection_t connection = xpc_dictionary_get_remote_connection(msg);
sendProgress(connection, progressRef, progressToken, info);
return true;
});
context = ctx.get();
}
if (CFRef<SecAssessmentRef> assessment = SecAssessmentCreate(CFTempURL(path), flags | kSecAssessmentFlagDirect | kSecAssessmentFlagIgnoreCache, context, &errors))
if (CFRef<CFDictionaryRef> result = SecAssessmentCopyResult(assessment, kSecAssessmentDefaultFlags, &errors)) {
CFRef<CFDataRef> resultData = makeCFData(result.get());
xpc_dictionary_set_data(reply, "result", CFDataGetBytePtr(resultData), CFDataGetLength(resultData));
}
if (errors)
xpc_dictionary_set_int64(reply, "error", CFErrorGetCode(errors));
if (!progressToken.empty()) {
StLock<Mutex> _(cancellationLock);
cancellationMailbox.erase(progressToken);
}
}
void sendProgress(xpc_connection_t connection, uint64_t ref, std::string token, CFDictionaryRef info)
{
CFDictionary state(info, 0);
unsigned current = cfNumber<unsigned>(state.get<CFNumberRef>("current"));
unsigned total = cfNumber<unsigned>(state.get<CFNumberRef>("total"));
xpc_object_t progress = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_string(progress, "function", "progress");
xpc_dictionary_set_uint64(progress, "current", current);
xpc_dictionary_set_uint64(progress, "total", total);
xpc_dictionary_set_uint64(progress, "ref", ref);
xpc_dictionary_set_string(progress, "token", token.c_str());
xpc_connection_send_message(connection, progress);
}
void doCancel(xpc_object_t msg)
{
if (const char* token = xpc_dictionary_get_string(msg, "token")) {
StLock<Mutex> _(cancellationLock);
cancellationMailbox.insert(token);
}
}
void doUpdate(xpc_object_t msg, xpc_object_t reply)
{
CFRef<CFTypeRef> target;
size_t length;
if (const void *reqblob = xpc_dictionary_get_data(msg, "requirement", &length))
MacOSError::check(SecRequirementCreateWithData(CFTempData(reqblob, length), kSecCSDefaultFlags, (SecRequirementRef *)&target.aref()));
else if (uint64_t rule = xpc_dictionary_get_uint64(msg, "rule"))
target.take(makeCFNumber(rule));
else if (const char *s = xpc_dictionary_get_string(msg, "url"))
target.take(makeCFURL(s));
uint64_t flags = xpc_dictionary_get_int64(msg, "flags");
const void *contextData = xpc_dictionary_get_data(msg, "context", &length);
CFRef<CFDictionaryRef> context = makeCFDictionaryFrom(contextData, length);
CFErrorRef errors = NULL;
if (CFRef<CFDictionaryRef> result = SecAssessmentCopyUpdate(target, flags | kSecAssessmentFlagDirect, context, &errors)) {
CFRef<CFDataRef> resultData = makeCFData(result.get());
xpc_dictionary_set_data(reply, "result", CFDataGetBytePtr(resultData), CFDataGetLength(resultData));
} else {
xpc_dictionary_set_int64(reply, "error", CFErrorGetCode(errors));
CFRelease(errors);
}
}
void doRecord(xpc_object_t msg, xpc_object_t reply, xpc_connection_t connection)
{
xpc_object_t entitlement = xpc_connection_copy_entitlement_value(connection, "com.apple.private.assessment.recording");
if (entitlement == NULL || entitlement == XPC_BOOL_FALSE) {
xpc_dictionary_set_int64(reply, "error", errSecAuthFailed);
return;
}
size_t length;
const void *infoData = xpc_dictionary_get_data(msg, "info", &length);
CFRef<CFDictionaryRef> info = makeCFDictionaryFrom(infoData, length);
CFErrorRef errors = NULL;
if (!SecAssessmentControl(CFSTR("ui-record-reject-local"), (void *)info.get(), &errors)) {
xpc_dictionary_set_int64(reply, "error", CFErrorGetCode(errors));
CFRelease(errors);
}
}
void doRearmCheck()
{
CFRef<CFBooleanRef> rearmPref = (CFBooleanRef)CFPreferencesCopyValue(CFSTR("GKAutoRearm"), CFSTR("com.apple.security"), kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
if (rearmPref == kCFBooleanFalse)
return;
CFBooleanRef status;
CFTimeInterval delta;
if (SecAssessmentControl(CFSTR("ui-status"), &status, NULL) && status == kCFBooleanFalse)
if (SecAssessmentControl(CFSTR("rearm-status"), &delta, NULL) && delta > rearmPeriod) {
SecAssessmentControl(CFSTR("ui-enable"), NULL, NULL); SecAssessmentControl(CFSTR("ui-enable-devid"), NULL, NULL); notify_post("com.apple.security.assessment.rearm");
}
}