Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a server-side function to support sasl binding step by step #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 113 additions & 1 deletion v3/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
enchex "encoding/hex"
"errors"
"fmt"
"github.com/Azure/go-ntlmssp"
"io/ioutil"
"math/rand"
"strings"

"github.com/Azure/go-ntlmssp"
ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -733,3 +733,115 @@ RESP:

return nil, GetLDAPError(packet)
}

// ServerBindStep sends the SASLBindRequest and return the result code, servercred and errors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a short example on how to use this function to our tests? Thank you!

// If the result code is not SUCCESS(0x00) or IN_PROGRESS(0x0e), it's also considered as an error.
func (l *Conn) ServerBindStep(clientCred []byte, dn string, methodName string, controls []Control) (resultCode uint16, serverCred []byte, err error) {
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this (until line 770) is a duplication of what doRequest does. The function signature also mismatches from the style how other functions like SimpleBind are structure. They take in a struct (which also satisfies the request interface). Additionally, there are simple wrappers for those functions like conn.Bind for simple use.

In my opinion we should stick to one style here and not mix them up. Could you please adapt your two functions please?

packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))

request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
request.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 3, "Version"))
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, dn, "User Name"))

auth := ber.Encode(ber.ClassContext, ber.TypeConstructed, 3, "", "authentication")
auth.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, methodName, "SASL Mech"))
auth.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, string(clientCred), "Credentials"))
request.AppendChild(auth)
packet.AppendChild(request)

if len(controls) > 0 {
packet.AppendChild(encodeControls(controls))
}

msgCtx, err := l.sendMessage(packet)
if err != nil {
return 0, nil, err
}
defer l.finishMessage(msgCtx)

packetResponse, ok := <-msgCtx.responses
if !ok {
return 0, nil, errors.New("ldap: response channel closed")
}
packet, err = packetResponse.ReadPacket()
if err != nil {
return 0, nil, err
}

bindResponse, err := parseBindResponse(packet)
if err != nil {
return 0, nil, err
}

// TODO: add global constants. As this package used `uint16` to represent the result code nearly everywhere,
// these constants were
const LDAPResultCodeSuccess = 0
const LDAPResultCodeSASLBindInProgress = 0x0e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both values are already defined in error.go, thought they're of type int unfortunately. I think it can stay like this and we take care of this later on 😄

if bindResponse.resultCode != LDAPResultCodeSuccess && bindResponse.resultCode != LDAPResultCodeSASLBindInProgress {
return bindResponse.resultCode, nil, &Error{
ResultCode: bindResponse.resultCode,
MatchedDN: bindResponse.matchedDN,
Err: fmt.Errorf("%s", bindResponse.errorMessage),
Packet: packet,
}
}
return bindResponse.resultCode, bindResponse.serverSaslCreds, nil
}

type bindResponse struct {
resultCode uint16
matchedDN string
errorMessage string
serverSaslCreds []byte
}

// parseBindResponse parses the bind response. The format of a BindResponse is like below:
//
// ```
//
// BindResponse ::= [APPLICATION 1] SEQUENCE {
// COMPONENTS OF LDAPResult,
// serverSaslCreds [7] OCTET STRING OPTIONAL }
//
// LDAPResult ::= SEQUENCE {
// resultCode ENUMERATED,
// matchedDN LDAPDN,
// errorMessage LDAPString,
// referral [3] Referral OPTIONAL }
//
// ```
//
// TODO: support `referral` field in this function
func parseBindResponse(packet *ber.Packet) (*bindResponse, error) {
if packet == nil {
return nil, &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty packet")}
}
if len(packet.Children) < 2 {
return nil, &Error{ResultCode: ErrorNetwork, Err: fmt.Errorf("Invalid packet format"), Packet: packet}
}

response := packet.Children[1]
if response == nil {
return nil, &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty response in packet"), Packet: packet}
}

if !(response.ClassType == ber.ClassApplication && response.TagType == ber.TypeConstructed && len(response.Children) >= 3) {
return nil, &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty response in packet"), Packet: packet}
}
resp := &bindResponse{
uint16(response.Children[0].Value.(int64)),
response.Children[1].Value.(string),
response.Children[2].Value.(string),
nil,
}
if len(response.Children) < 4 {
return resp, nil
}
// then the response.Children[3] can be an referral or serverSaslCreds. It can be asserted with tag
// TODO: also add referral
if response.Children[3].ClassType == ber.ClassContext && response.Children[3].Tag == ber.TagObjectDescriptor {
resp.serverSaslCreds = response.Children[3].Data.Bytes()
}
return resp, nil
}