2 * Copyright (c) 2016 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@
24 #import "CKKSKeychainView.h"
25 #import "CKKSGroupOperation.h"
26 #import "CKKSSynchronizeOperation.h"
27 #import "CKKSFetchAllRecordZoneChangesOperation.h"
28 #import "CKKSScanLocalItemsOperation.h"
29 #import "keychain/ckks/CloudKitCategories.h"
33 @interface CKKSSynchronizeOperation ()
34 @property int32_t restartCount;
37 @implementation CKKSSynchronizeOperation
39 - (instancetype)init {
42 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)ckks {
43 if(self = [super init]) {
51 __weak __typeof(self) weakSelf = self;
54 * Synchronizations (or resynchronizations) are complicated beasts. We will:
56 * 1. Finish processing the outgoing queue. You can't be in-sync with cloudkit if you have an update that hasn't propagated.
57 * 2. Kick off a normal CloudKit fetch.
58 * 3. Process the incoming queue as normal.
59 * (Note that this requires the keybag to be unlocked.)
61 * So far, this is normal operation. Now:
63 * 4. Start another CloudKit fetch, giving it the nil change tag. This fetches all objects in CloudKit.
64 * 4a. Compare those objects against our local mirror. If any discrepancies, this is a bug.
65 * 4b. All conflicts: CloudKit data wins.
66 * 4c. All items we have that CloudKit doesn't: delete locally.
68 * 5. Process the incoming queue again. This should be empty.
69 * 6. Scan the local keychain for items which exist locally but are not in CloudKit. Upload them.
70 * 7. If there are any such items in 6, restart the sync.
73 // Promote to strong reference
74 CKKSKeychainView* ckks = self.ckks;
76 // Synchronous, on some thread. Get back on the CKKS queue for SQL thread-safety.
77 [ckks dispatchSync: ^bool{
79 ckksnotice("ckksresync", ckks, "CKKSSynchronizeOperation cancelled, quitting");
83 ckks.lastSynchronizeOperation = self;
87 ckksinfo("ckksresync", ckks, "Beginning resynchronize (attempt %u)", self.restartCount);
89 CKOperationGroup* operationGroup = [CKOperationGroup CKKSGroupWithName:@"ckks-resync"];
92 CKKSOutgoingQueueOperation* outgoingOp = [ckks processOutgoingQueue: operationGroup];
93 outgoingOp.name = [NSString stringWithFormat: @"resync-step%u-outgoing", self.restartCount * steps + 1];
94 [self dependOnBeforeGroupFinished:outgoingOp];
97 CKKSFetchAllRecordZoneChangesOperation* fetchOp = [[CKKSFetchAllRecordZoneChangesOperation alloc] initWithCKKSKeychainView:ckks ckoperationGroup:operationGroup];
98 fetchOp.name = [NSString stringWithFormat: @"resync-step%u-fetch", self.restartCount * steps + 2];
99 [fetchOp addSuccessDependency: outgoingOp];
100 [self runBeforeGroupFinished: fetchOp];
103 CKKSIncomingQueueOperation* incomingOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
104 incomingOp.name = [NSString stringWithFormat: @"resync-step%u-incoming", self.restartCount * steps + 3];
105 [incomingOp addSuccessDependency:fetchOp];
106 [self runBeforeGroupFinished:incomingOp];
111 CKKSFetchAllRecordZoneChangesOperation* fetchAllOp = [[CKKSFetchAllRecordZoneChangesOperation alloc] initWithCKKSKeychainView:ckks ckoperationGroup:operationGroup];
112 fetchAllOp.resync = true;
113 fetchAllOp.name = [NSString stringWithFormat: @"resync-step%u-fetchAll", self.restartCount * steps + 4];
114 [fetchAllOp addSuccessDependency: incomingOp];
115 [self runBeforeGroupFinished: fetchAllOp];
118 CKKSIncomingQueueOperation* incomingResyncOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
119 incomingResyncOp.name = [NSString stringWithFormat: @"resync-step%u-incoming", self.restartCount * steps + 5];
120 [incomingResyncOp addSuccessDependency: fetchAllOp];
121 [self runBeforeGroupFinished:incomingResyncOp];
124 CKKSScanLocalItemsOperation* scan = [[CKKSScanLocalItemsOperation alloc] initWithCKKSKeychainView:ckks ckoperationGroup:operationGroup];
125 scan.name = [NSString stringWithFormat: @"resync-step%u-scan", self.restartCount * steps + 6];
126 [scan addSuccessDependency: incomingResyncOp];
127 [self runBeforeGroupFinished: scan];
130 CKKSResultOperation* restart = [[CKKSResultOperation alloc] init];
131 restart.name = [NSString stringWithFormat: @"resync-step%u-consider-restart", self.restartCount * steps + 7];
132 [restart addExecutionBlock:^{
133 __strong __typeof(weakSelf) strongSelf = weakSelf;
135 secerror("ckksresync: received callback for released object");
139 if(scan.recordsFound > 0) {
140 if(strongSelf.restartCount >= 3) {
141 // we've restarted too many times. Fail and stop.
142 ckkserror("ckksresync", ckks, "restarted synchronization too often; Failing");
143 strongSelf.error = [NSError errorWithDomain:@"securityd"
145 userInfo:@{NSLocalizedDescriptionKey: @"resynchronization restarted too many times; churn in database?"}];
147 // restart the sync operation.
148 strongSelf.restartCount += 1;
149 ckkserror("ckksresync", ckks, "restarting synchronization operation due to new local items");
150 [strongSelf groupStart];
155 [restart addSuccessDependency: scan];
156 [self runBeforeGroupFinished: restart];