Exemple #1
0
func (c *Conn) useDB(db string) error {
	db = strings.ToLower(db)
	if s := c.server.GetSchema(db); s == nil {
		return mysql.NewDefaultError(mysql.ER_BAD_DB_ERROR, db)
	} else {
		c.db = db
	}

	return nil
}
Exemple #2
0
func (c *Conn) readHandshakeResponse() error {
	data, err := c.readPacket()

	if err != nil {
		return errors.Trace(err)
	}

	pos := 0
	//capability
	c.capability = binary.LittleEndian.Uint32(data[:4])
	pos += 4
	//skip max packet size
	pos += 4
	//charset, skip, if you want to use another charset, use set names
	c.collation = mysql.CollationId(data[pos])
	pos++
	//skip reserved 23[00]
	pos += 23
	//user name
	c.user = string(data[pos : pos+bytes.IndexByte(data[pos:], 0)])
	pos += len(c.user) + 1
	//auth length and auth
	authLen := int(data[pos])
	pos++
	auth := data[pos : pos+authLen]
	checkAuth := mysql.CalcPassword(c.salt, []byte(c.server.CfgGetPwd()))
	if !bytes.Equal(auth, checkAuth) && !c.server.SkipAuth() {
		return errors.Trace(mysql.NewDefaultError(mysql.ER_ACCESS_DENIED_ERROR, c.c.RemoteAddr().String(), c.user, "Yes"))
	}

	pos += authLen
	if c.capability|mysql.CLIENT_CONNECT_WITH_DB > 0 {
		if len(data[pos:]) == 0 {
			return nil
		}

		db := string(data[pos : pos+bytes.IndexByte(data[pos:], 0)])
		if err := c.useDB(db); err != nil {
			return errors.Trace(err)
		}
	}

	return nil
}