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@
26 #include "SOSAccountPriv.h"
27 #include <SecureObjectSync/SOSPeerInfoCollections.h>
30 bool SOSAccountDestroyCirclePeerInfoNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
* error
) {
31 if (CFDictionaryGetValue(account
->circles
, name
) == NULL
) {
32 SOSCreateErrorWithFormat(kSOSErrorNoCircle
, NULL
, error
, NULL
, CFSTR("No circle named '%@'"), name
);
36 SOSFullPeerInfoRef circle_full_peer_info
= (SOSFullPeerInfoRef
) CFDictionaryGetValue(account
->circle_identities
, name
);
38 if (circle_full_peer_info
) {
39 SOSFullPeerInfoPurgePersistentKey(circle_full_peer_info
, NULL
);
42 CFDictionaryRemoveValue(account
->circle_identities
, name
);
47 bool SOSAccountDestroyCirclePeerInfo(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
) {
48 return SOSAccountDestroyCirclePeerInfoNamed(account
, SOSCircleGetName(circle
), error
);
51 SOSPeerInfoRef
SOSAccountGetMyPeerInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
) {
52 SOSFullPeerInfoRef fpi
= SOSAccountGetMyFullPeerInCircleNamedIfPresent(account
, SOSCircleGetName(circle
), error
);
54 return fpi
? SOSFullPeerInfoGetPeerInfo(fpi
) : NULL
;
57 SOSPeerInfoRef
SOSAccountGetMyPeerInCircleNamed(SOSAccountRef account
, CFStringRef name
, CFErrorRef
*error
)
59 SOSFullPeerInfoRef fpi
= SOSAccountGetMyFullPeerInCircleNamedIfPresent(account
, name
, error
);
61 return fpi
? SOSFullPeerInfoGetPeerInfo(fpi
) : NULL
;
65 bool SOSAccountIsActivePeerInCircleNamed(SOSAccountRef account
, CFStringRef circle_name
, CFStringRef peerid
, CFErrorRef
* error
) {
66 SOSCircleRef circle
= SOSAccountFindCircle(account
, circle_name
, error
);
67 if(!circle
) return false;
68 return SOSCircleHasActivePeerWithID(circle
, peerid
, error
);
71 bool SOSAccountIsMyPeerActiveInCircle(SOSAccountRef account
, SOSCircleRef circle
, CFErrorRef
* error
) {
72 SOSFullPeerInfoRef fpi
= SOSAccountGetMyFullPeerInCircleNamedIfPresent(account
, SOSCircleGetName(circle
), NULL
);
73 if(!fpi
) return false;
74 return SOSCircleHasActivePeer(circle
, SOSFullPeerInfoGetPeerInfo(fpi
), error
);
77 bool SOSAccountIsMyPeerActiveInCircleNamed(SOSAccountRef account
, CFStringRef circle_name
, CFErrorRef
* error
) {
78 SOSFullPeerInfoRef myfpi
= SOSAccountGetMyFullPeerInCircleNamedIfPresent(account
, circle_name
, NULL
);
79 if(!myfpi
) return false;
80 SOSPeerInfoRef mypi
= SOSFullPeerInfoGetPeerInfo(myfpi
);
81 if(!mypi
) return false;
82 CFStringRef peerid
= SOSPeerInfoGetPeerID(mypi
);
83 if (!peerid
) return false;
84 return SOSAccountIsActivePeerInCircleNamed(account
, circle_name
, peerid
, error
);
89 // MARK: Peer Querying
91 static CFArrayRef
SOSAccountCopySortedPeerArray(SOSAccountRef account
,
93 void (^action
)(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
)) {
94 if (!SOSAccountHasPublicKey(account
, error
))
97 CFMutableArrayRef peers
= CFArrayCreateMutableForCFTypes(kCFAllocatorDefault
);
99 SOSAccountForEachCircle(account
, ^(SOSCircleRef circle
) {
100 action(circle
, peers
);
103 CFArrayOfSOSPeerInfosSortByID(peers
);
109 CFArrayRef
SOSAccountCopyNotValidPeers(SOSAccountRef account
, CFErrorRef
*error
) {
110 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
111 SOSCircleForEachPeer(circle
, ^(SOSPeerInfoRef peer
) {
112 if(!SOSPeerInfoApplicationVerify(peer
, account
->user_public
, NULL
)) {
113 CFArrayAppendValue(appendPeersTo
, peer
);
120 CFArrayRef
SOSAccountCopyValidPeers(SOSAccountRef account
, CFErrorRef
*error
) {
121 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
122 SOSCircleForEachPeer(circle
, ^(SOSPeerInfoRef peer
) {
123 if(SOSPeerInfoApplicationVerify(peer
, account
->user_public
, NULL
)) {
124 CFArrayAppendValue(appendPeersTo
, peer
);
131 CFArrayRef
SOSAccountCopyRetired(SOSAccountRef account
, CFErrorRef
*error
) {
132 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
133 SOSCircleForEachRetiredPeer(circle
, ^(SOSPeerInfoRef peer
) {
134 CFArrayAppendValue(appendPeersTo
, peer
);
139 CFArrayRef
SOSAccountCopyApplicants(SOSAccountRef account
, CFErrorRef
*error
) {
140 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
141 SOSCircleForEachApplicant(circle
, ^(SOSPeerInfoRef peer
) {
142 CFArrayAppendValue(appendPeersTo
, peer
);
147 CFArrayRef
SOSAccountCopyPeers(SOSAccountRef account
, CFErrorRef
*error
) {
148 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
149 SOSCircleForEachPeer(circle
, ^(SOSPeerInfoRef peer
) {
150 CFArrayAppendValue(appendPeersTo
, peer
);
155 CFArrayRef
SOSAccountCopyActivePeers(SOSAccountRef account
, CFErrorRef
*error
) {
156 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
157 SOSCircleForEachActivePeer(circle
, ^(SOSPeerInfoRef peer
) {
158 CFArrayAppendValue(appendPeersTo
, peer
);
163 CFArrayRef
SOSAccountCopyActiveValidPeers(SOSAccountRef account
, CFErrorRef
*error
) {
164 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
165 SOSCircleForEachActiveValidPeer(circle
, account
->user_public
, ^(SOSPeerInfoRef peer
) {
166 CFArrayAppendValue(appendPeersTo
, peer
);
171 CFArrayRef
SOSAccountCopyConcurringPeers(SOSAccountRef account
, CFErrorRef
*error
)
173 return SOSAccountCopySortedPeerArray(account
, error
, ^(SOSCircleRef circle
, CFMutableArrayRef appendPeersTo
) {
174 SOSCircleAppendConcurringPeers(circle
, appendPeersTo
, NULL
);