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 #include "keychain/SecureObjectSync/SOSControlHelper.h"
37 #import <ipc/securityd_client.h>
41 #include "builtin_commands.h"
44 @interface SOSStatus : NSObject
45 @property NSXPCConnection *connection;
47 - (void)printPerformanceCounters:(bool)asPList;
50 @implementation SOSStatus
54 if ((self = [super init]) == NULL)
57 NSXPCInterface *interface = [NSXPCInterface interfaceWithProtocol:@protocol(SOSControlProtocol)];
58 _SOSControlSetupInterface(interface);
60 self.connection = [[NSXPCConnection alloc] initWithMachServiceName:@(kSecuritydSOSServiceName) options:0];
61 if (self.connection == NULL){
64 self.connection.remoteObjectInterface = interface;
66 [self.connection resume];
72 - (void)printPerformanceCounters:(bool)asPList
74 dispatch_semaphore_t sema1 = dispatch_semaphore_create(0);
75 dispatch_semaphore_t sema2 = dispatch_semaphore_create(0);
76 dispatch_semaphore_t sema3 = dispatch_semaphore_create(0);
78 NSMutableDictionary<NSString *, NSNumber *> *merged = [NSMutableDictionary dictionary];
79 __block NSMutableDictionary<NSString *, NSString *> *diagnostics = [NSMutableDictionary dictionary];
81 [[self.connection remoteObjectProxy] kvsPerformanceCounters:^(NSDictionary <NSString *, NSNumber *> *counters){
82 if (counters == NULL){
83 printf("no KVS counters!");
87 [merged addEntriesFromDictionary:counters];
88 dispatch_semaphore_signal(sema1);
91 [[self.connection remoteObjectProxy] rateLimitingPerformanceCounters:^(NSDictionary <NSString *, NSString *> *returnedDiagnostics){
92 if (returnedDiagnostics == NULL){
93 printf("no rate limiting counters!");
96 diagnostics = [[NSMutableDictionary alloc]initWithDictionary:returnedDiagnostics];
97 dispatch_semaphore_signal(sema3);
99 dispatch_semaphore_wait(sema1, DISPATCH_TIME_FOREVER);
100 dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER);
101 dispatch_semaphore_wait(sema3, DISPATCH_TIME_FOREVER);
104 NSData *data = [NSPropertyListSerialization dataWithPropertyList:merged format:NSPropertyListXMLFormat_v1_0 options:0 error:NULL];
106 printf("%.*s\n", (int)[data length], [data bytes]);
108 [merged enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber * obj, BOOL *stop) {
109 printf("%s - %lld\n", [key UTF8String], [obj longLongValue]);
113 [diagnostics enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSString * obj, BOOL * stop) {
114 printf("%s - %s\n", [key UTF8String], [obj UTF8String]);
122 usage(const char *command, struct option *options)
124 printf("%s %s [...options]\n", getprogname(), command);
125 for (unsigned n = 0; options[n].name; n++) {
126 printf("\t [-%c|--%s\n", options[n].val, options[n].name);
132 command_sos_stats(__unused int argc, __unused char * const * argv)
135 int option_index = 0, ch;
137 SOSStatus *control = [[SOSStatus alloc] init];
139 bool asPlist = false;
140 struct option long_options[] =
142 /* These options set a flag. */
143 {"plist", no_argument, NULL, 'p'},
147 while ((ch = getopt_long(argc, argv, "p", long_options, &option_index)) != -1) {
156 usage("sos-stats", long_options);
157 return SHOW_USAGE_MESSAGE;
162 [control printPerformanceCounters:asPlist];
168 static void printControlFailureMessage(NSError *error) {
169 printf("%s", [[NSString stringWithFormat:@"Failed to send messages to soscontrol object: %@\n", error] UTF8String]);
173 command_sos_control(__unused int argc, __unused char * const * argv)
176 int option_index = 0, ch;
178 bool assertStashAccountKey = false;
179 bool triggerSync = false;
180 NSMutableArray<NSString *>* syncingPeers = NULL;
181 bool triggerBackup = false;
182 NSMutableArray<NSString*>* backupPeers = NULL;
183 SOSAccountGhostBustingOptions gboptions = 0;
185 bool gbtriggered = false;
186 bool circleHash = false;
187 bool triggerRingUpdate = false;
189 static struct option long_options[] =
191 /* These options set a flag. */
192 {"assertStashAccountKey", no_argument, NULL, 'a'},
193 {"trigger-backup", optional_argument, NULL, 'B'},
194 {"trigger-ring-update", no_argument, NULL, 'R'},
195 {"trigger-sync", optional_argument, NULL, 's'},
196 {"circle-hash", optional_argument, NULL, 'H'},
197 {"ghostbustByMID", optional_argument, NULL, 'M'},
198 {"ghostbustBySerial", optional_argument, NULL, 'S'},
199 {"ghostbustiCloudIdentities", optional_argument, NULL, 'I'},
200 {"ghostbustByAge", optional_argument, NULL, 'A'},
201 {"ghostbustInfo", optional_argument, NULL, 'G'},
202 {"ghostbustTriggered", optional_argument, NULL, 'T'},
206 while ((ch = getopt_long(argc, argv, "as:AB:GHIMRST", long_options, &option_index)) != -1) {
209 assertStashAccountKey = true;
215 if (syncingPeers == NULL) {
216 syncingPeers = [NSMutableArray array];
218 [syncingPeers addObject:[NSString stringWithUTF8String:optarg]];
223 gboptions |= SOSGhostBustSerialByAge;
227 triggerBackup = true;
229 if (backupPeers == NULL) {
230 backupPeers = [NSMutableArray array];
232 [backupPeers addObject:[NSString stringWithUTF8String:optarg]];
241 gboptions |= SOSGhostBustByMID;
245 triggerRingUpdate = true;
248 gboptions |= SOSGhostBustBySerialNumber;
252 gboptions |= SOSGhostBustiCloudIdentities;
265 usage("sos-control", long_options);
266 return SHOW_USAGE_MESSAGE;
271 SOSStatus *control = [[SOSStatus alloc] init];
273 errx(1, "no SOS control object");
276 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
277 printControlFailureMessage(error);
278 }] ghostBustTriggerTimed:gboptions complete:^(bool ghostBusted, NSError *error) {
280 printf("ghostbusted devices\n");
282 printf("%s", [[NSString stringWithFormat:@"Failed ghostbusting: %@\n", error] UTF8String]);
284 printf("no ghosts found\n");
288 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
289 printControlFailureMessage(error);
290 }] ghostBustInfo: ^(NSData *json, NSError *error) {
291 printf("%s\n", [[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding] UTF8String]);
293 } else if (gboptions != 0) {
294 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
295 printControlFailureMessage(error);
296 }] ghostBust:gboptions complete:^(bool ghostBusted, NSError *error) {
298 printf("ghostbusted devices\n");
300 printf("%s", [[NSString stringWithFormat:@"Failed ghostbusting: %@\n", error] UTF8String]);
302 printf("no ghosts found\n");
305 } else if (triggerSync) {
306 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
307 printControlFailureMessage(error);
308 }] rpcTriggerSync:syncingPeers complete:^(bool res, NSError *error) {
310 printf("starting to sync was successful\n");
312 printf("%s", [[NSString stringWithFormat:@"Failed to start sync: %@\n", error] UTF8String]);
315 } else if (triggerBackup) {
316 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
317 printControlFailureMessage(error);
318 }] rpcTriggerBackup:backupPeers complete:^(NSError *error) {
320 printf("trigger backup was successful\n");
322 printf("%s", [[NSString stringWithFormat:@"Failed to start backup: %@\n", error] UTF8String]);
325 } else if (triggerRingUpdate) {
326 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
327 printControlFailureMessage(error);
328 }] rpcTriggerRingUpdate:^(NSError *error) {
330 printf("trigger ring update was successful\n");
332 printf("%s", [[NSString stringWithFormat:@"Failed to start ring update: %@\n", error] UTF8String]);
336 } else if (assertStashAccountKey) {
337 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
338 printControlFailureMessage(error);
339 }] assertStashedAccountCredential:^(BOOL res, NSError *error) {
341 printf("successfully asserted stashed credential\n");
343 printf("%s", [[NSString stringWithFormat:@"failed to assert stashed credential: %@\n", error] UTF8String]);
346 } else if (circleHash) {
347 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
348 printControlFailureMessage(error);
349 }] circleHash:^(NSString *hash, NSError *error) {
351 printf("%s", [[NSString stringWithFormat:@"circle hash: %@\n", hash] UTF8String]);
353 printf("%s", [[NSString stringWithFormat:@"failed to get circle digest: %@\n", error] UTF8String]);
359 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
360 printControlFailureMessage(error);
361 }] userPublicKey:^(BOOL trusted, NSData *spki, NSError *error) {
362 printf("trusted: %s\n", trusted ? "yes" : "no");
363 printf("userPublicKey: %s\n", [[spki base64EncodedStringWithOptions:0] UTF8String]);
366 [[control.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError *error) {
367 printControlFailureMessage(error);
368 }] stashedCredentialPublicKey:^(NSData *spki, NSError *error) {
369 NSString *pkey = [spki base64EncodedStringWithOptions:0];
371 pkey = @"not available";
372 printf("cachedCredentialPublicKey: %s\n", [pkey UTF8String]);
380 int command_watchdog(int argc, char* const * argv)
382 SOSStatus* control = [[SOSStatus alloc] init];
383 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
386 printf("getting watchdog parameters...\n");
387 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
388 printControlFailureMessage(error);
389 dispatch_semaphore_signal(semaphore);
390 }] getWatchdogParameters:^(NSDictionary* parameters, NSError* error) {
392 printf("error getting watchdog parameters: %s", error.localizedDescription.UTF8String);
395 printf("watchdog parameters:\n");
396 [parameters enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSObject* value, BOOL* stop) {
397 printf("\t%s - %s\n", key.description.UTF8String, value.description.UTF8String);
401 dispatch_semaphore_signal(semaphore);
405 printf("attempting to set watchdog parameters...\n");
406 NSString* parameter = [[NSString alloc] initWithUTF8String:argv[1]];
407 NSInteger value = [[[NSString alloc] initWithUTF8String:argv[2]] integerValue];
408 [[control.connection remoteObjectProxyWithErrorHandler:^(NSError* error) {
409 printControlFailureMessage(error);
410 dispatch_semaphore_signal(semaphore);
411 }] setWatchdogParmeters:@{parameter : @(value)} complete:^(NSError* error) {
413 printf("error attempting to set watchdog parameters: %s\n", error.localizedDescription.UTF8String);
416 printf("successfully set watchdog parameter\n");
419 dispatch_semaphore_signal(semaphore);
423 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);