]> git.saurik.com Git - apple/security.git/blob - OSX/Keychain/KDSecCircle.m
Security-57740.51.3.tar.gz
[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
30 #import <Security/SecureObjectSync/SOSCloudCircle.h>
31 #import <Security/SecureObjectSync/SOSPeerInfo.h>
32
33 #import <CloudServices/SecureBackup.h>
34
35 #include <utilities/debugging.h>
36
37 @interface KDSecCircle ()
38 @property (retain) NSMutableArray *callbacks;
39
40 @property (readwrite) unsigned long long changeCount;
41
42 @property (readwrite) SOSCCStatus rawStatus;
43
44 @property (readwrite) NSString *status;
45 @property (readwrite) NSError *error;
46
47 @property (readwrite) NSArray *peers;
48 @property (readwrite) NSArray *applicants;
49
50 @property (readwrite) dispatch_queue_t queue_;
51
52 @end
53
54 @implementation KDSecCircle
55
56 -(void)updateCheck
57 {
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]];
65 }];
66
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]];
71 }];
72
73 secdebug("kcn", "rawStatus %d, #applicants %lu, #peers %lu, err=%@", newRawStatus, (unsigned long)[newApplicants count], (unsigned long)[newPeers count], err);
74
75 dispatch_async(dispatch_get_main_queue(), ^{
76 self.rawStatus = newRawStatus;
77
78 switch (newRawStatus) {
79 case kSOSCCInCircle:
80 self.status = @"In Circle";
81 break;
82
83 case kSOSCCNotInCircle:
84 self.status = @"Not In Circle";
85 break;
86
87 case kSOSCCRequestPending:
88 self.status = @"Request Pending";
89 break;
90
91 case kSOSCCCircleAbsent:
92 self.status = @"Circle Absent";
93 break;
94
95 case kSOSCCError:
96 self.status = [NSString stringWithFormat:@"Error: %@", err];
97 break;
98
99 default:
100 self.status = [NSString stringWithFormat:@"Unknown status code %d", self.rawStatus];
101 break;
102 }
103
104 self.applicants = [newApplicants copy];
105 self.peers = [newPeers copy];
106 self.error = (__bridge NSError *)(err);
107
108 self.changeCount++;
109 for (dispatch_block_t callback in self.callbacks) {
110 callback();
111 }
112 });
113 }
114
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.
118 //
119 // Er, now that I look more closely maybe SOSPeerInfoGetPeerID...
120
121 typedef void (^applicantBlock)(id applicant);
122
123 -(void)forApplicantId:(NSString*)applicantId run:(applicantBlock)applicantBlock
124 {
125 dispatch_async(self.queue_, ^{
126 for (KDCirclePeer *applicant in self.applicants) {
127 if ([applicantId isEqualToString:applicantId]) {
128 applicantBlock(applicant.peerObject);
129 break;
130 }
131 }
132 });
133 }
134
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"
138
139 -(void)acceptApplicantId:(NSString*)applicantId
140 {
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);
145 }];
146 }
147
148 -(void)rejectApplicantId:(NSString*)applicantId
149 {
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);
154 }];
155 }
156
157 #pragma clang diagnostic pop
158
159 -(id)init
160 {
161 self = [super init];
162 int token;
163
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){
167 [self updateCheck];
168 });
169 return self;
170 }
171
172 -(void)addChangeCallback:(dispatch_block_t)callback
173 {
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), ^{
179 [self updateCheck];
180 });
181 }
182 }
183
184 -(BOOL)isInCircle
185 {
186 return (self.rawStatus == kSOSCCInCircle);
187 }
188
189 -(BOOL)isOutOfCircle
190 {
191 return (self.rawStatus == kSOSCCNotInCircle || self.rawStatus == kSOSCCCircleAbsent);
192 }
193
194 -(void)enableSync
195 {
196 CFErrorRef err = NULL;
197 if (self.rawStatus == kSOSCCCircleAbsent) {
198 SOSCCResetToOffering(&err);
199 } else {
200 SOSCCRequestToJoinCircle(&err);
201 }
202
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);
209
210 SOSCCViewSet(viewsToEnable, viewsToDisable);
211 CFRelease(viewsToEnable);
212 CFRelease(viewsToDisable);
213 }
214
215 -(void)disableSync
216 {
217 CFErrorRef err = NULL;
218 SOSCCRemoveThisDeviceFromCircle(&err);
219 }
220
221 @end