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 "keychain/ckks/CKKSKeychainView.h"
25 #import "keychain/ckks/CKKSGroupOperation.h"
26 #import "keychain/ckks/CKKSLocalSynchronizeOperation.h"
27 #import "keychain/ckks/CKKSFetchAllRecordZoneChangesOperation.h"
28 #import "keychain/ckks/CKKSScanLocalItemsOperation.h"
29 #import "keychain/ckks/CKKSMirrorEntry.h"
30 #import "keychain/ckks/CKKSIncomingQueueEntry.h"
31 #import "keychain/ckks/CloudKitCategories.h"
32 #import "keychain/categories/NSError+UsefulConstructors.h"
36 @interface CKKSLocalSynchronizeOperation ()
37 @property int32_t restartCount;
40 @implementation CKKSLocalSynchronizeOperation
42 - (instancetype)init {
45 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)ckks
47 if(self = [super init]) {
51 [self addNullableDependency:ckks.holdLocalSynchronizeOperation];
57 __weak __typeof(self) weakSelf = self;
60 * A local synchronize is very similar to a CloudKit synchronize, but it won't cause any (non-essential)
61 * CloudKit operations to occur.
63 * 1. Finish processing the outgoing queue. You can't be in-sync with cloudkit if you have an update that hasn't propagated.
64 * 2. Process anything in the incoming queue as normal.
65 * (Note that this might require the keybag to be unlocked.)
67 * 3. Take every item in the CKMirror, and check for its existence in the local keychain. If not present, add to the incoming queue.
68 * 4. Process the incoming queue again.
69 * 5. Scan the local keychain for items which exist locally but are not in CloudKit. Upload them.
70 * 6. If there are any such items in 4, restart the sync.
73 CKKSKeychainView* ckks = self.ckks;
75 // Synchronous, on some thread. Get back on the CKKS queue for SQL thread-safety.
76 [ckks dispatchSync: ^bool{
78 ckksnotice("ckksresync", ckks, "CKKSSynchronizeOperation cancelled, quitting");
82 //ckks.lastLocalSynchronizeOperation = self;
86 ckksinfo("ckksresync", ckks, "Beginning local resynchronize (attempt %u)", self.restartCount);
88 CKOperationGroup* operationGroup = [CKOperationGroup CKKSGroupWithName:@"ckks-resync-local"];
91 CKKSOutgoingQueueOperation* outgoingOp = [ckks processOutgoingQueue: operationGroup];
92 outgoingOp.name = [NSString stringWithFormat: @"resync-step%u-outgoing", self.restartCount * steps + 1];
93 [self dependOnBeforeGroupFinished:outgoingOp];
96 CKKSIncomingQueueOperation* incomingOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
97 incomingOp.name = [NSString stringWithFormat: @"resync-step%u-incoming", self.restartCount * steps + 2];
98 [incomingOp addSuccessDependency:outgoingOp];
99 [self runBeforeGroupFinished:incomingOp];
102 CKKSResultOperation* reloadOp = [[CKKSReloadAllItemsOperation alloc] initWithCKKSKeychainView:ckks];
103 reloadOp.name = [NSString stringWithFormat: @"resync-step%u-reload", self.restartCount * steps + 3];
104 [self runBeforeGroupFinished:reloadOp];
107 CKKSIncomingQueueOperation* incomingResyncOp = [[CKKSIncomingQueueOperation alloc] initWithCKKSKeychainView:ckks errorOnClassAFailure:true];
108 incomingResyncOp.name = [NSString stringWithFormat: @"resync-step%u-incoming-again", self.restartCount * steps + 4];
109 [incomingResyncOp addSuccessDependency: reloadOp];
110 [self runBeforeGroupFinished:incomingResyncOp];
113 CKKSScanLocalItemsOperation* scan = [[CKKSScanLocalItemsOperation alloc] initWithCKKSKeychainView:ckks ckoperationGroup:operationGroup];
114 scan.name = [NSString stringWithFormat: @"resync-step%u-scan", self.restartCount * steps + 5];
115 [scan addSuccessDependency: incomingResyncOp];
116 [self runBeforeGroupFinished: scan];
119 CKKSResultOperation* restart = [[CKKSResultOperation alloc] init];
120 restart.name = [NSString stringWithFormat: @"resync-step%u-consider-restart", self.restartCount * steps + 6];
121 [restart addExecutionBlock:^{
122 __strong __typeof(weakSelf) strongSelf = weakSelf;
124 secerror("ckksresync: received callback for released object");
128 NSError* error = nil;
129 NSArray<NSString*>* iqes = [CKKSIncomingQueueEntry allUUIDs:ckks.zoneID error:&error];
131 ckkserror("ckksresync", ckks, "Couldn't fetch IQEs: %@", error);
134 if(scan.recordsFound > 0 || iqes.count > 0) {
135 if(strongSelf.restartCount >= 3) {
136 // we've restarted too many times. Fail and stop.
137 ckkserror("ckksresync", ckks, "restarted synchronization too often; Failing");
138 strongSelf.error = [NSError errorWithDomain:@"securityd"
140 userInfo:@{NSLocalizedDescriptionKey: @"resynchronization restarted too many times; churn in database?"}];
142 // restart the sync operation.
143 strongSelf.restartCount += 1;
144 ckkserror("ckksresync", ckks, "restarting synchronization operation due to new local items");
145 [strongSelf groupStart];
150 [restart addSuccessDependency: scan];
151 [self runBeforeGroupFinished: restart];
159 #pragma mark - CKKSReloadAllItemsOperation
161 @implementation CKKSReloadAllItemsOperation
163 - (instancetype)init {
166 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)ckks
168 if(self = [super init]) {
175 CKKSKeychainView* strongCKKS = self.ckks;
177 [strongCKKS dispatchSync: ^bool{
178 NSError* error = nil;
179 NSArray<CKKSMirrorEntry*>* mirrorItems = [CKKSMirrorEntry all:strongCKKS.zoneID error:&error];
182 ckkserror("ckksresync", strongCKKS, "Couldn't fetch mirror items: %@", error);
187 // Reload all entries back into the local keychain
188 // We _could_ scan for entries, but that'd be expensive
189 // In 36044942, we used to store only the CKRecord system fields in the ckrecord. To work around this, make a whole new CKRecord from the item.
190 for(CKKSMirrorEntry* ckme in mirrorItems) {
191 CKRecord* ckmeRecord = [ckme.item CKRecordWithZoneID:strongCKKS.zoneID];
193 ckkserror("ckksresync", strongCKKS, "Couldn't make CKRecord for item: %@", ckme);
197 [strongCKKS _onqueueCKRecordChanged:ckmeRecord resync:true];