2 * Copyright (c) 2013-2014 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 #import "KDSecCircle.h"
26 #import "KDCirclePeer.h"
28 #include <dispatch/dispatch.h>
30 #import <Security/SecureObjectSync/SOSCloudCircle.h>
31 #import <Security/SecureObjectSync/SOSPeerInfo.h>
33 #import <CloudServices/SecureBackup.h>
35 #include <utilities/debugging.h>
37 @interface KDSecCircle ()
38 @property (retain) NSMutableArray *callbacks;
40 @property (readwrite) unsigned long long changeCount;
42 @property (readwrite) SOSCCStatus rawStatus;
44 @property (readwrite) NSString *status;
45 @property (readwrite) NSError *error;
47 @property (readwrite) NSArray *peers;
48 @property (readwrite) NSArray *applicants;
50 @property (readwrite) dispatch_queue_t queue_;
54 @implementation KDSecCircle
58 // XXX: assert not on main_queue
59 CFErrorRef err = NULL;
61 SOSCCValidateUserPublic(NULL); // requires the account queue - makes the rest of this wait for fresh info. This used to happen in SOSCCThisDeviceIsInCircle(below) before we made it use cached info.
62 SOSCCStatus newRawStatus = SOSCCThisDeviceIsInCircle(&err);
63 NSArray *peerInfos = (__bridge NSArray *) SOSCCCopyApplicantPeerInfo(&err);
64 NSMutableArray *newApplicants = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
65 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
66 [newApplicants addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
69 peerInfos = (__bridge NSArray *) SOSCCCopyPeerPeerInfo(&err);
70 NSMutableArray *newPeers = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
71 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
72 [newPeers addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
75 secdebug("kcn", "rawStatus %d, #applicants %lu, #peers %lu, err=%@", newRawStatus, (unsigned long)[newApplicants count], (unsigned long)[newPeers count], err);
77 dispatch_async(dispatch_get_main_queue(), ^{
78 self.rawStatus = newRawStatus;
80 switch (newRawStatus) {
82 self.status = @"In Circle";
85 case kSOSCCNotInCircle:
86 self.status = @"Not In Circle";
89 case kSOSCCRequestPending:
90 self.status = @"Request Pending";
93 case kSOSCCCircleAbsent:
94 self.status = @"Circle Absent";
98 self.status = [NSString stringWithFormat:@"Error: %@", err];
102 self.status = [NSString stringWithFormat:@"Unknown status code %d", self.rawStatus];
106 self.applicants = [newApplicants copy];
107 self.peers = [newPeers copy];
108 self.error = (__bridge NSError *)(err);
111 for (dispatch_block_t callback in self.callbacks) {
117 // XXX It's a botch to use the "name" and not applicant, but
118 // it is hard to get anythign else to survive a serialastion
119 // trip thoguth NSUserNotificationCenter.
121 // Er, now that I look more closely maybe SOSPeerInfoGetPeerID...
123 typedef void (^applicantBlock)(id applicant);
125 -(void)forApplicantId:(NSString*)applicantId run:(applicantBlock)applicantBlock
127 dispatch_async(self.queue_, ^{
128 for (KDCirclePeer *applicant in self.applicants) {
129 if ([applicantId isEqualToString:applicantId]) {
130 applicantBlock(applicant.peerObject);
137 // Tell clang that these bools are okay, even if NSAssert doesn't use them
138 #pragma clang diagnostic push
139 #pragma clang diagnostic ignored "-Wunused-variable"
141 -(void)acceptApplicantId:(NSString*)applicantId
143 [self forApplicantId:applicantId run:^void(id applicant) {
144 CFErrorRef err = NULL;
145 bool ok = SOSCCAcceptApplicants((__bridge CFArrayRef)(@[applicant]), &err);
146 NSAssert(ok, @"Error %@ while accepting %@ (%@)", err, applicantId, applicant);
150 -(void)rejectApplicantId:(NSString*)applicantId
152 [self forApplicantId:applicantId run:^void(id applicant) {
153 CFErrorRef err = NULL;
154 bool ok = SOSCCRejectApplicants((__bridge CFArrayRef)(@[applicant]), &err);
155 NSAssert(ok, @"Error %@ while rejecting %@ (%@)", err, applicantId, applicant);
159 #pragma clang diagnostic pop
166 self->_queue_ = dispatch_queue_create([[NSString stringWithFormat:@"KDSecCircle@%p", self] UTF8String], NULL);
167 self->_callbacks = [NSMutableArray new];
168 notify_register_dispatch(kSOSCCCircleChangedNotification, &token, self.queue_, ^(int token1){
174 -(void)addChangeCallback:(dispatch_block_t)callback
176 [self.callbacks addObject:callback];
177 if (self.changeCount) {
178 dispatch_async(dispatch_get_main_queue(), callback);
179 } else if (self.callbacks.count == 1) {
180 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
188 return (self.rawStatus == kSOSCCInCircle);
193 return (self.rawStatus == kSOSCCNotInCircle || self.rawStatus == kSOSCCCircleAbsent);
198 CFErrorRef err = NULL;
199 if (self.rawStatus == kSOSCCCircleAbsent) {
200 SOSCCResetToOffering(&err);
202 SOSCCRequestToJoinCircle(&err);
205 CFMutableSetRef viewsToEnable = CFSetCreateMutable(NULL, 0, NULL);
206 CFMutableSetRef viewsToDisable = CFSetCreateMutable(NULL, 0, NULL);
207 CFSetAddValue(viewsToEnable, (void*)kSOSViewWiFi);
208 CFSetAddValue(viewsToEnable, (void*)kSOSViewAutofillPasswords);
209 CFSetAddValue(viewsToEnable, (void*)kSOSViewSafariCreditCards);
210 CFSetAddValue(viewsToEnable, (void*)kSOSViewOtherSyncable);
212 SOSCCViewSet(viewsToEnable, viewsToDisable);
213 CFRelease(viewsToEnable);
214 CFRelease(viewsToDisable);
219 CFErrorRef err = NULL;
220 SOSCCRemoveThisDeviceFromCircle(&err);