auth.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 PingCAP, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package auth
  14. import (
  15. "bytes"
  16. "crypto/sha1"
  17. "encoding/hex"
  18. "fmt"
  19. "github.com/pingcap/errors"
  20. "github.com/pingcap/parser/format"
  21. "github.com/pingcap/parser/terror"
  22. )
  23. // UserIdentity represents username and hostname.
  24. type UserIdentity struct {
  25. Username string
  26. Hostname string
  27. CurrentUser bool
  28. AuthUsername string // Username matched in privileges system
  29. AuthHostname string // Match in privs system (i.e. could be a wildcard)
  30. }
  31. // Restore implements Node interface.
  32. func (user *UserIdentity) Restore(ctx *format.RestoreCtx) error {
  33. if user.CurrentUser {
  34. ctx.WriteKeyWord("CURRENT_USER")
  35. } else {
  36. ctx.WriteName(user.Username)
  37. ctx.WritePlain("@")
  38. ctx.WriteName(user.Hostname)
  39. }
  40. return nil
  41. }
  42. // String converts UserIdentity to the format user@host.
  43. func (user *UserIdentity) String() string {
  44. // TODO: Escape username and hostname.
  45. if user == nil {
  46. return ""
  47. }
  48. return fmt.Sprintf("%s@%s", user.Username, user.Hostname)
  49. }
  50. // AuthIdentityString returns matched identity in user@host format
  51. func (user *UserIdentity) AuthIdentityString() string {
  52. // TODO: Escape username and hostname.
  53. return fmt.Sprintf("%s@%s", user.AuthUsername, user.AuthHostname)
  54. }
  55. type RoleIdentity struct {
  56. Username string
  57. Hostname string
  58. }
  59. func (role *RoleIdentity) Restore(ctx *format.RestoreCtx) error {
  60. ctx.WriteName(role.Username)
  61. if role.Hostname != "" {
  62. ctx.WritePlain("@")
  63. ctx.WriteName(role.Hostname)
  64. }
  65. return nil
  66. }
  67. // String converts UserIdentity to the format user@host.
  68. func (role *RoleIdentity) String() string {
  69. // TODO: Escape username and hostname.
  70. return fmt.Sprintf("`%s`@`%s`", role.Username, role.Hostname)
  71. }
  72. // CheckScrambledPassword check scrambled password received from client.
  73. // The new authentication is performed in following manner:
  74. // SERVER: public_seed=create_random_string()
  75. // send(public_seed)
  76. // CLIENT: recv(public_seed)
  77. // hash_stage1=sha1("password")
  78. // hash_stage2=sha1(hash_stage1)
  79. // reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
  80. // // this three steps are done in scramble()
  81. // send(reply)
  82. // SERVER: recv(reply)
  83. // hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
  84. // candidate_hash2=sha1(hash_stage1)
  85. // check(candidate_hash2==hash_stage2)
  86. // // this three steps are done in check_scramble()
  87. func CheckScrambledPassword(salt, hpwd, auth []byte) bool {
  88. crypt := sha1.New()
  89. _, err := crypt.Write(salt)
  90. terror.Log(errors.Trace(err))
  91. _, err = crypt.Write(hpwd)
  92. terror.Log(errors.Trace(err))
  93. hash := crypt.Sum(nil)
  94. // token = scrambleHash XOR stage1Hash
  95. if len(auth) != len(hash) {
  96. return false
  97. }
  98. for i := range hash {
  99. hash[i] ^= auth[i]
  100. }
  101. return bytes.Equal(hpwd, Sha1Hash(hash))
  102. }
  103. // Sha1Hash is an util function to calculate sha1 hash.
  104. func Sha1Hash(bs []byte) []byte {
  105. crypt := sha1.New()
  106. _, err := crypt.Write(bs)
  107. terror.Log(errors.Trace(err))
  108. return crypt.Sum(nil)
  109. }
  110. // EncodePassword converts plaintext password to hashed hex string.
  111. func EncodePassword(pwd string) string {
  112. if len(pwd) == 0 {
  113. return ""
  114. }
  115. hash1 := Sha1Hash([]byte(pwd))
  116. hash2 := Sha1Hash(hash1)
  117. return fmt.Sprintf("*%X", hash2)
  118. }
  119. // DecodePassword converts hex string password without prefix '*' to byte array.
  120. func DecodePassword(pwd string) ([]byte, error) {
  121. x, err := hex.DecodeString(pwd[1:])
  122. if err != nil {
  123. return nil, errors.Trace(err)
  124. }
  125. return x, nil
  126. }