driver.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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. // Reading Handshake Initialization Packet
  75. cipher, err := mc.readInitPacket()
  76. if err != nil {
  77. mc.cleanup()
  78. return nil, err
  79. }
  80. // Send Client Authentication Packet
  81. if err = mc.writeAuthPacket(cipher); err != nil {
  82. mc.cleanup()
  83. return nil, err
  84. }
  85. // Handle response to auth packet, switch methods if possible
  86. if err = handleAuthResult(mc, cipher); err != nil {
  87. // Authentication failed and MySQL has already closed the connection
  88. // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
  89. // Do not send COM_QUIT, just cleanup and return the error.
  90. mc.cleanup()
  91. return nil, err
  92. }
  93. // Get max allowed packet size
  94. maxap, err := mc.getSystemVar("max_allowed_packet")
  95. if err != nil {
  96. mc.Close()
  97. return nil, err
  98. }
  99. mc.maxPacketAllowed = stringToInt(maxap) - 1
  100. if mc.maxPacketAllowed < maxPacketSize {
  101. mc.maxWriteSize = mc.maxPacketAllowed
  102. }
  103. // Handle DSN Params
  104. err = mc.handleParams()
  105. if err != nil {
  106. mc.Close()
  107. return nil, err
  108. }
  109. return mc, nil
  110. }
  111. func handleAuthResult(mc *mysqlConn, cipher []byte) error {
  112. // Read Result Packet
  113. err := mc.readResultOK()
  114. if err == nil {
  115. return nil // auth successful
  116. }
  117. if mc.cfg == nil {
  118. return err // auth failed and retry not possible
  119. }
  120. // Retry auth if configured to do so.
  121. if mc.cfg.AllowOldPasswords && err == ErrOldPassword {
  122. // Retry with old authentication method. Note: there are edge cases
  123. // where this should work but doesn't; this is currently "wontfix":
  124. // https://github.com/go-sql-driver/mysql/issues/184
  125. if err = mc.writeOldAuthPacket(cipher); err != nil {
  126. return err
  127. }
  128. err = mc.readResultOK()
  129. } else if mc.cfg.AllowCleartextPasswords && err == ErrCleartextPassword {
  130. // Retry with clear text password for
  131. // http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
  132. // http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
  133. if err = mc.writeClearAuthPacket(); err != nil {
  134. return err
  135. }
  136. err = mc.readResultOK()
  137. }
  138. return err
  139. }
  140. func init() {
  141. sql.Register("mysql", &MySQLDriver{})
  142. }