diff --git a/Configurations/ConfigRelaunch.xcconfig b/Configurations/ConfigRelaunch.xcconfig index c21e2b1f..c8cfa87b 100644 --- a/Configurations/ConfigRelaunch.xcconfig +++ b/Configurations/ConfigRelaunch.xcconfig @@ -3,6 +3,5 @@ INFOPLIST_FILE = Sparkle/Autoupdate/Autoupdate-Info.plist PRODUCT_NAME = $(SPARKLE_RELAUNCH_TOOL_NAME) SKIP_INSTALL = YES -ASSETCATALOG_COMPILER_APPICON_NAME = $(SPARKLE_ICON_NAME) CLANG_ENABLE_MODULES = NO OTHER_LDFLAGS = -Wl,-U,_NSURLQuarantinePropertiesKey diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index d0109367..c52495ee 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -17,7 +17,7 @@ { dispatch_async(dispatch_get_main_queue(), ^{ NSError *quarantineError = nil; - SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; + SUFileManager *fileManager = [SUFileManager fileManager]; if (![fileManager releaseItemFromQuarantineAtRootURL:[NSURL fileURLWithPath:installerPath] error:&quarantineError]) { // This may or may not be a fatal error depending on if the process is sandboxed or not diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index d141dec5..c95a4251 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -253,6 +253,10 @@ 72B09CE81CE6BCBD0052EF9E /* SUURLDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */; }; 72B09CEC1CEA18900052EF9E /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE91CEA18900052EF9E /* bscommon.c */; }; 72B09CEE1CEA21CD0052EF9E /* bscommon.c in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE91CEA18900052EF9E /* bscommon.c */; }; + 72B144501CF8826900B971EE /* SUAuthorizationEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B1444E1CF8826900B971EE /* SUAuthorizationEnvironment.h */; }; + 72B144511CF8826900B971EE /* SUAuthorizationEnvironment.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B1444F1CF8826900B971EE /* SUAuthorizationEnvironment.m */; }; + 72B144521CF8826900B971EE /* SUAuthorizationEnvironment.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B1444F1CF8826900B971EE /* SUAuthorizationEnvironment.m */; }; + 72B144531CF8992200B971EE /* SUApplicationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 725602D41C83551C00DAA70E /* SUApplicationInfo.m */; }; 72B767CA1C9B707000A07552 /* SUAppcastDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767C81C9B707000A07552 /* SUAppcastDriver.h */; }; 72B767CB1C9B707000A07552 /* SUAppcastDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B767C91C9B707000A07552 /* SUAppcastDriver.m */; }; 72B767CE1C9B924900A07552 /* SUInstallerDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767CC1C9B924900A07552 /* SUInstallerDriver.h */; }; @@ -887,6 +891,8 @@ 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUURLDownload.m; sourceTree = ""; }; 72B09CE91CEA18900052EF9E /* bscommon.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bscommon.c; sourceTree = ""; }; 72B09CEA1CEA18900052EF9E /* bscommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bscommon.h; sourceTree = ""; }; + 72B1444E1CF8826900B971EE /* SUAuthorizationEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAuthorizationEnvironment.h; sourceTree = ""; }; + 72B1444F1CF8826900B971EE /* SUAuthorizationEnvironment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAuthorizationEnvironment.m; sourceTree = ""; }; 72B767C81C9B707000A07552 /* SUAppcastDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAppcastDriver.h; sourceTree = ""; }; 72B767C91C9B707000A07552 /* SUAppcastDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAppcastDriver.m; sourceTree = ""; }; 72B767CC1C9B924900A07552 /* SUInstallerDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUInstallerDriver.h; sourceTree = ""; }; @@ -1362,6 +1368,8 @@ children = ( 7275F9BF1B5F1F2900B1D19E /* SUFileManager.h */, 7275F9C01B5F1F2900B1D19E /* SUFileManager.m */, + 72B1444E1CF8826900B971EE /* SUAuthorizationEnvironment.h */, + 72B1444F1CF8826900B971EE /* SUAuthorizationEnvironment.m */, 72A6F97D1C94A5C2005F404C /* SUInstallerProtocol.h */, 618FA4FF0DAE88B40026945C /* SUInstaller.h */, 618FA5000DAE88B40026945C /* SUInstaller.m */, @@ -1701,6 +1709,7 @@ files = ( 725F977B1C8A896F00265BE4 /* SUUserDriverCoreComponent.h in Headers */, 726E078D1CA891E9001A286B /* SUUpdaterSettings.h in Headers */, + 72B144501CF8826900B971EE /* SUAuthorizationEnvironment.h in Headers */, 72A6F98A1C94E2D6005F404C /* SUUpdaterDelegate.h in Headers */, 726E4A371C89116000C57C6A /* SUStandardUserDriverDelegate.h in Headers */, 726E4A0E1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h in Headers */, @@ -2409,8 +2418,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72B144531CF8992200B971EE /* SUApplicationInfo.m in Sources */, 72B09CEE1CEA21CD0052EF9E /* bscommon.c in Sources */, 72D9547E1CBA221E006F28BD /* SUAppcastItem.m in Sources */, + 72B144521CF8826900B971EE /* SUAuthorizationEnvironment.m in Sources */, 726E07671CA61648001A286B /* SUBinaryDeltaCommon.m in Sources */, 726E07661CA61621001A286B /* bspatch.c in Sources */, 726E07651CA61603001A286B /* SUBinaryDeltaApply.m in Sources */, @@ -2638,6 +2649,7 @@ 61B5FCDE09C52A9F00B25A18 /* SUUpdateAlert.m in Sources */, 612DCBB00D488BC60015DBEA /* SUUpdatePermissionPrompt.m in Sources */, 61B5F8EE09C4CE3C00B25A18 /* SUUpdater.m in Sources */, + 72B144511CF8826900B971EE /* SUAuthorizationEnvironment.m in Sources */, 72B767CB1C9B707000A07552 /* SUAppcastDriver.m in Sources */, 726E075D1CA3A6D6001A286B /* SUSecureCoding.m in Sources */, 72B767DB1C9CD2E400A07552 /* SUUIBasedUpdateDriver.m in Sources */, diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index ec5526dc..065a9efc 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -24,6 +24,8 @@ #import "SUFileManager.h" #import "SUInstallationInfo.h" #import "SUAppcastItem.h" +#import "SUAuthorizationEnvironment.h" +#import "SUApplicationInfo.h" /*! * Terminate the application after a delay from launching the new update to avoid OS activation issues @@ -53,7 +55,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic, assign) BOOL shouldShowUI; @property (nonatomic) NSData *installationInfoData; -@property (nonatomic) id installer; +@property (nonatomic) id installer; @property (nonatomic) BOOL willCompleteInstallation; @property (nonatomic) dispatch_queue_t installerQueue; @@ -415,7 +417,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; dispatch_async(self.installerQueue, ^{ NSError *installerError = nil; - id installer = [SUInstaller installerForHost:self.host updateDirectory:self.installationData.updateDirectoryPath versionComparator:[SUStandardVersionComparator standardVersionComparator] error:&installerError]; + id installer = [SUInstaller installerForHost:self.host updateDirectory:self.installationData.updateDirectoryPath versionComparator:[SUStandardVersionComparator standardVersionComparator] error:&installerError]; if (installer == nil) { SULog(@"Error: Failed to create installer instance with error: %@", installerError); @@ -470,7 +472,35 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; return; } + SUAuthorizationEnvironment *environment = nil; + NSURL *tempIconDirectoryURL = nil; if (self.shouldShowUI && !self.inheritsPrivileges && [self.installer mayNeedToRequestAuthorization]) { + NSString *prompt = [NSString stringWithFormat:NSLocalizedString(@"%1$@ wants to update.", nil), self.host.name]; + + NSString *iconPath = nil; + + // Authorization API requires a 32x32 icon image and accepts png (but not icns or tiff) + NSImage *icon = [SUApplicationInfo bestIconForBundle:self.host.bundle]; + [icon setSize:NSMakeSize(32, 32)]; + + CGImageRef iconRef = [icon CGImageForProposedRect:NULL context:nil hints:nil]; + NSBitmapImageRep *representation = [[NSBitmapImageRep alloc] initWithCGImage:iconRef]; + NSData *pngData = [representation representationUsingType:NSPNGFileType properties:@{}]; + + // Write the icon to a temporary directory. This icon file should be in a directory that's readable to everyone. + NSError *tempError = nil; + tempIconDirectoryURL = [[SUFileManager fileManager] makeTemporaryDirectoryWithPreferredName:@"sparkle-auth-icon" appropriateForDirectoryURL:[NSURL fileURLWithPath:NSTemporaryDirectory()] error:&tempError]; + if (tempIconDirectoryURL == nil) { + SULog(@"Failed to create temporary directory for authorization icon: %@", tempError); + } else { + NSURL *iconURL = [tempIconDirectoryURL URLByAppendingPathComponent:@"icon.png"]; + if ([pngData writeToURL:iconURL atomically:YES]) { + iconPath = iconURL.path; + } + } + + environment = [[SUAuthorizationEnvironment alloc] initWithPrompt:prompt iconPath:(iconPath != nil ? iconPath : @"")]; + dispatch_async(dispatch_get_main_queue(), ^{ // We should activate our app so that the auth prompt will be active [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; @@ -478,7 +508,13 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; } NSError *secondStageError = nil; - if (![self.installer performSecondStageAllowingAuthorization:!self.inheritsPrivileges allowingUI:self.shouldShowUI error:&secondStageError]) { + BOOL performedSecondStage = [self.installer performSecondStageAllowingAuthorization:!self.inheritsPrivileges withEnvironment:environment allowingUI:self.shouldShowUI error:&secondStageError]; + + if (tempIconDirectoryURL != nil) { + [[SUFileManager fileManager] removeItemAtURL:tempIconDirectoryURL error:NULL]; + } + + if (!performedSecondStage) { SULog(@"Error: Failed to resume installer on stage 2 with error: %@", secondStageError); [self.installer cleanup]; self.installer = nil; @@ -514,7 +550,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; NSURL *progressToolURL = [NSURL fileURLWithPath:progressToolPath]; if (progressToolURL != nil) { NSError *quarantineError = nil; - if (![[SUFileManager fileManagerAllowingAuthorization:NO] releaseItemFromQuarantineAtRootURL:progressToolURL error:&quarantineError]) { + if (![[SUFileManager fileManager] releaseItemFromQuarantineAtRootURL:progressToolURL error:&quarantineError]) { SULog(@"Error: Failed releasing quarantine from installer progress tool: %@", quarantineError); } diff --git a/Sparkle/Autoupdate/SUInstallerProtocol.h b/Sparkle/Autoupdate/SUInstallerProtocol.h index 72f9419d..c9ab3bcb 100644 --- a/Sparkle/Autoupdate/SUInstallerProtocol.h +++ b/Sparkle/Autoupdate/SUInstallerProtocol.h @@ -8,7 +8,11 @@ #import -@protocol SUInstaller +@class SUAuthorizationEnvironment; + +NS_ASSUME_NONNULL_BEGIN + +@protocol SUInstallerProtocol // Stage 1 is where any installation work can be done prior to user application being terminated and relaunched // No UI should occur during this stage (i.e, do not prompt for authorization prompts, show package installer apps, etc..) @@ -16,11 +20,12 @@ - (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 +// If the authorizationEnvironment flag is nil, then this and the 3rd stage cannot request authorization if the updater has insufficient privileges to install +// Otherwise if it's non-nil, then they can request authorization and they will use the environment that's passed. // 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)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError **)error; +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization withEnvironment:(SUAuthorizationEnvironment * _Nullable)authorizationEnvironment 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. @@ -47,3 +52,5 @@ - (void)cleanup; @end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUAuthorizationEnvironment.h b/Sparkle/SUAuthorizationEnvironment.h new file mode 100644 index 00000000..6d8d3114 --- /dev/null +++ b/Sparkle/SUAuthorizationEnvironment.h @@ -0,0 +1,43 @@ +// +// SUAuthorizationEnvironment.h +// Sparkle +// +// Created by Mayur Pawashe on 5/27/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + * A class used for specifying an environment for an authorization request. + * This includes a prompt message and an icon image that may show up on an authorization dialog + */ +@interface SUAuthorizationEnvironment : NSObject + +/** + * Creates an authorization environment + * + * @param prompt A message that will show up in the authorization prompt dialog. The system may append a message + * to this prompt. i.e, something like "Enter your password to allow changes." Supply empty string if the prompt is not important. + * + * @param iconPath A path to a icon on disk that will be used as the icon that shows up in the authorization dialog. + * This should be a PNG image that is 32x32 in dimension. It should also be a path that is readable from anybody, + * likely in a temporary directory. + * + * @return A new authorization environment instance + */ +- (instancetype)initWithPrompt:(NSString *)prompt iconPath:(NSString *)iconPath; + +/** + * Retrieve the authorization environment struct that can be used for the Authorization Service APIs + * + * @return A authorization environment reference. Because this returns an internal pointer, this may only be valid as long as this object instance is alive. + */ +@property (nonatomic, readonly) AuthorizationEnvironment *environment NS_RETURNS_INNER_POINTER; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUAuthorizationEnvironment.m b/Sparkle/SUAuthorizationEnvironment.m new file mode 100644 index 00000000..407fa69d --- /dev/null +++ b/Sparkle/SUAuthorizationEnvironment.m @@ -0,0 +1,48 @@ +// +// SUAuthorizationEnvironment.m +// Sparkle +// +// Created by Mayur Pawashe on 5/27/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUAuthorizationEnvironment.h" + +@implementation SUAuthorizationEnvironment +{ + // Declare data here so we won't have to do more dynamic allocations + char _prompt[1024]; + char _iconPath[PATH_MAX]; + AuthorizationItem _environmentItems[2]; + AuthorizationEnvironment _environment; +} + +- (instancetype)initWithPrompt:(NSString *)prompt iconPath:(NSString *)iconPath +{ + self = [super init]; + if (self != nil) { + if (![prompt getFileSystemRepresentation:_prompt maxLength:sizeof(_prompt)]) { + bzero(_prompt, sizeof(_prompt)); + } + + if (![iconPath getFileSystemRepresentation:_iconPath maxLength:sizeof(_iconPath)]) { + bzero(_iconPath, sizeof(_iconPath)); + } + + _environmentItems[0] = (AuthorizationItem){.name = kAuthorizationEnvironmentPrompt, .valueLength = strlen(_prompt), .value = _prompt, .flags = 0}; + _environmentItems[1] = (AuthorizationItem){.name = kAuthorizationEnvironmentIcon, .valueLength = strlen(_iconPath), .value = _iconPath, .flags = 0}; + + _environment = (AuthorizationEnvironment){.count = sizeof(_environmentItems) / sizeof(*_environmentItems), .items = _environmentItems}; + } + return self; +} + +- (AuthorizationEnvironment *)environment NS_RETURNS_INNER_POINTER +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdirect-ivar-access" + return &_environment; +#pragma clang diagnostic pop +} + +@end diff --git a/Sparkle/SUBinaryDeltaCommon.m b/Sparkle/SUBinaryDeltaCommon.m index a62b2948..7e4a6872 100644 --- a/Sparkle/SUBinaryDeltaCommon.m +++ b/Sparkle/SUBinaryDeltaCommon.m @@ -225,7 +225,7 @@ BOOL removeTree(NSString *path) BOOL copyTree(NSString *source, NSString *dest) { - return [[SUFileManager fileManagerAllowingAuthorization:NO] copyItemAtURL:[NSURL fileURLWithPath:source] toURL:[NSURL fileURLWithPath:dest] error:NULL]; + return [[SUFileManager fileManager] copyItemAtURL:[NSURL fileURLWithPath:source] toURL:[NSURL fileURLWithPath:dest] error:NULL]; } BOOL modifyPermissions(NSString *path, mode_t desiredPermissions) diff --git a/Sparkle/SUFileManager.h b/Sparkle/SUFileManager.h index 986b3829..cce1e4a8 100644 --- a/Sparkle/SUFileManager.h +++ b/Sparkle/SUFileManager.h @@ -9,6 +9,10 @@ #import #import "SUExport.h" +@class SUAuthorizationEnvironment; + +NS_ASSUME_NONNULL_BEGIN + /** * A class used for performing file operations that may also perform authentication if allowed and if permission is denied when trying to * perform them normally as the running user. All operations on this class may be used on thread other than the main thread. @@ -17,14 +21,20 @@ @interface SUFileManager : NSObject /** - * Creates a file manager that allows or disallows authorizing for file operations - * @param allowsAuthorization Specifies whether operations invoked on this instance are allowed to acquire authorization in order - * to perform file operations when read or write access is denied - * @return A new file manager instance - * - * This method just creates the file manager. It doesn't acquire authorization immediately if allowsAuthorization is YES + * Creates a file manager that disallows authorizing for file operations + * + * @return A new file manager instance that cannot make authorization requests when read/write access is denied */ -+ (instancetype)fileManagerAllowingAuthorization:(BOOL)allowsAuthorization; ++ (instancetype)fileManager; + +/** + * Creates a file manager that allows authorizing for file operations when needed. + * Authorization may be attempted after a read or write file operation is denied access. + * + * @param environment Specifies the authorization environment used when making an authorization request. Pass nil to use a default environment. + * @return A new file manager instance that can make authorization requests. Creating this instance does not acquire authorization immediately. + */ ++ (instancetype)fileManagerAllowingAuthorizationWithEnvironment:(SUAuthorizationEnvironment * _Nullable)environment; /** * Returns a file manager that allows or disallows authorizing for file operations based on the current file manager @@ -35,7 +45,7 @@ */ - (instancetype)fileManagerByPreservingAuthorizationRights; -- (BOOL)authorizeAndExecuteWithPrivilegesAtPath:(const char *)executablePath arguments:(char *const *)arguments; +- (BOOL)authorizeAndExecuteWithPrivilegesAtPath:(const char *)executablePath arguments:(char * _Nonnull const * _Nonnull)arguments; - (BOOL)grantAuthorizationPrivilegesWithError:(NSError **)error; @@ -149,3 +159,5 @@ - (BOOL)releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL error:(NSError **)error; @end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index f843dcbd..5ca4f359 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -7,6 +7,7 @@ // #import "SUFileManager.h" +#import "SUAuthorizationEnvironment.h" #import #import "SUErrors.h" @@ -82,28 +83,35 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { AuthorizationRef _auth; NSFileManager *_fileManager; BOOL _allowsAuthorization; + SUAuthorizationEnvironment *_environment; } -- (instancetype)initAllowingAuthorization:(BOOL)allowsAuthorization +- (instancetype)initAllowingAuthorization:(BOOL)allowsAuthorization environment:(SUAuthorizationEnvironment *)environment { self = [super init]; if (self != nil) { _fileManager = [[NSFileManager alloc] init]; _allowsAuthorization = allowsAuthorization; + _environment = environment; } return self; } -+ (instancetype)fileManagerAllowingAuthorization:(BOOL)allowsAuthorization ++ (instancetype)fileManager { - return [[self alloc] initAllowingAuthorization:allowsAuthorization]; + return [[self alloc] initAllowingAuthorization:NO environment:nil]; +} + ++ (instancetype)fileManagerAllowingAuthorizationWithEnvironment:(SUAuthorizationEnvironment *)environment +{ + return [[self alloc] initAllowingAuthorization:YES environment:environment]; } - (instancetype)fileManagerByPreservingAuthorizationRights { // Check if we don't allow authorization, or that we haven't needed to authorize yet, to create or re-use a // file manager instance with these restrictions - return (_allowsAuthorization && _auth != NULL) ? self : [SUFileManager fileManagerAllowingAuthorization:NO]; + return (_allowsAuthorization && _auth != NULL) ? self : [SUFileManager fileManager]; } // Acquires an authorization reference which is intended to be used for future authorized file operations @@ -120,15 +128,41 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { } return NO; } - - OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &_auth); - if (status != errAuthorizationSuccess) { + + // This should almost always succeed, fail in unusual/rare cases + OSStatus createStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &_auth); + if (createStatus != errAuthorizationSuccess) { if (error != NULL) { - *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed creating authorization reference with status code %d.", status] }]; + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed creating authorization reference with status code %d.", createStatus] }]; } _auth = NULL; return NO; } + + AuthorizationItem rightItems[] = { + // The right that allows us to run tools as root user + {.name = kAuthorizationRightExecute, .valueLength = 0, .value = NULL, .flags = 0} + }; + + AuthorizationRights rights = { + .count = sizeof(rightItems) / sizeof(*rightItems), + .items = rightItems + }; + + AuthorizationFlags flags = + (AuthorizationFlags)(kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights | kAuthorizationFlagPreAuthorize); + + // This will test if we can gain authorization for running utlities as root + OSStatus copyStatus = AuthorizationCopyRights(_auth, &rights, (_environment != nil ? _environment.environment : NULL), flags, NULL); + + if (copyStatus != errAuthorizationSuccess) { + if (error != NULL) { + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed granting authorization rights with status code %d.", copyStatus] }]; + } + _auth = NULL; + return NO; + } + return YES; } @@ -149,11 +183,7 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { - (BOOL)grantAuthorizationPrivilegesWithError:(NSError * __autoreleasing *)error { - BOOL success = [self authorizeAndExecuteWithPrivilegesAtPath:"/usr/bin/true" arguments:(char *[]){NULL}]; - if (!success && error != NULL) { - *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: @"Failed granting authorization." }]; - } - return success; + return [self _acquireAuthorizationWithError:error]; } // -[NSFileManager attributesOfItemAtPath:error:] won't follow symbolic links diff --git a/Sparkle/SUGuidedPackageInstaller.h b/Sparkle/SUGuidedPackageInstaller.h index 8f8b1605..6a950c19 100644 --- a/Sparkle/SUGuidedPackageInstaller.h +++ b/Sparkle/SUGuidedPackageInstaller.h @@ -22,7 +22,7 @@ A guided installation can be started by applications other than the application #import #import "SUInstallerProtocol.h" -@interface SUGuidedPackageInstaller : NSObject +@interface SUGuidedPackageInstaller : NSObject - (instancetype)initWithPackagePath:(NSString *)packagePath; diff --git a/Sparkle/SUGuidedPackageInstaller.m b/Sparkle/SUGuidedPackageInstaller.m index e08d7fa4..ec58377e 100644 --- a/Sparkle/SUGuidedPackageInstaller.m +++ b/Sparkle/SUGuidedPackageInstaller.m @@ -43,7 +43,7 @@ return YES; } -- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization withEnvironment:(SUAuthorizationEnvironment * _Nullable)authorizationEnvironment allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error { if (!allowsUI) { if (error != NULL) { @@ -53,7 +53,7 @@ } // If we're root, we can allow using the authorization APIs - self.fileManager = [SUFileManager fileManagerAllowingAuthorization:(allowsAuthorization || (geteuid() == 0))]; + self.fileManager = (allowsAuthorization || (geteuid() == 0)) ? [SUFileManager fileManagerAllowingAuthorizationWithEnvironment:authorizationEnvironment] : [SUFileManager fileManager]; return [self.fileManager grantAuthorizationPrivilegesWithError:error]; } diff --git a/Sparkle/SUInstaller.h b/Sparkle/SUInstaller.h index e7a7f8ce..2083ec27 100644 --- a/Sparkle/SUInstaller.h +++ b/Sparkle/SUInstaller.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUInstaller : NSObject -+ (nullable id)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id )comparator error:(NSError **)error; ++ (nullable id)installerForHost:(SUHost *)host 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/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index 1e4fa526..afa9d667 100644 --- a/Sparkle/SUInstaller.m +++ b/Sparkle/SUInstaller.m @@ -109,7 +109,7 @@ return newAppDownloadPath; } -+ (nullable id)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id )comparator error:(NSError * __autoreleasing *)error ++ (nullable id)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id )comparator error:(NSError * __autoreleasing *)error { BOOL isPackage = NO; BOOL isGuided = NO; @@ -122,7 +122,7 @@ return nil; } - id installer = nil; + id installer = nil; if (isPackage && isGuided) { installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath]; } else if (isPackage) { diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index cc22885d..4cd0f7f9 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -424,7 +424,7 @@ } else { NSString *targetPath = [self.cachePath stringByAppendingPathComponent:[pathToCopy lastPathComponent]]; - SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; + SUFileManager *fileManager = [SUFileManager fileManager]; NSURL *urlToCopy = [NSURL fileURLWithPath:pathToCopy]; NSURL *targetURL = [NSURL fileURLWithPath:targetPath]; diff --git a/Sparkle/SUPackageInstaller.h b/Sparkle/SUPackageInstaller.h index 2628daff..26b175b7 100644 --- a/Sparkle/SUPackageInstaller.h +++ b/Sparkle/SUPackageInstaller.h @@ -9,7 +9,7 @@ #import #import "SUInstallerProtocol.h" -@interface SUPackageInstaller : NSObject +@interface SUPackageInstaller : NSObject - (instancetype)initWithPackagePath:(NSString *)packagePath; diff --git a/Sparkle/SUPackageInstaller.m b/Sparkle/SUPackageInstaller.m index c95b779b..ec4333d3 100644 --- a/Sparkle/SUPackageInstaller.m +++ b/Sparkle/SUPackageInstaller.m @@ -47,7 +47,7 @@ static NSString *SUOpenUtilityPath = @"/usr/bin/open"; return YES; } -- (BOOL)performSecondStageAllowingAuthorization:(BOOL)__unused allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)__unused allowsAuthorization withEnvironment:(SUAuthorizationEnvironment * _Nullable)__unused authorizationEnvironment allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error { return allowsUI; } diff --git a/Sparkle/SUPlainInstaller.h b/Sparkle/SUPlainInstaller.h index 51ed4da2..0292ec9d 100644 --- a/Sparkle/SUPlainInstaller.h +++ b/Sparkle/SUPlainInstaller.h @@ -12,7 +12,7 @@ @class SUHost; @protocol SUVersionComparison; -@interface SUPlainInstaller : NSObject +@interface SUPlainInstaller : NSObject - (instancetype)initWithHost:(SUHost *)host applicationPath:(NSString *)applicationPath installationPath:(NSString *)installationPath versionComparator:(id )comparator; diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 1188bef0..f0b45118 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -224,9 +224,9 @@ return YES; } -- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error +- (BOOL)performSecondStageAllowingAuthorization:(BOOL)allowsAuthorization withEnvironment:(SUAuthorizationEnvironment * _Nullable)authorizationEnvironment allowingUI:(BOOL)allowsUI error:(NSError * __autoreleasing *)error { - self.fileManager = [SUFileManager fileManagerAllowingAuthorization:allowsAuthorization && allowsUI]; + self.fileManager = (allowsAuthorization && allowsUI) ? [SUFileManager fileManagerAllowingAuthorizationWithEnvironment:authorizationEnvironment] : [SUFileManager fileManager]; // Bring up authorization prompt right away if we need it BOOL canInstallSilently = [self canInstallSilently]; diff --git a/TestApplication/SUTestApplicationDelegate.m b/TestApplication/SUTestApplicationDelegate.m index bebf8b13..905e14f5 100644 --- a/TestApplication/SUTestApplicationDelegate.m +++ b/TestApplication/SUTestApplicationDelegate.m @@ -45,7 +45,7 @@ static NSString * const UPDATED_VERSION = @"2.0"; // I don't want to have to export SUFileManager in release mode. The test app is primarily // aimed to be used in debug mode, so I think this is a good compromise #ifdef DEBUG - SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; + SUFileManager *fileManager = [SUFileManager fileManager]; #else NSFileManager *fileManager = [NSFileManager defaultManager]; #endif