]> git.saurik.com Git - apple/security.git/blob - supdctl/main.m
Security-58286.240.4.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
30 /* Internal Topic Names */
31 NSString* const SFAnalyticsTopicKeySync = @"KeySyncTopic";
32 NSString* const SFAnaltyicsTopicTrust = @"TrustTopic";
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 getLoggingJSON(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 }] getLoggingJSON: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 forceUploadAnalytics(void)
99 {
100 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
101 NSXPCConnection* connection = getConnection();
102 [[connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
103 nsprintf(@"Could not communicate with supd: %@", error);
104 dispatch_semaphore_signal(sema);
105 }] forceUploadWithReply:^(BOOL success, NSError *error) {
106 if (success) {
107 printf("Supd reports successful upload\n");
108 } else {
109 nsprintf(@"Supd reports failure: %@", error);
110 }
111 dispatch_semaphore_signal(sema);
112 }];
113
114 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 20)) != 0) {
115 printf("\n\nError: timed out waiting for response from supd\n");
116 }
117 [connection invalidate];
118 }
119
120 static int forceUpload = false;
121 static int getJSON = false;
122 static int getSysdiagnose = false;
123 static char *topicName = nil;
124
125 int main(int argc, char **argv)
126 {
127 static struct argument options[] = {
128 { .shortname='t', .longname="topicName", .argument=&topicName, .description="Operate on a non-default topic"},
129
130 { .command="sysdiagnose", .flag=&getSysdiagnose, .flagval=true, .description="Retrieve the current sysdiagnose dump for security analytics"},
131 { .command="get", .flag=&getJSON, .flagval=true, .description="Get the JSON blob we would upload to the server if an upload were due"},
132 { .command="upload", .flag=&forceUpload, .flagval=true, .description="Force an upload of analytics data to server (ignoring privacy settings)"},
133 {} // Need this!
134 };
135
136 static struct arguments args = {
137 .programname="supdctl",
138 .description="Control and report on security analytics",
139 .arguments = options,
140 };
141
142 if(!options_parse(argc, argv, &args)) {
143 printf("\n");
144 print_usage(&args);
145 return -1;
146 }
147
148 @autoreleasepool {
149 if (forceUpload) {
150 forceUploadAnalytics();
151 } else if (getJSON) {
152 getLoggingJSON(topicName);
153 } else if (getSysdiagnose) {
154 getSysdiagnoseDump();
155 } else {
156 print_usage(&args);
157 return -1;
158 }
159 }
160 return 0;
161 }
162