Skip to content

Commit

Permalink
Fix old_password authentication via OldAuthSwitchRequest (go-sql-driv…
Browse files Browse the repository at this point in the history
…er#524)

If CLIENT_PLUGIN_AUTH capability is not supported, no new cipher is
sent have to keep using the cipher sent in the init packet.

Fixes go-sql-driver#518
  • Loading branch information
julienschmidt committed Nov 29, 2016
1 parent abfd04d commit 4ac31a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
}

// Handle response to auth packet, switch methods if possible
if err = handleAuthResult(mc); err != nil {
if err = handleAuthResult(mc, cipher); err != nil {
// Authentication failed and MySQL has already closed the connection
// (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
// Do not send COM_QUIT, just cleanup and return the error.
Expand Down Expand Up @@ -134,7 +134,7 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
return mc, nil
}

func handleAuthResult(mc *mysqlConn) error {
func handleAuthResult(mc *mysqlConn, oldCipher []byte) error {
// Read Result Packet
cipher, err := mc.readResultOK()
if err == nil {
Expand All @@ -150,6 +150,13 @@ func handleAuthResult(mc *mysqlConn) error {
// Retry with old authentication method. Note: there are edge cases
// where this should work but doesn't; this is currently "wontfix":
// https://github.com/go-sql-driver/mysql/issues/184

// If CLIENT_PLUGIN_AUTH capability is not supported, no new cipher is
// sent and we have to keep using the cipher sent in the init packet.
if cipher == nil {
cipher = oldCipher
}

if err = mc.writeOldAuthPacket(cipher); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ func (mc *mysqlConn) readResultOK() ([]byte, error) {
return cipher, ErrUnknownPlugin
}
} else {
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
return nil, ErrOldPassword
}

Expand Down

0 comments on commit 4ac31a9

Please sign in to comment.