Changeset 66

Show
Ignore:
Timestamp:
11/17/05 13:08:41 (3 years ago)
Author:
bart
Message:

Ticket #3 fixed: keys are only added without interaction if the unlocked keychain holds the key

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/AgentController.m

    r41 r66  
    225225                [allKeysOnAgentLock unlock]; 
    226226 
    227                 SecKeychainGetStatus(nil, &status); 
    228  
    229                 if((status & 1) && ([agent isRunning])) 
    230                 { 
    231                         [NSThread detachNewThreadSelector:@selector(addKeysToAgentWithoutInteractionInNewThread) 
     227                [NSThread detachNewThreadSelector:@selector(addKeysToAgentWithoutInteractionInNewThread) 
    232228                                                                toTarget:self withObject:self]; 
    233                 } 
    234229        } 
    235230} 
  • trunk/Controller.m

    r64 r66  
    336336                [appleKeychainUnlockedLock unlock]; 
    337337 
    338                 if([[NSUserDefaults standardUserDefaults] integerForKey:DisplayString] != 1) 
    339                 { 
    340                         giveFocusBack = YES; 
    341                         GetFrontProcess(&focusSerialNumber); 
    342                 } 
     338                GetFrontProcess(&focusSerialNumber); 
    343339 
    344340                [NSApp activateIgnoringOtherApps:YES]; 
    345341                SecKeychainUnlock(nil, 0, nil, 0); 
    346342 
    347                 if(giveFocusBack) 
    348                 { 
    349                         SetFrontProcess(&focusSerialNumber); 
    350                         giveFocusBack = NO; 
    351                 } 
     343                SetFrontProcess(&focusSerialNumber); 
    352344        } 
    353345} 
     
    359351        char *kcPassword; 
    360352        UInt32 passwordLength; 
    361         SecKeychainStatus status; 
     353        SecKeychainStatus keychainStatus; 
     354        OSStatus returnStatus; 
     355        SecKeychainRef keychain; 
     356 
     357        CFArrayRef searchList; 
    362358 
    363359        SInt32 error; 
     
    366362        CFStringRef enteredPassphrase; 
    367363 
    368         NSString *passphrase, *firstQuestion, *secondQuestion
     364        NSString *passphrase, *firstQuestion
    369365        NSMutableDictionary *dict; 
    370366        BOOL consultKeychain = NO; 
    371367 
    372368        ProcessSerialNumber focusSerialNumber; 
    373         BOOL giveFocusBack = NO; 
     369 
     370        SecKeychainSetUserInteractionAllowed(TRUE); 
     371 
     372        int i; 
    374373                 
    375374        [passphraseIsRequestedLock lock]; 
     
    385384 
    386385        firstQuestion = @"Enter passphrase for "; 
    387         secondQuestion = @"'s password: "; 
    388386 
    389387        if([question hasPrefix:firstQuestion]) 
     
    391389                consultKeychain = YES; 
    392390                accountName = [[[[question substringFromIndex:[firstQuestion length]] 
    393                         componentsSeparatedByString:@": "] objectAtIndex:0] cString]; 
     391                                               componentsSeparatedByString:@": "] objectAtIndex:0] cString]; 
    394392        } 
     393 
    395394        else 
    396395        { 
    397                 if ([question hasSuffix:secondQuestion]) 
     396                if([question hasSuffix:@"'s password: "]) 
    398397                { 
    399398                        consultKeychain = [[NSUserDefaults standardUserDefaults] boolForKey:AddInteractivePasswordString]; 
    400399                        accountName = [[[question componentsSeparatedByString:@"'s"] objectAtIndex:0] cString]; 
    401400                } 
     401 
    402402                else 
    403403                { 
    404                         if ([question hasPrefix:@"The authenticity of host"]) 
     404                        if([question hasPrefix:@"The authenticity of host"]) 
    405405                        { 
    406406                                [passphraseIsRequestedLock lock]; 
    407407                                passphraseIsRequested = NO; 
    408408                                [passphraseIsRequestedLock unlock]; 
    409                                 if (interaction) 
     409 
     410                                if(interaction) 
    410411                                { 
    411                                         int r = NSRunAlertPanel(local(@"UnknownHostKey"), 
    412                                                 question, 
    413                                                 local(@"No"), 
    414                                                 local(@"Yes"), 
    415                                                 nil); 
     412                                        int r = NSRunAlertPanel(local(@"UnknownHostKey"), question, local(@"No"), local(@"Yes"), nil); 
    416413                                        NSString *response = ( r == NSAlertAlternateReturn) ? @"yes" : @"no"; 
    417414                                        return response; 
    418415                                } 
     416 
    419417                                else 
    420418                                { 
     
    429427                serviceName = "SSHKeychain"; 
    430428 
    431                 SecKeychainGetStatus(nil, &status); 
    432  
    433                 [appleKeychainUnlockedLock lock]; 
    434                  
    435                 if(!appleKeychainUnlocked) 
    436                 { 
    437                         if([[NSUserDefaults standardUserDefaults] integerForKey:DisplayString] != 1) 
    438                         { 
    439                                 giveFocusBack = YES; 
    440                                 GetFrontProcess(&focusSerialNumber); 
     429                GetFrontProcess(&focusSerialNumber); 
     430 
     431                if(!interaction) 
     432                { 
     433                        SecKeychainCopySearchList(&searchList); 
     434                         
     435                        for(i=0; i < [(NSArray *)searchList count]; i++) { 
     436                                keychain = (SecKeychainRef)[(NSArray *)searchList objectAtIndex:i]; 
     437 
     438                                SecKeychainGetStatus(keychain, &keychainStatus); 
     439                                 
     440                                if(keychainStatus & 1) { 
     441                                        returnStatus = SecKeychainFindGenericPassword(keychain, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 
     442                                         
     443                                        if(returnStatus == 0) { 
     444                                                break; 
     445                                        }  
     446                                } 
    441447                        } 
    442  
    443                         [NSApp activateIgnoringOtherApps:YES]; 
    444                 } 
    445                  
    446                 [appleKeychainUnlockedLock unlock]; 
    447  
    448                 if(!interaction) 
    449                 { 
    450                         SecKeychainSetUserInteractionAllowed(NO); 
    451                         status = SecKeychainFindGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 
    452                         SecKeychainSetUserInteractionAllowed(YES); 
     448                         
     449                        CFRelease(searchList); 
    453450                } 
    454451                 
    455452                else 
    456453                { 
    457                         status = SecKeychainFindGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 
    458                 } 
    459                  
    460                 if(giveFocusBack) 
    461                 { 
    462                         SetFrontProcess(&focusSerialNumber); 
    463                         giveFocusBack = NO; 
    464                 } 
     454                        returnStatus = SecKeychainFindGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 
     455                } 
     456                 
     457                SetFrontProcess(&focusSerialNumber); 
    465458                 
    466459                [passphraseIsRequestedLock lock]; 
     
    468461                [passphraseIsRequestedLock unlock]; 
    469462                 
    470                 if(status == 0) 
     463                if(returnStatus == 0) 
    471464                { 
    472465                        kcPassword[passwordLength] = '\0'; 
     
    538531                                serviceName = "SSHKeychain"; 
    539532                                 
    540                                 [appleKeychainUnlockedLock lock]; 
    541  
    542                                 if(!appleKeychainUnlocked) 
    543                                 { 
    544                                         if([[NSUserDefaults standardUserDefaults] integerForKey:DisplayString] != 1) 
    545                                         { 
    546                                                 giveFocusBack = YES; 
    547                                                 GetFrontProcess(&focusSerialNumber); 
    548                                         } 
    549                                          
    550                                         [NSApp activateIgnoringOtherApps:YES]; 
    551                                 } 
    552                                  
    553                                 [appleKeychainUnlockedLock unlock]; 
     533                                GetFrontProcess(&focusSerialNumber); 
    554534                                 
    555535                                SecKeychainAddGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, [passphrase length], (const void *)[passphrase cString], nil); 
    556536                                 
    557                                 if(giveFocusBack) 
    558                                 { 
    559                                         SetFrontProcess(&focusSerialNumber); 
    560                                         giveFocusBack = NO; 
    561                                 } 
     537                                SetFrontProcess(&focusSerialNumber); 
    562538                        } 
    563539