5 #import <Foundation/Foundation.h>
6 #import <Foundation/NSXPCConnection_Private.h>
7 #import <Security/Security.h>
8 #import <Security/SecItemPriv.h>
12 #import "keychain/ckks/CKKS.h"
13 #import "keychain/ckks/CKKSControl.h"
15 #include "lib/SecArgParse.h"
17 static void nsprintf(NSString *fmt, ...) NS_FORMAT_FUNCTION(1, 2);
18 static void print_result(NSDictionary *dict, bool json_flag);
19 static void print_dict(NSDictionary *dict, int ind);
20 static void print_array(NSArray *array, int ind);
21 static void print_entry(id k, id v, int ind);
23 static void nsprintf(NSString *fmt, ...)
27 NSString *str = [[NSString alloc] initWithFormat:fmt arguments:ap];
30 puts([str UTF8String]);
31 #if !__has_feature(objc_arc)
36 // Mutual recursion to set up an object for jsonification
37 static NSDictionary* cleanDictionaryForJSON(NSDictionary* dict);
39 static id cleanObjectForJSON(id obj) {
43 if([obj isKindOfClass:[NSError class]]) {
44 NSError* obje = (NSError*) obj;
45 NSMutableDictionary* newErrorDict = [@{@"code": @(obje.code), @"domain": obje.domain} mutableCopy];
46 newErrorDict[@"userInfo"] = cleanDictionaryForJSON(obje.userInfo);
48 } else if([NSJSONSerialization isValidJSONObject:obj]) {
51 } else if ([obj isKindOfClass: [NSNumber class]]) {
54 } else if([obj isKindOfClass: [NSData class]]) {
55 NSData* dataObj = (NSData*)obj;
56 return [dataObj base64EncodedStringWithOptions:0];
58 } else if ([obj isKindOfClass: [NSDictionary class]]) {
59 return cleanDictionaryForJSON((NSDictionary*) obj);
61 } else if ([obj isKindOfClass: [NSArray class]]) {
62 NSArray* arrayObj = (NSArray*)obj;
63 NSMutableArray* cleanArray = [NSMutableArray arrayWithCapacity:arrayObj.count];
65 for(id x in arrayObj) {
66 [cleanArray addObject: cleanObjectForJSON(x)];
71 return [obj description];
75 static NSDictionary* cleanDictionaryForJSON(NSDictionary* dict) {
79 NSMutableDictionary* mutDict = [dict mutableCopy];
80 for(id key in mutDict.allKeys) {
81 id obj = mutDict[key];
82 mutDict[key] = cleanObjectForJSON(obj);
87 static void print_result(NSDictionary *dict, bool json_flag)
92 NSData *json = [NSJSONSerialization dataWithJSONObject:cleanDictionaryForJSON(dict)
93 options:(NSJSONWritingPrettyPrinted | NSJSONWritingSortedKeys)
96 NSLog(@"error: %@", err.localizedDescription);
98 printf("%s", [[[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding] UTF8String]);
105 static void print_dict(NSDictionary *dict, int ind)
107 NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
108 for (id k in sortedKeys) {
110 print_entry(k, v, ind);
114 static void print_array(NSArray *array, int ind)
116 [array enumerateObjectsUsingBlock:^(id v, NSUInteger i, BOOL *stop __unused) {
117 print_entry(@(i), v, ind);
121 static void print_entry(id k, id v, int ind)
123 if ([v isKindOfClass:[NSDictionary class]]) {
125 nsprintf(@"\n%*s%@ -", ind * 4, "", k);
126 nsprintf(@"%*s========================", ind * 4, "");
127 } else if (ind == 1) {
128 nsprintf(@"\n%*s%@ -", ind * 4, "", k);
129 nsprintf(@"%*s------------------------", ind * 4, "");
131 nsprintf(@"%*s%@ -", ind * 4, "", k);
134 print_dict(v, ind + 1);
135 } else if ([v isKindOfClass:[NSArray class]]) {
136 nsprintf(@"%*s%@ -", ind * 4, "", k);
137 print_array(v, ind + 1);
139 nsprintf(@"%*s%@: %@", ind * 4, "", k, v);
143 @interface CKKSControlCLI : NSObject
144 @property CKKSControl* control;
147 @implementation CKKSControlCLI
149 - (instancetype) initWithCKKSControl:(CKKSControl*)control {
150 if ((self = [super init])) {
157 - (NSDictionary<NSString *, id> *)fetchPerformanceCounters
159 NSMutableDictionary *perfDict = [[NSMutableDictionary alloc] init];
161 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
163 [self.control rpcPerformanceCounters:^(NSDictionary<NSString *,NSNumber *> * counters, NSError * error) {
165 perfDict[@"error"] = [error description];
168 [counters enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber * obj, BOOL *stop) {
172 dispatch_semaphore_signal(sema);
175 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60)) != 0) {
176 perfDict[@"error"] = @"timed out waiting for response";
183 - (long)resetLocal:(NSString*)view {
184 __block long ret = 0;
186 printf("Beginning local reset for %s...\n", view ? [[view description] UTF8String] : "all zones");
187 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
189 [self.control rpcResetLocal:view
190 reply:^(NSError *error) {
192 printf("reset complete.\n");
195 nsprintf(@"reset error: %@\n", error);
198 dispatch_semaphore_signal(sema);
201 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60 * 2)) != 0) {
202 printf("\n\nError: timed out waiting for response\n");
209 - (long)resetCloudKit:(NSString*)view {
210 __block long ret = 0;
212 printf("Beginning CloudKit reset for %s...\n", view ? [[view description] UTF8String] : "all zones");
213 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
215 [self.control rpcResetCloudKit:view reason:@"ckksctl" reply:^(NSError* error){
217 printf("CloudKit Reset complete.\n");
220 nsprintf(@"Reset error: %@\n", error);
223 dispatch_semaphore_signal(sema);
226 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60 * 5)) != 0) {
227 printf("\n\nError: timed out waiting for response\n");
234 - (long)resync:(NSString*)view {
235 __block long ret = 0;
237 printf("Beginning resync for %s...\n", view ? [[view description] UTF8String] : "all zones");
238 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
240 [self.control rpcResync:view reply:^(NSError* error){
242 printf("resync success.\n");
245 nsprintf(@"resync errored: %@\n", error);
248 dispatch_semaphore_signal(sema);
251 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60 * 2)) != 0) {
252 printf("\n\nError: timed out waiting for response\n");
259 - (NSDictionary<NSString *, id> *)fetchStatus: (NSString*) view {
260 NSMutableDictionary *status = [[NSMutableDictionary alloc] init];
262 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
264 [self.control rpcStatus: view reply: ^(NSArray<NSDictionary*>* result, NSError* error) {
266 status[@"error"] = [error description];
269 if(result.count <= 1u) {
270 printf("No CKKS views are active.\n");
274 for(NSDictionary* viewStatus in result) {
275 status[viewStatus[@"view"]] = viewStatus;
277 dispatch_semaphore_signal(sema);
280 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 30)) != 0) {
281 status[@"error"] = @"timed out";
287 - (void)printHumanReadableStatus:(NSString*)view shortenOutput:(BOOL)shortenOutput {
289 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
291 [self.control rpcStatus: view reply: ^(NSArray<NSDictionary*>* result, NSError* error) {
293 printf("ERROR FETCHING STATUS: %s\n", [[error description] UTF8String]);
296 #define pop(d, key, cls) ({ id x = d[key]; d[key] = nil; [x isKindOfClass: [cls class]] ? x : nil; })
298 // First result is always global state
299 // Ideally, this would come in another parameter, but we can't change the protocol until
300 // <rdar://problem/33583242> CKKS: remove PCS's use of CKKSControlProtocol
301 NSMutableDictionary* global = [result[0] mutableCopy];
303 NSString* reachability = pop(global, @"reachability", NSString);
304 NSString* ckdeviceID = pop(global, @"ckdeviceID", NSString);
305 NSString* ckdeviceIDError = pop(global, @"ckdeviceIDError", NSString);
306 NSString* lockStateTracker = pop(global,@"lockstatetracker", NSString);
307 NSString* retry = pop(global,@"cloudkitRetryAfter", NSString);
308 NSDate *lastCKKSPush = pop(global, @"lastCKKSPush", NSDate);
309 NSString *syncingPolicy = pop(global, @"policy", NSString);
310 NSString *viewsFromPolicy = pop(global, @"viewsFromPolicy", NSString);
313 printf("================================================================================\n\n");
314 printf("Global state:\n\n");
317 printf("Syncing Policy: %s\n", [[syncingPolicy description] UTF8String]);
318 printf("Views from policy: %s\n", [[viewsFromPolicy description] UTF8String]);
321 printf("Reachability: %s\n", [[reachability description] UTF8String]);
322 printf("Retry: %s\n", [[retry description] UTF8String]);
323 printf("CK DeviceID: %s\n", [[ckdeviceID description] UTF8String]);
324 printf("CK DeviceID Error: %s\n", [[ckdeviceIDError description] UTF8String]);
325 printf("Lock state: %s\n", [[lockStateTracker description] UTF8String]);
326 printf("Last CKKS push: %s\n", [[lastCKKSPush description] UTF8String]);
332 NSArray* remainingViews = result.count > 1 ? [result subarrayWithRange:NSMakeRange(1, result.count-1)] : @[];
334 if(remainingViews.count == 0u) {
335 printf("No CKKS views are active.\n");
338 for(NSDictionary* viewStatus in remainingViews) {
340 NSMutableDictionary* status = [viewStatus mutableCopy];
342 NSString* viewName = pop(status, @"view", NSString);
343 NSString* keystate = pop(status, @"keystate", NSString);
345 printf("%-25s: %s\n", [viewName UTF8String], [keystate UTF8String]);
349 NSMutableDictionary* status = [viewStatus mutableCopy];
351 NSString* viewName = pop(status,@"view", NSString);
352 NSString* accountStatus = pop(status,@"ckaccountstatus", NSString);
353 NSString* accountTracker = pop(status,@"accounttracker", NSString);
354 NSString* fetcher = pop(status,@"fetcher", NSString);
355 NSString* zoneCreated = pop(status,@"zoneCreated", NSString);
356 NSString* zoneSubscribed = pop(status,@"zoneSubscribed", NSString);
357 NSString* zoneInitializeScheduler = pop(status,@"zoneInitializeScheduler", NSString);
358 NSString* keystate = pop(status,@"keystate", NSString);
359 NSString* statusError = pop(status,@"statusError", NSString);
360 NSString* itemSyncEnabled = pop(status,@"itemsyncing", NSString);
361 NSString* currentTLK = pop(status,@"currentTLK", NSString);
362 NSString* currentClassA = pop(status,@"currentClassA", NSString);
363 NSString* currentClassC = pop(status,@"currentClassC", NSString);
364 NSString* currentTLKPtr = pop(status,@"currentTLKPtr", NSString);
365 NSString* currentClassAPtr = pop(status,@"currentClassAPtr", NSString);
366 NSString* currentClassCPtr = pop(status,@"currentClassCPtr", NSString);
367 NSArray* launchSequence = pop(status, @"launchSequence", NSArray);
369 NSDictionary* oqe = pop(status,@"oqe", NSDictionary);
370 NSDictionary* iqe = pop(status,@"iqe", NSDictionary);
371 NSDictionary* keys = pop(status,@"keys", NSDictionary);
372 NSDictionary* ckmirror = pop(status,@"ckmirror", NSDictionary);
373 NSArray* devicestates = pop(status, @"devicestates", NSArray);
374 NSArray* tlkshares = pop(status, @"tlkshares", NSArray);
376 NSString* lastIncomingQueueOperation = pop(status,@"lastIncomingQueueOperation", NSString);
377 NSString* lastNewTLKOperation = pop(status,@"lastNewTLKOperation", NSString);
378 NSString* lastOutgoingQueueOperation = pop(status,@"lastOutgoingQueueOperation", NSString);
379 NSString* lastProcessReceivedKeysOperation = pop(status,@"lastProcessReceivedKeysOperation", NSString);
380 NSString* lastReencryptOutgoingItemsOperation = pop(status,@"lastReencryptOutgoingItemsOperation", NSString);
382 printf("================================================================================\n\n");
384 printf("View: %s\n\n", [viewName UTF8String]);
386 if(statusError != nil) {
387 printf("ERROR FETCHING STATUS: %s\n\n", [statusError UTF8String]);
390 printf("CloudKit account: %s\n", [accountStatus UTF8String]);
391 printf("Account tracker: %s\n", [accountTracker UTF8String]);
393 if(!([zoneCreated isEqualToString:@"yes"] && [zoneSubscribed isEqualToString:@"yes"])) {
394 printf("CK Zone Created: %s\n", [[zoneCreated description] UTF8String]);
395 printf("CK Zone Subscribed: %s\n", [[zoneSubscribed description] UTF8String]);
396 printf("CK Zone initialize retry: %s\n", [[zoneInitializeScheduler description] UTF8String]);
400 printf("Key state: %s\n", [keystate UTF8String]);
401 printf("Current TLK: %s\n", currentTLK != nil
402 ? [currentTLK UTF8String]
403 : [[NSString stringWithFormat:@"missing; pointer is %@", currentTLKPtr] UTF8String]);
404 printf("Current ClassA: %s\n", currentClassA != nil
405 ? [currentClassA UTF8String]
406 : [[NSString stringWithFormat:@"missing; pointer is %@", currentClassAPtr] UTF8String]);
407 printf("Current ClassC: %s\n", currentClassC != nil
408 ? [currentClassC UTF8String]
409 : [[NSString stringWithFormat:@"missing; pointer is %@", currentClassCPtr] UTF8String]);
411 printf("TLK shares: %s\n", [[tlkshares description] UTF8String]);
413 printf("Item syncing: %s\n", [[itemSyncEnabled description] UTF8String]);
414 printf("Outgoing Queue counts: %s\n", [[oqe description] UTF8String]);
415 printf("Incoming Queue counts: %s\n", [[iqe description] UTF8String]);
416 printf("Key counts: %s\n", [[keys description] UTF8String]);
418 printf("Item counts (by key): %s\n", [[ckmirror description] UTF8String]);
419 printf("Peer states: %s\n", [[devicestates description] UTF8String]);
421 printf("zone change fetcher: %s\n", [[fetcher description] UTF8String]);
422 printf("lastIncomingQueueOperation: %s\n", lastIncomingQueueOperation == nil ? "never" : [lastIncomingQueueOperation UTF8String]);
423 printf("lastNewTLKOperation: %s\n", lastNewTLKOperation == nil ? "never" : [lastNewTLKOperation UTF8String]);
424 printf("lastOutgoingQueueOperation: %s\n", lastOutgoingQueueOperation == nil ? "never" : [lastOutgoingQueueOperation UTF8String]);
425 printf("lastProcessReceivedKeysOperation: %s\n", lastProcessReceivedKeysOperation == nil ? "never" : [lastProcessReceivedKeysOperation UTF8String]);
426 printf("lastReencryptOutgoingItemsOperation: %s\n", lastReencryptOutgoingItemsOperation == nil ? "never" : [lastReencryptOutgoingItemsOperation UTF8String]);
428 printf("Launch sequence:\n");
429 for (NSString *event in launchSequence) {
430 printf("\t%s\n", [[event description] UTF8String]);
433 if(status.allKeys.count > 0u) {
434 printf("\nExtra information: %s\n", [[status description] UTF8String]);
438 dispatch_semaphore_signal(sema);
441 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 30)) != 0) {
442 printf("\n\nError: timed out waiting for response\n");
447 - (long)fetch:(NSString*)view {
448 __block long ret = 0;
450 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
452 [self.control rpcFetchAndProcessChanges:view reply:^(NSError* error) {
454 printf("Error fetching: %s\n", [[error description] UTF8String]);
455 ret = (error.code == 0 ? -1 : error.code);
457 printf("Complete.\n");
461 dispatch_semaphore_signal(sema);
464 // The maximum device-side delay to start a fetch is 120s, so we must wait longer than that for a response.
465 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 135)) != 0) {
466 printf("\n\nError: timed out waiting for response\n");
473 - (long)push:(NSString*)view {
474 __block long ret = 0;
476 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
478 [self.control rpcPushOutgoingChanges:view reply:^(NSError* error) {
480 printf("Error pushing: %s\n", [[error description] UTF8String]);
481 ret = (error.code == 0 ? -1 : error.code);
483 printf("Complete.\n");
486 dispatch_semaphore_signal(sema);
489 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 65)) != 0) {
490 printf("\n\nError: timed out waiting for response\n");
499 __block long ret = 0;
501 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
503 [self.control rpcCKMetric:@"testMetric" attributes:@{ @"testAttribute" : @"value" } reply:^(NSError* error) {
505 printf("Error sending metric: %s\n", [[error description] UTF8String]);
506 ret = (error.code == 0 ? -1 : error.code);
508 printf("Complete.\n");
511 dispatch_semaphore_signal(sema);
514 if(dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 65)) != 0) {
515 printf("\n\nError: timed out waiting for response\n");
526 static int perfCounters = false;
527 static int status = false;
528 static int resync = false;
529 static int reset = false;
530 static int resetCloudKit = false;
531 static int fetch = false;
532 static int push = false;
533 static int json = false;
534 static int shortOutput = false;
535 static int ckmetric = false;
537 static char* viewArg = NULL;
539 int main(int argc, char **argv)
541 static struct argument options[] = {
542 { .shortname='p', .longname="perfcounters", .flag=&perfCounters, .flagval=true, .description="Print CKKS performance counters"},
543 { .shortname='j', .longname="json", .flag=&json, .flagval=true, .description="Output in JSON format"},
544 { .shortname='s', .longname="short", .flag=&shortOutput, .flagval=true, .description="Output a short format"},
545 { .shortname='v', .longname="view", .argument=&viewArg, .description="Operate on a single view"},
547 { .command="status", .flag=&status, .flagval=true, .description="Report status on CKKS views"},
548 { .command="fetch", .flag=&fetch, .flagval=true, .description="Fetch all new changes in CloudKit and attempt to process them"},
549 { .command="push", .flag=&push, .flagval=true, .description="Push all pending local changes to CloudKit"},
550 { .command="resync", .flag=&resync, .flagval=true, .description="Resync all data with what's in CloudKit"},
551 { .command="reset", .flag=&reset, .flagval=true, .description="All local data will be wiped, and data refetched from CloudKit"},
552 { .command="reset-cloudkit", .flag=&resetCloudKit, .flagval=true, .description="All data in CloudKit will be removed and replaced with what's local"},
553 { .command="ckmetric", .flag=&ckmetric, .flagval=true, .description="Push CloudKit metric"},
557 static struct arguments args = {
558 .programname="ckksctl",
559 .description="Control and report on CKKS",
560 .arguments = options,
563 if(!options_parse(argc, argv, &args)) {
570 NSError* error = nil;
572 CKKSControl* rpc = [CKKSControl CKKSControlObject:false error:&error];
574 errx(1, "no CKKSControl failed: %s", [[error description] UTF8String]);
577 CKKSControlCLI* ctl = [[CKKSControlCLI alloc] initWithCKKSControl:rpc];
579 NSString* view = viewArg ? [NSString stringWithCString: viewArg encoding: NSUTF8StringEncoding] : nil;
582 // Complicated logic, but you can choose any combination of (json, perfcounters) that you like.
583 NSMutableDictionary *statusDict = [[NSMutableDictionary alloc] init];
585 statusDict[@"performance"] = [ctl fetchPerformanceCounters];
588 statusDict[@"status"] = [ctl fetchStatus:view];
590 if(json || perfCounters) {
591 print_result(statusDict, true);
596 [ctl printHumanReadableStatus:view shortenOutput:shortOutput];
599 } else if(perfCounters) {
600 NSMutableDictionary *statusDict = [[NSMutableDictionary alloc] init];
601 statusDict[@"performance"] = [ctl fetchPerformanceCounters];
602 print_result(statusDict, false);
605 return (int)[ctl fetch:view];
607 return (int)[ctl push:view];
609 return (int)[ctl resetLocal:view];
610 } else if(resetCloudKit) {
611 return (int)[ctl resetCloudKit:view];
613 return (int)[ctl resync:view];
614 } else if(ckmetric) {
615 return (int)[ctl ckmetric];