]> git.saurik.com Git - apple/security.git/blob - supdctl/main.m
Security-59306.101.1.tar.gz
[apple/security.git] / supdctl / main.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
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"
30
31 /* Internal Topic Names */
32 NSString* const SFAnalyticsTopicKeySync = @"KeySyncTopic";
33 NSString* const SFAnalyticsTopicTrust = @"TrustTopic";
34 NSString* const SFAnalyticsTopicTransparency = @"TransparencyTopic";
35
36 static void nsprintf(NSString *fmt, ...) NS_FORMAT_FUNCTION(1, 2);
37 static void nsprintf(NSString *fmt, ...)
38 {
39 va_list ap;
40 va_start(ap, fmt);
41 NSString *str = [[NSString alloc] initWithFormat:fmt arguments:ap];
42 va_end(ap);
43
44 puts([str UTF8String]);
45 #if !__has_feature(objc_arc)
46 [str release];
47 #endif
48 }
49
50 static NSXPCConnection* getConnection()
51 {
52 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@"com.apple.securityuploadd" options:0];
53 connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(supdProtocol)];
54 [connection resume];
55 return connection;
56 }
57
58 static void getSysdiagnoseDump(void)
59 {
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);
68 }];
69
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");
72 }
73 [connection invalidate];
74 }
75
76 static void createLoggingJSON(char *topicName)
77 {
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) {
85 if (data) {
86 // Success! Only print the JSON blob to make output easier to parse
87 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
88 } else {
89 nsprintf(@"supd gave us an error: %@", error);
90 }
91 dispatch_semaphore_signal(sema);
92 }];
93
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");
96 }
97 [connection invalidate];
98 }
99
100 static void createChunkedLoggingJSON(char *topicName)
101 {
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) {
109 if (data) {
110 // Success! Only print the JSON blob to make output easier to parse
111 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
112 } else {
113 nsprintf(@"supd gave us an error: %@", error);
114 }
115 dispatch_semaphore_signal(sema);
116 }];
117
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");
120 }
121 [connection invalidate];
122 }
123
124 static void forceUploadAnalytics(void)
125 {
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) {
132 if (success) {
133 printf("Supd reports successful upload\n");
134 } else {
135 nsprintf(@"Supd reports failure: %@", error);
136 }
137 dispatch_semaphore_signal(sema);
138 }];
139
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");
142 }
143 [connection invalidate];
144 }
145
146 static void
147 getInfoDump(void)
148 {
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) {
155 if (info) {
156 nsprintf(@"%@\n", info);
157 } else {
158 nsprintf(@"Supd reports failure: %@", error);
159 }
160 dispatch_semaphore_signal(sema);
161 }];
162
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");
165 }
166 [connection invalidate];
167 }
168
169 static void
170 forceOldUploadDate(void)
171 {
172 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
173 NSXPCConnection* connection = getConnection();
174
175 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-7 * 24 * 3600.0)];
176
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);
183 }
184 dispatch_semaphore_signal(sema);
185 }];
186
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");
189 }
190 [connection invalidate];
191 }
192
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;
200
201 int main(int argc, char **argv)
202 {
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"},
211 {} // Need this!
212 };
213
214 static struct arguments args = {
215 .programname="supdctl",
216 .description="Control and report on security analytics",
217 .arguments = options,
218 };
219
220 if(!options_parse(argc, argv, &args)) {
221 printf("\n");
222 print_usage(&args);
223 return -1;
224 }
225
226 if (!SecIsInternalRelease()) {
227 abort();
228 }
229
230 @autoreleasepool {
231 if (forceUpload) {
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) {
240 getInfoDump();
241 } else if (setOldUploadDate) {
242 forceOldUploadDate();
243 } else {
244 print_usage(&args);
245 return -1;
246 }
247 }
248 return 0;
249 }
250