bind.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package ldap
  2. import (
  3. "errors"
  4. "fmt"
  5. ber "github.com/go-asn1-ber/asn1-ber"
  6. )
  7. // SimpleBindRequest represents a username/password bind operation
  8. type SimpleBindRequest struct {
  9. // Username is the name of the Directory object that the client wishes to bind as
  10. Username string
  11. // Password is the credentials to bind with
  12. Password string
  13. // Controls are optional controls to send with the bind request
  14. Controls []Control
  15. // AllowEmptyPassword sets whether the client allows binding with an empty password
  16. // (normally used for unauthenticated bind).
  17. AllowEmptyPassword bool
  18. }
  19. // SimpleBindResult contains the response from the server
  20. type SimpleBindResult struct {
  21. Controls []Control
  22. }
  23. // NewSimpleBindRequest returns a bind request
  24. func NewSimpleBindRequest(username string, password string, controls []Control) *SimpleBindRequest {
  25. return &SimpleBindRequest{
  26. Username: username,
  27. Password: password,
  28. Controls: controls,
  29. AllowEmptyPassword: false,
  30. }
  31. }
  32. func (req *SimpleBindRequest) appendTo(envelope *ber.Packet) error {
  33. pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
  34. pkt.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 3, "Version"))
  35. pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.Username, "User Name"))
  36. pkt.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, req.Password, "Password"))
  37. envelope.AppendChild(pkt)
  38. if len(req.Controls) > 0 {
  39. envelope.AppendChild(encodeControls(req.Controls))
  40. }
  41. return nil
  42. }
  43. // SimpleBind performs the simple bind operation defined in the given request
  44. func (l *Conn) SimpleBind(simpleBindRequest *SimpleBindRequest) (*SimpleBindResult, error) {
  45. if simpleBindRequest.Password == "" && !simpleBindRequest.AllowEmptyPassword {
  46. return nil, NewError(ErrorEmptyPassword, errors.New("ldap: empty password not allowed by the client"))
  47. }
  48. msgCtx, err := l.doRequest(simpleBindRequest)
  49. if err != nil {
  50. return nil, err
  51. }
  52. defer l.finishMessage(msgCtx)
  53. packet, err := l.readPacket(msgCtx)
  54. if err != nil {
  55. return nil, err
  56. }
  57. result := &SimpleBindResult{
  58. Controls: make([]Control, 0),
  59. }
  60. if len(packet.Children) == 3 {
  61. for _, child := range packet.Children[2].Children {
  62. decodedChild, decodeErr := DecodeControl(child)
  63. if decodeErr != nil {
  64. return nil, fmt.Errorf("failed to decode child control: %s", decodeErr)
  65. }
  66. result.Controls = append(result.Controls, decodedChild)
  67. }
  68. }
  69. err = GetLDAPError(packet)
  70. return result, err
  71. }
  72. // Bind performs a bind with the given username and password.
  73. //
  74. // It does not allow unauthenticated bind (i.e. empty password). Use the UnauthenticatedBind method
  75. // for that.
  76. func (l *Conn) Bind(username, password string) error {
  77. req := &SimpleBindRequest{
  78. Username: username,
  79. Password: password,
  80. AllowEmptyPassword: false,
  81. }
  82. _, err := l.SimpleBind(req)
  83. return err
  84. }
  85. // UnauthenticatedBind performs an unauthenticated bind.
  86. //
  87. // A username may be provided for trace (e.g. logging) purpose only, but it is normally not
  88. // authenticated or otherwise validated by the LDAP server.
  89. //
  90. // See https://tools.ietf.org/html/rfc4513#section-5.1.2 .
  91. // See https://tools.ietf.org/html/rfc4513#section-6.3.1 .
  92. func (l *Conn) UnauthenticatedBind(username string) error {
  93. req := &SimpleBindRequest{
  94. Username: username,
  95. Password: "",
  96. AllowEmptyPassword: true,
  97. }
  98. _, err := l.SimpleBind(req)
  99. return err
  100. }
  101. var externalBindRequest = requestFunc(func(envelope *ber.Packet) error {
  102. pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
  103. pkt.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 3, "Version"))
  104. pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "User Name"))
  105. saslAuth := ber.Encode(ber.ClassContext, ber.TypeConstructed, 3, "", "authentication")
  106. saslAuth.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "EXTERNAL", "SASL Mech"))
  107. saslAuth.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "SASL Cred"))
  108. pkt.AppendChild(saslAuth)
  109. envelope.AppendChild(pkt)
  110. return nil
  111. })
  112. // ExternalBind performs SASL/EXTERNAL authentication.
  113. //
  114. // Use ldap.DialURL("ldapi://") to connect to the Unix socket before ExternalBind.
  115. //
  116. // See https://tools.ietf.org/html/rfc4422#appendix-A
  117. func (l *Conn) ExternalBind() error {
  118. msgCtx, err := l.doRequest(externalBindRequest)
  119. if err != nil {
  120. return err
  121. }
  122. defer l.finishMessage(msgCtx)
  123. packet, err := l.readPacket(msgCtx)
  124. if err != nil {
  125. return err
  126. }
  127. return GetLDAPError(packet)
  128. }