mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-29 05:24:26 +00:00
Allow updater to be ran as root & inherit privileges
The installer can now inherit privileges from the updater, which inherits privileges from the process that spawns the updater. As such, we can now run the updater & installer as root, and no authorization dialog can prompt up. The installer now must abort or install if the target app has terminated. Zombie processes will no longer stay around. Because the root user has no trash directory, we ignore logging the error if it was not found. We also avoid invoking mdimport which fails as root on the installed app. mdimport may have been used before for updating mdimporter files, however, it should be the developer's responsibility to ensure their mdimporter files have an updated date stamp, which will work if a user updates an app manually. Other than that, we already touch the app to register it with LaunchServices, so that should suffice.
This commit is contained in:
1 parent
65e2bac89e
commit
ef134d9245
14 files changed
+97
-75
No files matched your search
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
Vendored
+1
-1
@@ -11,7 +11,7 @@
|
||||
|
||||
@interface AppInstaller : NSObject <SUUnarchiverDelegate>
|
||||
|
||||
- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier;
|
||||
- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier inheritsPrivileges:(BOOL)inheritsPrivileges;
|
||||
|
||||
- (void)start;
|
||||
|
||||
|
||||
Vendored
+15
-28
@@ -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];
|
||||
});
|
||||
|
||||
Vendored
+4
-3
@@ -32,14 +32,15 @@ int main(int __unused argc, const char __unused *argv[])
|
||||
{
|
||||
@autoreleasepool
|
||||
{
|
||||
NSArray *args = [[NSProcessInfo processInfo] arguments];
|
||||
if (args.count != 2) {
|
||||
NSArray<NSString *> *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];
|
||||
|
||||
|
||||
+2
-1
@@ -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.
|
||||
|
||||
Vendored
+3
-2
@@ -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];
|
||||
}
|
||||
|
||||
Vendored
-2
@@ -22,8 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
+ (NSString *)installationPathForHost:(SUHost *)host;
|
||||
|
||||
+ (void)mdimportInstallationPath:(NSString *)installationPath;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
Vendored
-20
@@ -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
|
||||
Vendored
+7
-2
@@ -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];
|
||||
|
||||
Vendored
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
Vendored
+9
-5
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Vendored
+15
@@ -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.
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#import <Sparkle/Sparkle.h>
|
||||
#import "SUCommandLineUserDriver.h"
|
||||
|
||||
@interface AppDelegate ()
|
||||
@interface AppDelegate () <SUUpdaterDelegate>
|
||||
|
||||
@property (nonatomic, readonly) SUUpdater *updater;
|
||||
|
||||
@@ -35,11 +35,16 @@
|
||||
}
|
||||
|
||||
id <SUUserDriver> 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
|
||||
|
||||
Reference in new issue
Block a user