diff options
Diffstat (limited to 'Source/SPDatabaseDocument.m')
-rw-r--r-- | Source/SPDatabaseDocument.m | 75 |
1 files changed, 58 insertions, 17 deletions
diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index fe555241..2340b97e 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -119,6 +119,7 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; - (void) closeAndDisconnect; +- (NSString *)keychainPasswordForConnection:(SPMySQLConnection *)connection; - (NSString *)keychainPasswordForSSHConnection:(SPMySQLConnection *)connection; @end @@ -217,6 +218,8 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; alterDatabaseCharsetHelper = nil; //init in awakeFromNib addDatabaseCharsetHelper = nil; + keyChainID = nil; + #ifndef SP_CODA /* init ivars */ statusValues = nil; printThread = nil; @@ -610,6 +613,14 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; return mySQLConnection; } +/** + * Sets this connection's Keychain ID. + */ +- (void)setKeychainID:(NSString *)theId +{ + keyChainID = [[NSString stringWithString:theId] retain]; +} + #pragma mark - #pragma mark Database methods @@ -2852,6 +2863,11 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; return thePort; } +- (NSString *)keyChainID +{ + return keyChainID; +} + - (BOOL)isSaveInBundle { return _isSavedInBundle; @@ -4658,8 +4674,7 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; } [connection setObject:connectionType forKey:@"type"]; - NSString *kcid = [connectionController connectionKeychainID]; - if ([kcid length]) [connection setObject:kcid forKey:@"kcid"]; + if ([[self keyChainID] length]) [connection setObject:[self keyChainID] forKey:@"kcid"]; [connection setObject:[self name] forKey:@"name"]; [connection setObject:[self host] forKey:@"host"]; [connection setObject:[self user] forKey:@"user"]; @@ -4671,7 +4686,7 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; [connection setObject:[self database] forKey:@"database"]; if (includePasswords) { - NSString *pw = [connectionController keychainPassword]; + NSString *pw = [self keychainPasswordForConnection:nil]; if (!pw) pw = [connectionController password]; if (pw) [connection setObject:pw forKey:@"password"]; @@ -4860,10 +4875,9 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; [connectionController setSslCACertFileLocation:[connection objectForKey:@"sslCACertFileLocation"]]; // Set the keychain details if available - NSString *kcid = (NSString *)[connection objectForKey:@"kcid"]; - if ([kcid length]) { - [connectionController setConnectionKeychainID:kcid]; - [connectionController setConnectionKeychainItemName:[keychain nameForFavoriteName:[connectionController name] id:kcid]]; + if ([connection objectForKey:@"kcid"] && [(NSString *)[connection objectForKey:@"kcid"] length]) { + [self setKeychainID:[connection objectForKey:@"kcid"]]; + [connectionController setConnectionKeychainItemName:[keychain nameForFavoriteName:[connectionController name] id:[self keyChainID]]]; [connectionController setConnectionKeychainItemAccount:[keychain accountForUser:[connectionController user] host:[connectionController host] database:[connection objectForKey:@"database"]]]; } @@ -4871,8 +4885,9 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; if ([connection objectForKey:@"password"]) [connectionController setPassword:[connection objectForKey:@"password"]]; else { - NSString *pw = [connectionController keychainPassword]; - if (pw) [connectionController setPassword:pw]; + NSString *pw = [self keychainPasswordForConnection:nil]; + if (pw) + [connectionController setPassword:pw]; } // Set the socket details, whether or not the type is a socket @@ -4895,8 +4910,8 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; if ([connection objectForKey:@"ssh_password"]) [connectionController setSshPassword:[connection objectForKey:@"ssh_password"]]; else { - if ([kcid length]) { - [connectionController setConnectionSSHKeychainItemName:[keychain nameForSSHForFavoriteName:[connectionController name] id:kcid]]; + if ([connection objectForKey:@"kcid"] && [(NSString *)[connection objectForKey:@"kcid"] length]) { + [connectionController setConnectionSSHKeychainItemName:[keychain nameForSSHForFavoriteName:[connectionController name] id:[self keyChainID]]]; [connectionController setConnectionSSHKeychainItemAccount:[keychain accountForSSHUser:[connectionController sshUser] sshHost:[connectionController sshHost]]]; } NSString *sshpw = [self keychainPasswordForSSHConnection:nil]; @@ -7133,11 +7148,21 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; } /** - * Invoked when the current connection needs a password. + * Invoked when the current connection needs a password from the Keychain. */ -- (NSString *)passwordForConnection:(SPMySQLConnection *)connection authPlugin:(NSString *)pluginName +- (NSString *)keychainPasswordForConnection:(SPMySQLConnection *)connection { - return [[connectionController onMainThread] actualPasswordForAuthPlugin:pluginName]; + // If no keychain item is available, return an empty password + if (![connectionController connectionKeychainItemName]) return nil; + + // Otherwise, pull the password from the keychain using the details from this connection + SPKeychain *keychain = [[SPKeychain alloc] init]; + + NSString *password = [keychain getPasswordForName:[connectionController connectionKeychainItemName] account:[connectionController connectionKeychainItemAccount]]; + + [keychain release]; + + return password; } /** @@ -7148,10 +7173,25 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; - (NSString *)keychainPasswordForSSHConnection:(SPMySQLConnection *)connection { // If no keychain item is available, return an empty password - NSString *password = [connectionController keychainPasswordForSSH]; - if (!password) return @""; + if (![connectionController connectionKeychainItemName]) return @""; - return password; + // Otherwise, pull the password from the keychain using the details from this connection + SPKeychain *keychain = [[SPKeychain alloc] init]; + + NSString *connectionSSHKeychainItemName = [[keychain nameForSSHForFavoriteName:[connectionController name] id:[self keyChainID]] retain]; + NSString *connectionSSHKeychainItemAccount = [[keychain accountForSSHUser:[connectionController sshUser] sshHost:[connectionController sshHost]] retain]; + NSString *sshPassword = [keychain getPasswordForName:connectionSSHKeychainItemName account:connectionSSHKeychainItemAccount]; + + if (!sshPassword || ![sshPassword length]) { + sshPassword = @""; + } + + if (connectionSSHKeychainItemName) [connectionSSHKeychainItemName release]; + if (connectionSSHKeychainItemAccount) [connectionSSHKeychainItemAccount release]; + + [keychain release]; + + return sshPassword; } /** @@ -7714,6 +7754,7 @@ static int64_t SPDatabaseDocumentInstanceCounter = 0; if (spfPreferences) SPClear(spfPreferences); if (spfSession) SPClear(spfSession); if (spfDocData) SPClear(spfDocData); + if (keyChainID) SPClear(keyChainID); if (mainToolbar) SPClear(mainToolbar); if (titleAccessoryView) SPClear(titleAccessoryView); if (taskProgressWindow) SPClear(taskProgressWindow); |