2 * Copyright (c) 2013-2017 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@
24 #import <Accounts/Accounts.h>
25 #import <Accounts/ACAccountStore_Private.h>
26 #import <Accounts/ACAccountType_Private.h>
27 #import <AggregateDictionary/ADClient.h>
28 #import <AppSupport/AppSupportUtils.h>
29 #import <AppleAccount/AppleAccount.h>
30 #import <AppleAccount/ACAccountStore+AppleAccount.h>
31 #import <CloudServices/SecureBackup.h>
32 #import <CoreFoundation/CFUserNotification.h>
33 #import <Foundation/Foundation.h>
34 #import <ManagedConfiguration/MCProfileConnection.h>
35 #import <ManagedConfiguration/MCFeatures.h>
36 #import <MobileCoreServices/MobileCoreServices.h>
37 #import <MobileCoreServices/LSApplicationWorkspace.h>
38 #import <MobileGestalt.h>
39 #import <ProtectedCloudStorage/CloudIdentity.h>
40 #import <Security/SecFrameworkStrings.h>
41 #import <SpringBoardServices/SBSCFUserNotificationKeys.h>
42 #include <dispatch/dispatch.h>
43 #include "SecureObjectSync/SOSCloudCircle.h"
44 #include "SecureObjectSync/SOSCloudCircleInternal.h"
45 #include "SecureObjectSync/SOSPeerInfo.h"
46 #include "SecureObjectSync/SOSInternal.h"
50 #import "NSArray+map.h"
51 #import "PersistentState.h"
52 #include <xpc/private.h>
54 #import "NSDate+TimeIntervalDescription.h"
55 #include <xpc/activity.h>
56 #include <xpc/private.h>
57 #import "os/activity.h"
59 #include "utilities/SecCFRelease.h"
60 #include "utilities/debugging.h"
61 #include "utilities/SecAKSWrappers.h"
62 #include "utilities/SecCFWrappers.h"
63 #include <utilities/SecXPCError.h>
65 #import "CoreCDP/CDPFollowUpController.h"
66 #import "CoreCDP/CDPFollowUpContext.h"
67 #import <CoreCDP/CDPAccount.h>
69 // As long as we are logging the failure use exit code of zero to make launchd happy
70 #define EXIT_LOGGED_FAILURE(code) xpc_transaction_end(); exit(0)
72 const char *kLaunchLaterXPCName = "com.apple.security.CircleJoinRequestedTick";
73 CFRunLoopSourceRef currentAlertSource = NULL;
74 CFUserNotificationRef currentAlert = NULL;
75 bool currentAlertIsForApplicants = true;
76 bool currentAlertIsForKickOut = false;
77 NSMutableDictionary *applicants = nil;
78 volatile NSString *debugState = @"main?";
79 dispatch_block_t doOnceInMainBlockChain = NULL;
80 bool _isLocked = true;
81 bool processApplicantsAfterUnlock = false;
82 bool _unlockedSinceBoot = false;
83 bool _hasPostedFollowupAndStillInError = false;
84 bool _isAccountICDP = false;
85 bool _executeProcessEventsOnce = false;
87 NSString *castleKeychainUrl = @"prefs:root=APPLE_ACCOUNT&path=ICLOUD_SERVICE/com.apple.Dataclass.KeychainSync/ADVANCED";
88 NSString *rejoinICDPUrl = @"prefs:root=APPLE_ACCOUNT&aaaction=CDP&command=rejoin";
90 BOOL processRequests(CFErrorRef *error);
92 static bool PSKeychainSyncPrimaryAcccountExists(void)
94 ACAccountStore *accountStore = [[ACAccountStore alloc] init];
95 return [accountStore aa_primaryAppleAccount] != NULL;
98 static void PSKeychainSyncIsUsingICDP(void)
100 ACAccountStore *accountStore = [[ACAccountStore alloc] init];
101 ACAccount *account = [accountStore aa_primaryAppleAccount];
102 NSString *dsid = account.accountProperties[@"personID"];
103 BOOL isICDPEnabled = NO;
105 isICDPEnabled = [CDPAccount isICDPEnabledForDSID:dsid];
106 NSLog(@"iCDP: PSKeychainSyncIsUsingICDP returning %@", isICDPEnabled ? @"TRUE" : @"FALSE");
108 NSLog(@"iCDP: no primary account");
111 _isAccountICDP = isICDPEnabled;
112 secnotice("cjr", "account is icdp: %d", _isAccountICDP);
115 static void keybagDidLock()
117 secnotice("cjr", "keybagDidLock");
120 static void keybagDidUnlock()
122 secnotice("cjr", "keybagDidUnlock");
124 CFErrorRef error = NULL;
126 if(processApplicantsAfterUnlock){
127 processRequests(&error);
128 processApplicantsAfterUnlock = false;
131 SOSCCStatus circleStatus = SOSCCThisDeviceIsInCircle(&error);
132 if (circleStatus == kSOSCCError && error && CFEqual(sSecXPCErrorDomain, CFErrorGetDomain(error))) {
133 secnotice("cjr", "unable to determine circle status due to xpc failure: %@", error);
136 else if (_isAccountICDP && (circleStatus == kSOSCCError || circleStatus == kSOSCCCircleAbsent || circleStatus == kSOSCCNotInCircle) && _hasPostedFollowupAndStillInError == false) {
137 NSError *localError = nil;
138 CDPFollowUpContext *context = [CDPFollowUpContext contextForStateRepair];
139 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
140 [cdpd postFollowUpWithContext:context error:&localError ];
141 secnotice("cjr", "account is icdp");
143 secnotice("cjr", "request to CoreCDP to follow up failed: %@", localError);
146 secnotice("cjr", "CoreCDP handling follow up");
147 _hasPostedFollowupAndStillInError = true;
150 else if(_isAccountICDP && circleStatus == kSOSCCInCircle){
151 _hasPostedFollowupAndStillInError = false;
154 secnotice("cjr", "account not icdp");
158 static bool updateIsLocked ()
160 CFErrorRef aksError = NULL;
161 if (!SecAKSGetIsLocked(&_isLocked, &aksError)) {
163 secerror("Got error querying lock state: %@", aksError);
164 CFReleaseSafe(aksError);
168 _unlockedSinceBoot = YES;
172 static void keybagStateChange ()
174 secerror("osactivity initiated");
175 os_activity_initiate("keybagStateChanged", OS_ACTIVITY_FLAG_DEFAULT, ^{
176 BOOL wasLocked = _isLocked;
177 if ( updateIsLocked()) {
178 if (wasLocked == _isLocked)
179 secerror("still %s ignoring", _isLocked ? "locked" : "unlocked");
188 static void doOnceInMain(dispatch_block_t block)
190 if (doOnceInMainBlockChain) {
191 doOnceInMainBlockChain = ^{
192 doOnceInMainBlockChain();
196 doOnceInMainBlockChain = block;
201 static NSString *appleIDAccountName()
203 ACAccountStore *accountStore = [[ACAccountStore alloc] init];
204 ACAccount *primaryAppleAccount = [accountStore aa_primaryAppleAccount];
205 return primaryAppleAccount.username;
209 static CFOptionFlags flagsForAsk(Applicant *applicant)
211 return kCFUserNotificationPlainAlertLevel | CFUserNotificationSecureTextField(0);
215 // NOTE: gives precedence to OnScreen
216 static Applicant *firstApplicantWaitingOrOnScreen()
218 Applicant *waiting = nil;
219 for (Applicant *applicant in [applicants objectEnumerator]) {
220 if (applicant.applicantUIState == ApplicantOnScreen) {
222 } else if (applicant.applicantUIState == ApplicantWaiting) {
231 static NSMutableArray *applicantsInState(ApplicantUIState state)
233 NSMutableArray *results = [NSMutableArray new];
234 for (Applicant *applicant in [applicants objectEnumerator]) {
235 if (applicant.applicantUIState == state) {
236 [results addObject:applicant];
244 BOOL processRequests(CFErrorRef *error) {
245 NSMutableArray *toAccept = [[applicantsInState(ApplicantAccepted) mapWithBlock:^id(id obj) {return (id)[obj rawPeerInfo];}] mutableCopy];
246 NSMutableArray *toReject = [[applicantsInState(ApplicantRejected) mapWithBlock:^id(id obj) {return (id)[obj rawPeerInfo];}] mutableCopy];
249 if ([toAccept count]) {
250 secnotice("cjr", "Process accept: %@", toAccept);
251 ok = ok && SOSCCAcceptApplicants((__bridge CFArrayRef) toAccept, error);
253 secnotice("cjr", "kSOSCCHoldLockForInitialSync");
254 notify_post(kSOSCCHoldLockForInitialSync);
258 if ([toReject count]) {
259 secnotice("cjr", "Process reject: %@", toReject);
260 ok = ok && SOSCCRejectApplicants((__bridge CFArrayRef) toReject, error);
267 static void cancelCurrentAlert(bool stopRunLoop) {
268 if (currentAlertSource) {
269 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), currentAlertSource, kCFRunLoopDefaultMode);
270 CFReleaseNull(currentAlertSource);
273 CFUserNotificationCancel(currentAlert);
274 CFReleaseNull(currentAlert);
277 CFRunLoopStop(CFRunLoopGetCurrent());
279 currentAlertIsForKickOut = currentAlertIsForApplicants = false;
283 static void askAboutAll(bool passwordFailure);
286 static void applicantChoice(CFUserNotificationRef userNotification, CFOptionFlags responseFlags)
288 ApplicantUIState choice;
290 if (kCFUserNotificationAlternateResponse == responseFlags) {
291 choice = ApplicantRejected;
292 } else if (kCFUserNotificationDefaultResponse == responseFlags) {
293 choice = ApplicantAccepted;
295 secnotice("cjr", "Unexpected response %lu", responseFlags);
296 choice = ApplicantRejected;
300 CFErrorRef error = NULL;
301 NSArray *onScreen = applicantsInState(ApplicantOnScreen);
303 [onScreen enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
304 Applicant* applicant = (Applicant *) obj;
305 applicant.applicantUIState = choice;
308 if (choice == ApplicantRejected) {
309 // If this device has ever set up the public key this should work without the password...
310 processed = processRequests(&error);
312 secnotice("cjr", "Didn't need password to process %@", onScreen);
313 cancelCurrentAlert(true);
316 // ...however if the public key gets lost we should "just" fall through to the validate
318 secnotice("cjr", "Couldn't process reject without password (e=%@) for %@ (will try with password next)", error, onScreen);
320 if(CFErrorIsMalfunctioningKeybagError(error)){
321 secnotice("cjr", "system is locked, dismiss the notification");
322 processApplicantsAfterUnlock = true;
326 CFReleaseNull(error);
329 NSString *password = (__bridge NSString *) CFUserNotificationGetResponseValue(userNotification, kCFUserNotificationTextFieldValuesKey, 0);
331 secnotice("cjr", "No password given, retry");
335 const char *passwordUTF8 = [password UTF8String];
336 NSData *passwordBytes = [NSData dataWithBytes:passwordUTF8 length:strlen(passwordUTF8)];
338 // Sometimes securityd crashes between SOSCCRegisterUserCredentials and processRequests
339 // (which results in a process error -- I think this is 13355140), as a workaround we retry
340 // failure a few times before we give up.
341 for (int try = 0; try < 5 && !processed; try++) {
342 if (!SOSCCTryUserCredentials(CFSTR(""), (__bridge CFDataRef) passwordBytes, &error)) {
343 secnotice("cjr", "Try user credentials failed %@", error);
344 if ((error == NULL) ||
345 (CFEqual(kSOSErrorDomain, CFErrorGetDomain(error)) && kSOSErrorWrongPassword == CFErrorGetCode(error))) {
346 secnotice("cjr", "Calling askAboutAll again...");
347 [onScreen enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
348 Applicant *applicant = (Applicant*) obj;
349 applicant.applicantUIState = ApplicantWaiting;
352 CFReleaseNull(error);
355 EXIT_LOGGED_FAILURE(EX_DATAERR);
358 processed = processRequests(&error);
360 secnotice("cjr", "Can't processRequests: %@ for %@", error, onScreen);
362 CFReleaseNull(error);
365 if (processed && firstApplicantWaitingOrOnScreen()) {
366 cancelCurrentAlert(false);
369 cancelCurrentAlert(true);
374 static void passwordFailurePrompt()
376 #pragma clang diagnostic push
377 #pragma clang diagnostic ignored "-Wformat-nonliteral"
378 NSString *pwIncorrect = [NSString stringWithFormat:(NSString *)CFBridgingRelease(SecCopyCKString(SEC_CK_PASSWORD_INCORRECT)), appleIDAccountName()];
379 #pragma clang diagnostic pop
380 NSString *tryAgain = CFBridgingRelease(SecCopyCKString(SEC_CK_TRY_AGAIN));
381 NSDictionary *noteAttributes = @{
382 (id) kCFUserNotificationAlertHeaderKey : pwIncorrect,
383 (id) kCFUserNotificationDefaultButtonTitleKey : tryAgain,
384 (id) kCFUserNotificationAlertTopMostKey : @YES, // get us onto the lock screen
385 (__bridge id) SBUserNotificationDontDismissOnUnlock: @YES,
386 (__bridge id) SBUserNotificationDismissOnLock : @NO,
388 CFOptionFlags flags = kCFUserNotificationPlainAlertLevel;
390 CFUserNotificationRef note = CFUserNotificationCreate(NULL, 0.0, flags, &err, (__bridge CFDictionaryRef) noteAttributes);
393 CFUserNotificationReceiveResponse(note, 0.0, &flags);
399 static NSString *getLocalizedApprovalBody(NSString *deviceType) {
400 CFStringRef applicationReminder = NULL;
402 if ([deviceType isEqualToString:@"iPhone"])
403 applicationReminder = SecCopyCKString(SEC_CK_APPROVAL_BODY_IOS_IPHONE);
404 else if ([deviceType isEqualToString:@"iPod"])
405 applicationReminder = SecCopyCKString(SEC_CK_APPROVAL_BODY_IOS_IPOD);
406 else if ([deviceType isEqualToString:@"iPad"])
407 applicationReminder = SecCopyCKString(SEC_CK_APPROVAL_BODY_IOS_IPAD);
408 else if ([deviceType isEqualToString:@"Mac"])
409 applicationReminder = SecCopyCKString(SEC_CK_APPROVAL_BODY_IOS_MAC);
411 applicationReminder = SecCopyCKString(SEC_CK_APPROVAL_BODY_IOS_GENERIC);
413 return (__bridge_transfer NSString *) applicationReminder;
417 static NSDictionary *createNote(Applicant *applicantToAskAbout)
419 if(!applicantToAskAbout || !applicantToAskAbout.name || !applicantToAskAbout.deviceType)
422 #pragma clang diagnostic push
423 #pragma clang diagnostic ignored "-Wformat-nonliteral"
424 NSString *header = [NSString stringWithFormat: (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_APPROVAL_TITLE), applicantToAskAbout.name];
425 NSString *body = [NSString stringWithFormat: getLocalizedApprovalBody(applicantToAskAbout.deviceType), appleIDAccountName()];
426 #pragma clang diagnostic pop
429 (id) kCFUserNotificationAlertHeaderKey : header,
430 (id) kCFUserNotificationAlertMessageKey : body,
431 (id) kCFUserNotificationDefaultButtonTitleKey : (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_APPROVE),
432 (id) kCFUserNotificationAlternateButtonTitleKey: (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_DECLINE),
433 (id) kCFUserNotificationTextFieldTitlesKey : (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_ICLOUD_PASSWORD),
434 (id) kCFUserNotificationAlertTopMostKey : @YES, // get us onto the lock screen
435 (__bridge_transfer id) SBUserNotificationDontDismissOnUnlock: @YES,
436 (__bridge_transfer id) SBUserNotificationDismissOnLock : @NO,
441 static void askAboutAll(bool passwordFailure)
443 if ([[MCProfileConnection sharedConnection] effectiveBoolValueForSetting: MCFeatureAccountModificationAllowed] == MCRestrictedBoolExplicitNo) {
444 secnotice("cjr", "Account modifications not allowed.");
448 if (passwordFailure) {
449 passwordFailurePrompt();
452 if ((passwordFailure || !currentAlertIsForApplicants) && currentAlert) {
453 if (!currentAlertIsForApplicants) {
454 CFUserNotificationCancel(currentAlert);
456 // after password failure we need to remove the existing alert and supporting objects
457 // because we can't reuse them.
458 CFReleaseNull(currentAlert);
459 CFReleaseNull(currentAlertSource);
461 currentAlertIsForApplicants = true;
463 Applicant *applicantToAskAbout = firstApplicantWaitingOrOnScreen();
464 secnotice("cjr", "Asking about: %@ (of: %@)", applicantToAskAbout, applicants);
466 NSDictionary *noteAttributes = createNote(applicantToAskAbout);
467 if(!noteAttributes) {
468 secnotice("cjr", "NULL data for %@", applicantToAskAbout);
469 cancelCurrentAlert(true);
473 CFOptionFlags flags = flagsForAsk(applicantToAskAbout);
476 SInt32 err = CFUserNotificationUpdate(currentAlert, 0, flags, (__bridge CFDictionaryRef) noteAttributes);
478 secnotice("cjr", "CFUserNotificationUpdate err=%d", (int)err);
479 EXIT_LOGGED_FAILURE(EX_SOFTWARE);
483 currentAlert = CFUserNotificationCreate(NULL, 0.0, flags, &err, (__bridge CFDictionaryRef) noteAttributes);
485 secnotice("cjr", "Can't make notification for %@ err=%x", applicantToAskAbout, (int)err);
486 EXIT_LOGGED_FAILURE(EX_SOFTWARE);
489 currentAlertSource = CFUserNotificationCreateRunLoopSource(NULL, currentAlert, applicantChoice, 0);
490 CFRunLoopAddSource(CFRunLoopGetCurrent(), currentAlertSource, kCFRunLoopDefaultMode);
493 applicantToAskAbout.applicantUIState = ApplicantOnScreen;
497 static void scheduleActivity(int alertInterval)
499 xpc_object_t options = xpc_dictionary_create(NULL, NULL, 0);
500 xpc_dictionary_set_uint64(options, XPC_ACTIVITY_DELAY, alertInterval);
501 xpc_dictionary_set_uint64(options, XPC_ACTIVITY_GRACE_PERIOD, XPC_ACTIVITY_INTERVAL_1_MIN);
502 xpc_dictionary_set_bool(options, XPC_ACTIVITY_REPEATING, false);
503 xpc_dictionary_set_bool(options, XPC_ACTIVITY_ALLOW_BATTERY, true);
504 xpc_dictionary_set_string(options, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_UTILITY);
506 xpc_activity_register(kLaunchLaterXPCName, options, ^(xpc_activity_t activity) {
507 secnotice("cjr", "activity handler fired");
512 static void reminderChoice(CFUserNotificationRef userNotification, CFOptionFlags responseFlags) {
513 if (responseFlags == kCFUserNotificationAlternateResponse || responseFlags == kCFUserNotificationDefaultResponse) {
514 PersistentState *state = [PersistentState loadFromStorage];
515 NSDate *nowish = [NSDate new];
516 state.pendingApplicationReminder = [nowish dateByAddingTimeInterval: state.pendingApplicationReminderAlertInterval];
517 scheduleActivity(state.pendingApplicationReminderAlertInterval);
518 [state writeToStorage];
519 if (responseFlags == kCFUserNotificationAlternateResponse) {
521 BOOL ok = [[LSApplicationWorkspace defaultWorkspace] openSensitiveURL:[NSURL URLWithString:castleKeychainUrl] withOptions:nil];
522 secnotice("cjr", "%s iCSC: opening %@ ok=%d", __FUNCTION__, castleKeychainUrl, ok);
526 cancelCurrentAlert(true);
530 static bool iCloudResetAvailable() {
531 SecureBackup *backupd = [SecureBackup new];
532 NSDictionary *backupdResults;
533 NSError *error = [backupd getAccountInfoWithInfo:nil results:&backupdResults];
534 secnotice("cjr", "SecureBackup e=%@ r=%@", error, backupdResults);
535 return (error == nil && [backupdResults[kSecureBackupIsEnabledKey] isEqualToNumber:@YES]);
539 static NSString *getLocalizedApplicationReminder() {
540 CFStringRef applicationReminder = NULL;
541 switch (MGGetSInt32Answer(kMGQDeviceClassNumber, MGDeviceClassInvalid)) {
542 case MGDeviceClassiPhone:
543 applicationReminder = SecCopyCKString(SEC_CK_REMINDER_BODY_IOS_IPHONE);
545 case MGDeviceClassiPod:
546 applicationReminder = SecCopyCKString(SEC_CK_REMINDER_BODY_IOS_IPOD);
548 case MGDeviceClassiPad:
549 applicationReminder = SecCopyCKString(SEC_CK_REMINDER_BODY_IOS_IPAD);
552 applicationReminder = SecCopyCKString(SEC_CK_REMINDER_BODY_IOS_GENERIC);
555 return (__bridge_transfer NSString *) applicationReminder;
559 static void postApplicationReminderAlert(NSDate *nowish, PersistentState *state, unsigned int alertInterval)
561 NSString *body = getLocalizedApplicationReminder();
562 bool has_iCSC = iCloudResetAvailable();
564 if (CPIsInternalDevice() &&
565 state.defaultPendingApplicationReminderAlertInterval != state.pendingApplicationReminderAlertInterval) {
567 body = [body stringByAppendingFormat: @"〖debug interval %u; wait time %@〗",
568 state.pendingApplicationReminderAlertInterval,
569 [nowish copyDescriptionOfIntervalSince:state.applicationDate]];
573 NSDictionary *pendingAttributes = @{
574 (id) kCFUserNotificationAlertHeaderKey : CFBridgingRelease(SecCopyCKString(SEC_CK_REMINDER_TITLE_IOS)),
575 (id) kCFUserNotificationAlertMessageKey : body,
576 (id) kCFUserNotificationDefaultButtonTitleKey : CFBridgingRelease(SecCopyCKString(SEC_CK_REMINDER_BUTTON_OK)),
577 (id) kCFUserNotificationAlternateButtonTitleKey: has_iCSC ? CFBridgingRelease(SecCopyCKString(SEC_CK_REMINDER_BUTTON_ICSC)) : @"",
578 (id) kCFUserNotificationAlertTopMostKey : @YES,
579 (__bridge id) SBUserNotificationDontDismissOnUnlock : @YES,
580 (__bridge id) SBUserNotificationDismissOnLock : @NO,
583 currentAlert = CFUserNotificationCreate(NULL, 0.0, kCFUserNotificationPlainAlertLevel, &err, (__bridge CFDictionaryRef) pendingAttributes);
586 secnotice("cjr", "Can't make pending notification err=%x", (int)err);
588 currentAlertIsForApplicants = false;
589 currentAlertSource = CFUserNotificationCreateRunLoopSource(NULL, currentAlert, reminderChoice, 0);
590 CFRunLoopAddSource(CFRunLoopGetCurrent(), currentAlertSource, kCFRunLoopDefaultMode);
595 static void kickOutChoice(CFUserNotificationRef userNotification, CFOptionFlags responseFlags) {
596 secnotice("cjr", "kOC %@ %lu", userNotification, responseFlags);
598 //default response: continue -> settings pref pane advanced keychain sync page
599 if (responseFlags == kCFUserNotificationDefaultResponse) {
600 // We need to let things unwind to main for the new state to get saved
602 NSURL *url = [NSURL URLWithString: _isAccountICDP ? rejoinICDPUrl : castleKeychainUrl];
603 BOOL ok = [[LSApplicationWorkspace defaultWorkspace] openSensitiveURL:url withOptions:nil];
604 secnotice("cjr","kickOutChoice account is iCDP: %d", _isAccountICDP);
605 secnotice("cjr", "ok=%d opening %@", ok, url);
608 //alternate response: later -> call CD
609 else if (responseFlags == kCFUserNotificationAlternateResponse) {
610 // We need to let things unwind to main for the new state to get saved
613 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
614 NSError *localError = nil;
615 CDPFollowUpContext *context = [CDPFollowUpContext contextForStateRepair];
616 [cdpd postFollowUpWithContext:context error:&localError ];
618 secnotice("cjr", "request to CoreCDP to follow up failed: %@", localError);
621 secnotice("cjr", "CoreCDP handling follow up");
622 _hasPostedFollowupAndStillInError = true;
628 cancelCurrentAlert(true);
632 CFStringRef const CJRAggdDepartureReasonKey = CFSTR("com.apple.security.circlejoinrequested.departurereason");
633 CFStringRef const CJRAggdNumCircleDevicesKey = CFSTR("com.apple.security.circlejoinrequested.numcircledevices");
635 static void postKickedOutAlert(enum DepartureReason reason)
637 NSString *header = nil;
638 NSString *message = nil;
640 // <rdar://problem/20358568> iCDP telemetry: ☂ Statistics on circle reset and drop out events
641 ADClientSetValueForScalarKey(CJRAggdDepartureReasonKey, reason);
643 int64_t num_peers = 0;
644 CFArrayRef peerList = SOSCCCopyPeerPeerInfo(NULL);
646 num_peers = CFArrayGetCount(peerList);
647 if (num_peers > 99) {
648 // Round down # peers to 2 significant digits
650 for (factor = 10; num_peers >= 100*factor; factor *= 10) ;
651 num_peers = (num_peers / factor) * factor;
655 ADClientSetValueForScalarKey(CJRAggdNumCircleDevicesKey, num_peers);
657 debugState = @"pKOA A";
658 secnotice("cjr", "DepartureReason %d", reason);
660 case kSOSDiscoveredRetirement:
661 case kSOSLostPrivateKey:
662 case kSOSWithdrewMembership:
663 // Was: SEC_CK_CR_BODY_WITHDREW
664 // "... if you turn off a switch you have some idea why the light is off" - Murf
667 case kSOSNeverAppliedToCircle:
668 // We didn't get kicked out, we were never here. This should only happen if we changed iCloud accounts
669 // (and we had sync on in the previous one, and never had it on in the new one). As this is explicit
670 // user action alot of the "Light switch" argument (above) applies.
673 case kSOSPasswordChanged:
674 case kSOSNeverLeftCircle:
675 case kSOSMembershipRevoked:
676 case kSOSLeftUntrustedCircle:
678 header = (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_PWD_REQUIRED_TITLE);
679 message = (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_PWD_REQUIRED_BODY_IOS);
683 if (CPIsInternalDevice()) {
684 static const char *departureReasonStrings[] = {
685 "kSOSDepartureReasonError",
686 "kSOSNeverLeftCircle",
687 "kSOSWithdrewMembership",
688 "kSOSMembershipRevoked",
689 "kSOSLeftUntrustedCircle",
690 "kSOSNeverAppliedToCircle",
691 "kSOSDiscoveredRetirement",
692 "kSOSLostPrivateKey",
693 "kSOSPasswordChanged",
696 int idx = (kSOSDepartureReasonError <= reason && reason <= kSOSLostPrivateKey) ? reason : (kSOSLostPrivateKey + 1);
697 #pragma clang diagnostic push
698 #pragma clang diagnostic ignored "-Wformat-nonliteral"
699 NSString *reason_str = [NSString stringWithFormat:(__bridge_transfer NSString *) SecCopyCKString(SEC_CK_CR_REASON_INTERNAL),
700 departureReasonStrings[idx]];
701 #pragma clang diagnostic pop
702 message = [message stringByAppendingString: reason_str];
705 NSDictionary *kickedAttributes = @{
706 (id) kCFUserNotificationAlertHeaderKey : header,
707 (id) kCFUserNotificationAlertMessageKey : message,
708 (id) kCFUserNotificationDefaultButtonTitleKey : (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_CONTINUE),
709 (id) kCFUserNotificationAlternateButtonTitleKey: (__bridge_transfer NSString *) SecCopyCKString(SEC_CK_NOT_NOW),
710 (id) kCFUserNotificationAlertTopMostKey : @YES,
711 (__bridge id) SBUserNotificationDismissOnLock : @NO,
712 (__bridge id) SBUserNotificationDontDismissOnUnlock : @YES,
716 if (currentAlertIsForKickOut) {
717 debugState = @"pKOA B";
718 secnotice("cjr", "Updating existing alert %@ with %@", currentAlert, kickedAttributes);
719 CFUserNotificationUpdate(currentAlert, 0, kCFUserNotificationPlainAlertLevel, (__bridge CFDictionaryRef) kickedAttributes);
721 debugState = @"pKOA C";
723 CFUserNotificationRef note = CFUserNotificationCreate(NULL, 0.0, kCFUserNotificationPlainAlertLevel, &err, (__bridge CFDictionaryRef) kickedAttributes);
724 assert((note == NULL) == (err != 0));
726 secnotice("cjr", "Can't make kicked out notification err=%x", (int)err);
729 currentAlertIsForApplicants = false;
730 currentAlertIsForKickOut = true;
733 secnotice("cjr", "New ko alert %@ a=%@", currentAlert, kickedAttributes);
734 currentAlertSource = CFUserNotificationCreateRunLoopSource(NULL, currentAlert, kickOutChoice, 0);
735 CFRunLoopAddSource(CFRunLoopGetCurrent(), currentAlertSource, kCFRunLoopDefaultMode);
736 int backupStateChangeToken;
737 notify_register_dispatch("com.apple.EscrowSecurityAlert.reset", &backupStateChangeToken, dispatch_get_main_queue(), ^(int token) {
738 if (currentAlert == note) {
739 secnotice("cjr", "Backup state might have changed (dS=%@)", debugState);
740 postKickedOutAlert(reason);
742 secnotice("cjr", "Backup state may have changed, but we don't care anymore (dS=%@)", debugState);
746 debugState = @"pKOA D";
748 debugState = @"pKOA E";
749 notify_cancel(backupStateChangeToken);
752 debugState = @"pKOA Z";
756 static bool processEvents()
758 debugState = @"processEvents A";
760 CFErrorRef error = NULL;
761 CFErrorRef departError = NULL;
762 SOSCCStatus circleStatus = SOSCCThisDeviceIsInCircleNonCached(&error);
763 enum DepartureReason departureReason = SOSCCGetLastDepartureReason(&departError);
765 // Error due to XPC failure does not provide information about the circle.
766 if (circleStatus == kSOSCCError && error && (CFEqual(sSecXPCErrorDomain, CFErrorGetDomain(error)))) {
767 secnotice("cjr", "XPC error while checking circle status: \"%@\", not processing events", error);
769 } else if (departureReason == kSOSDepartureReasonError && departError && (CFEqual(sSecXPCErrorDomain, CFErrorGetDomain(departError)))) {
770 secnotice("cjr", "XPC error while checking last departure reason: \"%@\", not processing events", departError);
774 NSDate *nowish = [NSDate date];
775 PersistentState *state = [PersistentState loadFromStorage];
776 secnotice("cjr", "CircleStatus %d -> %d{%d} (s=%p)", state.lastCircleStatus, circleStatus, departureReason, state);
778 // Pending application reminder
779 NSTimeInterval timeUntilApplicationAlert = [state.pendingApplicationReminder timeIntervalSinceDate:nowish];
780 secnotice("cjr", "Time until pendingApplicationReminder (%@) %f", [state.pendingApplicationReminder debugDescription], timeUntilApplicationAlert);
781 if (circleStatus == kSOSCCRequestPending) {
782 if (timeUntilApplicationAlert <= 0) {
783 debugState = @"reminderAlert";
784 postApplicationReminderAlert(nowish, state, state.pendingApplicationReminderAlertInterval);
786 scheduleActivity(ceil(timeUntilApplicationAlert));
790 PSKeychainSyncIsUsingICDP();
792 // Refresh because sometimes we're fixed elsewhere before we get here.
793 CFReleaseNull(error);
794 circleStatus = SOSCCThisDeviceIsInCircleNonCached(&error);
797 * Double check that the account still exists before doing anything rash (like posting a CFU or throw up a dialog)
800 if (!PSKeychainSyncPrimaryAcccountExists()) {
801 secnotice("cjr", "no primary account, bailing");
806 if((circleStatus == kSOSCCError || circleStatus == kSOSCCCircleAbsent || circleStatus == kSOSCCNotInCircle) && _hasPostedFollowupAndStillInError == false) {
807 if(circleStatus == kSOSCCError) {
808 secnotice("cjr", "error from SOSCCThisDeviceIsInCircle: %@", error);
810 secnotice("cjr", "iCDP: We need to get back into the circle");
812 NSError *localError = nil;
813 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
814 CDPFollowUpContext *context = [CDPFollowUpContext contextForStateRepair];
815 [cdpd postFollowUpWithContext:context error:&localError ];
817 secnotice("cjr", "request to CoreCDP to follow up failed: %@", localError);
820 secnotice("cjr", "CoreCDP handling follow up");
821 _hasPostedFollowupAndStillInError = true;
824 state.lastCircleStatus = circleStatus;
825 _executeProcessEventsOnce = true;
828 else if(circleStatus == kSOSCCInCircle){
829 secnotice("cjr", "follow up should be resolved");
830 _executeProcessEventsOnce = true;
831 _hasPostedFollowupAndStillInError = false;
834 secnotice("cjr", "followup not resolved");
835 _executeProcessEventsOnce = true;
838 } else if(circleStatus == kSOSCCError && state.lastCircleStatus != kSOSCCError && (departureReason == kSOSNeverLeftCircle)) {
839 secnotice("cjr", "SA: error from SOSCCThisDeviceIsInCircle: %@", error);
840 CFIndex errorCode = CFErrorGetCode(error);
841 if(errorCode == kSOSErrorPublicKeyAbsent){
842 secnotice("cjr", "SA: We need the password to re-validate ourselves - it's changed on another device");
843 postKickedOutAlert(kSOSPasswordChanged);
844 state.lastCircleStatus = kSOSCCError;
845 [state writeToStorage];
849 // No longer in circle?
850 if ((state.lastCircleStatus == kSOSCCInCircle && (circleStatus == kSOSCCNotInCircle || circleStatus == kSOSCCCircleAbsent)) ||
851 (state.lastCircleStatus == kSOSCCCircleAbsent && circleStatus == kSOSCCNotInCircle && state.absentCircleWithNoReason) ||
852 state.debugShowLeftReason) {
853 // Used to be in the circle, now we aren't - tell the user why
854 debugState = @"processEvents B";
856 if (state.debugShowLeftReason) {
857 secnotice("cjr", "debugShowLeftReason: %@", state.debugShowLeftReason);
858 departureReason = [state.debugShowLeftReason intValue];
859 state.debugShowLeftReason = nil;
860 CFReleaseNull(departError);
861 [state writeToStorage];
864 if (departureReason != kSOSDepartureReasonError) {
865 state.absentCircleWithNoReason = (circleStatus == kSOSCCCircleAbsent && departureReason == kSOSNeverLeftCircle);
866 secnotice("cjr", "Depature reason %d", departureReason);
868 secnotice("cjr", "posting revocation notification!");
869 postKickedOutAlert(departureReason);
871 else if(_isAccountICDP && _hasPostedFollowupAndStillInError == false){
872 NSError *localError = nil;
873 CDPFollowUpController *cdpd = [[CDPFollowUpController alloc] init];
874 CDPFollowUpContext *context = [CDPFollowUpContext contextForStateRepair];
875 [cdpd postFollowUpWithContext:context error:&localError ];
877 secnotice("cjr", "request to CoreCDP to follow up failed: %@", localError);
880 secnotice("cjr", "CoreCDP handling follow up");
881 _hasPostedFollowupAndStillInError = true;
885 secnotice("cjr", "still waiting for followup to resolve");
887 secnotice("cjr", "pKOA returned (cS %d lCS %d)", circleStatus, state.lastCircleStatus);
889 secnotice("cjr", "Couldn't get last departure reason: %@", departError);
894 // Circle applications: pending request(s) started / completed
895 debugState = @"processEvents C";
896 if (circleStatus != state.lastCircleStatus) {
897 SOSCCStatus lastCircleStatus = state.lastCircleStatus;
898 state.lastCircleStatus = circleStatus;
900 if (lastCircleStatus != kSOSCCRequestPending && circleStatus == kSOSCCRequestPending) {
901 secnotice("cjr", "Pending request started");
902 state.applicationDate = nowish;
903 state.pendingApplicationReminder = [nowish dateByAddingTimeInterval: state.pendingApplicationReminderAlertInterval];
904 scheduleActivity(state.pendingApplicationReminderAlertInterval);
906 if (lastCircleStatus == kSOSCCRequestPending && circleStatus != kSOSCCRequestPending) {
907 secnotice("cjr", "Pending request completed");
908 state.applicationDate = [NSDate distantPast];
909 state.pendingApplicationReminder = [NSDate distantFuture];
912 [state writeToStorage];
915 if (circleStatus != kSOSCCInCircle) {
916 if (circleStatus == kSOSCCRequestPending && currentAlert) {
918 CFUserNotificationRef postedAlert = currentAlert;
920 debugState = @"processEvents D1";
921 notify_register_dispatch(kSOSCCCircleChangedNotification, ¬ifyToken, dispatch_get_main_queue(), ^(int token) {
922 if (postedAlert != currentAlert) {
923 secnotice("cjr", "-- CC after original alert gone (currentAlertIsForApplicants %d, pA %p, cA %p -- %@)",
924 currentAlertIsForApplicants, postedAlert, currentAlert, currentAlert);
925 notify_cancel(token);
927 CFErrorRef localError = NULL;
928 SOSCCStatus newCircleStatus = SOSCCThisDeviceIsInCircle(&localError);
929 if (newCircleStatus != kSOSCCRequestPending) {
930 if (newCircleStatus == kSOSCCError)
931 secnotice("cjr", "No longer pending (nCS=%d, alert=%@) error: %@", newCircleStatus, currentAlert, localError);
933 secnotice("cjr", "No longer pending (nCS=%d, alert=%@)", newCircleStatus, currentAlert);
934 cancelCurrentAlert(true);
936 secnotice("cjr", "Still pending...");
938 CFReleaseNull(localError);
941 debugState = @"processEvents D2";
942 secnotice("cjr", "NOTE: currentAlertIsForApplicants %d, token %d", currentAlertIsForApplicants, notifyToken);
946 debugState = @"processEvents D4";
947 secnotice("cjr", "SOSCCThisDeviceIsInCircle status %d, not checking applicants", circleStatus);
953 debugState = @"processEvents E";
954 applicants = [NSMutableDictionary new];
955 for (id applicantInfo in (__bridge_transfer NSArray *) SOSCCCopyApplicantPeerInfo(&error)) {
956 Applicant *applicant = [[Applicant alloc] initWithPeerInfo:(__bridge SOSPeerInfoRef) applicantInfo];
957 applicants[applicant.idString] = applicant;
960 // Log error from SOSCCCopyApplicantPeerInfo() above?
961 CFReleaseNull(error);
963 int notify_token = -42;
964 debugState = @"processEvents F";
965 int notify_register_status = notify_register_dispatch(kSOSCCCircleChangedNotification, ¬ify_token, dispatch_get_main_queue(), ^(int token) {
966 secnotice("cjr", "Notified: %s", kSOSCCCircleChangedNotification);
967 CFErrorRef circleStatusError = NULL;
969 bool needsUpdate = false;
970 CFErrorRef copyPeerError = NULL;
971 NSMutableSet *newIds = [NSMutableSet new];
972 for (id applicantInfo in (__bridge_transfer NSArray *) SOSCCCopyApplicantPeerInfo(©PeerError)) {
973 Applicant *newApplicant = [[Applicant alloc] initWithPeerInfo:(__bridge SOSPeerInfoRef) applicantInfo];
974 [newIds addObject:newApplicant.idString];
975 Applicant *existingApplicant = applicants[newApplicant.idString];
976 if (existingApplicant) {
977 switch (existingApplicant.applicantUIState) {
978 case ApplicantWaiting:
979 applicants[newApplicant.idString] = newApplicant;
982 case ApplicantOnScreen:
983 newApplicant.applicantUIState = ApplicantOnScreen;
984 applicants[newApplicant.idString] = newApplicant;
988 secnotice("cjr", "Update to %@ >> %@ with pending order, should work out ok though", existingApplicant, newApplicant);
993 applicants[newApplicant.idString] = newApplicant;
997 secnotice("cjr", "Could not update peer info array: %@", copyPeerError);
998 CFRelease(copyPeerError);
1002 NSMutableArray *idsToRemoveFromApplicants = [NSMutableArray new];
1003 for (NSString *exisitngId in [applicants keyEnumerator]) {
1004 if (![newIds containsObject:exisitngId]) {
1005 [idsToRemoveFromApplicants addObject:exisitngId];
1009 [applicants removeObjectsForKeys:idsToRemoveFromApplicants];
1011 if (newIds.count == 0) {
1012 secnotice("cjr", "All applicants were handled elsewhere");
1013 cancelCurrentAlert(true);
1015 SOSCCStatus currentCircleStatus = SOSCCThisDeviceIsInCircle(&circleStatusError);
1016 if (kSOSCCInCircle != currentCircleStatus) {
1017 secnotice("cjr", "Left circle (%d), not handling remaining %lu applicants", currentCircleStatus, (unsigned long)newIds.count);
1018 cancelCurrentAlert(true);
1023 secnotice("cjr", "needsUpdate false, not updating alert");
1025 // Log circleStatusError?
1026 CFReleaseNull(circleStatusError);
1028 secnotice("cjr", "ACC token %d, status %d", notify_token, notify_register_status);
1029 debugState = @"processEvents F2";
1031 if (applicants.count == 0) {
1032 secnotice("cjr", "No applicants");
1034 debugState = @"processEvents F3";
1036 debugState = @"processEvents F4";
1038 debugState = @"processEvents F5";
1043 debugState = @"processEvents F6";
1044 notify_cancel(notify_token);
1045 debugState = @"processEvents DONE";
1051 int main (int argc, const char * argv[]) {
1053 xpc_transaction_begin();
1057 // NOTE: DISPATCH_QUEUE_PRIORITY_LOW will not actually manage to drain events in a lot of cases (like circleStatus != kSOSCCInCircle)
1058 xpc_set_event_stream_handler("com.apple.notifyd.matching", dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(xpc_object_t object) {
1059 char *event_description = xpc_copy_description(object);
1060 const char *notificationName = xpc_dictionary_get_string(object, "Notification");
1062 if (notificationName && strcmp(notificationName, kUserKeybagStateChangeNotification)==0) {
1063 secnotice("cjr", "keybag changed!");
1064 keybagStateChange();
1067 secnotice("cjr", "notifyd event: %s\nAlert (%p) %s %s\ndebugState: %@", event_description, currentAlert,
1068 currentAlertIsForApplicants ? "for applicants" : "!applicants",
1069 currentAlertIsForKickOut ? "KO" : "!KO", debugState);
1070 free(event_description);
1073 xpc_activity_register(kLaunchLaterXPCName, XPC_ACTIVITY_CHECK_IN, ^(xpc_activity_t activity) {
1076 int falseInARow = 0;
1077 while (falseInARow < 2 && !_executeProcessEventsOnce) {
1078 if (processEvents()) {
1079 secnotice("cjr", "Processed events!!!");
1084 cancelCurrentAlert(false);
1085 if (doOnceInMainBlockChain) {
1086 doOnceInMainBlockChain();
1087 doOnceInMainBlockChain = NULL;
1092 secnotice("cjr", "Done");
1093 xpc_transaction_end();