From 646ed6f3c453ec97f752a3bac9969f096e46e546 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 26 Jul 2016 20:00:56 -0400 Subject: [PATCH] Improve logic for allowing installer interaction We now distinguish between the updater delegate allowing interaction and the update driver allowing interaction. We also remove an interaction allowed flag from the AppInstaller, letting shouldShowUI flag suffice. --- Autoupdate/AppInstaller.h | 2 +- Autoupdate/AppInstaller.m | 8 ++------ Autoupdate/SUInstaller.h | 2 +- Autoupdate/SUInstaller.m | 11 ++--------- Autoupdate/main.m | 5 ++--- InstallerLauncher/SUInstallerLauncher.m | 18 +++++++++--------- .../SUInstallerLauncherProtocol.h | 2 +- Sparkle/SUInstallerDriver.m | 14 ++++++++++---- 8 files changed, 28 insertions(+), 34 deletions(-) diff --git a/Autoupdate/AppInstaller.h b/Autoupdate/AppInstaller.h index ea1ea48e..a342051d 100644 --- a/Autoupdate/AppInstaller.h +++ b/Autoupdate/AppInstaller.h @@ -11,7 +11,7 @@ @interface AppInstaller : NSObject -- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier allowingInteraction:(BOOL)allowsInteraction; +- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier; - (void)start; diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 942caf53..853a8a6c 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -60,7 +60,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic, strong) TerminationListener *terminationListener; @property (nonatomic, readonly, copy) NSString *hostBundleIdentifier; -@property (nonatomic, readonly) BOOL allowsInteraction; @property (nonatomic) SUHost *host; @property (nonatomic) SUInstallationInputData *installationData; @property (nonatomic, assign) BOOL shouldRelaunch; @@ -87,7 +86,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize agentConnection = _agentConnection; @synthesize receivedUpdaterPong = _receivedUpdaterPong; @synthesize hostBundleIdentifier = _hostBundleIdentifier; -@synthesize allowsInteraction = _allowsInteraction; @synthesize terminationListener = _terminationListener; @synthesize host = _host; @synthesize installationData = _installationData; @@ -102,7 +100,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize performedStage3Installation = _performedStage3Installation; @synthesize agentConnectionCounter = _agentConnectionCounter; -- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier allowingInteraction:(BOOL)allowsInteraction +- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier { if (!(self = [super init])) { return nil; @@ -110,8 +108,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; _hostBundleIdentifier = [hostBundleIdentifier copy]; - _allowsInteraction = allowsInteraction; - _xpcListener = [[NSXPCListener alloc] initWithMachServiceName:SUInstallerServiceNameForBundleIdentifier(hostBundleIdentifier)]; _xpcListener.delegate = self; @@ -443,7 +439,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; dispatch_async(self.installerQueue, ^{ NSError *installerError = nil; - id installer = [SUInstaller installerForHost:self.host expectedInstallationType:self.installationData.installationType updateDirectory:self.installationData.updateDirectoryPath allowingInteraction:self.allowsInteraction versionComparator:[SUStandardVersionComparator standardVersionComparator] error:&installerError]; + id installer = [SUInstaller installerForHost:self.host expectedInstallationType:self.installationData.installationType updateDirectory:self.installationData.updateDirectoryPath versionComparator:[SUStandardVersionComparator standardVersionComparator] error:&installerError]; if (installer == nil) { SULog(@"Error: Failed to create installer instance with error: %@", installerError); diff --git a/Autoupdate/SUInstaller.h b/Autoupdate/SUInstaller.h index 9f8952fc..0f3b73ad 100644 --- a/Autoupdate/SUInstaller.h +++ b/Autoupdate/SUInstaller.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUInstaller : NSObject -+ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory allowingInteraction:(BOOL)allowsInteraction versionComparator:(id )comparator error:(NSError **)error; ++ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory versionComparator:(id )comparator error:(NSError **)error; + (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr isGuided:(nullable BOOL *)isGuidedPtr; diff --git a/Autoupdate/SUInstaller.m b/Autoupdate/SUInstaller.m index 63604e33..f0b8afb2 100644 --- a/Autoupdate/SUInstaller.m +++ b/Autoupdate/SUInstaller.m @@ -113,7 +113,7 @@ return newAppDownloadPath; } -+ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory allowingInteraction:(BOOL)allowsInteraction versionComparator:(id )comparator error:(NSError * __autoreleasing *)error ++ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory versionComparator:(id )comparator error:(NSError * __autoreleasing *)error { BOOL isPackage = NO; BOOL isGuided = NO; @@ -144,14 +144,7 @@ *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Found package installer but '%@=%@' was probably missing in the appcast item enclosure", SUAppcastAttributeInstallationType, SUInstallationTypePackage] }]; } } else { -#warning review this - // Even if we expect an ordinary package, if we don't allow interaction (eg: because we're already running root) - - // we should try a guided installation instead - because an ordinary package installation may not work well - if (allowsInteraction) { - installer = [[SUPackageInstaller alloc] initWithPackagePath:newDownloadPath]; - } else { - installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath]; - } + installer = [[SUPackageInstaller alloc] initWithPackagePath:newDownloadPath]; } } else { if (![expectedInstallationType isEqualToString:SUInstallationTypeApplication]) { diff --git a/Autoupdate/main.m b/Autoupdate/main.m index f8a664ca..e41c19c5 100644 --- a/Autoupdate/main.m +++ b/Autoupdate/main.m @@ -6,14 +6,13 @@ int main(int __unused argc, const char __unused *argv[]) @autoreleasepool { NSArray *args = [[NSProcessInfo processInfo] arguments]; - if (args.count != 3) { + if (args.count != 2) { return EXIT_FAILURE; } NSString *hostBundleIdentifier = args[1]; - BOOL allowsInteraction = args[2].boolValue; - AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostBundleIdentifier:hostBundleIdentifier allowingInteraction:allowsInteraction]; + AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostBundleIdentifier:hostBundleIdentifier]; [appInstaller start]; // Ignore SIGTERM because we are going to catch it ourselves diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index 5d95bd79..e2b61ea8 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -90,7 +90,7 @@ return (submittedJob == true); } -- (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle authorizationPrompt:(NSString *)authorizationPrompt allowingInteraction:(BOOL)allowingInteraction inSystemDomain:(BOOL)systemDomain +- (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle authorizationPrompt:(NSString *)authorizationPrompt inSystemDomain:(BOOL)systemDomain { SUFileManager *fileManager = [SUFileManager defaultManager]; @@ -100,7 +100,7 @@ NSString *hostBundleIdentifier = hostBundle.bundleIdentifier; assert(hostBundleIdentifier != nil); - NSArray *arguments = @[installerPath, hostBundleIdentifier, @(allowingInteraction).stringValue]; + NSArray *arguments = @[installerPath, hostBundleIdentifier]; AuthorizationRef auth = NULL; OSStatus createStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); @@ -260,7 +260,7 @@ return reply; } -- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler +- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingDriverInteraction:(BOOL)allowingDriverInteraction allowingUpdaterInteraction:(BOOL)allowingUpdaterInteraction completion:(void (^)(SUAuthorizationReply))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ NSBundle *hostBundle = [NSBundle bundleWithPath:hostBundlePath]; @@ -268,14 +268,12 @@ BOOL needsSystemAuthorization = SUNeedsSystemAuthorizationAccess(hostBundlePath, installationType); // if we need to use the system domain and we aren't allowed interaction, then try sometime later when interaction is allowed - if (needsSystemAuthorization && !allowingInteraction) { + if (needsSystemAuthorization && !allowingUpdaterInteraction) { + completionHandler(SUAuthorizationReplyFailure); + } else if (needsSystemAuthorization && !allowingDriverInteraction) { completionHandler(SUAuthorizationReplyAuthorizeLater); } else { - SUAuthorizationReply installerReply = [self submitInstallerAtPath:installerPath withHostBundle:hostBundle authorizationPrompt:authorizationPrompt allowingInteraction:allowingInteraction inSystemDomain:needsSystemAuthorization]; - - if (installerReply == SUAuthorizationReplyFailure) { - SULog(@"Failed to submit installer job"); - } + SUAuthorizationReply installerReply = [self submitInstallerAtPath:installerPath withHostBundle:hostBundle authorizationPrompt:authorizationPrompt inSystemDomain:needsSystemAuthorization]; BOOL submittedProgressTool = NO; if (installerReply == SUAuthorizationReplySuccess) { @@ -284,6 +282,8 @@ if (!submittedProgressTool) { SULog(@"Failed to submit progress tool job"); } + } else if (installerReply == SUAuthorizationReplyFailure) { + SULog(@"Failed to submit installer job"); } if (installerReply == SUAuthorizationReplyCanceled) { diff --git a/InstallerLauncher/SUInstallerLauncherProtocol.h b/InstallerLauncher/SUInstallerLauncherProtocol.h index 5fd9fa3c..7894e4c9 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 authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler; +- (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingDriverInteraction:(BOOL)allowingDriverInteraction allowingUpdaterInteraction:(BOOL)allowingUpdaterInteraction completion:(void (^)(SUAuthorizationReply))completionHandler; @end diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 8a285fc8..adea8e28 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -416,12 +416,17 @@ installerLauncher = launcherConnection.remoteObjectProxy; } -#warning review this... - BOOL shouldAllowInstallerInteraction = !silently; + // This determines if our updater delegate allows interaction + // If the delegate disallows interaction, then the update cannot be continued + BOOL updaterAllowsInteraction = YES; if ([self.updaterDelegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)]) { - shouldAllowInstallerInteraction = [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]; + updaterAllowsInteraction = [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]; } + // Our driver (automatic or UI based) has a say if interaction is allowed as well + // An automatic driver may disallow interaction but the updater could try again later a UI based driver that does allow interaction + BOOL driverAllowsInteraction = !silently; + NSString *hostBundlePath = self.host.bundle.bundlePath; assert(hostBundlePath != nil); @@ -430,13 +435,14 @@ // 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 installationType:installationType allowingInteraction:shouldAllowInstallerInteraction completion:^(SUAuthorizationReply result) { + [installerLauncher launchInstallerAtPath:relaunchToolPath progressToolPath:progressToolPath withHostBundlePath:hostBundlePath authorizationPrompt:authorizationPrompt installationType:installationType allowingDriverInteraction:driverAllowsInteraction allowingUpdaterInteraction:updaterAllowsInteraction completion:^(SUAuthorizationReply result) { dispatch_async(dispatch_get_main_queue(), ^{ retrievedLaunchStatus = YES; [launcherConnection invalidate]; switch (result) { case SUAuthorizationReplyFailure: + SULog(@"Error: Failed to gain authorization required to update target"); completionHandler([NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred while launching the installer. Please try again later.", nil) }]); break; case SUAuthorizationReplyCanceled: