Skip to content

Commit 6898f14

Browse files
FlipBFilip Björck
authored andcommitted
Add support for SSPI GSSAPI SASL mechanism bind (go-ldap#402)
* Add support for SSPI GSSAPI SASL mechanism bind This change allows Windows clients to use current process' credentials for bind authentication. Co-authored-by: Filip Björck <filipbj@axis.com>
1 parent 9c4dfbe commit 6898f14

10 files changed

+884
-34
lines changed

bind.go

+176
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,179 @@ func (l *Conn) NTLMChallengeBind(ntlmBindRequest *NTLMBindRequest) (*NTLMBindRes
558558
err = GetLDAPError(packet)
559559
return result, err
560560
}
561+
562+
// GSSAPIClient interface is used as the client-side implementation for the
563+
// GSSAPI SASL mechanism.
564+
// Interface inspired by GSSAPIClient from golang.org/x/crypto/ssh
565+
type GSSAPIClient interface {
566+
// InitSecContext initiates the establishment of a security context for
567+
// GSS-API between the client and server.
568+
// Initially the token parameter should be specified as nil.
569+
// The routine may return a outputToken which should be transferred to
570+
// the server, where the server will present it to AcceptSecContext.
571+
// If no token need be sent, InitSecContext will indicate this by setting
572+
// needContinue to false. To complete the context
573+
// establishment, one or more reply tokens may be required from the server;
574+
// if so, InitSecContext will return a needContinue which is true.
575+
// In this case, InitSecContext should be called again when the
576+
// reply token is received from the server, passing the reply token
577+
// to InitSecContext via the token parameters.
578+
// See RFC 4752 section 3.1.
579+
InitSecContext(target string, token []byte) (outputToken []byte, needContinue bool, err error)
580+
// NegotiateSaslAuth performs the last step of the Sasl handshake.
581+
// It takes a token, which, when unwrapped, describes the servers supported
582+
// security layers (first octet) and maximum receive buffer (remaining
583+
// three octets).
584+
// If the received token is unacceptable an error must be returned to abort
585+
// the handshake.
586+
// Outputs a signed token describing the client's selected security layer
587+
// and receive buffer size and optionally an authorization identity.
588+
// The returned token will be sent to the server and the handshake considered
589+
// completed successfully and the server authenticated.
590+
// See RFC 4752 section 3.1.
591+
NegotiateSaslAuth(token []byte, authzid string) ([]byte, error)
592+
// DeleteSecContext destroys any established secure context.
593+
DeleteSecContext() error
594+
}
595+
596+
// GSSAPIBindRequest represents a GSSAPI SASL mechanism bind request.
597+
// See rfc4752 and rfc4513 section 5.2.1.2.
598+
type GSSAPIBindRequest struct {
599+
// Service Principal Name user for the service ticket. Eg. "ldap/<host>"
600+
ServicePrincipalName string
601+
// (Optional) Authorization entity
602+
AuthZID string
603+
// (Optional) Controls to send with the bind request
604+
Controls []Control
605+
}
606+
607+
// GSSAPIBind performs the GSSAPI SASL bind using the provided GSSAPI client.
608+
func (l *Conn) GSSAPIBind(client GSSAPIClient, servicePrincipal, authzid string) error {
609+
return l.GSSAPIBindRequest(client, &GSSAPIBindRequest{
610+
ServicePrincipalName: servicePrincipal,
611+
AuthZID: authzid,
612+
})
613+
}
614+
615+
// GSSAPIBindRequest performs the GSSAPI SASL bind using the provided GSSAPI client.
616+
func (l *Conn) GSSAPIBindRequest(client GSSAPIClient, req *GSSAPIBindRequest) error {
617+
// nolint:errcheck
618+
defer client.DeleteSecContext()
619+
620+
var err error
621+
var reqToken []byte
622+
var recvToken []byte
623+
needInit := true
624+
for {
625+
if needInit {
626+
// Establish secure context between client and server.
627+
reqToken, needInit, err = client.InitSecContext(req.ServicePrincipalName, recvToken)
628+
if err != nil {
629+
return err
630+
}
631+
} else {
632+
// Secure context is set up, perform the last step of SASL handshake.
633+
reqToken, err = client.NegotiateSaslAuth(recvToken, req.AuthZID)
634+
if err != nil {
635+
return err
636+
}
637+
}
638+
// Send Bind request containing the current token and extract the
639+
// token sent by server.
640+
recvToken, err = l.saslBindTokenExchange(req.Controls, reqToken)
641+
if err != nil {
642+
return err
643+
}
644+
645+
if !needInit && len(recvToken) == 0 {
646+
break
647+
}
648+
}
649+
650+
return nil
651+
}
652+
653+
func (l *Conn) saslBindTokenExchange(reqControls []Control, reqToken []byte) ([]byte, error) {
654+
655+
// Construct LDAP Bind request with GSSAPI SASL mechanism.
656+
envelope := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
657+
envelope.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
658+
659+
request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
660+
request.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 3, "Version"))
661+
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "User Name"))
662+
663+
auth := ber.Encode(ber.ClassContext, ber.TypeConstructed, 3, "", "authentication")
664+
auth.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "GSSAPI", "SASL Mech"))
665+
if len(reqToken) > 0 {
666+
auth.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, string(reqToken), "Credentials"))
667+
}
668+
request.AppendChild(auth)
669+
envelope.AppendChild(request)
670+
if len(reqControls) > 0 {
671+
envelope.AppendChild(encodeControls(reqControls))
672+
}
673+
674+
msgCtx, err := l.sendMessage(envelope)
675+
if err != nil {
676+
return nil, err
677+
}
678+
defer l.finishMessage(msgCtx)
679+
680+
packet, err := l.readPacket(msgCtx)
681+
if err != nil {
682+
return nil, err
683+
}
684+
l.Debug.Printf("%d: got response %p", msgCtx.id, packet)
685+
if l.Debug {
686+
if err = addLDAPDescriptions(packet); err != nil {
687+
return nil, err
688+
}
689+
ber.PrintPacket(packet)
690+
}
691+
692+
// https://www.rfc-editor.org/rfc/rfc4511#section-4.1.1
693+
// packet is an envelope
694+
// child 0 is message id
695+
// child 1 is protocolOp
696+
if len(packet.Children) != 2 {
697+
return nil, fmt.Errorf("bad bind response")
698+
}
699+
700+
protocolOp := packet.Children[1]
701+
RESP:
702+
switch protocolOp.Description {
703+
case "Bind Response": // Bind Response
704+
// Bind Reponse is an LDAP Response (https://www.rfc-editor.org/rfc/rfc4511#section-4.1.9)
705+
// with an additional optional serverSaslCreds string (https://www.rfc-editor.org/rfc/rfc4511#section-4.2.2)
706+
// child 0 is resultCode
707+
resultCode := protocolOp.Children[0]
708+
if resultCode.Tag != ber.TagEnumerated {
709+
break RESP
710+
}
711+
switch resultCode.Value.(int64) {
712+
case 14: // Sasl bind in progress
713+
if len(protocolOp.Children) < 3 {
714+
break RESP
715+
}
716+
referral := protocolOp.Children[3]
717+
switch referral.Description {
718+
case "Referral":
719+
if referral.ClassType != ber.ClassContext || referral.Tag != ber.TagObjectDescriptor {
720+
break RESP
721+
}
722+
return ioutil.ReadAll(referral.Data)
723+
}
724+
// Optional:
725+
//if len(protocolOp.Children) == 4 {
726+
// serverSaslCreds := protocolOp.Children[4]
727+
//}
728+
case 0: // Success - Bind OK.
729+
// SASL layer in effect (if any) (See https://www.rfc-editor.org/rfc/rfc4513#section-5.2.1.4)
730+
// NOTE: SASL security layers are not supported currently.
731+
return nil, nil
732+
}
733+
}
734+
735+
return nil, GetLDAPError(packet)
736+
}

