2 * Copyright (c) 2015 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@
25 * This is to fool os services to not provide the Keychain manager
26 * interface that doesn't work since we don't have unified headers
27 * between iOS and OS X. rdar://23405418/
29 #define __KEYCHAINCORE__ 1
31 #import <Foundation/Foundation.h>
32 #import <Foundation/NSXPCConnection_Private.h>
33 #import <Security/Security.h>
34 #import <Security/SecItemPriv.h>
35 #import <Security/SecureObjectSync/SOSTypes.h>
36 #import <Security/SecureObjectSync/SOSControlHelper.h>
40 #include "builtin_commands.h"
43 @interface SOSStatus : NSObject
44 @property NSXPCConnection *connection;
46 - (void)printPerformanceCounters:(bool)asPList;
49 @implementation SOSStatus
51 - (instancetype) initWithEndpoint:(xpc_endpoint_t)endpoint
53 if ((self = [super init]) == NULL)
56 NSXPCInterface *interface = [NSXPCInterface interfaceWithProtocol:@protocol(SOSControlProtocol)];
57 _SOSControlSetupInterface(interface);
58 NSXPCListenerEndpoint *listenerEndpoint = [[NSXPCListenerEndpoint alloc] init];
60 [listenerEndpoint _setEndpoint:endpoint];
62 self.connection = [[NSXPCConnection alloc] initWithListenerEndpoint:listenerEndpoint];
63 if (self.connection == NULL)
66 self.connection.remoteObjectInterface = interface;
68 [self.connection resume];
74 - (void)printPerformanceCounters:(bool)asPList
76 dispatch_semaphore_t sema1 = dispatch_semaphore_create(0);
77 dispatch_semaphore_t sema2 = dispatch_semaphore_create(0);
78 dispatch_semaphore_t sema3 = dispatch_semaphore_create(0);
80 NSMutableDictionary<NSString *, NSNumber *> *merged = [NSMutableDictionary dictionary];
81 __block NSMutableDictionary<NSString *, NSString *> *diagnostics = [NSMutableDictionary dictionary];
83 [[self.connection remoteObjectProxy] kvsPerformanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
84 if (counters == NULL){
85 printf("no KVS counters!");
89 [merged addEntriesFromDictionary:counters];
90 dispatch_semaphore_signal(sema1);
94 [[self.connection remoteObjectProxy] idsPerformanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
95 if (counters == NULL){
96 printf("no IDS counters!");
99 [merged addEntriesFromDictionary:counters];
100 dispatch_semaphore_signal(sema2);
103 [[self.connection remoteObjectProxy] rateLimitingPerformanceCounters:^(NSDictionary <NSString *, NSString *> *returnedDiagnostics){
104 if (returnedDiagnostics == NULL){
105 printf("no rate limiting counters!");
108 diagnostics = [[NSMutableDictionary alloc]initWithDictionary:returnedDiagnostics];
109 dispatch_semaphore_signal(sema3);
111 dispatch_semaphore_wait(sema1, DISPATCH_TIME_FOREVER);
112 dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER);
113 dispatch_semaphore_wait(sema3, DISPATCH_TIME_FOREVER);
116 NSData *data = [NSPropertyListSerialization dataWithPropertyList:merged format:NSPropertyListXMLFormat_v1_0 options:0 error:NULL];
118 printf("%.*s\n", (int)[data length], [data bytes]);
120 [merged enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber * obj, BOOL *stop) {
121 printf("%s - %lld\n", [key UTF8String], [obj longLongValue]);
125 [diagnostics enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSString * obj, BOOL * stop) {
126 printf("%s - %s\n", [key UTF8String], [obj UTF8String]);
134 usage(const char *command, struct option *options)
136 printf("%s %s [...options]\n", getprogname(), command);
137 for (unsigned n = 0; options[n].name; n++) {
138 printf("\t [-%c|--%s\n", options[n].val, options[n].name);
144 command_sos_stats(__unused int argc, __unused char * const * argv)
147 int option_index = 0, ch;
149 xpc_endpoint_t endpoint = _SecSecuritydCopySOSStatusEndpoint(NULL);
150 if (endpoint == NULL)
151 errx(1, "no SOS endpoint");
153 SOSStatus *control = [[SOSStatus alloc] initWithEndpoint:endpoint];
155 bool asPlist = false;
156 struct option long_options[] =
158 /* These options set a flag. */
159 {"plist", no_argument, NULL, 'p'},
163 while ((ch = getopt_long(argc, argv, "p", long_options, &option_index)) != -1) {
172 usage("sos-stats", long_options);
178 [control printPerformanceCounters:asPlist];
185 command_sos_control(__unused int argc, __unused char * const * argv)
188 int option_index = 0, ch;
190 bool assertStashAccountKey = false;
191 bool triggerSync = false;
192 NSString *syncingPeer = NULL;
194 static struct option long_options[] =
196 /* These options set a flag. */
197 {"assertStashAccountKey", no_argument, NULL, 'A'},
198 {"trigger-sync", optional_argument, NULL, 's'},
202 while ((ch = getopt_long(argc, argv, "A", long_options, &option_index)) != -1) {
205 assertStashAccountKey = true;
211 syncingPeer = [NSString stringWithUTF8String:optarg];
218 usage("sos-control", long_options);
224 xpc_endpoint_t endpoint = _SecSecuritydCopySOSStatusEndpoint(NULL);
225 if (endpoint == NULL)
226 errx(1, "no SOS endpoint");
228 SOSStatus *control = [[SOSStatus alloc] initWithEndpoint:endpoint];
230 errx(1, "no SOS control object");
232 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
235 NSMutableArray<NSString *> *peers = [NSMutableArray array];
237 [peers addObject:syncingPeer];
240 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError *error) {
241 printf("%s", [[NSString stringWithFormat:@"Failed to sending messages to soscontrol object: %@\n", error] UTF8String]);
242 dispatch_semaphore_signal(sema);
243 }] triggerSync:peers complete:^(bool res, NSError *error) {
245 printf("starting to sync was successful\n");
247 printf("%s", [[NSString stringWithFormat:@"Failed to start sync: %@\n", error] UTF8String]);
249 dispatch_semaphore_signal(sema);
251 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
252 } else if (assertStashAccountKey) {
253 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError *error) {
254 printf("%s", [[NSString stringWithFormat:@"Failed to sending messages to soscontrol object: %@\n", error] UTF8String]);
255 dispatch_semaphore_signal(sema);
256 }] assertStashedAccountCredential:^(BOOL res, NSError *error) {
258 printf("successfully asserted stashed credential\n");
260 printf("%s", [[NSString stringWithFormat:@"failed to assert stashed credential: %@\n", error] UTF8String]);
262 dispatch_semaphore_signal(sema);
264 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
268 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError *error) {
269 printf("%s", [[NSString stringWithFormat:@"Failed to sending messages to soscontrol object: %@\n", error] UTF8String]);
270 dispatch_semaphore_signal(sema);
271 }] userPublicKey:^(BOOL trusted, NSData *spki, NSError *error) {
272 printf("trusted: %s\n", trusted ? "yes" : "no");
273 printf("userPublicKey: %s\n", [[spki base64EncodedStringWithOptions:0] UTF8String]);
274 dispatch_semaphore_signal(sema);
276 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
278 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError *error) {
279 printf("%s", [[NSString stringWithFormat:@"Failed to sending messages to soscontrol object: %@\n", error] UTF8String]);
280 dispatch_semaphore_signal(sema);
281 }] stashedCredentialPublicKey:^(NSData *spki, NSError *error) {
282 NSString *pkey = [spki base64EncodedStringWithOptions:0];
284 pkey = @"no available";
285 printf("cachedCredentialPublicKey: %s\n", [pkey UTF8String]);
286 dispatch_semaphore_signal(sema);
288 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
295 int command_watchdog(int argc, char* const * argv)
297 xpc_endpoint_t endpoint = _SecSecuritydCopySOSStatusEndpoint(NULL);
299 errx(1, "no SOS endpoint");
303 SOSStatus* control = [[SOSStatus alloc] initWithEndpoint:endpoint];
304 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
307 printf("getting watchdog parameters...\n");
308 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
309 printf("%s", [[NSString stringWithFormat:@"Failed to sending messages to soscontrol object: %@\n", error] UTF8String]);
310 dispatch_semaphore_signal(semaphore);
311 }] getWatchdogParameters:^(NSDictionary* parameters, NSError* error) {
313 printf("error getting watchdog parameters: %s", error.localizedDescription.UTF8String);
316 printf("watchdog parameters:\n");
317 [parameters enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSObject* value, BOOL* stop) {
318 printf("\t%s - %s\n", key.description.UTF8String, value.description.UTF8String);
322 dispatch_semaphore_signal(semaphore);
326 printf("attempting to set watchdog parameters...\n");
327 NSString* parameter = [[NSString alloc] initWithUTF8String:argv[1]];
328 NSInteger value = [[[NSString alloc] initWithUTF8String:argv[2]] integerValue];
329 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
330 printf("%s", [[NSString stringWithFormat:@"Failed to sending messages to soscontrol object: %@\n", error] UTF8String]);
331 dispatch_semaphore_signal(semaphore);
332 }] setWatchdogParmeters:@{parameter : @(value)} complete:^(NSError* error) {
334 printf("error attempting to set watchdog parameters: %s\n", error.localizedDescription.UTF8String);
337 printf("successfully set watchdog parameter\n");
340 dispatch_semaphore_signal(semaphore);
344 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);