diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index 8b4ee411..001e1fa2 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -11,7 +11,6 @@ #import "SULog.h" #import "SUMessageTypes.h" #import "SUSystemAuthorization.h" -#import "SUSubmitInstaller.h" @implementation SUInstallerLauncher @@ -85,6 +84,100 @@ return (submittedJob == true); } +- (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle allowingInteraction:(BOOL)allowingInteraction inSystemDomain:(BOOL)systemDomain +{ + SUFileManager *fileManager = [SUFileManager defaultManager]; + + NSURL *installerURL = [NSURL fileURLWithPath:installerPath]; + + NSError *quarantineError = nil; + if (![fileManager releaseItemFromQuarantineAtRootURL:installerURL error:&quarantineError]) { + // Probably not a fatal error because we are submitting the executable through launchd + SULog(@"Failed to release quarantine on installer at %@ with error %@", installerPath, quarantineError); + } + + NSString *hostBundleIdentifier = hostBundle.bundleIdentifier; + assert(hostBundleIdentifier != nil); + + NSArray *arguments = @[installerPath, hostBundleIdentifier, @(allowingInteraction).stringValue]; + + AuthorizationRef auth = NULL; + OSStatus createStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); + if (createStatus != errAuthorizationSuccess) { + auth = NULL; + SULog(@"Failed to create authorization reference: %d", createStatus); + } + + BOOL canceledAuthorization = NO; + BOOL failedToUseSystemDomain = NO; + if (auth != NULL && systemDomain) { + AuthorizationItem rightItems[] = { + { .name = kSMRightModifySystemDaemons, .valueLength = 0, .value = NULL, .flags = 0} + }; + + AuthorizationRights rights = { + .count = sizeof(rightItems) / sizeof(*rightItems), + .items = rightItems, + }; + + AuthorizationFlags flags = + (AuthorizationFlags)(kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize | 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; + } + } + } + + Boolean submittedJob = false; + if (!failedToUseSystemDomain && auth != NULL) { + CFStringRef domain = (systemDomain ? kSMDomainSystemLaunchd : kSMDomainUserLaunchd); + NSString *label = [NSString stringWithFormat:@"%@-sparkle-updater", hostBundleIdentifier]; + + // Try to remove the job from launchd if it is already running + // We could invoke SMJobCopyDictionary() first to see if the job exists, but I'd rather avoid + // using it because the headers indicate it may be removed one day without any replacement + CFErrorRef removeError = NULL; + if (!SMJobRemove(domain, (__bridge CFStringRef)(label), auth, true, &removeError)) { + 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); + } + CFRelease(removeError); + } + } + + NSDictionary *jobDictionary = @{@"Label" : label, @"ProgramArguments" : arguments, @"EnableTransactions" : @NO, @"KeepAlive" : @{@"SuccessfulExit" : @NO}, @"RunAtLoad" : @NO, @"NICE" : @0, @"LaunchOnlyOnce": @YES, @"MachServices" : @{SUInstallerServiceNameForBundleIdentifier(hostBundleIdentifier) : @YES, SUProgressAgentServiceNameForBundleIdentifier(hostBundleIdentifier) : @YES}}; + + CFErrorRef submitError = NULL; + submittedJob = SMJobSubmit(domain, (__bridge CFDictionaryRef)(jobDictionary), auth, &submitError); + if (!submittedJob) { + if (submitError != NULL) { + SULog(@"Submit error: %@", submitError); + CFRelease(submitError); + } + } + + AuthorizationFree(auth, kAuthorizationFlagDefaults); + } + + SUAuthorizationReply reply; + if (submittedJob == true) { + reply = SUAuthorizationReplySuccess; + } else if (canceledAuthorization) { + reply = SUAuthorizationReplyCancelled; + } else { + reply = SUAuthorizationReplyFailure; + } + return reply; +} + - (void)launchInstallerAtPath:(NSString *)installerPath progressToolPath:(NSString *)progressToolPath withHostBundlePath:(NSString *)hostBundlePath guidedInstallation:(BOOL)guidedInstallation allowingInteraction:(BOOL)allowingInteraction completion:(void (^)(SUAuthorizationReply))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ @@ -97,7 +190,7 @@ if (needsSystemAuthorization && !preflighted && !allowingInteraction) { completionHandler(SUAuthorizationReplyAuthorizeLater); } else { - BOOL submittedInstaller = ([SUSubmitInstaller submitInstallerAtPath:installerPath withHostBundle:hostBundle allowingInteraction:allowingInteraction inSystemDomain:needsSystemAuthorization] == SUAuthorizationReplySuccess); + BOOL submittedInstaller = ([self submitInstallerAtPath:installerPath withHostBundle:hostBundle allowingInteraction:allowingInteraction inSystemDomain:needsSystemAuthorization] == SUAuthorizationReplySuccess); if (!submittedInstaller) { SULog(@"Failed to submit installer job"); diff --git a/InstallerLauncher/SUSubmitInstaller.h b/InstallerLauncher/SUSubmitInstaller.h deleted file mode 100644 index 4f0eb014..00000000 --- a/InstallerLauncher/SUSubmitInstaller.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// SUSubmitInstaller.h -// Sparkle -// -// Created by Mayur Pawashe on 7/17/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -#import -#import "SUAuthorizationReply.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface SUSubmitInstaller : NSObject - -+ (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle allowingInteraction:(BOOL)allowingInteraction inSystemDomain:(BOOL)systemDomain; - -@end - -NS_ASSUME_NONNULL_END diff --git a/InstallerLauncher/SUSubmitInstaller.m b/InstallerLauncher/SUSubmitInstaller.m deleted file mode 100644 index 1efc9ec6..00000000 --- a/InstallerLauncher/SUSubmitInstaller.m +++ /dev/null @@ -1,111 +0,0 @@ -// -// SUSubmitInstaller.m -// Sparkle -// -// Created by Mayur Pawashe on 7/17/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -#import "SUSubmitInstaller.h" -#import "SUFileManager.h" -#import "SULog.h" -#import "SUMessageTypes.h" -#import - -@implementation SUSubmitInstaller - -+ (SUAuthorizationReply)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle allowingInteraction:(BOOL)allowingInteraction inSystemDomain:(BOOL)systemDomain -{ - SUFileManager *fileManager = [SUFileManager defaultManager]; - - NSURL *installerURL = [NSURL fileURLWithPath:installerPath]; - - NSError *quarantineError = nil; - if (![fileManager releaseItemFromQuarantineAtRootURL:installerURL error:&quarantineError]) { - // Probably not a fatal error because we are submitting the executable through launchd - SULog(@"Failed to release quarantine on installer at %@ with error %@", installerPath, quarantineError); - } - - NSString *hostBundleIdentifier = hostBundle.bundleIdentifier; - assert(hostBundleIdentifier != nil); - - NSArray *arguments = @[installerPath, hostBundleIdentifier, @(allowingInteraction).stringValue]; - - AuthorizationRef auth = NULL; - OSStatus createStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth); - if (createStatus != errAuthorizationSuccess) { - auth = NULL; - SULog(@"Failed to create authorization reference: %d", createStatus); - } - - BOOL canceledAuthorization = NO; - BOOL failedToUseSystemDomain = NO; - if (auth != NULL && systemDomain) { - AuthorizationItem rightItems[] = { - { .name = kSMRightModifySystemDaemons, .valueLength = 0, .value = NULL, .flags = 0} - }; - - AuthorizationRights rights = { - .count = sizeof(rightItems) / sizeof(*rightItems), - .items = rightItems, - }; - - AuthorizationFlags flags = - (AuthorizationFlags)(kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize | 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; - } - } - } - - Boolean submittedJob = false; - if (!failedToUseSystemDomain && auth != NULL) { - CFStringRef domain = (systemDomain ? kSMDomainSystemLaunchd : kSMDomainUserLaunchd); - NSString *label = [NSString stringWithFormat:@"%@-sparkle-updater", hostBundleIdentifier]; - - // Try to remove the job from launchd if it is already running - // We could invoke SMJobCopyDictionary() first to see if the job exists, but I'd rather avoid - // using it because the headers indicate it may be removed one day without any replacement - CFErrorRef removeError = NULL; - if (!SMJobRemove(domain, (__bridge CFStringRef)(label), auth, true, &removeError)) { - 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); - } - CFRelease(removeError); - } - } - - NSDictionary *jobDictionary = @{@"Label" : label, @"ProgramArguments" : arguments, @"EnableTransactions" : @NO, @"KeepAlive" : @{@"SuccessfulExit" : @NO}, @"RunAtLoad" : @NO, @"NICE" : @0, @"LaunchOnlyOnce": @YES, @"MachServices" : @{SUInstallerServiceNameForBundleIdentifier(hostBundleIdentifier) : @YES, SUProgressAgentServiceNameForBundleIdentifier(hostBundleIdentifier) : @YES}}; - - CFErrorRef submitError = NULL; - submittedJob = SMJobSubmit(domain, (__bridge CFDictionaryRef)(jobDictionary), auth, &submitError); - if (!submittedJob) { - if (submitError != NULL) { - SULog(@"Submit error: %@", submitError); - CFRelease(submitError); - } - } - - AuthorizationFree(auth, kAuthorizationFlagDefaults); - } - - SUAuthorizationReply reply; - if (submittedJob == true) { - reply = SUAuthorizationReplySuccess; - } else if (canceledAuthorization) { - reply = SUAuthorizationReplyCancelled; - } else { - reply = SUAuthorizationReplyFailure; - } - return reply; -} - -@end diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index c690a9e7..e92f2b9d 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -100,7 +100,6 @@ 7210C7681B9A9A1500EB90AC /* SUUnarchiverTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */; }; 72162B061C82C8B30013C1C5 /* SUParameterAssert.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B051C82C8B30013C1C5 /* SUParameterAssert.h */; }; 72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B071C82C9600013C1C5 /* SULocalizations.h */; }; - 721652611D3C437E00FD13D8 /* SUSubmitInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 722194771D3C4251004C34FF /* SUSubmitInstaller.m */; }; 721652621D3C44A100FD13D8 /* SUSystemAuthorization.m in Sources */ = {isa = PBXBuildFile; fileRef = 724BB3AC1D347EDE005D534A /* SUSystemAuthorization.m */; }; 721BC20E1D1CDE55002BC71E /* SULocalCacheDirectory.h in Headers */ = {isa = PBXBuildFile; fileRef = 721BC20C1D1CDE55002BC71E /* SULocalCacheDirectory.h */; }; 721BC20F1D1CDE55002BC71E /* SULocalCacheDirectory.m in Sources */ = {isa = PBXBuildFile; fileRef = 721BC20D1D1CDE55002BC71E /* SULocalCacheDirectory.m */; }; @@ -116,9 +115,7 @@ 721CF1A91AD7644C00D9AC09 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; settings = {COMPILER_FLAGS = "-w"; }; }; 721CF1AA1AD7647000D9AC09 /* libxar.1.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D1AF5890FD7678C0065DB48 /* libxar.1.dylib */; }; 721CF1AB1AD764EB00D9AC09 /* libbz2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D06E8FB0FD68D61005AE3F6 /* libbz2.dylib */; }; - 722194791D3C4251004C34FF /* SUSubmitInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 722194771D3C4251004C34FF /* SUSubmitInstaller.m */; }; 7221947A1D3C42EA004C34FF /* ServiceManagement.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 724BB3B51D35AAC3005D534A /* ServiceManagement.framework */; }; - 7221947D1D3C433C004C34FF /* SUSubmitInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 722194771D3C4251004C34FF /* SUSubmitInstaller.m */; }; 7223E7631AD1AEFF008E3161 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; }; 7229E1B61C97C91100CB50D0 /* SUUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1B51C97C91100CB50D0 /* SUUpdateDriver.h */; }; 7229E1B91C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1B71C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h */; }; @@ -770,8 +767,6 @@ 721C24581CB7567D005440CB /* InstallerProgress-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "InstallerProgress-Info.plist"; path = "Sparkle/InstallerProgress/InstallerProgress-Info.plist"; sourceTree = SOURCE_ROOT; }; 722194511D3BF987004C34FF /* SUInstallerAgentProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SUInstallerAgentProtocol.h; path = Sparkle/InstallerProgress/SUInstallerAgentProtocol.h; sourceTree = SOURCE_ROOT; }; 722194521D3BFEB7004C34FF /* SUInstallerAgentInitiationProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SUInstallerAgentInitiationProtocol.h; path = Sparkle/InstallerProgress/SUInstallerAgentInitiationProtocol.h; sourceTree = SOURCE_ROOT; }; - 722194761D3C4251004C34FF /* SUSubmitInstaller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUSubmitInstaller.h; sourceTree = ""; }; - 722194771D3C4251004C34FF /* SUSubmitInstaller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUSubmitInstaller.m; sourceTree = ""; }; 7223E7611AD1AEFF008E3161 /* sais.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sais.c; sourceTree = ""; }; 7223E7621AD1AEFF008E3161 /* sais.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sais.h; sourceTree = ""; }; 7229E1B51C97C91100CB50D0 /* SUUpdateDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdateDriver.h; sourceTree = ""; }; @@ -1631,8 +1626,6 @@ 726E07AF1CAF08D6001A286B /* SUInstallerLauncherProtocol.h */, 726E07B01CAF08D6001A286B /* SUInstallerLauncher.h */, 726E07B11CAF08D6001A286B /* SUInstallerLauncher.m */, - 722194761D3C4251004C34FF /* SUSubmitInstaller.h */, - 722194771D3C4251004C34FF /* SUSubmitInstaller.m */, 726E07B31CAF08D6001A286B /* main.m */, 726E07B51CAF08D6001A286B /* Info.plist */, ); @@ -2583,7 +2576,6 @@ 72D954811CBACC35006F28BD /* InstallerProgressAppController.m in Sources */, 72EB87EA1CB8798800C37F42 /* ShowInstallerProgress.m in Sources */, 721C24611CB75C5D005440CB /* SUHost.m in Sources */, - 722194791D3C4251004C34FF /* SUSubmitInstaller.m in Sources */, 721C245E1CB757DE005440CB /* SUConstants.m in Sources */, 721C245D1CB757C4005440CB /* SUApplicationInfo.m in Sources */, 721C245C1CB7576E005440CB /* SUStatusController.m in Sources */, @@ -2623,7 +2615,6 @@ buildActionMask = 2147483647; files = ( 7267E5E11D3D901600D1BF90 /* SUMessageTypes.m in Sources */, - 721652611D3C437E00FD13D8 /* SUSubmitInstaller.m in Sources */, 724BB3AF1D34828A005D534A /* SUSystemAuthorization.m in Sources */, 726E07C01CAF15B7001A286B /* SULog.m in Sources */, 726E07BF1CAF0C6C001A286B /* SUConstants.m in Sources */, @@ -2718,7 +2709,6 @@ buildActionMask = 2147483647; files = ( 7267E5DF1D3D8FFA00D1BF90 /* SUInstallationInputData.m in Sources */, - 7221947D1D3C433C004C34FF /* SUSubmitInstaller.m in Sources */, 724BB3B01D3482BA005D534A /* SUSystemAuthorization.m in Sources */, 724BB3AA1D3347C2005D534A /* SUInstallerStatus.m in Sources */, 724BB3891D32B915005D534A /* SUInstallerConnection.m in Sources */,