diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 48355761..2f2b8fc8 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -24,7 +24,7 @@ @property (readonly, copy) NSString *displayVersion; @property (getter=isRunningOnReadOnlyVolume, readonly) BOOL runningOnReadOnlyVolume; @property (readonly, copy) NSString *publicDSAKey; -@property (readonly, copy) NSString *publicDSAKeyFileKey; +@property (readonly, nonatomic, copy) NSString *publicDSAKeyFileKey; - (id)objectForInfoDictionaryKey:(NSString *)key; - (BOOL)boolForInfoDictionaryKey:(NSString *)key; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 67889195..8bd84028 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -15,10 +15,11 @@ #error This is a "core" class and should NOT import AppKit #endif -// This class should also be process independent +// This class should not rely on AppKit and should also be process independent // For example, it should not have code that tests writabilty to somewhere on disk, // as that may depend on the privileges of the process owner. Or code that depends on -// if the process is sandboxed or not; eg: finding user caches directory +// if the process is sandboxed or not; eg: finding the user's caches directory. Or code that depends +// on compilation flags and if other files exist relative to the host bundle. @interface SUHost () diff --git a/Sparkle/SUSystemUpdateInfo.h b/Sparkle/SUSystemUpdateInfo.h new file mode 100644 index 00000000..28f89978 --- /dev/null +++ b/Sparkle/SUSystemUpdateInfo.h @@ -0,0 +1,21 @@ +// +// SUSystemUpdateInfo.h +// Sparkle +// +// Created by Mayur Pawashe on 12/23/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@class SUHost; + +@interface SUSystemUpdateInfo : NSObject + ++ (BOOL)systemAllowsAutomaticUpdatesForHost:(SUHost *)host; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUSystemUpdateInfo.m b/Sparkle/SUSystemUpdateInfo.m new file mode 100644 index 00000000..7ef2dd8f --- /dev/null +++ b/Sparkle/SUSystemUpdateInfo.m @@ -0,0 +1,58 @@ +// +// SUSystemUpdateInfo.m +// Sparkle +// +// Created by Mayur Pawashe on 12/23/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUSystemUpdateInfo.h" +#import "SUHost.h" +#import "SUConstants.h" +#import "SUFileManager.h" + +@implementation SUSystemUpdateInfo + ++ (BOOL)systemAllowsAutomaticUpdatesForHost:(SUHost *)host +{ + // Does the developer want us to disable automatic updates? + NSNumber *developerAllowsAutomaticUpdates = [host objectForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; + if (developerAllowsAutomaticUpdates != nil && !developerAllowsAutomaticUpdates.boolValue) { + return NO; + } + + // Can we automatically update in the background without bugging the user (e.g, with a administrator password prompt)? + // Note it's very well possible to have the bundle be writable but not be able to write into the parent directory + // And if the bundle isn't writable, but we can write into the parent directory, we will still need to authorize to replace it + NSString *bundlePath = [host bundlePath]; + if (![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath.stringByDeletingLastPathComponent] || ![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath]) { + return NO; + } + + // Just because we have writability access does not mean we can set the correct owner/group silently + // Test if we can set the owner/group on a temporarily created file + // If we can, then we can probably perform an update without authorization + // One place where this matters is if you copy and run an app from /tmp/ + + NSString *tempFilename = @"permission_test" ; + + SUFileManager *suFileManager = [SUFileManager defaultManager]; + NSURL *tempDirectoryURL = [suFileManager makeTemporaryDirectoryWithPreferredName:tempFilename appropriateForDirectoryURL:[NSURL fileURLWithPath:NSTemporaryDirectory()] error:NULL]; + + if (tempDirectoryURL == nil) { + // I don't imagine this ever happening but in case it does, requesting authorization may be the better option + return NO; + } + + NSURL *tempFileURL = [tempDirectoryURL URLByAppendingPathComponent:tempFilename]; + + BOOL changeOwnerAndGroupSuccess = + [[NSData data] writeToURL:tempFileURL atomically:NO] && + [suFileManager changeOwnerAndGroupOfItemAtRootURL:tempFileURL toMatchURL:host.bundle.bundleURL error:NULL]; + + [suFileManager removeItemAtURL:tempDirectoryURL error:NULL]; + + return changeOwnerAndGroupSuccess; +} + +@end