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
98 fetchReasons:[NSSet setWithObject:CKKSFetchBecauseResync]
99 ckoperationGroup:operationGroup];
100 fetchOp.name = [NSString stringWithFormat: @"resync-step%u-fetch", self.restartCount * steps + 2];
101 [fetchOp addSuccessDependency: outgoingOp];
102 [self runBeforeGroupFinished: fetchOp];
105 CKKSIncomingQueueOperation* incomingOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
106 incomingOp.name = [NSString stringWithFormat: @"resync-step%u-incoming", self.restartCount * steps + 3];
107 [incomingOp addSuccessDependency:fetchOp];
108 [self runBeforeGroupFinished:incomingOp];
113 CKKSFetchAllRecordZoneChangesOperation* fetchAllOp = [[CKKSFetchAllRecordZoneChangesOperation alloc] initWithCKKSKeychainView:ckks
114 fetchReasons:[NSSet setWithObject:CKKSFetchBecauseResync]
115 ckoperationGroup:operationGroup];
116 fetchAllOp.resync = true;
117 fetchAllOp.name = [NSString stringWithFormat: @"resync-step%u-fetchAll", self.restartCount * steps + 4];
118 [fetchAllOp addSuccessDependency: incomingOp];
119 [self runBeforeGroupFinished: fetchAllOp];
122 CKKSIncomingQueueOperation* incomingResyncOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
123 incomingResyncOp.name = [NSString stringWithFormat: @"resync-step%u-incoming", self.restartCount * steps + 5];
124 [incomingResyncOp addSuccessDependency: fetchAllOp];
125 [self runBeforeGroupFinished:incomingResyncOp];
128 CKKSScanLocalItemsOperation* scan = [[CKKSScanLocalItemsOperation alloc] initWithCKKSKeychainView:ckks ckoperationGroup:operationGroup];
129 scan.name = [NSString stringWithFormat: @"resync-step%u-scan", self.restartCount * steps + 6];
130 [scan addSuccessDependency: incomingResyncOp];
131 [self runBeforeGroupFinished: scan];
134 CKKSResultOperation* restart = [[CKKSResultOperation alloc] init];
135 restart.name = [NSString stringWithFormat: @"resync-step%u-consider-restart", self.restartCount * steps + 7];
136 [restart addExecutionBlock:^{
137 __strong __typeof(weakSelf) strongSelf = weakSelf;
139 secerror("ckksresync: received callback for released object");
143 if(scan.recordsFound > 0) {
144 if(strongSelf.restartCount >= 3) {
145 // we've restarted too many times. Fail and stop.
146 ckkserror("ckksresync", ckks, "restarted synchronization too often; Failing");
147 strongSelf.error = [NSError errorWithDomain:@"securityd"
149 userInfo:@{NSLocalizedDescriptionKey: @"resynchronization restarted too many times; churn in database?"}];
151 // restart the sync operation.
152 strongSelf.restartCount += 1;
153 ckkserror("ckksresync", ckks, "restarting synchronization operation due to new local items");
154 [strongSelf groupStart];
159 [restart addSuccessDependency: scan];
160 [self runBeforeGroupFinished: restart];