mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-28 05:14:34 +00:00
Add authorization environment when using file manager
If the file manager is used for authorization, we can now supply an authorization environment. This allows us to specify a customized prompt message and an icon to use in the authorization dialog window. We no longer need to include an icon for Autoupdate app. We now also see if we can get authorization rights access right away, rather than lazily when executing a tool.
This commit is contained in:
1 parent
d182b28d09
commit
64a7624744
20 files changed
+231
-44
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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 = "<group>"; };
|
||||
72B09CE91CEA18900052EF9E /* bscommon.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bscommon.c; sourceTree = "<group>"; };
|
||||
72B09CEA1CEA18900052EF9E /* bscommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bscommon.h; sourceTree = "<group>"; };
|
||||
72B1444E1CF8826900B971EE /* SUAuthorizationEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAuthorizationEnvironment.h; sourceTree = "<group>"; };
|
||||
72B1444F1CF8826900B971EE /* SUAuthorizationEnvironment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAuthorizationEnvironment.m; sourceTree = "<group>"; };
|
||||
72B767C81C9B707000A07552 /* SUAppcastDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAppcastDriver.h; sourceTree = "<group>"; };
|
||||
72B767C91C9B707000A07552 /* SUAppcastDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAppcastDriver.m; sourceTree = "<group>"; };
|
||||
72B767CC1C9B924900A07552 /* SUInstallerDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUInstallerDriver.h; sourceTree = "<group>"; };
|
||||
@@ -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 */,
|
||||
|
||||
Vendored
+40
-4
@@ -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<SUInstaller> installer;
|
||||
@property (nonatomic) id<SUInstallerProtocol> 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 <SUInstaller> installer = [SUInstaller installerForHost:self.host updateDirectory:self.installationData.updateDirectoryPath versionComparator:[SUStandardVersionComparator standardVersionComparator] error:&installerError];
|
||||
id <SUInstallerProtocol> 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);
|
||||
}
|
||||
|
||||
|
||||
+10
-3
@@ -8,7 +8,11 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@protocol SUInstaller <NSObject>
|
||||
@class SUAuthorizationEnvironment;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol SUInstallerProtocol <NSObject>
|
||||
|
||||
// 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
|
||||
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// SUAuthorizationEnvironment.h
|
||||
// Sparkle
|
||||
//
|
||||
// Created by Mayur Pawashe on 5/27/16.
|
||||
// Copyright © 2016 Sparkle Project. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Security/Security.h>
|
||||
|
||||
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
|
||||
Vendored
+48
@@ -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
|
||||
Vendored
+1
-1
@@ -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)
|
||||
|
||||
Vendored
+20
-8
@@ -9,6 +9,10 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#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
|
||||
Vendored
+43
-13
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "SUFileManager.h"
|
||||
#import "SUAuthorizationEnvironment.h"
|
||||
#import <Foundation/Foundation.h>
|
||||
#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
|
||||
|
||||
Vendored
+1
-1
@@ -22,7 +22,7 @@ A guided installation can be started by applications other than the application
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SUInstallerProtocol.h"
|
||||
|
||||
@interface SUGuidedPackageInstaller : NSObject <SUInstaller>
|
||||
@interface SUGuidedPackageInstaller : NSObject <SUInstallerProtocol>
|
||||
|
||||
- (instancetype)initWithPackagePath:(NSString *)packagePath;
|
||||
|
||||
|
||||
Vendored
+2
-2
@@ -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];
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SUInstaller : NSObject
|
||||
|
||||
+ (nullable id<SUInstaller>)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id <SUVersionComparison>)comparator error:(NSError **)error;
|
||||
+ (nullable id<SUInstallerProtocol>)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id <SUVersionComparison>)comparator error:(NSError **)error;
|
||||
|
||||
+ (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr isGuided:(nullable BOOL *)isGuidedPtr;
|
||||
|
||||
|
||||
Vendored
+2
-2
@@ -109,7 +109,7 @@
|
||||
return newAppDownloadPath;
|
||||
}
|
||||
|
||||
+ (nullable id<SUInstaller>)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id <SUVersionComparison>)comparator error:(NSError * __autoreleasing *)error
|
||||
+ (nullable id<SUInstallerProtocol>)installerForHost:(SUHost *)host updateDirectory:(NSString *)updateDirectory versionComparator:(id <SUVersionComparison>)comparator error:(NSError * __autoreleasing *)error
|
||||
{
|
||||
BOOL isPackage = NO;
|
||||
BOOL isGuided = NO;
|
||||
@@ -122,7 +122,7 @@
|
||||
return nil;
|
||||
}
|
||||
|
||||
id <SUInstaller> installer = nil;
|
||||
id <SUInstallerProtocol> installer = nil;
|
||||
if (isPackage && isGuided) {
|
||||
installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath];
|
||||
} else if (isPackage) {
|
||||
|
||||
Vendored
+1
-1
@@ -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];
|
||||
|
||||
Vendored
+1
-1
@@ -9,7 +9,7 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SUInstallerProtocol.h"
|
||||
|
||||
@interface SUPackageInstaller : NSObject <SUInstaller>
|
||||
@interface SUPackageInstaller : NSObject <SUInstallerProtocol>
|
||||
|
||||
- (instancetype)initWithPackagePath:(NSString *)packagePath;
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -12,7 +12,7 @@
|
||||
@class SUHost;
|
||||
@protocol SUVersionComparison;
|
||||
|
||||
@interface SUPlainInstaller : NSObject <SUInstaller>
|
||||
@interface SUPlainInstaller : NSObject <SUInstallerProtocol>
|
||||
|
||||
- (instancetype)initWithHost:(SUHost *)host applicationPath:(NSString *)applicationPath installationPath:(NSString *)installationPath versionComparator:(id <SUVersionComparison>)comparator;
|
||||
|
||||
|
||||
Vendored
+2
-2
@@ -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];
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user