2 // KeychainSyncAccountNotification.m
6 #import "KeychainSyncAccountNotification.h"
7 #import <Accounts/Accounts.h>
8 #import <Accounts/Accounts_Private.h>
10 #import <AppleAccount/ACAccount+AppleAccount.h>
12 #import <AOSAccounts/ACAccount+iCloudAccount.h>
14 #import <AccountsDaemon/ACDAccountStore.h>
15 #import <Security/SecureObjectSync/SOSCloudCircle.h>
17 #import <keychain/ot/OTControl.h>
18 #include <utilities/SecCFRelease.h>
20 #import "utilities/debugging.h"
24 static bool SecOTIsEnabled(void)
26 bool userDefaultsShouldBottledPeer = true;
27 CFBooleanRef enabled = (CFBooleanRef)CFPreferencesCopyValue(CFSTR("EnableOTRestore"),
28 CFSTR("com.apple.security"),
29 kCFPreferencesAnyUser, kCFPreferencesAnyHost);
30 if(enabled && CFGetTypeID(enabled) == CFBooleanGetTypeID()){
31 if(enabled == kCFBooleanFalse){
32 secnotice("octagon", "Octagon Restore Disabled");
33 userDefaultsShouldBottledPeer = false;
35 if(enabled == kCFBooleanTrue){
36 secnotice("octagon", "Octagon Restore Enabled");
37 userDefaultsShouldBottledPeer = true;
41 CFReleaseNull(enabled);
42 return userDefaultsShouldBottledPeer;
47 @implementation KeychainSyncAccountNotification
49 - (bool)accountIsPrimary:(ACAccount *)account
52 return [account aa_isPrimaryAccount];
54 return [account icaIsPrimaryAccount];
58 - (BOOL)account:(ACAccount *)account willChangeWithType:(ACAccountChangeType)changeType inStore:(ACDAccountStore *)store oldAccount:(ACAccount *)oldAccount {
59 NSString* oldAccountIdentifier = oldAccount.identifier;
60 NSString* accountIdentifier = account.identifier;
62 if((changeType == kACAccountChangeTypeAdded) &&
63 [account.accountType.identifier isEqualToString: ACAccountTypeIdentifierAppleAccount] &&
64 [self accountIsPrimary:account]) {
67 __block NSError* error = nil;
68 NSString *dsid = account.accountProperties[@"personID"];
69 OTControl* otcontrol = [OTControl controlObject:&error];
71 if (nil == otcontrol) {
72 secerror("octagon: Failed to get OTControl: %@", error.localizedDescription);
74 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
76 [otcontrol signIn:dsid reply:^(BOOL result, NSError * _Nullable signedInError) {
77 if(!result || signedInError){
78 secerror("octagon: error signing in: %s", [[signedInError description] UTF8String]);
81 secnotice("octagon", "signed into octagon trust");
83 dispatch_semaphore_signal(sema);
86 if (0 != dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60 * 5))) {
87 secerror("octagon: Timed out signing in");
91 secerror("Octagon not enabled!");
95 if ((changeType == kACAccountChangeTypeDeleted) && [oldAccount.accountType.identifier isEqualToString:ACAccountTypeIdentifierAppleAccount]) {
96 if(oldAccountIdentifier != NULL && oldAccount.username !=NULL) {
97 if ([self accountIsPrimary:oldAccount]) {
98 CFErrorRef removalError = NULL;
100 secinfo("accounts", "Performing SOS circle credential removal for account %@: %@", oldAccountIdentifier, oldAccount.username);
102 if (!SOSCCLoggedOutOfAccount(&removalError)) {
103 secerror("Account %@ could not leave the SOS circle: %@", oldAccountIdentifier, removalError);
106 if(SecOTIsEnabled()){
107 __block NSError* error = nil;
108 OTControl* otcontrol = [OTControl controlObject:&error];
110 if (nil == otcontrol) {
111 secerror("octagon: Failed to get OTControl: %@", error.localizedDescription);
113 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
115 [otcontrol signOut:^(BOOL result, NSError * _Nullable signedOutError) {
116 if(!result || signedOutError){
117 secerror("octagon: error signing out: %s", [[signedOutError description] UTF8String]);
120 secnotice("octagon", "signed out of octagon trust");
122 dispatch_semaphore_signal(sema);
124 if (0 != dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 60 * 5))) {
125 secerror("octagon: Timed out signing out");
130 secerror("Octagon not enabled!");
134 secinfo("accounts", "NOT performing SOS circle credential removal for secondary account %@: %@", accountIdentifier, account.username);
137 secinfo("accounts", "Already logged out of account");