diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index f8257b69..d0109367 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -13,7 +13,7 @@ @implementation SUInstallerLauncher -- (void)launchInstallerAtPath:(NSString *)installerPath withArguments:(NSArray *)arguments completion:(void (^)(BOOL success))completionHandler +- (void)launchInstallerAtPath:(NSString *)installerPath withHostBundleIdentifier:(NSString *)hostBundleIdentifier inheritingPrivileges:(BOOL)inheritingPrivileges completion:(void (^)(BOOL success))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ NSError *quarantineError = nil; @@ -24,14 +24,39 @@ SULog(@"Failed to release quarantine on installer at %@ with error %@", installerPath, quarantineError); } - NSError *launchError = nil; - NSRunningApplication *runningApplication = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:installerPath] options:(NSWorkspaceLaunchOptions)(NSWorkspaceLaunchDefault | NSWorkspaceLaunchNewInstance) configuration:@{NSWorkspaceLaunchConfigurationArguments : arguments} error:&launchError]; + NSArray *arguments = @[hostBundleIdentifier, @(inheritingPrivileges).stringValue]; - if (runningApplication == nil) { - SULog(@"Failed to launch AutoUpdate app with error: %@", launchError); + if (!inheritingPrivileges) { + // This new process does not inherit privileges because it's being launched via LaunchServices + // This allows the installer app to make authorization requests for example from a non-sandboxed XPC service + NSError *launchError = nil; + NSRunningApplication *runningApplication = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:installerPath] options:(NSWorkspaceLaunchOptions)(NSWorkspaceLaunchDefault | NSWorkspaceLaunchNewInstance) configuration:@{NSWorkspaceLaunchConfigurationArguments : arguments} error:&launchError]; + + if (runningApplication == nil) { + SULog(@"Failed to launch %@ app with error: %@", @SPARKLE_RELAUNCH_TOOL_NAME, launchError); + } + + completionHandler(runningApplication != nil); + } else { + // This new process inherits privileges and thus will not use the authorization reference API, nor request a dialog for authorization + // If we have root permissions, so will this new process + // The reason I don't create a NSBundle to locate the executable is because I worry about NSBundle caching and if we enter here multiple times + NSString *executablePath = [[[installerPath stringByAppendingPathComponent:@"Contents"] stringByAppendingPathComponent:@"MacOS"] stringByAppendingPathComponent:@SPARKLE_RELAUNCH_TOOL_NAME]; + + NSTask *task = [[NSTask alloc] init]; + task.launchPath = executablePath; + task.arguments = arguments; + + BOOL success = NO; + @try { + [task launch]; + success = YES; + } @catch (NSException *exception) { + SULog(@"Failed to launch %@ app with exception: %@", @SPARKLE_RELAUNCH_TOOL_NAME, exception); + } + + completionHandler(success); } - - completionHandler(runningApplication != nil); }); } diff --git a/InstallerLauncher/SUInstallerLauncherProtocol.h b/InstallerLauncher/SUInstallerLauncherProtocol.h index ca43fa74..72b84794 100644 --- a/InstallerLauncher/SUInstallerLauncherProtocol.h +++ b/InstallerLauncher/SUInstallerLauncherProtocol.h @@ -10,6 +10,6 @@ @protocol SUInstallerLauncherProtocol -- (void)launchInstallerAtPath:(NSString *)installerPath withArguments:(NSArray *)arguments completion:(void (^)(BOOL success))completionHandler; +- (void)launchInstallerAtPath:(NSString *)installerPath withHostBundleIdentifier:(NSString *)hostBundleIdentifier inheritingPrivileges:(BOOL)inheritingPrivileges completion:(void (^)(BOOL success))completionHandler; @end diff --git a/Sparkle/Autoupdate/AppInstaller.h b/Sparkle/Autoupdate/AppInstaller.h index a244aaf5..8f0fe7b0 100644 --- a/Sparkle/Autoupdate/AppInstaller.h +++ b/Sparkle/Autoupdate/AppInstaller.h @@ -11,7 +11,7 @@ @interface AppInstaller : NSObject -- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier; +- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier inheritsPrivileges:(BOOL)inheritsPrivileges; - (void)start; diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index d38646e0..242ec375 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -44,6 +44,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic, strong) TerminationListener *terminationListener; @property (nonatomic, readonly, copy) NSString *hostBundleIdentifier; +@property (nonatomic, readonly) BOOL inheritsPrivileges; @property (nonatomic) SUHost *host; @property (nonatomic) SULocalMessagePort *localPort; @property (nonatomic) SURemoteMessagePort *remotePort; @@ -59,13 +60,13 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic) BOOL performedStage1Installation; @property (nonatomic) BOOL performedStage2Installation; @property (nonatomic) BOOL performedStage3Installation; -@property (nonatomic) BOOL waitingForStage2Feedback; @end @implementation AppInstaller @synthesize hostBundleIdentifier = _hostBundleIdentifier; +@synthesize inheritsPrivileges = _inheritsPrivileges; @synthesize terminationListener = _terminationListener; @synthesize localPort = _localPort; @synthesize remotePort = _remotePort; @@ -80,7 +81,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize performedStage1Installation = _performedStage1Installation; @synthesize performedStage2Installation = _performedStage2Installation; @synthesize performedStage3Installation = _performedStage3Installation; -@synthesize waitingForStage2Feedback = _waitingForStage2Feedback; /* * hostPath - path to host (original) application @@ -92,7 +92,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; * shouldShowUI - indicates if we should show the status window when installing the update */ -- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier +- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier inheritsPrivileges:(BOOL)inheritsPrivileges { if (!(self = [super init])) { return nil; @@ -100,6 +100,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; _hostBundleIdentifier = [hostBundleIdentifier copy]; + _inheritsPrivileges = inheritsPrivileges; + self.localPort = [[SULocalMessagePort alloc] initWithServiceName:SUAutoUpdateServiceNameForBundleIdentifier(hostBundleIdentifier) @@ -434,26 +436,18 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; NSData *sendData = [NSData dataWithBytes:sendInformation length:sizeof(sendInformation)]; - self.waitingForStage2Feedback = (canPerformSilentInstall == 0); - [self.remotePort sendMessageWithIdentifier:SUInstallationFinishedStage1 data:sendData completion:^(BOOL success) { if (!success) { SULog(@"Error sending stage 1 finish"); - - if (self.waitingForStage2Feedback) { - SULog(@"Exiting because sending Stage 1 Installation Failed and we need to wait for continuation response"); - [self cleanupAndExitWithStatus:EXIT_FAILURE]; - } + [self cleanupAndExitWithStatus:EXIT_FAILURE]; } }]; self.performedStage1Installation = YES; - if (!self.waitingForStage2Feedback) { - // Stage 2 can still be run before we finish installation - // if the updater requests for it before the app is terminated - [self finishInstallationAfterHostTermination]; - } + // Stage 2 can still be run before we finish installation + // if the updater requests for it before the app is terminated + [self finishInstallationAfterHostTermination]; }); }); } @@ -464,14 +458,15 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; return; } - if (self.shouldShowUI && [self.installer mayNeedToRequestAuthorization]) { -#warning should probably do this on main thread - // We should activate our app so that the auth prompt will be active - [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; + if (self.shouldShowUI && !self.inheritsPrivileges && [self.installer mayNeedToRequestAuthorization]) { + dispatch_async(dispatch_get_main_queue(), ^{ + // We should activate our app so that the auth prompt will be active + [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; + }); } NSError *secondStageError = nil; - if (![self.installer performSecondStageAllowingUI:self.shouldShowUI error:&secondStageError]) { + if (![self.installer performSecondStageAllowingAuthorization:!self.inheritsPrivileges allowingUI:self.shouldShowUI error:&secondStageError]) { SULog(@"Error: Failed to resume installer on stage 2 with error: %@", secondStageError); [self.installer cleanup]; self.installer = nil; @@ -491,12 +486,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; SULog(@"Error sending stage 2 finish"); } }]; - - if (self.waitingForStage2Feedback) { - self.waitingForStage2Feedback = NO; - - [self finishInstallationAfterHostTermination]; - } // otherwise stage 3 will perform when the target terminates }); } } @@ -589,8 +578,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; dispatch_async(self.installerQueue, ^{ [self.installer cleanup]; - [SUInstaller mdimportInstallationPath:installationPath]; - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self cleanupAndExitWithStatus:EXIT_SUCCESS]; }); diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index 09619336..5854c220 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -32,14 +32,15 @@ int main(int __unused argc, const char __unused *argv[]) { @autoreleasepool { - NSArray *args = [[NSProcessInfo processInfo] arguments]; - if (args.count != 2) { + NSArray *args = [[NSProcessInfo processInfo] arguments]; + if (args.count != 3) { return EXIT_FAILURE; } NSString *hostBundleIdentifier = args[1]; + BOOL inheritsPrivileges = args[2].boolValue; - AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostBundleIdentifier:hostBundleIdentifier]; + AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostBundleIdentifier:hostBundleIdentifier inheritsPrivileges:inheritsPrivileges]; AppDelegate *delegate = [[AppDelegate alloc] initWithAppInstaller:appInstaller]; diff --git a/Sparkle/Autoupdate/SUInstallerProtocol.h b/Sparkle/Autoupdate/SUInstallerProtocol.h index b56c19be..72f9419d 100644 --- a/Sparkle/Autoupdate/SUInstallerProtocol.h +++ b/Sparkle/Autoupdate/SUInstallerProtocol.h @@ -16,10 +16,11 @@ - (BOOL)performFirstStage:(NSError **)error; // Stage 2 is where any further installation work can be done prior to the user application being terminated +// The allowsAuthorization flag indicates whether this and the 3rd stage can request authorization if the updater has insufficient privileges to install // The allowsUI flag indicates whether this and the 3rd stage can show UI or not, possibly affecting whether or not this stage succeeds. // Eg: This may be appropriate for first showing an authorization prompt before the user application is terminated (if the operation succeeds) // Should be able to be called from non-main thread -- (BOOL)performSecondStageAllowingUI:(BOOL)allowsUI error:(NSError **)error; +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError **)error; // Stage 3 occurs after the user application has has been terminated. This is where the final installation work can be done. // After this stage is done, the user application will be relaunched. diff --git a/Sparkle/SUGuidedPackageInstaller.m b/Sparkle/SUGuidedPackageInstaller.m index 4c8d1f41..e08d7fa4 100644 --- a/Sparkle/SUGuidedPackageInstaller.m +++ b/Sparkle/SUGuidedPackageInstaller.m @@ -43,7 +43,7 @@ return YES; } -- (BOOL)performSecondStageAllowingUI:(BOOL)allowsUI error:(NSError *__autoreleasing *)error +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error { if (!allowsUI) { if (error != NULL) { @@ -52,7 +52,8 @@ return NO; } - self.fileManager = [SUFileManager fileManagerAllowingAuthorization:YES]; + // If we're root, we can allow using the authorization APIs + self.fileManager = [SUFileManager fileManagerAllowingAuthorization:(allowsAuthorization || (geteuid() == 0))]; return [self.fileManager grantAuthorizationPrivilegesWithError:error]; } diff --git a/Sparkle/SUInstaller.h b/Sparkle/SUInstaller.h index 585528a0..e7a7f8ce 100644 --- a/Sparkle/SUInstaller.h +++ b/Sparkle/SUInstaller.h @@ -22,8 +22,6 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)installationPathForHost:(SUHost *)host; -+ (void)mdimportInstallationPath:(NSString *)installationPath; - @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index a3464dce..1e4fa526 100644 --- a/Sparkle/SUInstaller.m +++ b/Sparkle/SUInstaller.m @@ -149,24 +149,4 @@ return [bundle bundlePath]; } -+ (void)mdimportInstallationPath:(NSString *)installationPath -{ - // *** GETS CALLED ON NON-MAIN THREAD! - - SULog(@"mdimporting"); - - NSTask *mdimport = [[NSTask alloc] init]; - [mdimport setLaunchPath:@"/usr/bin/mdimport"]; - [mdimport setArguments:@[installationPath]]; - @try { - [mdimport launch]; - [mdimport waitUntilExit]; - } - @catch (NSException *launchException) - { - // No big deal. - SULog(@"Error: %@", [launchException description]); - } -} - @end diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 089ac2f3..d604c40a 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -334,7 +334,7 @@ hasTargetTerminated = (BOOL)*((const uint8_t *)data.bytes + 1); } - [self.delegate installerDidFinishPreparationAndWillInstallImmediately:(hasTargetTerminated && canInstallSilently) silently:canInstallSilently]; + [self.delegate installerDidFinishPreparationAndWillInstallImmediately:hasTargetTerminated silently:canInstallSilently]; } else if (identifier == SUInstallationFinishedStage2) { self.currentStage = identifier; if (!self.startedInstalling) { @@ -495,7 +495,12 @@ installerLauncher = launcherConnection.remoteObjectProxy; } - [installerLauncher launchInstallerAtPath:relaunchToolPath withArguments:@[hostBundleIdentifier] completion:^(BOOL success) { + BOOL shouldInheritPrivileges = NO; + if ([self.updaterDelegate respondsToSelector:@selector(updaterShouldInheritInstallPrivileges:)]) { + shouldInheritPrivileges = [self.updaterDelegate updaterShouldInheritInstallPrivileges:self.updater]; + } + + [installerLauncher launchInstallerAtPath:relaunchToolPath withHostBundleIdentifier:hostBundleIdentifier inheritingPrivileges:shouldInheritPrivileges completion:^(BOOL success) { dispatch_async(dispatch_get_main_queue(), ^{ retrievedLaunchStatus = YES; [launcherConnection invalidate]; diff --git a/Sparkle/SUPackageInstaller.m b/Sparkle/SUPackageInstaller.m index 7d5a12e3..c95b779b 100644 --- a/Sparkle/SUPackageInstaller.m +++ b/Sparkle/SUPackageInstaller.m @@ -47,7 +47,7 @@ static NSString *SUOpenUtilityPath = @"/usr/bin/open"; return YES; } -- (BOOL)performSecondStageAllowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)__unused allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error { return allowsUI; } diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 9f0b0a1d..1188bef0 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -224,15 +224,17 @@ return YES; } -- (BOOL)performSecondStageAllowingUI:(BOOL)allowsUI error:(NSError *__autoreleasing *)error +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error { - self.fileManager = [SUFileManager fileManagerAllowingAuthorization:allowsUI]; + self.fileManager = [SUFileManager fileManagerAllowingAuthorization:allowsAuthorization && allowsUI]; // Bring up authorization prompt right away if we need it - if (![self canInstallSilently]) { + BOOL canInstallSilently = [self canInstallSilently]; + if (allowsAuthorization && !canInstallSilently) { return [self.fileManager grantAuthorizationPrivilegesWithError:error]; + } else { + return canInstallSilently; } - return YES; } - (BOOL)performThirdStage:(NSError * __autoreleasing *)error @@ -272,8 +274,10 @@ if (oldTempURL != nil) { // Cleanup: move the old app to the trash + // If we are the root user it's very possible that there is no trash directory to move the file into + // In that case, don't log out an error. Removing tempOldDirectoryURL later will remove the app NSError *trashError = nil; - if (![fileManager moveItemAtURLToTrash:oldTempURL error:&trashError]) { + if (![fileManager moveItemAtURLToTrash:oldTempURL error:&trashError] && trashError.code != NSFileNoSuchFileError) { SULog(@"Failed to move %@ to trash with error %@", oldTempURL, trashError); } diff --git a/Sparkle/SUUpdaterDelegate.h b/Sparkle/SUUpdaterDelegate.h index 7cc5bd8f..c240c0c1 100644 --- a/Sparkle/SUUpdaterDelegate.h +++ b/Sparkle/SUUpdaterDelegate.h @@ -219,6 +219,21 @@ SU_EXPORT extern NSString *const SUUpdaterAppcastNotificationKey; */ - (NSString *)pathToInstallerProgressAppForUpdater:(SUUpdater *)updater; +/*! + Returns whether or not the updater should inherit its parent's privileges during installation + + Use this to override the default behavior which is to not inherit privileges. + + If privileges are inherited, then no authorization prompt will show up, and + the installation will succeed only if the process has sufficient privileges. + + On the other hand, if privileges are not inherited (the default), the installer may + request an authorization prompt to obtain additional privileges. + + \param updater The SUUpdater instance. + */ +- (BOOL)updaterShouldInheritInstallPrivileges:(SUUpdater *)updater; + /*! Returns the decryption password (if any) which is used to extract the update archive DMG. diff --git a/sparkle-cli/AppDelegate.m b/sparkle-cli/AppDelegate.m index 1455d287..e310ac8e 100644 --- a/sparkle-cli/AppDelegate.m +++ b/sparkle-cli/AppDelegate.m @@ -10,7 +10,7 @@ #import #import "SUCommandLineUserDriver.h" -@interface AppDelegate () +@interface AppDelegate () @property (nonatomic, readonly) SUUpdater *updater; @@ -35,11 +35,16 @@ } id userDriver = [[SUCommandLineUserDriver alloc] initWithBundle:bundle]; - _updater = [[SUUpdater alloc] initWithHostBundle:bundle userDriver:userDriver delegate:nil]; + _updater = [[SUUpdater alloc] initWithHostBundle:bundle userDriver:userDriver delegate:self]; } return self; } +- (BOOL)updaterShouldInheritInstallPrivileges:(SUUpdater *)__unused updater +{ + return YES; +} + - (void)applicationDidFinishLaunching:(NSNotification *)__unused aNotification { // Kick off a scheduled update check once we start the updater later