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 getLoggingJSON(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 }] getLoggingJSON: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 forceUploadAnalytics(void)
102 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
103 NSXPCConnection* connection = getConnection();
104 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
105 nsprintf(@"Could not communicate with supd: %@", error);
106 dispatch_semaphore_signal(sema);
107 }] forceUploadWithReply:^(BOOL success, NSError *error) {
109 printf("Supd reports successful upload\n");
111 nsprintf(@"Supd reports failure: %@", 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];
125 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
126 NSXPCConnection* connection = getConnection();
127 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
128 nsprintf(@"Could not communicate with supd: %@", error);
129 dispatch_semaphore_signal(sema);
130 }] clientStatus:^(NSDictionary<NSString *,id> *info, NSError *error) {
132 nsprintf(@"%@\n", info);
134 nsprintf(@"Supd reports failure: %@", error);
136 dispatch_semaphore_signal(sema);
139 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
140 printf("\n\nError: timed out waiting for response from supd\n");
142 [connection invalidate];
146 forceOldUploadDate(void)
148 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
149 NSXPCConnection* connection = getConnection();
151 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-7 * 24 * 3600.0)];
153 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
154 nsprintf(@"Could not communicate with supd: %@", error);
155 dispatch_semaphore_signal(sema);
156 }] setUploadDateWith:date reply:^(BOOL success, NSError *error) {
157 if (!success && error) {
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];
169 static int forceUpload = false;
170 static int getJSON = false;
171 static int getSysdiagnose = false;
172 static int getInfo = false;
173 static int setOldUploadDate = false;
174 static char *topicName = nil;
176 int main(int argc, char **argv)
178 static struct argument options[] = {
179 { .shortname='t', .longname="topicName", .argument=&topicName, .description="Operate on a non-default topic"},
181 { .command="sysdiagnose", .flag=&getSysdiagnose, .flagval=true, .description="Retrieve the current sysdiagnose dump for security analytics"},
182 { .command="get", .flag=&getJSON, .flagval=true, .description="Get the JSON blob we would upload to the server if an upload were due"},
183 { .command="upload", .flag=&forceUpload, .flagval=true, .description="Force an upload of analytics data to server (ignoring privacy settings)"},
184 { .command="info", .flag=&getInfo, .flagval=true, .description="Request info about clients"},
185 { .command="set-old-upload-date", .flag=&setOldUploadDate, .flagval=true, .description="Clear last upload date"},
189 static struct arguments args = {
190 .programname="supdctl",
191 .description="Control and report on security analytics",
192 .arguments = options,
195 if(!options_parse(argc, argv, &args)) {
201 if (!SecIsInternalRelease()) {
207 forceUploadAnalytics();
208 } else if (getJSON) {
209 getLoggingJSON(topicName);
210 } else if (getSysdiagnose) {
211 getSysdiagnoseDump();
212 } else if (getInfo) {
214 } else if (setOldUploadDate) {
215 forceOldUploadDate();