]> git.saurik.com Git - apple/security.git/blob - supdctl/main.m
Security-59754.41.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
34 static void nsprintf(NSString *fmt, ...) NS_FORMAT_FUNCTION(1, 2);
35 static void nsprintf(NSString *fmt, ...)
36 {
37 va_list ap;
38 va_start(ap, fmt);
39 NSString *str = [[NSString alloc] initWithFormat:fmt arguments:ap];
40 va_end(ap);
41
42 puts([str UTF8String]);
43 #if !__has_feature(objc_arc)
44 [str release];
45 #endif
46 }
47
48 static NSXPCConnection* getConnection()
49 {
50 NSXPCConnection* connection = [[NSXPCConnection alloc] initWithMachServiceName:@"com.apple.securityuploadd" options:0];
51 connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(supdProtocol)];
52 [connection resume];
53 return connection;
54 }
55
56 static void getSysdiagnoseDump(void)
57 {
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);
66 }];
67
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");
70 }
71 [connection invalidate];
72 }
73
74 static void createLoggingJSON(char *topicName)
75 {
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) {
83 if (data) {
84 // Success! Only print the JSON blob to make output easier to parse
85 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
86 } else {
87 nsprintf(@"supd gave us an error: %@", error);
88 }
89 dispatch_semaphore_signal(sema);
90 }];
91
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");
94 }
95 [connection invalidate];
96 }
97
98 static void createChunkedLoggingJSON(char *topicName)
99 {
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) {
107 if (data) {
108 // Success! Only print the JSON blob to make output easier to parse
109 nsprintf(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
110 } else {
111 nsprintf(@"supd gave us an error: %@", error);
112 }
113 dispatch_semaphore_signal(sema);
114 }];
115
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");
118 }
119 [connection invalidate];
120 }
121
122 static void forceUploadAnalytics(void)
123 {
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) {
130 if (success) {
131 printf("Supd reports successful upload\n");
132 } else {
133 nsprintf(@"Supd reports failure: %@", error);
134 }
135 dispatch_semaphore_signal(sema);
136 }];
137
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");
140 }
141 [connection invalidate];
142 }
143
144 static void
145 getInfoDump(void)
146 {
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) {
153 if (info) {
154 nsprintf(@"%@\n", info);
155 } else {
156 nsprintf(@"Supd reports failure: %@", error);
157 }
158 dispatch_semaphore_signal(sema);
159 }];
160
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");
163 }
164 [connection invalidate];
165 }
166
167 static void
168 forceOldUploadDate(void)
169 {
170 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
171 NSXPCConnection* connection = getConnection();
172
173 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-7 * 24 * 3600.0)];
174
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);
181 }
182 dispatch_semaphore_signal(sema);
183 }];
184
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");
187 }
188 [connection invalidate];
189 }
190
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;
198
199 int main(int argc, char **argv)
200 {
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"},
209 {} // Need this!
210 };
211
212 static struct arguments args = {
213 .programname="supdctl",
214 .description="Control and report on security analytics",
215 .arguments = options,
216 };
217
218 if(!options_parse(argc, argv, &args)) {
219 printf("\n");
220 print_usage(&args);
221 return -1;
222 }
223
224 if (!SecIsInternalRelease()) {
225 abort();
226 }
227
228 @autoreleasepool {
229 if (forceUpload) {
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) {
238 getInfoDump();
239 } else if (setOldUploadDate) {
240 forceOldUploadDate();
241 } else {
242 print_usage(&args);
243 return -1;
244 }
245 }
246 return 0;
247 }
248