You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

driver.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // Package mysql provides 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. // MySQLDriver 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. maxAllowedPacket: maxPacketSize,
  46. maxWriteSize: maxPacketSize - 1,
  47. }
  48. mc.cfg, err = ParseDSN(dsn)
  49. if err != nil {
  50. return nil, err
  51. }
  52. mc.parseTime = mc.cfg.ParseTime
  53. mc.strict = mc.cfg.Strict
  54. // Connect to Server
  55. if dial, ok := dials[mc.cfg.Net]; ok {
  56. mc.netConn, err = dial(mc.cfg.Addr)
  57. } else {
  58. nd := net.Dialer{Timeout: mc.cfg.Timeout}
  59. mc.netConn, err = nd.Dial(mc.cfg.Net, mc.cfg.Addr)
  60. }
  61. if err != nil {
  62. return nil, err
  63. }
  64. // Enable TCP Keepalives on TCP connections
  65. if tc, ok := mc.netConn.(*net.TCPConn); ok {
  66. if err := tc.SetKeepAlive(true); err != nil {
  67. // Don't send COM_QUIT before handshake.
  68. mc.netConn.Close()
  69. mc.netConn = nil
  70. return nil, err
  71. }
  72. }
  73. mc.buf = newBuffer(mc.netConn)
  74. // Set I/O timeouts
  75. mc.buf.timeout = mc.cfg.ReadTimeout
  76. mc.writeTimeout = mc.cfg.WriteTimeout
  77. // Reading Handshake Initialization Packet
  78. cipher, err := mc.readInitPacket()
  79. if err != nil {
  80. mc.cleanup()
  81. return nil, err
  82. }
  83. // Send Client Authentication Packet
  84. if err = mc.writeAuthPacket(cipher); err != nil {
  85. mc.cleanup()
  86. return nil, err
  87. }
  88. // Handle response to auth packet, switch methods if possible
  89. if err = handleAuthResult(mc); err != nil {
  90. // Authentication failed and MySQL has already closed the connection
  91. // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
  92. // Do not send COM_QUIT, just cleanup and return the error.
  93. mc.cleanup()
  94. return nil, err
  95. }
  96. if mc.cfg.MaxAllowedPacket > 0 {
  97. mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
  98. } else {
  99. // Get max allowed packet size
  100. maxap, err := mc.getSystemVar("max_allowed_packet")
  101. if err != nil {
  102. mc.Close()
  103. return nil, err
  104. }
  105. mc.maxAllowedPacket = stringToInt(maxap) - 1
  106. }
  107. if mc.maxAllowedPacket < maxPacketSize {
  108. mc.maxWriteSize = mc.maxAllowedPacket
  109. }
  110. // Handle DSN Params
  111. err = mc.handleParams()
  112. if err != nil {
  113. mc.Close()
  114. return nil, err
  115. }
  116. return mc, nil
  117. }
  118. func handleAuthResult(mc *mysqlConn) error {
  119. // Read Result Packet
  120. cipher, err := mc.readResultOK()
  121. if err == nil {
  122. return nil // auth successful
  123. }
  124. if mc.cfg == nil {
  125. return err // auth failed and retry not possible
  126. }
  127. // Retry auth if configured to do so.
  128. if mc.cfg.AllowOldPasswords && err == ErrOldPassword {
  129. // Retry with old authentication method. Note: there are edge cases
  130. // where this should work but doesn't; this is currently "wontfix":
  131. // https://github.com/go-sql-driver/mysql/issues/184
  132. if err = mc.writeOldAuthPacket(cipher); err != nil {
  133. return err
  134. }
  135. _, err = mc.readResultOK()
  136. } else if mc.cfg.AllowCleartextPasswords && err == ErrCleartextPassword {
  137. // Retry with clear text password for
  138. // http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
  139. // http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
  140. if err = mc.writeClearAuthPacket(); err != nil {
  141. return err
  142. }
  143. _, err = mc.readResultOK()
  144. } else if mc.cfg.AllowNativePasswords && err == ErrNativePassword {
  145. if err = mc.writeNativeAuthPacket(cipher); err != nil {
  146. return err
  147. }
  148. _, err = mc.readResultOK()
  149. }
  150. return err
  151. }
  152. func init() {
  153. sql.Register("mysql", &MySQLDriver{})
  154. }