utils.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 Julien Schmidt. All rights reserved.
  4. // http://www.julienschmidt.com
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public
  7. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  8. // You can obtain one at http://mozilla.org/MPL/2.0/.
  9. package mysql
  10. import (
  11. "crypto/sha1"
  12. "io"
  13. "log"
  14. "os"
  15. "regexp"
  16. "strings"
  17. )
  18. // Logger
  19. var (
  20. errLog *log.Logger
  21. )
  22. func init() {
  23. errLog = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
  24. dsnPattern = regexp.MustCompile(
  25. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  26. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  27. `\/(?P<dbname>.*?)` + // /dbname
  28. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  29. }
  30. // Data Source Name Parser
  31. var dsnPattern *regexp.Regexp
  32. func parseDSN(dsn string) *config {
  33. cfg := new(config)
  34. cfg.params = make(map[string]string)
  35. matches := dsnPattern.FindStringSubmatch(dsn)
  36. names := dsnPattern.SubexpNames()
  37. for i, match := range matches {
  38. switch names[i] {
  39. case "user":
  40. cfg.user = match
  41. case "passwd":
  42. cfg.passwd = match
  43. case "net":
  44. cfg.net = match
  45. case "addr":
  46. cfg.addr = match
  47. case "dbname":
  48. cfg.dbname = match
  49. case "params":
  50. for _, v := range strings.Split(match, "&") {
  51. param := strings.SplitN(v, "=", 2)
  52. if len(param) != 2 {
  53. continue
  54. }
  55. cfg.params[param[0]] = param[1]
  56. }
  57. }
  58. }
  59. // Set default network if empty
  60. if cfg.net == "" {
  61. cfg.net = "tcp"
  62. }
  63. // Set default adress if empty
  64. if cfg.addr == "" {
  65. cfg.addr = "127.0.0.1:3306"
  66. }
  67. return cfg
  68. }
  69. // Encrypt password using 4.1+ method
  70. // http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
  71. func scramblePassword(scramble, password []byte) []byte {
  72. if len(password) == 0 {
  73. return nil
  74. }
  75. // stage1Hash = SHA1(password)
  76. crypt := sha1.New()
  77. crypt.Write(password)
  78. stage1Hash := crypt.Sum(nil)
  79. // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
  80. // inner Hash
  81. crypt.Reset()
  82. crypt.Write(stage1Hash)
  83. scrambleHash := crypt.Sum(nil)
  84. // outer Hash
  85. crypt.Reset()
  86. crypt.Write(scramble)
  87. crypt.Write(scrambleHash)
  88. scrambleHash = crypt.Sum(nil)
  89. // token = scrambleHash XOR stage1Hash
  90. result := make([]byte, 20)
  91. for i := range result {
  92. result[i] = scrambleHash[i] ^ stage1Hash[i]
  93. }
  94. return result[0:]
  95. }
  96. /******************************************************************************
  97. * Convert from and to bytes *
  98. ******************************************************************************/
  99. func uint24ToBytes(n uint32) []byte {
  100. return []byte{
  101. byte(n),
  102. byte(n >> 8),
  103. byte(n >> 16),
  104. }
  105. }
  106. func uint32ToBytes(n uint32) []byte {
  107. return []byte{
  108. byte(n),
  109. byte(n >> 8),
  110. byte(n >> 16),
  111. byte(n >> 24),
  112. }
  113. }
  114. func uint64ToBytes(n uint64) []byte {
  115. return []byte{
  116. byte(n),
  117. byte(n >> 8),
  118. byte(n >> 16),
  119. byte(n >> 24),
  120. byte(n >> 32),
  121. byte(n >> 40),
  122. byte(n >> 48),
  123. byte(n >> 56),
  124. }
  125. }
  126. func readLengthEnodedString(b []byte) ([]byte, int, error) {
  127. // Get length
  128. num, _, n, err := readLengthEncodedInteger(b)
  129. if err != nil || num < 1 {
  130. return nil, n, err
  131. }
  132. n += int(num)
  133. // Check data length
  134. if len(b) >= n {
  135. return b[n-int(num) : n], n, err
  136. }
  137. return nil, n, io.EOF
  138. }
  139. func readAndDropLengthEnodedString(b []byte) (n int, err error) {
  140. // Get length
  141. num, _, n, err := readLengthEncodedInteger(b)
  142. if err != nil || num < 1 {
  143. return
  144. }
  145. n += int(num)
  146. // Check data length
  147. if len(b) >= n {
  148. return
  149. }
  150. return n, io.EOF
  151. }
  152. func readLengthEncodedInteger(b []byte) (num uint64, isNull bool, n int, err error) {
  153. switch (b)[0] {
  154. // 251: NULL
  155. case 0xfb:
  156. n = 1
  157. isNull = true
  158. return
  159. // 252: value of following 2
  160. case 0xfc:
  161. n = 3
  162. // 253: value of following 3
  163. case 0xfd:
  164. n = 4
  165. // 254: value of following 8
  166. case 0xfe:
  167. n = 9
  168. // 0-250: value of first byte
  169. default:
  170. num = uint64(b[0])
  171. n = 1
  172. return
  173. }
  174. switch n - 1 {
  175. case 2:
  176. num = uint64(b[0]) | uint64(b[1])<<8
  177. return
  178. case 3:
  179. num = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16
  180. return
  181. default:
  182. num = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 |
  183. uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 |
  184. uint64(b[6])<<48 | uint64(b[7])<<54
  185. }
  186. return
  187. }
  188. func lengthEncodedIntegerToBytes(n uint64) []byte {
  189. switch {
  190. case n <= 250:
  191. return []byte{byte(n)}
  192. case n <= 0xffff:
  193. return []byte{0xfc, byte(n), byte(n >> 8)}
  194. case n <= 0xffffff:
  195. return []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
  196. }
  197. return nil
  198. }