]> git.saurik.com Git - apple/security.git/blob - OSX/Keychain/KDSecCircle.m
c053ee2f839197078b573154d969c234be5d947c
[apple/security.git] / OSX / Keychain / KDSecCircle.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #import "KDSecCircle.h"
26 #import "KDCirclePeer.h"
27 #include <notify.h>
28 #include <dispatch/dispatch.h>
29 #import "SecureObjectSync/SOSCloudCircle.h"
30 #include "SecureObjectSync/SOSPeerInfo.h"
31 #import <CloudServices/SecureBackup.h>
32 #include "../utilities/utilities/debugging.h"
33
34 @interface KDSecCircle ()
35 @property (retain) NSMutableArray *callbacks;
36
37 @property (readwrite) unsigned long long changeCount;
38
39 @property (readwrite) SOSCCStatus rawStatus;
40
41 @property (readwrite) NSString *status;
42 @property (readwrite) NSError *error;
43
44 @property (readwrite) NSArray *peers;
45 @property (readwrite) NSArray *applicants;
46
47 @property (readwrite) dispatch_queue_t queue_;
48
49 @end
50
51 @implementation KDSecCircle
52
53 -(void)updateCheck
54 {
55 // XXX: assert not on main_queue
56 CFErrorRef err = NULL;
57 SOSCCStatus newRawStatus = SOSCCThisDeviceIsInCircle(&err);
58 NSArray *peerInfos = (__bridge NSArray *) SOSCCCopyApplicantPeerInfo(&err);
59 NSMutableArray *newApplicants = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
60 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
61 [newApplicants addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
62 }];
63
64 peerInfos = (__bridge NSArray *) SOSCCCopyPeerPeerInfo(&err);
65 NSMutableArray *newPeers = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
66 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
67 [newPeers addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
68 }];
69
70 NSLog(@"rawStatus %d, #applicants %lu, #peers %lu, err=%@", newRawStatus, (unsigned long)[newApplicants count], (unsigned long)[newPeers count], err);
71
72 dispatch_async(dispatch_get_main_queue(), ^{
73 self.rawStatus = newRawStatus;
74
75 switch (newRawStatus) {
76 case kSOSCCInCircle:
77 self.status = @"In Circle";
78 break;
79
80 case kSOSCCNotInCircle:
81 self.status = @"Not In Circle";
82 break;
83
84 case kSOSCCRequestPending:
85 self.status = @"Request Pending";
86 break;
87
88 case kSOSCCCircleAbsent:
89 self.status = @"Circle Absent";
90 break;
91
92 case kSOSCCError:
93 self.status = [NSString stringWithFormat:@"Error: %@", err];
94 break;
95
96 default:
97 self.status = [NSString stringWithFormat:@"Unknown status code %d", self.rawStatus];
98 break;
99 }
100
101 self.applicants = [newApplicants copy];
102 self.peers = [newPeers copy];
103 self.error = (__bridge NSError *)(err);
104
105 self.changeCount++;
106 for (dispatch_block_t callback in self.callbacks) {
107 callback();
108 }
109 });
110 }
111
112 // XXX It's a botch to use the "name" and not applicant, but
113 // it is hard to get anythign else to survive a serialastion
114 // trip thoguth NSUserNotificationCenter.
115 //
116 // Er, now that I look more closely maybe SOSPeerInfoGetPeerID...
117
118 typedef void (^applicantBlock)(id applicant);
119
120 -(void)forApplicantId:(NSString*)applicantId run:(applicantBlock)applicantBlock
121 {
122 dispatch_async(self.queue_, ^{
123 for (KDCirclePeer *applicant in self.applicants) {
124 if ([applicantId isEqualToString:applicantId]) {
125 applicantBlock(applicant.peerObject);
126 break;
127 }
128 }
129 });
130 }
131
132 -(void)acceptApplicantId:(NSString*)applicantId
133 {
134 [self forApplicantId:applicantId run:^void(id applicant) {
135 CFErrorRef err = NULL;
136 bool ok = SOSCCAcceptApplicants((__bridge CFArrayRef)(@[applicant]), &err);
137 NSAssert(ok, @"Error %@ while accepting %@ (%@)", err, applicantId, applicant);
138 }];
139 }
140
141 -(void)rejectApplicantId:(NSString*)applicantId
142 {
143 [self forApplicantId:applicantId run:^void(id applicant) {
144 CFErrorRef err = NULL;
145 bool ok = SOSCCRejectApplicants((__bridge CFArrayRef)(@[applicant]), &err);
146 NSAssert(ok, @"Error %@ while rejecting %@ (%@)", err, applicantId, applicant);
147 }];
148 }
149
150 -(id)init
151 {
152 self = [super init];
153 int token;
154
155 self->_queue_ = dispatch_queue_create([[NSString stringWithFormat:@"KDSecCircle@%p", self] UTF8String], NULL);
156 self->_callbacks = [NSMutableArray new];
157 notify_register_dispatch(kSOSCCCircleChangedNotification, &token, self.queue_, ^(int token){
158 [self updateCheck];
159 });
160
161 return self;
162 }
163
164 -(void)addChangeCallback:(dispatch_block_t)callback
165 {
166 [self.callbacks addObject:callback];
167 if (self.changeCount) {
168 dispatch_async(dispatch_get_main_queue(), callback);
169 } else if (self.callbacks.count == 1) {
170 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
171 [self updateCheck];
172 });
173 }
174 }
175
176 -(BOOL)isInCircle
177 {
178 return (self.rawStatus == kSOSCCInCircle);
179 }
180
181 -(BOOL)isOutOfCircle
182 {
183 return (self.rawStatus == kSOSCCNotInCircle || self.rawStatus == kSOSCCCircleAbsent);
184 }
185
186 -(void)enableSync
187 {
188 CFErrorRef err = NULL;
189 if (self.rawStatus == kSOSCCCircleAbsent) {
190 SOSCCResetToOffering(&err);
191 } else {
192 SOSCCRequestToJoinCircle(&err);
193 }
194 }
195
196 -(void)disableSync
197 {
198 CFErrorRef err = NULL;
199 SOSCCRemoveThisDeviceFromCircle(&err);
200 }
201
202 @end