From 85472b64341edbbe26ac5689c6bdf7185f4c564d Mon Sep 17 00:00:00 2001 From: andym Date: Mon, 31 Mar 2008 09:09:05 +0000 Subject: [PATCH] Fixing #151, courtesy Mark Mentovai. This adds support for apps that quarantine their files. --- NSFileManager+Authentication.m | 166 +++++++++++++++++++++++++----- Sparkle.xcodeproj/project.pbxproj | 10 +- 2 files changed, 148 insertions(+), 28 deletions(-) diff --git a/NSFileManager+Authentication.m b/NSFileManager+Authentication.m index eb9d2474..a6907b09 100644 --- a/NSFileManager+Authentication.m +++ b/NSFileManager+Authentication.m @@ -17,6 +17,37 @@ #import #import +#import "NSFileManager+ExtendedAttributes.h" + +// TN OV02 says that the range between 1000 and 9999 can be used for +// application-defined errors. errToolFailedError will indicate that an +// executed program crashed or indicated failure with a nonzero exit status +static const OSStatus errToolFailedError = 1000; + +static OSStatus AuthorizationExecuteWithPrivilegesAndWait( + AuthorizationRef authorization, + const char* executablePath, + AuthorizationFlags options, + const char* const* arguments) { + sig_t oldSigChildHandler = signal(SIGCHLD, SIG_DFL); + OSStatus ret; + ret = AuthorizationExecuteWithPrivileges(authorization, + executablePath, + options, + (char* const*)arguments, + NULL); + if (ret == errAuthorizationSuccess) { + int status; + pid_t pid = wait(&status); + if (pid == -1 || + !WIFEXITED(status) || WEXITSTATUS(status) != 0) { + ret = errToolFailedError; + } + } + signal(SIGCHLD, oldSigChildHandler); + return ret; +} + @implementation NSFileManager (SUAuthenticationAdditions) - (BOOL)currentUserOwnsPath:(NSString *)oPath @@ -57,36 +88,103 @@ - (BOOL)_copyPathWithForcedAuthentication:(NSString *)src toPath:(NSString *)dst { NSString *tmp = [[[dst stringByDeletingPathExtension] stringByAppendingString:@".old"] stringByAppendingPathExtension:[dst pathExtension]]; - BOOL res = NO; - struct stat sb; - if((stat([src fileSystemRepresentation], &sb) != 0) || (stat([tmp fileSystemRepresentation], &sb) == 0) || stat([dst fileSystemRepresentation], &sb) != 0) - return false; + const char* srcPath = [src fileSystemRepresentation]; + const char* tmpPath = [tmp fileSystemRepresentation]; + const char* dstPath = [dst fileSystemRepresentation]; - NSString *command = [NSString stringWithFormat:@"mv -f \"%@\" \"%@\" && cp -f -R \"%@\" \"%@\" && rm -rf \"%@\" && chown -R %d:%d \"%@\"", - dst, - tmp, - src, - dst, - tmp, - sb.st_uid, - sb.st_gid, - dst]; - - AuthorizationRef auth = NULL; - if(AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth) == errAuthorizationSuccess) - { - char const* arguments[] = { "-c", [command fileSystemRepresentation], NULL }; - if(AuthorizationExecuteWithPrivileges(auth, "/bin/sh", kAuthorizationFlagDefaults, (char *const *)arguments, NULL) == errAuthorizationSuccess) - { - int status; - pid_t pid = wait(&status); - if(pid != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0) - res = YES; - } - AuthorizationFree(auth, 0); + struct stat sb, dstSB; + if ((stat(srcPath, &sb) != 0) || + (stat(tmpPath, &sb) == 0) || + (stat(dstPath, &dstSB) != 0)) { + return NO; } - return res; + AuthorizationRef auth = NULL; + OSStatus authStat = errAuthorizationDenied; + while (authStat == errAuthorizationDenied) { + authStat = AuthorizationCreate(NULL, + kAuthorizationEmptyEnvironment, + kAuthorizationFlagDefaults, + &auth); + } + + BOOL res = NO; + if (authStat == errAuthorizationSuccess) { + res = YES; + + char uidgid[42]; + snprintf(uidgid, sizeof(uidgid), "%d:%d", + dstSB.st_uid, dstSB.st_gid); + + const char* executables[] = { + "/bin/mv", + "/bin/mv", + "/bin/rm", + NULL, // pause here and do some housekeeping before + // continuing + "/usr/sbin/chown", + NULL // stop here for real + }; + + // 4 is the maximum number of arguments to any command, + // including the NULL that signals the end of an argument + // list. + const char* const argumentLists[][4] = { + { "-f", dstPath, tmpPath, NULL }, // mv + { "-f", srcPath, dstPath, NULL }, // mv + { "-rf", tmpPath, NULL }, // rm + { NULL }, // pause + { "-R", uidgid, dstPath, NULL }, // chown + { NULL } // stop + }; + + // Process the commands up until the first NULL + int commandIndex = 0; + for (; executables[commandIndex] != NULL; ++commandIndex) { + if (res) { + res = ( + AuthorizationExecuteWithPrivilegesAndWait(auth, + executables[commandIndex], + kAuthorizationFlagDefaults, + argumentLists[commandIndex]) == + errAuthorizationSuccess); + } + } + + // If the currently-running application is trusted, the new + // version should be trusted as well. Remove it from the + // quarantine to avoid a delay at launch, and to avoid + // presenting the user with a confusing trust dialog. + // + // This needs to be done after the application is moved to its + // new home with "mv" in case it's moved across filesystems: if + // that happens, "mv" actually performs a copy and may result + // in the application being quarantined. It also needs to be + // done before "chown" changes ownership, because the ownership + // change will almost certainly make it impossible to change + // attributes to release the files from the quarantine. + if (res) { + [self releaseFromQuarantine:dst]; + } + + // Now move past the NULL we found and continue executing + // commands from the list. + ++commandIndex; + + for (; executables[commandIndex] != NULL; ++commandIndex) { + if (res) { + res = ( + AuthorizationExecuteWithPrivilegesAndWait(auth, + executables[commandIndex], + kAuthorizationFlagDefaults, + argumentLists[commandIndex]) == + errAuthorizationSuccess); + } + } + + AuthorizationFree(auth, 0); + } + return res; } - (BOOL)copyPath:(NSString *)src overPath:(NSString *)dst withAuthentication:(BOOL)useAuthentication @@ -96,6 +194,20 @@ NSInteger tag = 0; BOOL result = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[dst stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[dst lastPathComponent]] tag:&tag]; result &= [[NSFileManager defaultManager] copyPath:src toPath:dst handler:nil]; + + // If the currently-running application is trusted, the new + // version should be trusted as well. Remove it from the + // quarantine to avoid a delay at launch, and to avoid + // presenting the user with a confusing trust dialog. + // + // This needs to be done after the application is moved to its + // new home in case it's moved across filesystems: if that + // happens, the move is actually a copy, and it may result + // in the application being quarantined. + if (result) { + [self releaseFromQuarantine:dst]; + } + return result; } else if (useAuthentication == YES) diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 327f6b01..1eb1c022 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -68,6 +68,8 @@ 61B5FCDE09C52A9F00B25A18 /* SUUpdateAlert.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B5FCA109C5228F00B25A18 /* SUUpdateAlert.m */; }; 61B5FCDF09C52A9F00B25A18 /* SUUpdateAlert.h in Headers */ = {isa = PBXBuildFile; fileRef = 61B5FCA009C5228F00B25A18 /* SUUpdateAlert.h */; settings = {ATTRIBUTES = (Public, ); }; }; 61BBDF820A49220C00378739 /* Sparkle.icns in Resources */ = {isa = PBXBuildFile; fileRef = 61BBDF810A49220C00378739 /* Sparkle.icns */; }; + 61C1F2780DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 61C1F2760DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 61C1F2790DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = 61C1F2770DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.m */; }; 61C46F340D9C54F300B06326 /* SUUpdatePermissionPrompt.nib in Resources */ = {isa = PBXBuildFile; fileRef = 61C46F330D9C54F300B06326 /* SUUpdatePermissionPrompt.nib */; }; D1E42C370CE754C700F50EB9 /* relaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 613242130CD06CEF00106AA4 /* relaunch.m */; }; D1E42C3B0CE755A000F50EB9 /* relaunch.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = D1E42C2F0CE754AE00F50EB9 /* relaunch.app */; }; @@ -258,7 +260,9 @@ 61B5FCA009C5228F00B25A18 /* SUUpdateAlert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdateAlert.h; sourceTree = ""; }; 61B5FCA109C5228F00B25A18 /* SUUpdateAlert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUpdateAlert.m; sourceTree = ""; }; 61BBDF810A49220C00378739 /* Sparkle.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Sparkle.icns; sourceTree = ""; }; - 61C46F350D9C54F300B06326 /* en */ = {isa = PBXFileReference; lastKnownFileType = file; name = en; path = en.lproj/SUUpdatePermissionPrompt.nib; sourceTree = ""; }; + 61C1F2760DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFileManager+ExtendedAttributes.h"; sourceTree = ""; }; + 61C1F2770DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileManager+ExtendedAttributes.m"; sourceTree = ""; }; + 61C46F350D9C54F300B06326 /* en */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = en; path = en.lproj/SUUpdatePermissionPrompt.nib; sourceTree = ""; }; 8DC2EF5A0486A6940098B216 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = ""; }; 8DC2EF5B0486A6940098B216 /* Sparkle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sparkle.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D1E42C2F0CE754AE00F50EB9 /* relaunch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = relaunch.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -453,6 +457,8 @@ 61A2262A0D1C58FD00430CCD /* SUUserDefaults.m */, 61A2279A0D1CEE7600430CCD /* SUSystemProfiler.h */, 61A2279B0D1CEE7600430CCD /* SUSystemProfiler.m */, + 61C1F2760DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.h */, + 61C1F2770DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.m */, ); includeInIndex = 1; name = Utilities; @@ -520,6 +526,7 @@ 612DCBAF0D488BC60015DBEA /* SUUpdatePermissionPrompt.h in Headers */, 6171D9070D57B81800BFE886 /* NSFileManager+Aliases.h in Headers */, 61180BCA0D64138900B4E0D1 /* SUWindowController.h in Headers */, + 61C1F2780DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -721,6 +728,7 @@ 612DCBB00D488BC60015DBEA /* SUUpdatePermissionPrompt.m in Sources */, 6171D9080D57B81800BFE886 /* NSFileManager+Aliases.m in Sources */, 61180BCB0D64138900B4E0D1 /* SUWindowController.m in Sources */, + 61C1F2790DA066C2007ACE13 /* NSFileManager+ExtendedAttributes.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };