driver.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. maxPacketAllowed: 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, cipher); 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. // Get max allowed packet size
  97. maxap, err := mc.getSystemVar("max_allowed_packet")
  98. if err != nil {
  99. mc.Close()
  100. return nil, err
  101. }
  102. mc.maxPacketAllowed = stringToInt(maxap) - 1
  103. if mc.maxPacketAllowed < maxPacketSize {
  104. mc.maxWriteSize = mc.maxPacketAllowed
  105. }
  106. // Handle DSN Params
  107. err = mc.handleParams()
  108. if err != nil {
  109. mc.Close()
  110. return nil, err
  111. }
  112. return mc, nil
  113. }
  114. func handleAuthResult(mc *mysqlConn, cipher []byte) error {
  115. // Read Result Packet
  116. err := mc.readResultOK()
  117. if err == nil {
  118. return nil // auth successful
  119. }
  120. if mc.cfg == nil {
  121. return err // auth failed and retry not possible
  122. }
  123. // Retry auth if configured to do so.
  124. if mc.cfg.AllowOldPasswords && err == ErrOldPassword {
  125. // Retry with old authentication method. Note: there are edge cases
  126. // where this should work but doesn't; this is currently "wontfix":
  127. // https://github.com/go-sql-driver/mysql/issues/184
  128. if err = mc.writeOldAuthPacket(cipher); err != nil {
  129. return err
  130. }
  131. err = mc.readResultOK()
  132. } else if mc.cfg.AllowCleartextPasswords && err == ErrCleartextPassword {
  133. // Retry with clear text password for
  134. // http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
  135. // http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
  136. if err = mc.writeClearAuthPacket(); err != nil {
  137. return err
  138. }
  139. err = mc.readResultOK()
  140. }
  141. return err
  142. }
  143. func init() {
  144. sql.Register("mysql", &MySQLDriver{})
  145. }