From e5643e29904ee5d765d195663c6223e26e527068 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jul 2016 17:05:26 -0400 Subject: [PATCH] Improve authorization prompt handling We now present a better worded dialog (X wants to update. Please type your password to allow this. OK/Cancel) We still need to set the icon for the dialog window. I also fixed some issues with canceling the dialog still throwing an error. --- InstallerLauncher/SUInstallerLauncher.m | 53 ++++++++++++------- .../SUInstallerLauncherProtocol.h | 2 +- Sparkle/SUInstallerDriver.m | 4 +- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index 001e1fa2..fae94cd9 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -84,7 +84,7 @@ return (submittedJob == true); } -- (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle allowingInteraction:(BOOL)allowingInteraction inSystemDomain:(BOOL)systemDomain +- (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle authorizationPrompt:(NSString *)authorizationPrompt allowingInteraction:(BOOL)allowingInteraction inSystemDomain:(BOOL)systemDomain { SUFileManager *fileManager = [SUFileManager defaultManager]; @@ -111,31 +111,40 @@ BOOL canceledAuthorization = NO; BOOL failedToUseSystemDomain = NO; if (auth != NULL && systemDomain) { - AuthorizationItem rightItems[] = { - { .name = kSMRightModifySystemDaemons, .valueLength = 0, .value = NULL, .flags = 0} - }; + // See Apple's 'EvenBetterAuthorizationSample' sample code and + // https://developer.apple.com/library/mac/technotes/tn2095/_index.html#//apple_ref/doc/uid/DTS10003110-CH1-SECTION7 + // We can set a custom right name for authenticating as an administrator + // Using this right rather than using something like kSMRightModifySystemDaemons allows us to present a better worded prompt - AuthorizationRights rights = { - .count = sizeof(rightItems) / sizeof(*rightItems), - .items = rightItems, - }; + const char *rightName = [[NSString stringWithFormat:@"%@.sparkle-auth", hostBundleIdentifier] UTF8String]; + assert(rightName != NULL); - AuthorizationFlags flags = - (AuthorizationFlags)(kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize | kAuthorizationFlagInteractionAllowed); + OSStatus getRightResult = AuthorizationRightGet(rightName, NULL); + if (getRightResult == errAuthorizationDenied) { + if (AuthorizationRightSet(auth, rightName, (__bridge CFTypeRef _Nonnull)(@(kAuthorizationRuleAuthenticateAsAdmin)), (__bridge CFStringRef _Nullable)(authorizationPrompt), NULL, NULL) != errAuthorizationSuccess) { + SULog(@"Failed to make auth right set"); + } + } + + AuthorizationItem right = { .name = rightName, .valueLength = 0, .value = NULL, .flags = 0 }; + AuthorizationRights rights = { .count = 1, .items = &right }; + + AuthorizationFlags flags = (AuthorizationFlags)(kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed); OSStatus copyStatus = AuthorizationCopyRights(auth, &rights, kAuthorizationEmptyEnvironment, flags, NULL); if (copyStatus != errAuthorizationSuccess) { - SULog(@"Failed copying system domain rights with kSMRightModifySystemDaemons: %d", copyStatus); failedToUseSystemDomain = YES; if (copyStatus == errAuthorizationCanceled) { canceledAuthorization = YES; + } else { + SULog(@"Failed copying system domain rights: %d", copyStatus); } } } Boolean submittedJob = false; - if (!failedToUseSystemDomain && auth != NULL) { + if (!canceledAuthorization && !failedToUseSystemDomain && auth != NULL) { CFStringRef domain = (systemDomain ? kSMDomainSystemLaunchd : kSMDomainUserLaunchd); NSString *label = [NSString stringWithFormat:@"%@-sparkle-updater", hostBundleIdentifier]; @@ -147,7 +156,7 @@ if (removeError != NULL) { // It's normal for a job to not be found, so this is not an interesting error if (CFErrorGetCode(removeError) != kSMErrorJobNotFound) { - SULog(@"Remove error: %@", removeError); + SULog(@"Remove job error: %@", removeError); } CFRelease(removeError); } @@ -163,7 +172,9 @@ CFRelease(submitError); } } - + } + + if (auth != NULL) { AuthorizationFree(auth, kAuthorizationFlagDefaults); } @@ -178,7 +189,7 @@ return reply; } -- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath guidedInstallation:(BOOL)guidedInstallation allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler +- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt guidedInstallation:(BOOL)guidedInstallation allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ NSBundle *hostBundle = [NSBundle bundleWithPath:hostBundlePath]; @@ -190,14 +201,14 @@ if (needsSystemAuthorization && !preflighted && !allowingInteraction) { completionHandler(SUAuthorizationReplyAuthorizeLater); } else { - BOOL submittedInstaller = ([self submitInstallerAtPath:installerPath withHostBundle:hostBundle allowingInteraction:allowingInteraction inSystemDomain:needsSystemAuthorization] == SUAuthorizationReplySuccess); + SUAuthorizationReply installerReply = [self submitInstallerAtPath:installerPath withHostBundle:hostBundle authorizationPrompt:authorizationPrompt allowingInteraction:allowingInteraction inSystemDomain:needsSystemAuthorization]; - if (!submittedInstaller) { + if (installerReply == SUAuthorizationReplyFailure) { SULog(@"Failed to submit installer job"); } BOOL submittedProgressTool = NO; - if (submittedInstaller) { + if (installerReply == SUAuthorizationReplySuccess) { submittedProgressTool = [self submitProgressToolAtPath:progressToolPath withHostBundle:hostBundle inSystemDomainForInstaller:needsSystemAuthorization]; if (!submittedProgressTool) { @@ -205,7 +216,11 @@ } } - completionHandler(submittedProgressTool ? SUAuthorizationReplySuccess : SUAuthorizationReplyFailure); + if (installerReply == SUAuthorizationReplyCancelled) { + completionHandler(installerReply); + } else { + completionHandler(submittedProgressTool ? SUAuthorizationReplySuccess : SUAuthorizationReplyFailure); + } } }); } diff --git a/InstallerLauncher/SUInstallerLauncherProtocol.h b/InstallerLauncher/SUInstallerLauncherProtocol.h index aa76076f..d0175c2d 100644 --- a/InstallerLauncher/SUInstallerLauncherProtocol.h +++ b/InstallerLauncher/SUInstallerLauncherProtocol.h @@ -11,6 +11,6 @@ @protocol SUInstallerLauncherProtocol -- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath guidedInstallation:(BOOL)guidedInstallation allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler; +- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt guidedInstallation:(BOOL)guidedInstallation allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler; @end diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 3d1e89cc..40d93b92 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -436,7 +436,9 @@ assert(hostBundlePath != nil); #warning pass actual guided flag eventually - [installerLauncher launchInstallerAtPath:relaunchToolPath progressToolPath:progressToolPath withHostBundlePath:hostBundlePath guidedInstallation:NO allowingInteraction:shouldAllowInstallerInteraction completion:^(SUAuthorizationReply result) { + // The installer launcher could be in a XPC service, so we don't want to do localization in there + NSString *authorizationPrompt = [NSString stringWithFormat:SULocalizedString(@"%1$@ wants to update.", nil), self.host.name]; + [installerLauncher launchInstallerAtPath:relaunchToolPath progressToolPath:progressToolPath withHostBundlePath:hostBundlePath authorizationPrompt:authorizationPrompt guidedInstallation:NO allowingInteraction:shouldAllowInstallerInteraction completion:^(SUAuthorizationReply result) { dispatch_async(dispatch_get_main_queue(), ^{ retrievedLaunchStatus = YES; [launcherConnection invalidate];