diff --git a/Autoupdate/AppInstaller.h b/Autoupdate/AppInstaller.h index 22a97eac..66746209 100644 --- a/Autoupdate/AppInstaller.h +++ b/Autoupdate/AppInstaller.h @@ -13,7 +13,7 @@ NS_ASSUME_NONNULL_BEGIN @interface AppInstaller : NSObject -- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier; +- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName; - (void)start; diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 9f13b664..f0c3232e 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -55,6 +55,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic) SUUpdateValidator *updateValidator; @property (nonatomic, readonly, copy) NSString *hostBundleIdentifier; +@property (nonatomic, readonly) NSString *homeDirectory; +@property (nonatomic, readonly) NSString *userName; @property (nonatomic) SUHost *host; @property (nonatomic, copy) NSString *updateDirectoryPath; @property (nonatomic, copy) NSString *downloadName; @@ -86,6 +88,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize agentConnection = _agentConnection; @synthesize receivedUpdaterPong = _receivedUpdaterPong; @synthesize hostBundleIdentifier = _hostBundleIdentifier; +@synthesize homeDirectory = _homeDirectory; +@synthesize userName = _userName; @synthesize terminationListener = _terminationListener; @synthesize updateValidator = _updateValidator; @synthesize host = _host; @@ -107,7 +111,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize finishedValidation = _finishedValidation; @synthesize agentInitiatedConnection = _agentInitiatedConnection; -- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier +- (instancetype)initWithHostBundleIdentifier:(NSString *)hostBundleIdentifier homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName { if (!(self = [super init])) { return nil; @@ -115,6 +119,9 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; _hostBundleIdentifier = [hostBundleIdentifier copy]; + _homeDirectory = [homeDirectory copy]; + _userName = [userName copy]; + _xpcListener = [[NSXPCListener alloc] initWithMachServiceName:SPUInstallerServiceNameForBundleIdentifier(hostBundleIdentifier)]; _xpcListener.delegate = self; @@ -454,7 +461,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; dispatch_async(self.installerQueue, ^{ NSError *installerError = nil; - id installer = [SUInstaller installerForHost:self.host expectedInstallationType:self.installationType updateDirectory:self.updateDirectoryPath error:&installerError]; + id installer = [SUInstaller installerForHost:self.host expectedInstallationType:self.installationType updateDirectory:self.updateDirectoryPath homeDirectory:self.homeDirectory userName:self.userName error:&installerError]; if (installer == nil) { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/Autoupdate/SUGuidedPackageInstaller.h b/Autoupdate/SUGuidedPackageInstaller.h index e297cf7c..8bdef5e5 100644 --- a/Autoupdate/SUGuidedPackageInstaller.h +++ b/Autoupdate/SUGuidedPackageInstaller.h @@ -21,6 +21,6 @@ A guided installation can be started by applications other than the application @interface SUGuidedPackageInstaller : NSObject -- (instancetype)initWithPackagePath:(NSString *)packagePath; +- (instancetype)initWithPackagePath:(NSString *)packagePath homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName; @end diff --git a/Autoupdate/SUGuidedPackageInstaller.m b/Autoupdate/SUGuidedPackageInstaller.m index 5632f0ae..5b6c8b60 100644 --- a/Autoupdate/SUGuidedPackageInstaller.m +++ b/Autoupdate/SUGuidedPackageInstaller.m @@ -16,18 +16,24 @@ @interface SUGuidedPackageInstaller () @property (nonatomic, readonly, copy) NSString *packagePath; +@property (nonatomic, readonly, copy) NSString *homeDirectory; +@property (nonatomic, readonly, copy) NSString *userName; @end @implementation SUGuidedPackageInstaller @synthesize packagePath = _packagePath; +@synthesize homeDirectory = _homeDirectory; +@synthesize userName = _userName; -- (instancetype)initWithPackagePath:(NSString *)packagePath +- (instancetype)initWithPackagePath:(NSString *)packagePath homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName { self = [super init]; if (self != nil) { _packagePath = [packagePath copy]; + _homeDirectory = [homeDirectory copy]; + _userName = [userName copy]; } return self; } @@ -45,6 +51,8 @@ NSTask *task = [[NSTask alloc] init]; task.launchPath = installerPath; task.arguments = @[@"-pkg", self.packagePath, @"-target", @"/"]; + // Set the $HOME and $USER variables so pre/post install scripts reference the correct user environment + task.environment = @{@"HOME": self.homeDirectory, @"USER": self.userName}; task.standardError = [NSPipe pipe]; task.standardOutput = [NSPipe pipe]; diff --git a/Autoupdate/SUInstaller.h b/Autoupdate/SUInstaller.h index 2e84049d..83273039 100644 --- a/Autoupdate/SUInstaller.h +++ b/Autoupdate/SUInstaller.h @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUInstaller : NSObject -+ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory error:(NSError **)error; ++ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName error:(NSError **)error; + (nullable 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 7db302e8..9c687632 100644 --- a/Autoupdate/SUInstaller.m +++ b/Autoupdate/SUInstaller.m @@ -112,7 +112,7 @@ return newAppDownloadPath; } -+ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory error:(NSError * __autoreleasing *)error ++ (nullable id)installerForHost:(SUHost *)host expectedInstallationType:(NSString *)expectedInstallationType updateDirectory:(NSString *)updateDirectory homeDirectory:(NSString *)homeDirectory userName:(NSString *)userName error:(NSError * __autoreleasing *)error { BOOL isPackage = NO; BOOL isGuided = NO; @@ -135,7 +135,7 @@ *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Found guided package installer but '%@=%@' was probably missing in the appcast item enclosure", SUAppcastAttributeInstallationType, SPUInstallationTypeGuidedPackage] }]; } } else { - installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath]; + installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath homeDirectory:homeDirectory userName:userName]; } } else if (isPackage) { if (![expectedInstallationType isEqualToString:SPUInstallationTypeInteractivePackage]) { diff --git a/Autoupdate/main.m b/Autoupdate/main.m index 1e4f3d06..101b6e6b 100644 --- a/Autoupdate/main.m +++ b/Autoupdate/main.m @@ -9,13 +9,15 @@ int main(int __unused argc, const char __unused *argv[]) @autoreleasepool { NSArray *args = [[NSProcessInfo processInfo] arguments]; - if (args.count != 2) { + if (args.count != 4) { return EXIT_FAILURE; } NSString *hostBundleIdentifier = args[1]; + NSString *homeDirectory = args[2]; + NSString *userName = args[3]; - AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostBundleIdentifier:hostBundleIdentifier]; + AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostBundleIdentifier:hostBundleIdentifier homeDirectory:homeDirectory userName:userName]; [appInstaller start]; // Ignore SIGTERM because we are going to catch it ourselves diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index f1466e78..d60e6be9 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -109,11 +109,19 @@ NSString *hostBundleIdentifier = hostBundle.bundleIdentifier; assert(hostBundleIdentifier != nil); + NSString *homeDirectory = NSHomeDirectory(); + assert(homeDirectory != nil); + + NSString *userName = NSUserName(); + assert(userName != nil); + // The first argument has to be the path to the program, and the second is a host identifier so that the installer knows what mach services to host + // The third and forth arguments are for home directory and user name which only pkg installer scripts may need // We intentionally do not pass any more arguments. Anything else should be done via IPC. // This is compatible to SMJobBless() which does not allow arguments // Even though we aren't using that function for now, it'd be wise to not decrease compatibility to it - NSArray *arguments = @[installerPath, hostBundleIdentifier]; + + NSArray *arguments = @[installerPath, hostBundleIdentifier, homeDirectory, userName]; AuthorizationRef auth = NULL; OSStatus createStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); diff --git a/Tests/SUInstallerTest.m b/Tests/SUInstallerTest.m index 4f698f1d..dcfb18b9 100644 --- a/Tests/SUInstallerTest.m +++ b/Tests/SUInstallerTest.m @@ -54,7 +54,9 @@ SUHost *host = [[SUHost alloc] initWithBundle:bundle]; NSError *installerError = nil; - id installer = [SUInstaller installerForHost:host expectedInstallationType:SPUInstallationTypeGuidedPackage updateDirectory:[path stringByDeletingLastPathComponent] error:&installerError]; + // Note: we may not be using the "correct" home directory or user name (they will be root) but our test pkg does not have + // pre/post install scripts so it doesn't matter + id installer = [SUInstaller installerForHost:host expectedInstallationType:SPUInstallationTypeGuidedPackage updateDirectory:[path stringByDeletingLastPathComponent] homeDirectory:NSHomeDirectory() userName:NSUserName() error:&installerError]; if (installer == nil) { XCTFail(@"Installer is nil with error: %@", installerError);