diff --git a/Sparkle/SUGuidedPackageInstaller.h b/Sparkle/SUGuidedPackageInstaller.h index 37df2657..429b6f5a 100644 --- a/Sparkle/SUGuidedPackageInstaller.h +++ b/Sparkle/SUGuidedPackageInstaller.h @@ -11,34 +11,10 @@ A guided installation allows Sparkle to download and install a package (pkg) or multi-package (mpkg) without user interaction. -A guided installation occurs when Sparkle finds a `.sparkle_guide.plist` in the root of the download; the file contains the relative path of the installer package to install. - -The file must be a property list and have a dictionary root. The only required key pair value is `package`. - -The `package` key value pair must be set to a relative path to the package or multi-package to install. The path is relative to the guide file. - -Example Contents of Sparkle Guide file: - - - - - - package - My Package.pkg - - - -Example Path of Sparkle Guide file: - - /Volumes/MyUpdateDiskImage/.sparkle_guide.plist - The installer package is installed using Mac OS X's built-in command line installer, `/usr/sbin/installer`. No installation interface is shown to the user. A guided installation can be started by applications other than the application being replaced. This is particularly useful where helper applications or agents are used. -## Notes -This method has been tested and successfully deployed on Mac OS X 10.4 - 10.8. - ## To Do - Replace the use of `AuthorizationExecuteWithPrivilegesAndWait`. This method remains because it is well supported and tested. Ideally a helper tool or XPC would be used. */ @@ -49,14 +25,11 @@ This method has been tested and successfully deployed on Mac OS X 10.4 - 10.8. #import "Sparkle.h" #import "SUInstaller.h" -extern NSString* SUInstallerGuidedInstallerFilename; // default filename for guided installer property list - -@interface SUGuidedPackageInstaller : SUInstaller { } -/*! Search for and return any installer guide */ -+ (NSString *)installerGuideWithinUpdateFolder:(NSString *)updateFolder; +@interface SUGuidedPackageInstaller : SUInstaller { +} /*! Perform the guided installation */ -+ (void)performInstallationToPath:(NSString *)path fromPath:(NSString *)installerGuide host:(SUHost *)host delegate:delegate synchronously:(BOOL)synchronously versionComparator:(id )comparator; ++ (void)performInstallationToPath:(NSString *)path fromPath:(NSString *)installerGuide host:(SUHost *)host delegate:delegate versionComparator:(id)comparator; @end #endif diff --git a/Sparkle/SUGuidedPackageInstaller.m b/Sparkle/SUGuidedPackageInstaller.m index 4cab7d42..0437d33b 100644 --- a/Sparkle/SUGuidedPackageInstaller.m +++ b/Sparkle/SUGuidedPackageInstaller.m @@ -11,18 +11,6 @@ #import "SUGuidedPackageInstaller.h" -NSString* SUInstallerGuidedInstallerFilename = @".sparkle_guide.plist"; // top level file to search for within update folder - -// Constants -static NSString* SUGuidedPackageInstallerKeyGuidePath = @"SUGuidedPackageInstallerKeyGuidePath"; // NSString* -static NSString* SUGuidedPackageInstallerKeyGuide = @"SUGuidedPackageInstallerKeyGuide"; // NSDictionary* -static NSString* SUGuidedPackageInstallerKeyHost = @"SUGuidedPackageInstallerKeyHost"; // SUHost* -static NSString* SUGuidedPackageInstallerKeyDelegate = @"SUGuidedPackageInstallerKeyDelegate"; // id -static NSString* SUGuidedPackageInstallerKeyResult = @"SUGuidedPackageInstallerKeyResult"; // NSNumber[bool] -static NSString* SUGuidedPackageInstallerKeyError = @"SUGuidedPackageInstallerKeyError"; // NSError* - -static NSString* SUGuidedPackageInstallerGuideKeyPackage = @"package"; // NSString* - static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authorization, const char* executablePath, AuthorizationFlags options, const char* const* arguments) { sig_t oldSigChildHandler = signal(SIGCHLD, SIG_DFL); @@ -46,83 +34,6 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza return returnValue; } -@interface SUGuidedPackageInstaller (SUGuidedPackageInstallerAuthentication) -+ (AuthorizationRef)authorizationForExecutable:(NSString*)executablePath; -@end - -@interface SUGuidedPackageInstaller (SUGuidedPackageInstallerThreads) -// Perform the installation script -+ (void)performInstallationWithInfo:(NSDictionary *)theInfo; -// Complete the installation process -+ (void)finishInstallationWithInfo:(NSDictionary *)theInfo; -@end - -@implementation SUGuidedPackageInstaller - -// Search for and return any installer guide -+ (NSString *)installerGuideWithinUpdateFolder:(NSString *)updateFolder -{ - NSParameterAssert(updateFolder); - - NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:updateFolder]; - NSString *currentFile; - while ((currentFile = [dirEnum nextObject])) - { - if ([[currentFile lastPathComponent] isEqualToString:SUInstallerGuidedInstallerFilename]) - { - // Found an installer guide - return [updateFolder stringByAppendingPathComponent:currentFile]; - } - } - - // No guide found - return nil; -} - -+ (void)performInstallationToPath:(NSString *) __unused path fromPath:(NSString *)installerGuide host:(SUHost *)host delegate:delegate synchronously:(BOOL)synchronously versionComparator:(id ) __unused comparator -{ - NSParameterAssert(installerGuide); - NSParameterAssert(host); - - // Fetch the contents of the guide - NSDictionary* guide = [NSDictionary dictionaryWithContentsOfFile:installerGuide]; - if (guide == nil) - { - NSString* errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Installer guide contents are malformed '%@'.",installerGuide]; - NSError* error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; - [self finishInstallationToPath:installerGuide withResult:NO host:host error:error delegate:delegate]; - return; - } - - // Sanity check the contents of the guide - if ([guide objectForKey:SUGuidedPackageInstallerGuideKeyPackage] == NO) - { - NSString* errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Installer guide is missing '%@' entry.",SUGuidedPackageInstallerGuideKeyPackage]; - NSError* error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; - [self finishInstallationToPath:installerGuide withResult:NO host:host error:error delegate:delegate]; - return; - } - - // Package up installation details to allow for synchronous installation - NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys: - installerGuide,SUGuidedPackageInstallerKeyGuidePath, - guide,SUGuidedPackageInstallerKeyGuide, - host,SUGuidedPackageInstallerKeyHost, - delegate,SUGuidedPackageInstallerKeyDelegate, - nil]; - - if (synchronously) - { - [self performInstallationWithInfo:info]; - } - else - { - [NSThread detachNewThreadSelector:@selector(performInstallationWithInfo:) toTarget:self withObject:info]; - } -} - -@end - @implementation SUGuidedPackageInstaller (SUGuidedPackageInstallerAuthentication) + (AuthorizationRef)authorizationForExecutable:(NSString*)executablePath @@ -177,99 +88,54 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza @end -@implementation SUGuidedPackageInstaller (SUGuidedPackageInstallerThreads) +@implementation SUGuidedPackageInstaller -// Perform the installation script -+ (void)performInstallationWithInfo:(NSDictionary *)theInfo ++ (void)performInstallationToPath:(NSString *)destinationPath fromPath:(NSString *)packagePath host:(SUHost *)host delegate:delegate versionComparator:(id)__unused comparator { - NSParameterAssert(theInfo); - - // Preflight - NSString* installerPath = @"/usr/sbin/installer"; // Mac OS X 10.2+ command line installer tool - NSError* error = nil; - - // Check installer executable exists - if ([[NSFileManager defaultManager] isExecutableFileAtPath:installerPath] == NO) - { - NSString* errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Guide installer tool is missing."]; - error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; - } - - // Package is relative to installer guide file - NSString* absolutePackagePath = nil; - if (error == nil) - { - NSString* installerGuideParentFolderPath = [[theInfo objectForKey:SUGuidedPackageInstallerKeyGuidePath] stringByDeletingLastPathComponent]; - NSString* packagePath = [(NSDictionary*)[theInfo objectForKey:SUGuidedPackageInstallerKeyGuide] objectForKey:SUGuidedPackageInstallerGuideKeyPackage]; - NSAssert(packagePath,@"package entry is missing from guide"); - absolutePackagePath = [installerGuideParentFolderPath stringByAppendingPathComponent:packagePath]; - - // Sanity check absolute package path exists - if ([[NSFileManager defaultManager] fileExistsAtPath:absolutePackagePath] == NO) - { - NSString* errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Guide installer tool is missing."]; - error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; - } - } - - // Create authorization for installer executable - AuthorizationRef auth = nil; - BOOL validInstallation = NO; - if (error == nil) - { - auth = [self authorizationForExecutable:installerPath]; - if (auth != NULL) - { - // Permission was granted to execute the installer with privileges - const char* const arguments[] = { -// [installerPath fileSystemRepresentation], - "-pkg", - [absolutePackagePath fileSystemRepresentation], - "-target", - "/", - NULL - }; - validInstallation = AuthorizationExecuteWithPrivilegesAndWait(auth, - [installerPath fileSystemRepresentation], - kAuthorizationFlagDefaults, - arguments); - // TODO: wait for communications pipe to close via fileno & CFSocketCreateWithNative - } - else - { - NSString* errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Script authorization denied."]; - error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; - } - } - - // Release any authorization - if (auth) - { - AuthorizationFree(auth,kAuthorizationFlagDefaults); - } - - // Modify info to include the installation script's outcome - NSMutableDictionary* resultInfo = [theInfo mutableCopy]; - [resultInfo setObject:[NSNumber numberWithBool:validInstallation] forKey:SUGuidedPackageInstallerKeyResult]; - if ((validInstallation == NO) && - (error)) - { - [resultInfo setObject:error forKey:SUGuidedPackageInstallerKeyError]; - } - - [self performSelectorOnMainThread:@selector(finishInstallationWithInfo:) withObject:resultInfo waitUntilDone:NO]; -} + NSParameterAssert(packagePath); + NSParameterAssert(host); -// Complete the installation process -+ (void)finishInstallationWithInfo:(NSDictionary *)theInfo -{ - NSParameterAssert(theInfo); - - [self finishInstallationToPath:[theInfo objectForKey:SUGuidedPackageInstallerKeyGuidePath] - withResult:[[theInfo objectForKey:SUGuidedPackageInstallerKeyResult] boolValue] - host:[theInfo objectForKey:SUGuidedPackageInstallerKeyHost] - error:[theInfo objectForKey:SUGuidedPackageInstallerKeyError] - delegate:[theInfo objectForKey:SUGuidedPackageInstallerKeyDelegate]]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + + // Preflight + NSString* installerPath = @"/usr/sbin/installer"; // Mac OS X 10.2+ command line installer tool + NSError* error = nil; + + // Create authorization for installer executable + BOOL validInstallation = NO; + AuthorizationRef auth = [self authorizationForExecutable:installerPath]; + if (auth != NULL) + { + // Permission was granted to execute the installer with privileges + const char* const arguments[] = { + "-pkg", + [packagePath fileSystemRepresentation], + "-target", + "/", + NULL + }; + validInstallation = AuthorizationExecuteWithPrivilegesAndWait(auth, + [installerPath fileSystemRepresentation], + kAuthorizationFlagDefaults, + arguments); + // TODO: wait for communications pipe to close via fileno & CFSocketCreateWithNative + AuthorizationFree(auth,kAuthorizationFlagDefaults); + } + else + { + NSString* errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Script authorization denied."]; + error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; + } + + dispatch_async(dispatch_get_main_queue(), ^{ + [self finishInstallationToPath:destinationPath + withResult:validInstallation + host:host + error:error + delegate:delegate]; + + }); + }); } @end diff --git a/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index c10434ae..9a40001f 100644 --- a/Sparkle/SUInstaller.m +++ b/Sparkle/SUInstaller.m @@ -14,7 +14,6 @@ #import "SUConstants.h" #import "SULog.h" - @implementation SUInstaller static NSString *sUpdateFolder = nil; @@ -43,7 +42,7 @@ static NSString *sUpdateFolder = nil; return NO; } -+ (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr ++ (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr isGuided:(BOOL *)isGuidedPtr { // Search subdirectories for the application NSString *currentFile, @@ -51,14 +50,14 @@ static NSString *sUpdateFolder = nil; *bundleFileName = [[host bundlePath] lastPathComponent], *alternateBundleFileName = [[host name] stringByAppendingPathExtension:[[host bundlePath] pathExtension]]; BOOL isPackage = NO; + BOOL isGuided = NO; NSString *fallbackPackagePath = nil; NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:inUpdateFolder]; NSString *bundleFileNameNoExtension = [bundleFileName stringByDeletingPathExtension]; - + sUpdateFolder = inUpdateFolder; - while ((currentFile = [dirEnum nextObject])) - { + while ((currentFile = [dirEnum nextObject])) { NSString *currentPath = [inUpdateFolder stringByAppendingPathComponent:currentFile]; NSString *currentFilename = [currentFile lastPathComponent]; NSString *currentExtension = [currentFile pathExtension]; @@ -69,28 +68,20 @@ static NSString *sUpdateFolder = nil; isPackage = NO; newAppDownloadPath = currentPath; break; - } - else if ([currentExtension isEqualToString:@"pkg"] || - [currentExtension isEqualToString:@"mpkg"]) - { - if ([currentFilenameNoExtension isEqualToString:bundleFileNameNoExtension]) - { + } else if ([currentExtension isEqualToString:@"pkg"] || + [currentExtension isEqualToString:@"mpkg"]) { + if ([currentFilenameNoExtension isEqualToString:bundleFileNameNoExtension]) { isPackage = YES; newAppDownloadPath = currentPath; break; - } - else - { + } else { // Remember any other non-matching packages we have seen should we need to use one of them as a fallback. fallbackPackagePath = currentPath; } - } - else - { + } else { // Try matching on bundle identifiers in case the user has changed the name of the host app NSBundle *incomingBundle = [NSBundle bundleWithPath:currentPath]; - if(incomingBundle && [[incomingBundle bundleIdentifier] isEqualToString:[[host bundle] bundleIdentifier]]) - { + if (incomingBundle && [[incomingBundle bundleIdentifier] isEqualToString:[[host bundle] bundleIdentifier]]) { isPackage = NO; newAppDownloadPath = currentPath; break; @@ -104,45 +95,49 @@ static NSString *sUpdateFolder = nil; // We don't have a valid path. Try to use the fallback package. - if (newAppDownloadPath == nil && fallbackPackagePath != nil) - { + if (newAppDownloadPath == nil && fallbackPackagePath != nil) { isPackage = YES; newAppDownloadPath = fallbackPackagePath; } - if (isPackagePtr) *isPackagePtr = isPackage; + if (isPackage) { + // foo.app -> foo.sparkle_guided.pkg or foo.sparkle_guided.mpkg + if ([[[newAppDownloadPath stringByDeletingPathExtension] pathExtension] isEqualToString:@"sparkle_guided"]) { + isGuided = YES; + } + } + + if (isPackagePtr) + *isPackagePtr = isPackage; + if (isGuidedPtr) + *isGuidedPtr = isGuided; return newAppDownloadPath; } + (NSString *)appPathInUpdateFolder:(NSString *)updateFolder forHost:(SUHost *)host { BOOL isPackage = NO; - NSString *path = [self installSourcePathInUpdateFolder:updateFolder forHost:host isPackage:&isPackage]; + NSString *path = [self installSourcePathInUpdateFolder:updateFolder forHost:host isPackage:&isPackage isGuided:nil]; return isPackage ? nil : path; } + (void)installFromUpdateFolder:(NSString *)inUpdateFolder overHost:(SUHost *)host installationPath:(NSString *)installationPath delegate:(id)delegate versionComparator:(id)comparator { - // Look for installer guide - NSString* installerGuide = [SUGuidedPackageInstaller installerGuideWithinUpdateFolder:inUpdateFolder]; - if (installerGuide) - { - [SUGuidedPackageInstaller performInstallationToPath:installationPath fromPath:installerGuide host:host delegate:delegate synchronously:NO versionComparator:comparator]; - } - else - { - BOOL isPackage = NO; - NSString *newAppDownloadPath = [self installSourcePathInUpdateFolder:inUpdateFolder forHost:host isPackage:&isPackage]; + BOOL isPackage = NO; + BOOL isGuided = NO; + NSString *newAppDownloadPath = [self installSourcePathInUpdateFolder:inUpdateFolder forHost:host isPackage:&isPackage isGuided:&isGuided]; - if (newAppDownloadPath == nil) - { - [self finishInstallationToPath:installationPath withResult:NO host:host error:[NSError errorWithDomain:SUSparkleErrorDomain code:SUMissingUpdateError userInfo:@{ NSLocalizedDescriptionKey: @"Couldn't find an appropriate update in the downloaded package." }] delegate:delegate]; - } - else - { - [(isPackage ? [SUPackageInstaller class] : [SUPlainInstaller class])performInstallationToPath:installationPath fromPath:newAppDownloadPath host:host delegate:delegate versionComparator:comparator]; - } - } + if (newAppDownloadPath == nil) { + [self finishInstallationToPath:installationPath withResult:NO host:host error:[NSError errorWithDomain:SUSparkleErrorDomain code:SUMissingUpdateError userInfo:@{ NSLocalizedDescriptionKey: @"Couldn't find an appropriate update in the downloaded package." }] delegate:delegate]; + } else { + if (isPackage && isGuided) { + [SUGuidedPackageInstaller performInstallationToPath:installationPath fromPath:newAppDownloadPath host:host delegate:delegate versionComparator:comparator]; + } else if (isPackage) { + [SUPackageInstaller performInstallationToPath:installationPath fromPath:newAppDownloadPath host:host delegate:delegate versionComparator:comparator]; + } else { + [SUPlainInstaller performInstallationToPath:installationPath fromPath:newAppDownloadPath host:host delegate:delegate versionComparator:comparator]; + } + } } + (void)mdimportInstallationPath:(NSString *)installationPath