Skip to content

Commit 4ac31a9

Browse files
Fix old_password authentication via OldAuthSwitchRequest (go-sql-driver#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
1 parent abfd04d commit 4ac31a9

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

driver.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
101101
}
102102

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

137-
func handleAuthResult(mc *mysqlConn) error {
137+
func handleAuthResult(mc *mysqlConn, oldCipher []byte) error {
138138
// Read Result Packet
139139
cipher, err := mc.readResultOK()
140140
if err == nil {
@@ -150,6 +150,13 @@ func handleAuthResult(mc *mysqlConn) error {
150150
// Retry with old authentication method. Note: there are edge cases
151151
// where this should work but doesn't; this is currently "wontfix":
152152
// https://github.com/go-sql-driver/mysql/issues/184
153+
154+
// If CLIENT_PLUGIN_AUTH capability is not supported, no new cipher is
155+
// sent and we have to keep using the cipher sent in the init packet.
156+
if cipher == nil {
157+
cipher = oldCipher
158+
}
159+
153160
if err = mc.writeOldAuthPacket(cipher); err != nil {
154161
return err
155162
}

packets.go

+1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ func (mc *mysqlConn) readResultOK() ([]byte, error) {
499499
return cipher, ErrUnknownPlugin
500500
}
501501
} else {
502+
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
502503
return nil, ErrOldPassword
503504
}
504505

0 commit comments

Comments
 (0)