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";
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 createLoggingJSON(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 }] createLoggingJSON: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 createChunkedLoggingJSON(char *topicName)
100 NSString *topic = topicName ? [NSString stringWithUTF8String:topicName] : SFAnalyticsTopicKeySync;
101 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
102 NSXPCConnection* connection = getConnection();
103 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
104 nsprintf(@"Could not communicate with supd: %@", error);
105 dispatch_semaphore_signal(sema);
106 }] createChunkedLoggingJSON:YES topic:topic reply:^(NSData* data, NSError* error) {
108 // Success! Only print the JSON blob to make output easier to parse
109 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
111 nsprintf(@"supd gave us an error: %@", error);
113 dispatch_semaphore_signal(sema);
116 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
117 printf("\n\nError: timed out waiting for response from supd\n");
119 [connection invalidate];
122 static void forceUploadAnalytics(void)
124 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
125 NSXPCConnection* connection = getConnection();
126 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
127 nsprintf(@"Could not communicate with supd: %@", error);
128 dispatch_semaphore_signal(sema);
129 }] forceUploadWithReply:^(BOOL success, NSError *error) {
131 printf("Supd reports successful upload\n");
133 nsprintf(@"Supd reports failure: %@", error);
135 dispatch_semaphore_signal(sema);
138 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
139 printf("\n\nError: timed out waiting for response from supd\n");
141 [connection invalidate];
147 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
148 NSXPCConnection* connection = getConnection();
149 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
150 nsprintf(@"Could not communicate with supd: %@", error);
151 dispatch_semaphore_signal(sema);
152 }] clientStatus:^(NSDictionary<NSString *,id> *info, NSError *error) {
154 nsprintf(@"%@\n", info);
156 nsprintf(@"Supd reports failure: %@", error);
158 dispatch_semaphore_signal(sema);
161 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
162 printf("\n\nError: timed out waiting for response from supd\n");
164 [connection invalidate];
168 forceOldUploadDate(void)
170 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
171 NSXPCConnection* connection = getConnection();
173 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-7 * 24 * 3600.0)];
175 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
176 nsprintf(@"Could not communicate with supd: %@", error);
177 dispatch_semaphore_signal(sema);
178 }] setUploadDateWith:date reply:^(BOOL success, NSError *error) {
179 if (!success && error) {
180 nsprintf(@"Supd reports failure: %@", error);
182 dispatch_semaphore_signal(sema);
185 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
186 printf("\n\nError: timed out waiting for response from supd\n");
188 [connection invalidate];
191 static int forceUpload = false;
192 static int getJSON = false;
193 static int getChunkedJSON = false;
194 static int getSysdiagnose = false;
195 static int getInfo = false;
196 static int setOldUploadDate = false;
197 static char *topicName = nil;
199 int main(int argc, char **argv)
201 static struct argument options[] = {
202 { .shortname='t', .longname="topicName", .argument=&topicName, .description="Operate on a non-default topic"},
203 { .command="sysdiagnose", .flag=&getSysdiagnose, .flagval=true, .description="Retrieve the current sysdiagnose dump for security analytics"},
204 { .command="get", .flag=&getJSON, .flagval=true, .description="Get the JSON blob we would upload to the server if an upload were due"},
205 { .command="getChunked", .flag=&getChunkedJSON, .flagval=true, .description="Chunk the JSON blob"},
206 { .command="upload", .flag=&forceUpload, .flagval=true, .description="Force an upload of analytics data to server (ignoring privacy settings)"},
207 { .command="info", .flag=&getInfo, .flagval=true, .description="Request info about clients"},
208 { .command="set-old-upload-date", .flag=&setOldUploadDate, .flagval=true, .description="Clear last upload date"},
212 static struct arguments args = {
213 .programname="supdctl",
214 .description="Control and report on security analytics",
215 .arguments = options,
218 if(!options_parse(argc, argv, &args)) {
224 if (!SecIsInternalRelease()) {
230 forceUploadAnalytics();
231 } else if (getJSON) {
232 createLoggingJSON(topicName);
233 } else if (getChunkedJSON) {
234 createChunkedLoggingJSON(topicName);
235 } else if (getSysdiagnose) {
236 getSysdiagnoseDump();
237 } else if (getInfo) {
239 } else if (setOldUploadDate) {
240 forceOldUploadDate();