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>
29 #import "SecInternalReleasePriv.h"
31 /* Internal Topic Names */
32 NSString* const SFAnalyticsTopicKeySync = @"KeySyncTopic";
33 NSString* const SFAnalyticsTopicTrust = @"TrustTopic";
34 NSString* const SFAnalyticsTopicTransparency = @"TransparencyTopic";
36 static void nsprintf(NSString *fmt, ...) NS_FORMAT_FUNCTION(1, 2);
37 static void nsprintf(NSString *fmt, ...)
41 NSString *str = [[NSString alloc] initWithFormat:fmt arguments:ap];
44 puts([str UTF8String]);
45 #if !__has_feature(objc_arc)
50 static NSXPCConnection* getConnection()
52 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@"com.apple.securityuploadd" options:0];
53 connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(supdProtocol)];
58 static void getSysdiagnoseDump(void)
60 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
61 NSXPCConnection* connection = getConnection();
62 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
63 nsprintf(@"Could not communicate with supd: %@", error);
64 dispatch_semaphore_signal(sema);
65 }] getSysdiagnoseDumpWithReply:^(NSString * sysdiagnoseString) {
66 nsprintf(@"Analytics sysdiagnose: \n%@", sysdiagnoseString);
67 dispatch_semaphore_signal(sema);
70 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
71 printf("\n\nError: timed out waiting for response from supd\n");
73 [connection invalidate];
76 static void createLoggingJSON(char *topicName)
78 NSString *topic = topicName ? [NSString stringWithUTF8String:topicName] : SFAnalyticsTopicKeySync;
79 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
80 NSXPCConnection* connection = getConnection();
81 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
82 nsprintf(@"Could not communicate with supd: %@", error);
83 dispatch_semaphore_signal(sema);
84 }] createLoggingJSON:YES topic:topic reply:^(NSData* data, NSError* error) {
86 // Success! Only print the JSON blob to make output easier to parse
87 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
89 nsprintf(@"supd gave us an error: %@", error);
91 dispatch_semaphore_signal(sema);
94 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
95 printf("\n\nError: timed out waiting for response from supd\n");
97 [connection invalidate];
100 static void createChunkedLoggingJSON(char *topicName)
102 NSString *topic = topicName ? [NSString stringWithUTF8String:topicName] : SFAnalyticsTopicKeySync;
103 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
104 NSXPCConnection* connection = getConnection();
105 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
106 nsprintf(@"Could not communicate with supd: %@", error);
107 dispatch_semaphore_signal(sema);
108 }] createChunkedLoggingJSON:YES topic:topic reply:^(NSData* data, NSError* error) {
110 // Success! Only print the JSON blob to make output easier to parse
111 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
113 nsprintf(@"supd gave us an error: %@", error);
115 dispatch_semaphore_signal(sema);
118 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
119 printf("\n\nError: timed out waiting for response from supd\n");
121 [connection invalidate];
124 static void forceUploadAnalytics(void)
126 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
127 NSXPCConnection* connection = getConnection();
128 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
129 nsprintf(@"Could not communicate with supd: %@", error);
130 dispatch_semaphore_signal(sema);
131 }] forceUploadWithReply:^(BOOL success, NSError *error) {
133 printf("Supd reports successful upload\n");
135 nsprintf(@"Supd reports failure: %@", error);
137 dispatch_semaphore_signal(sema);
140 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
141 printf("\n\nError: timed out waiting for response from supd\n");
143 [connection invalidate];
149 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
150 NSXPCConnection* connection = getConnection();
151 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
152 nsprintf(@"Could not communicate with supd: %@", error);
153 dispatch_semaphore_signal(sema);
154 }] clientStatus:^(NSDictionary<NSString *,id> *info, NSError *error) {
156 nsprintf(@"%@\n", info);
158 nsprintf(@"Supd reports failure: %@", error);
160 dispatch_semaphore_signal(sema);
163 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
164 printf("\n\nError: timed out waiting for response from supd\n");
166 [connection invalidate];
170 forceOldUploadDate(void)
172 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
173 NSXPCConnection* connection = getConnection();
175 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-7 * 24 * 3600.0)];
177 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
178 nsprintf(@"Could not communicate with supd: %@", error);
179 dispatch_semaphore_signal(sema);
180 }] setUploadDateWith:date reply:^(BOOL success, NSError *error) {
181 if (!success && error) {
182 nsprintf(@"Supd reports failure: %@", error);
184 dispatch_semaphore_signal(sema);
187 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
188 printf("\n\nError: timed out waiting for response from supd\n");
190 [connection invalidate];
193 static int forceUpload = false;
194 static int getJSON = false;
195 static int getChunkedJSON = false;
196 static int getSysdiagnose = false;
197 static int getInfo = false;
198 static int setOldUploadDate = false;
199 static char *topicName = nil;
201 int main(int argc, char **argv)
203 static struct argument options[] = {
204 { .shortname='t', .longname="topicName", .argument=&topicName, .description="Operate on a non-default topic"},
205 { .command="sysdiagnose", .flag=&getSysdiagnose, .flagval=true, .description="Retrieve the current sysdiagnose dump for security analytics"},
206 { .command="get", .flag=&getJSON, .flagval=true, .description="Get the JSON blob we would upload to the server if an upload were due"},
207 { .command="getChunked", .flag=&getChunkedJSON, .flagval=true, .description="Chunk the JSON blob"},
208 { .command="upload", .flag=&forceUpload, .flagval=true, .description="Force an upload of analytics data to server (ignoring privacy settings)"},
209 { .command="info", .flag=&getInfo, .flagval=true, .description="Request info about clients"},
210 { .command="set-old-upload-date", .flag=&setOldUploadDate, .flagval=true, .description="Clear last upload date"},
214 static struct arguments args = {
215 .programname="supdctl",
216 .description="Control and report on security analytics",
217 .arguments = options,
220 if(!options_parse(argc, argv, &args)) {
226 if (!SecIsInternalRelease()) {
232 forceUploadAnalytics();
233 } else if (getJSON) {
234 createLoggingJSON(topicName);
235 } else if (getChunkedJSON) {
236 createChunkedLoggingJSON(topicName);
237 } else if (getSysdiagnose) {
238 getSysdiagnoseDump();
239 } else if (getInfo) {
241 } else if (setOldUploadDate) {
242 forceOldUploadDate();