diff --git a/packet/conn.go b/packet/conn.go index 3772e1a33..1868031d1 100644 --- a/packet/conn.go +++ b/packet/conn.go @@ -7,7 +7,7 @@ import ( "net" "github.com/juju/errors" - . "github.com/siddontang/go-mysql/mysql" + . "github.com/space307/go-mysql/mysql" ) /* diff --git a/server/auth.go b/server/auth.go index b66ea4e0c..43145dd72 100644 --- a/server/auth.go +++ b/server/auth.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/binary" - . "github.com/siddontang/go-mysql/mysql" + . "github.com/space307/go-mysql/mysql" ) func (c *Conn) writeInitialHandshake() error { @@ -58,7 +58,7 @@ func (c *Conn) writeInitialHandshake() error { return c.WritePacket(data) } -func (c *Conn) readHandshakeResponse(password string) error { +func (c *Conn) readHandshakeResponse(authenticator Authentificator) error { data, err := c.ReadPacket() if err != nil { @@ -85,8 +85,9 @@ func (c *Conn) readHandshakeResponse(password string) error { user := string(data[pos : pos+bytes.IndexByte(data[pos:], 0)]) pos += len(user) + 1 - if c.user != user { - return NewDefaultError(ER_NO_SUCH_USER, user, c.RemoteAddr().String()) + password, err := authenticator.UserPass(user) + if err != nil { + return err } //auth length and auth @@ -100,6 +101,8 @@ func (c *Conn) readHandshakeResponse(password string) error { return NewDefaultError(ER_ACCESS_DENIED_ERROR, c.RemoteAddr().String(), c.user, "Yes") } + c.user = user + pos += authLen if c.capability|CLIENT_CONNECT_WITH_DB > 0 { diff --git a/server/command.go b/server/command.go index 3bc23ac33..bc01b6a43 100644 --- a/server/command.go +++ b/server/command.go @@ -4,8 +4,8 @@ import ( "bytes" "fmt" - . "github.com/siddontang/go-mysql/mysql" "github.com/siddontang/go/hack" + . "github.com/space307/go-mysql/mysql" ) type Handler interface { diff --git a/server/conn.go b/server/conn.go index d6ea846ae..cf1a210e5 100644 --- a/server/conn.go +++ b/server/conn.go @@ -4,11 +4,15 @@ import ( "net" "sync/atomic" - . "github.com/siddontang/go-mysql/mysql" - "github.com/siddontang/go-mysql/packet" "github.com/siddontang/go/sync2" + . "github.com/space307/go-mysql/mysql" + "github.com/space307/go-mysql/packet" ) +type Authentificator interface { + UserPass(string) (string, error) +} + /* Conn acts like a MySQL server connection, you can use MySQL client to communicate with it. */ @@ -35,12 +39,11 @@ type Conn struct { var baseConnID uint32 = 10000 -func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) { +func NewConn(conn net.Conn, auth Authentificator, h Handler) (*Conn, error) { c := new(Conn) c.h = h - - c.user = user + c.Conn = packet.NewConn(conn) c.connectionID = atomic.AddUint32(&baseConnID, 1) @@ -51,7 +54,7 @@ func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, err c.closed.Set(false) - if err := c.handshake(password); err != nil { + if err := c.handshake(auth); err != nil { c.Close() return nil, err } @@ -59,12 +62,12 @@ func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, err return c, nil } -func (c *Conn) handshake(password string) error { +func (c *Conn) handshake(auth Authentificator) error { if err := c.writeInitialHandshake(); err != nil { return err } - if err := c.readHandshakeResponse(password); err != nil { + if err := c.readHandshakeResponse(auth); err != nil { c.writeError(err) return err diff --git a/server/resp.go b/server/resp.go index 1123032dd..77dc18301 100644 --- a/server/resp.go +++ b/server/resp.go @@ -3,7 +3,7 @@ package server import ( "fmt" - . "github.com/siddontang/go-mysql/mysql" + . "github.com/space307/go-mysql/mysql" ) func (c *Conn) writeOK(r *Result) error { diff --git a/server/stmt.go b/server/stmt.go index 7a325d71e..6b9b52be9 100644 --- a/server/stmt.go +++ b/server/stmt.go @@ -7,7 +7,7 @@ import ( "strconv" "github.com/juju/errors" - . "github.com/siddontang/go-mysql/mysql" + . "github.com/space307/go-mysql/mysql" ) var paramFieldData []byte