examples_windows_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package ldap
5+
6+
import (
7+
"log"
8+
9+
"github.com/go-ldap/ldap/gssapi"
10+
)
11+
12+
// This example demonstrates passwordless bind using the current process' user
13+
// credentials on Windows (SASL GSSAPI mechanism bind with SSPI client).
14+
func ExampleConn_SSPIClient_GSSAPIBind() {
15+
16+
// Windows only: Create a GSSAPIClient using Windows built-in SSPI lib
17+
// (secur32.dll).
18+
// This will use the credentials of the current process' user.
19+
sspiClient, err := gssapi.NewSSPIClient()
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
defer sspiClient.Close()
24+
25+
l, err := DialURL("ldap://ldap.example.com:389")
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
defer l.Close()
30+
31+
// Bind using supplied GSSAPIClient implementation
32+
err = l.GSSAPIBind(sspiClient, "ldap/ldap.example.com", "")
33+
if err != nil {
34+
log.Fatal(err)
35+
}
36+
}

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ go 1.14
44

55
require (
66
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e
7+
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74
78
github.com/go-asn1-ber/asn1-ber v1.5.4
8-
github.com/stretchr/testify v1.7.2 // indirect
9-
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
9+
github.com/stretchr/testify v1.8.0
10+
golang.org/x/crypto v0.1.0 // indirect
1011
)

go.sum

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
1-
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28=
2-
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
3-
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e h1:ZU22z/2YRFLyf/P4ZwUYSdNCWsMEI0VeyrFoI2rAhJQ=
4-
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
51
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e h1:NeAW1fUYUEWhft7pkxDf6WoUvEZJ/uOKsvtpjLnn8MU=
62
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
7-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
3+
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3UuJRqlA3JxYxBZEqCeOmATOvrbT4p9RA=
4+
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
85
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
98
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
109
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
1110
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1211
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
1412
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
15-
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
16-
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
17-
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o=
18-
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
19-
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
20-
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
21-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
13+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
14+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
15+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
16+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
17+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
18+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
19+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
20+
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
21+
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
22+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
23+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
24+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
25+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
26+
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
27+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
28+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
29+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2230
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2431
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2535
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
26-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
36+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
37+
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
38+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
39+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
40+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
41+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
2742
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
43+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
44+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
45+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
46+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2847
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
48+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2949
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3050
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)