From d3f975816ea14f3eecd33e5b87b856e72b983c20 Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Tue, 3 Jul 2012 18:37:50 -0700 Subject: [PATCH] Delegated password prompting to the update driver. The SUDiskImageUnarchiver shouldn't know about UI, and besides, it's important that update drivers which should not display UI (like the automatically-installing and probing drivers) should not display password prompts. --- SUDiskImageUnarchiver.m | 73 ++++++++++++++++++++++++----------------- SUUIBasedUpdateDriver.m | 14 ++++++++ SUUnarchiver.h | 1 + 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/SUDiskImageUnarchiver.m b/SUDiskImageUnarchiver.m index 45220b68..e0099066 100644 --- a/SUDiskImageUnarchiver.m +++ b/SUDiskImageUnarchiver.m @@ -11,7 +11,6 @@ #import "NTSynchronousTask.h" #import "SULog.h" #import -#import "SUPasswordPrompt.h" @implementation SUDiskImageUnarchiver @@ -20,11 +19,28 @@ return [[path pathExtension] isEqualToString:@"dmg"]; } +// Called on a non-main thread. - (void)extractDMG -{ - // GETS CALLED ON NON-MAIN THREAD!!! +{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSData *result = [NTSynchronousTask task:@"/usr/bin/hdiutil" directory:@"/" withArgs:[NSArray arrayWithObjects: @"isencrypted", archivePath, nil] input:NULL]; + if([self isEncrypted:result] && [delegate respondsToSelector:@selector(unarchiver:requiresPasswordReturnedViaInvocation:)]) { + [self performSelectorOnMainThread:@selector(requestPasswordFromDelegate) withObject:nil waitUntilDone:NO]; + } else { + [self extractDMGWithPassword:nil]; + } + + [pool release]; +} + +// Called on a non-main thread. +- (void)extractDMGWithPassword:(NSString *)password +{ + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + BOOL mountedSuccessfully = NO; SULog(@"Extracting %@ as a DMG", archivePath); @@ -49,37 +65,18 @@ } while (noErr == FSPathMakeRefWithOptions((UInt8 *)[mountPoint fileSystemRepresentation], kFSPathMakeRefDoNotFollowLeafSymlink, &tmpRef, NULL)); - BOOL isEncrypted = NO; - NSData *result = [NTSynchronousTask task:@"/usr/bin/hdiutil" directory:@"/" withArgs:[NSArray arrayWithObjects: @"isencrypted", archivePath, nil] input:NULL]; - if([self isEncrypted:result]) - isEncrypted = YES; - - NSArray* arguments = [NSArray arrayWithObjects:@"attach", archivePath, @"-mountpoint", mountPoint, /*@"-noverify",*/ @"-nobrowse", @"-noautoopen", nil]; - // set up a pipe and push "yes" (y works too), this will accept any license agreement crap - // not every .dmg needs this, but this will make sure it works with everyone - - NSData* promptData; - if(isEncrypted) { - SUPasswordPrompt *prompt = [[SUPasswordPrompt alloc] initWithHost:(SUHost*)[delegate host]]; - if([prompt run]) - { - NSString *password = [prompt password]; - if(![password length]) - goto reportError; - NSString *data = [NSString stringWithFormat:@"%@\nyes\n", password]; - const char *bytes = [data cStringUsingEncoding:NSUTF8StringEncoding]; - NSUInteger length = [data lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; - promptData = [NSData dataWithBytes:bytes length:length]; - } - else - { - goto reportError; - } - [prompt release]; + NSData *promptData = nil; + if (password) { + NSString *data = [NSString stringWithFormat:@"%@\nyes\n", password]; + const char *bytes = [data cStringUsingEncoding:NSUTF8StringEncoding]; + NSUInteger length = [data lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + promptData = [NSData dataWithBytes:bytes length:length]; } else promptData = [NSData dataWithBytes:"yes\n" length:4]; + NSArray* arguments = [NSArray arrayWithObjects:@"attach", archivePath, @"-mountpoint", mountPoint, /*@"-noverify",*/ @"-nobrowse", @"-noautoopen", nil]; + NSData *output = nil; NSInteger taskResult = -1; @try @@ -92,7 +89,7 @@ output = [[[task output] copy] autorelease]; [task release]; } - @catch (NSException *localException) + @catch (NSException *localException) { goto reportError; } @@ -187,4 +184,18 @@ finally: return result; } +- (void)requestPasswordFromDelegate +{ + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(continueWithPassword:)]]; + [invocation setSelector:@selector(continueWithPassword:)]; + [invocation setTarget:self]; + [invocation retainArguments]; + [delegate unarchiver:self requiresPasswordReturnedViaInvocation:invocation]; +} + +- (void)continueWithPassword:(NSString *)password +{ + [NSThread detachNewThreadSelector:@selector(extractDMGWithPassword:) toTarget:self withObject:password]; +} + @end diff --git a/SUUIBasedUpdateDriver.m b/SUUIBasedUpdateDriver.m index 2b209590..dc37e056 100644 --- a/SUUIBasedUpdateDriver.m +++ b/SUUIBasedUpdateDriver.m @@ -13,6 +13,7 @@ #import "SUHost.h" #import "SUStatusController.h" #import "SUConstants.h" +#import "SUPasswordPrompt.h" @implementation SUUIBasedUpdateDriver @@ -160,6 +161,19 @@ [NSApp requestUserAttention:NSInformationalRequest]; } +- (void)unarchiver:(SUUnarchiver *)unarchiver requiresPasswordReturnedViaInvocation:(NSInvocation *)invocation +{ + SUPasswordPrompt *prompt = [[SUPasswordPrompt alloc] initWithHost:host]; + NSString *password = nil; + if([prompt run]) + { + password = [prompt password]; + } + [prompt release]; + [invocation setArgument:&password atIndex:2]; + [invocation invoke]; +} + - (void)installAndRestart: (id)sender { [self installWithToolAndRelaunch:YES]; diff --git a/SUUnarchiver.h b/SUUnarchiver.h index 261187c5..c726d174 100644 --- a/SUUnarchiver.h +++ b/SUUnarchiver.h @@ -28,6 +28,7 @@ - (void)unarchiver:(SUUnarchiver *)unarchiver extractedLength:(unsigned long)length; - (void)unarchiverDidFinish:(SUUnarchiver *)unarchiver; - (void)unarchiverDidFail:(SUUnarchiver *)unarchiver; +- (void)unarchiver:(SUUnarchiver *)unarchiver requiresPasswordReturnedViaInvocation:(NSInvocation *)invocation; @end #endif