2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Foundation/Foundation.h>
25 #include "lib/SecArgParse.h"
26 #import "supd/supdProtocol.h"
27 #import <Foundation/NSXPCConnection_Private.h>
28 #import <Security/SFAnalytics.h>
30 /* Internal Topic Names */
31 NSString* const SFAnalyticsTopicKeySync = @"KeySyncTopic";
32 NSString* const SFAnaltyicsTopicTrust = @"TrustTopic";
34 static void nsprintf(NSString *fmt, ...) NS_FORMAT_FUNCTION(1, 2);
35 static void nsprintf(NSString *fmt, ...)
39 NSString *str = [[NSString alloc] initWithFormat:fmt arguments:ap];
42 puts([str UTF8String]);
43 #if !__has_feature(objc_arc)
48 static NSXPCConnection* getConnection()
50 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@"com.apple.securityuploadd" options:0];
51 connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(supdProtocol)];
56 static void getSysdiagnoseDump(void)
58 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
59 NSXPCConnection* connection = getConnection();
60 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
61 nsprintf(@"Could not communicate with supd: %@", error);
62 dispatch_semaphore_signal(sema);
63 }] getSysdiagnoseDumpWithReply:^(NSString * sysdiagnoseString) {
64 nsprintf(@"Analytics sysdiagnose: \n%@", sysdiagnoseString);
65 dispatch_semaphore_signal(sema);
68 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
69 printf("\n\nError: timed out waiting for response from supd\n");
71 [connection invalidate];
74 static void getLoggingJSON(char *topicName)
76 NSString *topic = topicName ? [NSString stringWithUTF8String:topicName] : SFAnalyticsTopicKeySync;
77 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
78 NSXPCConnection* connection = getConnection();
79 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
80 nsprintf(@"Could not communicate with supd: %@", error);
81 dispatch_semaphore_signal(sema);
82 }] getLoggingJSON:YES topic:topic reply:^(NSData* data, NSError* error) {
84 // Success! Only print the JSON blob to make output easier to parse
85 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
87 nsprintf(@"supd gave us an error: %@", error);
89 dispatch_semaphore_signal(sema);
92 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
93 printf("\n\nError: timed out waiting for response from supd\n");
95 [connection invalidate];
98 static void forceUploadAnalytics(void)
100 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
101 NSXPCConnection* connection = getConnection();
102 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
103 nsprintf(@"Could not communicate with supd: %@", error);
104 dispatch_semaphore_signal(sema);
105 }] forceUploadWithReply:^(BOOL success, NSError *error) {
107 printf("Supd reports successful upload\n");
109 nsprintf(@"Supd reports failure: %@", error);
111 dispatch_semaphore_signal(sema);
114 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
115 printf("\n\nError: timed out waiting for response from supd\n");
117 [connection invalidate];
120 static int forceUpload = false;
121 static int getJSON = false;
122 static int getSysdiagnose = false;
123 static char *topicName = nil;
125 int main(int argc, char **argv)
127 static struct argument options[] = {
128 { .shortname='t', .longname="topicName", .argument=&topicName, .description="Operate on a non-default topic"},
130 { .command="sysdiagnose", .flag=&getSysdiagnose, .flagval=true, .description="Retrieve the current sysdiagnose dump for security analytics"},
131 { .command="get", .flag=&getJSON, .flagval=true, .description="Get the JSON blob we would upload to the server if an upload were due"},
132 { .command="upload", .flag=&forceUpload, .flagval=true, .description="Force an upload of analytics data to server (ignoring privacy settings)"},
136 static struct arguments args = {
137 .programname="supdctl",
138 .description="Control and report on security analytics",
139 .arguments = options,
142 if(!options_parse(argc, argv, &args)) {
150 forceUploadAnalytics();
151 } else if (getJSON) {
152 getLoggingJSON(topicName);
153 } else if (getSysdiagnose) {
154 getSysdiagnoseDump();