driver.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  7. //
  8. // The driver should be used via the database/sql package:
  9. //
  10. // import "database/sql"
  11. // import _ "github.com/go-sql-driver/mysql"
  12. //
  13. // db, err := sql.Open("mysql", "user:password@/dbname")
  14. //
  15. // See https://github.com/go-sql-driver/mysql#usage for details
  16. package mysql
  17. import (
  18. "database/sql"
  19. "database/sql/driver"
  20. "net"
  21. )
  22. // This struct is exported to make the driver directly accessible.
  23. // In general the driver is used via the database/sql package.
  24. type MySQLDriver struct{}
  25. // DialFunc is a function which can be used to establish the network connection.
  26. // Custom dial functions must be registered with RegisterDial
  27. type DialFunc func(addr string) (net.Conn, error)
  28. var dials map[string]DialFunc
  29. // RegisterDial registers a custom dial function. It can then be used by the
  30. // network address mynet(addr), where mynet is the registered new network.
  31. // addr is passed as a parameter to the dial function.
  32. func RegisterDial(net string, dial DialFunc) {
  33. if dials == nil {
  34. dials = make(map[string]DialFunc)
  35. }
  36. dials[net] = dial
  37. }
  38. // Open new Connection.
  39. // See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
  40. // the DSN string is formated
  41. func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
  42. var err error
  43. // New mysqlConn
  44. mc := &mysqlConn{
  45. maxPacketAllowed: maxPacketSize,
  46. maxWriteSize: maxPacketSize - 1,
  47. }
  48. mc.cfg, err = parseDSN(dsn)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // Connect to Server
  53. if dial, ok := dials[mc.cfg.net]; ok {
  54. mc.netConn, err = dial(mc.cfg.addr)
  55. } else {
  56. nd := net.Dialer{Timeout: mc.cfg.timeout}
  57. mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. // Enable TCP Keepalives on TCP connections
  63. if tc, ok := mc.netConn.(*net.TCPConn); ok {
  64. if err := tc.SetKeepAlive(true); err != nil {
  65. // Don't send COM_QUIT before handshake.
  66. mc.netConn.Close()
  67. mc.netConn = nil
  68. return nil, err
  69. }
  70. }
  71. mc.buf = newBuffer(mc.netConn)
  72. // Reading Handshake Initialization Packet
  73. cipher, err := mc.readInitPacket()
  74. if err != nil {
  75. mc.cleanup()
  76. return nil, err
  77. }
  78. // Send Client Authentication Packet
  79. if err = mc.writeAuthPacket(cipher); err != nil {
  80. mc.cleanup()
  81. return nil, err
  82. }
  83. // Handle response to auth packet, switch methods if possible
  84. if err = handleAuthResult(mc, cipher); err != nil {
  85. // Authentication failed and MySQL has already closed the connection
  86. // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
  87. // Do not send COM_QUIT, just cleanup and return the error.
  88. mc.cleanup()
  89. return nil, err
  90. }
  91. // Get max allowed packet size
  92. maxap, err := mc.getSystemVar("max_allowed_packet")
  93. if err != nil {
  94. mc.Close()
  95. return nil, err
  96. }
  97. mc.maxPacketAllowed = stringToInt(maxap) - 1
  98. if mc.maxPacketAllowed < maxPacketSize {
  99. mc.maxWriteSize = mc.maxPacketAllowed
  100. }
  101. // Handle DSN Params
  102. err = mc.handleParams()
  103. if err != nil {
  104. mc.Close()
  105. return nil, err
  106. }
  107. return mc, nil
  108. }
  109. func handleAuthResult(mc *mysqlConn, cipher []byte) error {
  110. // Read Result Packet
  111. err := mc.readResultOK()
  112. if err == nil {
  113. return nil // auth successful
  114. }
  115. if mc.cfg == nil {
  116. return err // auth failed and retry not possible
  117. }
  118. // Retry auth if configured to do so.
  119. if mc.cfg.allowOldPasswords && err == ErrOldPassword {
  120. // Retry with old authentication method. Note: there are edge cases
  121. // where this should work but doesn't; this is currently "wontfix":
  122. // https://github.com/go-sql-driver/mysql/issues/184
  123. if err = mc.writeOldAuthPacket(cipher); err != nil {
  124. return err
  125. }
  126. err = mc.readResultOK()
  127. } else if mc.cfg.allowCleartextPasswords && err == ErrCleartextPassword {
  128. // Retry with clear text password for
  129. // http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
  130. // http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
  131. if err = mc.writeClearAuthPacket(); err != nil {
  132. return err
  133. }
  134. err = mc.readResultOK()
  135. }
  136. return err
  137. }
  138. func init() {
  139. sql.Register("mysql", &MySQLDriver{})
  140. }