-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Support authentication switch with mysql_native_password authentication #494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0f6bfaf
d6357d4
6be36f5
f01f372
6dbcd3c
5926162
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,15 @@ Default: false | |
``` | ||
`allowOldPasswords=true` allows the usage of the insecure old password method. This should be avoided, but is necessary in some cases. See also [the old_passwords wiki page](https://github.com/go-sql-driver/mysql/wiki/old_passwords). | ||
|
||
##### `allowNativeasswords` | ||
|
||
``` | ||
Type: bool | ||
Valid Values: true, false | ||
Default: false | ||
``` | ||
`allowNativeasswords=true` allows the usage of the mysql native password method.. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And only one . at the end of the sentence. |
||
|
||
##### `charset` | ||
|
||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ type Config struct { | |
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE | ||
AllowCleartextPasswords bool // Allows the cleartext client side plugin | ||
AllowOldPasswords bool // Allows the old insecure password method | ||
AllowNativePasswords bool // Allows mysql native password authentication method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove "mysql". It's obvious were not using MongoDB's native password authentication method |
||
ClientFoundRows bool // Return number of matching rows instead of rows changed | ||
ColumnsWithAlias bool // Prepend table alias to column names | ||
InterpolateParams bool // Interpolate placeholders into query string | ||
|
@@ -376,6 +377,14 @@ func parseDSNParams(cfg *Config, params string) (err error) { | |
return errors.New("invalid bool value: " + value) | ||
} | ||
|
||
// Use mysql native password authentication | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
case "allowNativePasswords": | ||
var isBool bool | ||
cfg.AllowNativePasswords, isBool = readBool(value) | ||
if !isBool { | ||
return errors.New("invalid bool value: " + value) | ||
} | ||
|
||
// Switch "rowsAffected" mode | ||
case "clientFoundRows": | ||
var isBool bool | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ var ( | |
ErrNoTLS = errors.New("TLS requested but server does not support TLS") | ||
ErrOldPassword = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords") | ||
ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN") | ||
ErrNativePassword = errors.New("this user requires mysql native password authentication.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
ErrUnknownPlugin = errors.New("this authentication plugin is not supported") | ||
ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+") | ||
ErrPktSync = errors.New("commands out of sync. You can't run this command now") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,6 +372,26 @@ func (mc *mysqlConn) writeClearAuthPacket() error { | |
return mc.writePacket(data) | ||
} | ||
|
||
// MySql native password authentication method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove "MySql" |
||
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse | ||
func (mc *mysqlConn) writeNativeAuthPacket(cipher []byte) error { | ||
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.Passwd)) | ||
|
||
// Calculate the packet length and add a tailing 0 | ||
pktLen := len(scrambleBuff) | ||
data := mc.buf.takeSmallBuffer(4 + pktLen) | ||
if data == nil { | ||
// can not take the buffer. Something must be wrong with the connection | ||
errLog.Print(ErrBusyBuffer) | ||
return driver.ErrBadConn | ||
} | ||
|
||
// Add the scramble | ||
copy(data[4:], scrambleBuff) | ||
|
||
return mc.writePacket(data) | ||
} | ||
|
||
/****************************************************************************** | ||
* Command Packets * | ||
******************************************************************************/ | ||
|
@@ -445,36 +465,42 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error { | |
******************************************************************************/ | ||
|
||
// Returns error if Packet is not an 'Result OK'-Packet | ||
func (mc *mysqlConn) readResultOK() error { | ||
func (mc *mysqlConn) readResultOK() (error, []byte) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flip the return values and name them so it's |
||
data, err := mc.readPacket() | ||
if err == nil { | ||
// packet indicator | ||
switch data[0] { | ||
|
||
case iOK: | ||
return mc.handleOkPacket(data) | ||
return mc.handleOkPacket(data), nil | ||
|
||
case iEOF: | ||
if len(data) > 1 { | ||
plugin := string(data[1:bytes.IndexByte(data, 0x00)]) | ||
pluginEndIndex := bytes.IndexByte(data, 0x00) | ||
plugin := string(data[1:pluginEndIndex]) | ||
cipher := data[pluginEndIndex + 1: len(data) - 1] | ||
|
||
if plugin == "mysql_old_password" { | ||
// using old_passwords | ||
return ErrOldPassword | ||
return ErrOldPassword, cipher | ||
} else if plugin == "mysql_clear_password" { | ||
// using clear text password | ||
return ErrCleartextPassword | ||
return ErrCleartextPassword, cipher | ||
} else if plugin == "mysql_native_password" { | ||
// using mysql default authentication method | ||
return ErrNativePassword, cipher | ||
} else { | ||
return ErrUnknownPlugin | ||
return ErrUnknownPlugin, cipher | ||
} | ||
} else { | ||
return ErrOldPassword | ||
return ErrOldPassword, nil | ||
} | ||
|
||
default: // Error otherwise | ||
return mc.handleErrorPacket(data) | ||
return mc.handleErrorPacket(data), nil | ||
} | ||
} | ||
return err | ||
return err, nil | ||
} | ||
|
||
// Result Set Header Packet | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should probably be
allowNativePasswords