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;
60 SOSCCStatus newRawStatus = SOSCCThisDeviceIsInCircle(&err);
61 NSArray *peerInfos = (__bridge NSArray *) SOSCCCopyApplicantPeerInfo(&err);
62 NSMutableArray *newApplicants = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
63 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
64 [newApplicants addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
67 peerInfos = (__bridge NSArray *) SOSCCCopyPeerPeerInfo(&err);
68 NSMutableArray *newPeers = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
69 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
70 [newPeers addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
73 secdebug("kcn", "rawStatus %d, #applicants %lu, #peers %lu, err=%@", newRawStatus, (unsigned long)[newApplicants count], (unsigned long)[newPeers count], err);
75 dispatch_async(dispatch_get_main_queue(), ^{
76 self.rawStatus = newRawStatus;
78 switch (newRawStatus) {
80 self.status = @"In Circle";
83 case kSOSCCNotInCircle:
84 self.status = @"Not In Circle";
87 case kSOSCCRequestPending:
88 self.status = @"Request Pending";
91 case kSOSCCCircleAbsent:
92 self.status = @"Circle Absent";
96 self.status = [NSString stringWithFormat:@"Error: %@", err];
100 self.status = [NSString stringWithFormat:@"Unknown status code %d", self.rawStatus];
104 self.applicants = [newApplicants copy];
105 self.peers = [newPeers copy];
106 self.error = (__bridge NSError *)(err);
109 for (dispatch_block_t callback in self.callbacks) {
115 // XXX It's a botch to use the "name" and not applicant, but
116 // it is hard to get anythign else to survive a serialastion
117 // trip thoguth NSUserNotificationCenter.
119 // Er, now that I look more closely maybe SOSPeerInfoGetPeerID...
121 typedef void (^applicantBlock)(id applicant);
123 -(void)forApplicantId:(NSString*)applicantId run:(applicantBlock)applicantBlock
125 dispatch_async(self.queue_, ^{
126 for (KDCirclePeer *applicant in self.applicants) {
127 if ([applicantId isEqualToString:applicantId]) {
128 applicantBlock(applicant.peerObject);
135 // Tell clang that these bools are okay, even if NSAssert doesn't use them
136 #pragma clang diagnostic push
137 #pragma clang diagnostic ignored "-Wunused-variable"
139 -(void)acceptApplicantId:(NSString*)applicantId
141 [self forApplicantId:applicantId run:^void(id applicant) {
142 CFErrorRef err = NULL;
143 bool ok = SOSCCAcceptApplicants((__bridge CFArrayRef)(@[applicant]), &err);
144 NSAssert(ok, @"Error %@ while accepting %@ (%@)", err, applicantId, applicant);
148 -(void)rejectApplicantId:(NSString*)applicantId
150 [self forApplicantId:applicantId run:^void(id applicant) {
151 CFErrorRef err = NULL;
152 bool ok = SOSCCRejectApplicants((__bridge CFArrayRef)(@[applicant]), &err);
153 NSAssert(ok, @"Error %@ while rejecting %@ (%@)", err, applicantId, applicant);
157 #pragma clang diagnostic pop
164 self->_queue_ = dispatch_queue_create([[NSString stringWithFormat:@"KDSecCircle@%p", self] UTF8String], NULL);
165 self->_callbacks = [NSMutableArray new];
166 notify_register_dispatch(kSOSCCCircleChangedNotification, &token, self.queue_, ^(int token1){
172 -(void)addChangeCallback:(dispatch_block_t)callback
174 [self.callbacks addObject:callback];
175 if (self.changeCount) {
176 dispatch_async(dispatch_get_main_queue(), callback);
177 } else if (self.callbacks.count == 1) {
178 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
186 return (self.rawStatus == kSOSCCInCircle);
191 return (self.rawStatus == kSOSCCNotInCircle || self.rawStatus == kSOSCCCircleAbsent);
196 CFErrorRef err = NULL;
197 if (self.rawStatus == kSOSCCCircleAbsent) {
198 SOSCCResetToOffering(&err);
200 SOSCCRequestToJoinCircle(&err);
203 CFMutableSetRef viewsToEnable = CFSetCreateMutable(NULL, 0, NULL);
204 CFMutableSetRef viewsToDisable = CFSetCreateMutable(NULL, 0, NULL);
205 CFSetAddValue(viewsToEnable, (void*)kSOSViewWiFi);
206 CFSetAddValue(viewsToEnable, (void*)kSOSViewAutofillPasswords);
207 CFSetAddValue(viewsToEnable, (void*)kSOSViewSafariCreditCards);
208 CFSetAddValue(viewsToEnable, (void*)kSOSViewOtherSyncable);
210 SOSCCViewSet(viewsToEnable, viewsToDisable);
211 CFRelease(viewsToEnable);
212 CFRelease(viewsToDisable);
217 CFErrorRef err = NULL;
218 SOSCCRemoveThisDeviceFromCircle(&err);