2020-08-09 15:29:54 -07:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
ber "github.com/go-asn1-ber/asn1-ber"
|
2024-05-24 14:08:09 -07:00
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
2020-08-09 15:29:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errRespChanClosed = errors.New("ldap: response channel closed")
|
|
|
|
errCouldNotRetMsg = errors.New("ldap: could not retrieve message")
|
|
|
|
)
|
|
|
|
|
|
|
|
type request interface {
|
|
|
|
appendTo(*ber.Packet) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestFunc func(*ber.Packet) error
|
|
|
|
|
|
|
|
func (f requestFunc) appendTo(p *ber.Packet) error {
|
|
|
|
return f(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Conn) doRequest(req request) (*messageContext, error) {
|
|
|
|
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
|
|
|
|
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
|
|
|
|
if err := req.appendTo(packet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-24 14:08:09 -07:00
|
|
|
l.Debug.Log("Sending package", PacketToField(packet))
|
2020-08-09 15:29:54 -07:00
|
|
|
|
|
|
|
msgCtx, err := l.sendMessage(packet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-24 14:08:09 -07:00
|
|
|
|
|
|
|
l.Debug.Log("Send package", mlog.Int("id", msgCtx.id))
|
2020-08-09 15:29:54 -07:00
|
|
|
return msgCtx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Conn) readPacket(msgCtx *messageContext) (*ber.Packet, error) {
|
2024-05-24 14:08:09 -07:00
|
|
|
l.Debug.Log("Waiting for response", mlog.Int("id", msgCtx.id))
|
2020-08-09 15:29:54 -07:00
|
|
|
packetResponse, ok := <-msgCtx.responses
|
|
|
|
if !ok {
|
|
|
|
return nil, NewError(ErrorNetwork, errRespChanClosed)
|
|
|
|
}
|
|
|
|
packet, err := packetResponse.ReadPacket()
|
2024-05-24 14:08:09 -07:00
|
|
|
if l.Debug.Enabled() {
|
|
|
|
if err := addLDAPDescriptions(packet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l.Debug.Log("Got response", mlog.Int("id", msgCtx.id), PacketToField(packet), mlog.Err(err))
|
|
|
|
}
|
|
|
|
|
2020-08-09 15:29:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet == nil {
|
|
|
|
return nil, NewError(ErrorNetwork, errCouldNotRetMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return packet, nil
|
|
|
|
}
|