Skip to content
Open
Show file tree
Hide file tree
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
83 changes: 41 additions & 42 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package ldap
import (
"errors"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

func (l *Conn) Bind(username, password string) error {
Expand Down Expand Up @@ -55,45 +55,44 @@ func (l *Conn) Bind(username, password string) error {
}

func (l *Conn) Unbind() error {
defer l.Close()

messageID := l.nextMessageID()

packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID"))
unbindRequest := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationUnbindRequest, nil, "Unbind Request")
packet.AppendChild(unbindRequest)

if l.Debug {
ber.PrintPacket(packet)
}

channel, err := l.sendMessage(packet)
if err != nil {
return err
}
if channel == nil {
return NewError(ErrorNetwork, errors.New("ldap: could not send message"))
}
defer l.finishMessage(messageID)

packet = <-channel
if packet == nil {
return NewError(ErrorNetwork, errors.New("ldap: could not retrieve response"))
}

if l.Debug {
if err := addLDAPDescriptions(packet); err != nil {
return err
}
ber.PrintPacket(packet)
}

resultCode, resultDescription := getLDAPResultCode(packet)
if resultCode != 0 {
return NewError(resultCode, errors.New(resultDescription))
}

return nil
}
defer l.Close()

messageID := l.nextMessageID()

packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID"))
unbindRequest := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationUnbindRequest, nil, "Unbind Request")
packet.AppendChild(unbindRequest)

if l.Debug {
ber.PrintPacket(packet)
}

channel, err := l.sendMessage(packet)
if err != nil {
return err
}
if channel == nil {
return NewError(ErrorNetwork, errors.New("ldap: could not send message"))
}
defer l.finishMessage(messageID)

packet = <-channel
if packet == nil {
return NewError(ErrorNetwork, errors.New("ldap: could not retrieve response"))
}

if l.Debug {
if err := addLDAPDescriptions(packet); err != nil {
return err
}
ber.PrintPacket(packet)
}

resultCode, resultDescription := getLDAPResultCode(packet)
if resultCode != 0 {
return NewError(resultCode, errors.New(resultDescription))
}

return nil
}
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"sync"
"time"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

const (
Expand Down Expand Up @@ -296,7 +296,7 @@ func (l *Conn) reader() {
addLDAPDescriptions(packet)
message := &messagePacket{
Op: MessageResponse,
MessageID: packet.Children[0].Value.(uint64),
MessageID: uint64(packet.Children[0].Value.(int64)), //figure out if its really unsigned
Packet: packet,
}
if !l.sendProcessMessage(message) {
Expand Down
7 changes: 4 additions & 3 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package ldap

import (
"strings"
"fmt"
"github.com/nmcclain/asn1-ber"
"strings"

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

const (
Expand Down Expand Up @@ -129,7 +130,7 @@ func DecodeControl(packet *ber.Packet) Control {
value.Description = "Search Control Value"
value.Children[0].Description = "Paging Size"
value.Children[1].Description = "Cookie"
c.PagingSize = uint32(value.Children[0].Value.(uint64))
c.PagingSize = uint32(value.Children[0].Value.(int64))
c.Cookie = value.Children[1].Data.Bytes()
value.Children[1].Value = c.Cookie
return c
Expand Down
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ldap
import (
"log"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

// debbuging type
Expand Down
4 changes: 2 additions & 2 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"unicode/utf8"

ber "github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

const (
Expand All @@ -26,7 +26,7 @@ const (
FilterExtensibleMatch = 9
)

var FilterMap = map[uint8]string{
var FilterMap = map[ber.Tag]string{
FilterAnd: "And",
FilterOr: "Or",
FilterNot: "Not",
Expand Down
6 changes: 3 additions & 3 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"reflect"
"testing"

ber "github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

type compileTest struct {
filterStr string
filterType uint8
filterType ber.Tag
}

var testFilters = []compileTest{
Expand All @@ -33,7 +33,7 @@ func TestFilter(t *testing.T) {
filter, err := CompileFilter(i.filterStr)
if err != nil {
t.Errorf("Problem compiling %s - %s", i.filterStr, err.Error())
} else if filter.Tag != uint8(i.filterType) {
} else if filter.Tag != i.filterType {
t.Errorf("%q Expected %q got %q", i.filterStr, FilterMap[i.filterType], FilterMap[filter.Tag])
} else {
o, err := DecompileFilter(filter)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/nmcclain/ldap

go 1.14

require github.com/nmcclain/asn1-ber v0.0.0-20170104154839-2661553a0484
require github.com/go-asn1-ber/asn1-ber v1.5.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/nmcclain/asn1-ber v0.0.0-20170104154839-2661553a0484 h1:D9EvfGQvlkKaDr2CRKN++7HbSXbefUNDrPq60T+g24s=
github.com/nmcclain/asn1-ber v0.0.0-20170104154839-2661553a0484/go.mod h1:O1EljZ+oHprtxDDPHiMWVo/5dBT6PlvWX5PSwj80aBA=
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
9 changes: 5 additions & 4 deletions ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

// LDAP Application Codes
Expand All @@ -36,7 +37,7 @@ const (
ApplicationExtendedResponse = 24
)

var ApplicationMap = map[uint8]string{
var ApplicationMap = map[ber.Tag]string{
ApplicationBindRequest: "Bind Request",
ApplicationBindResponse: "Bind Response",
ApplicationUnbindRequest: "Unbind Request",
Expand Down Expand Up @@ -307,7 +308,7 @@ func DebugBinaryFile(fileName string) error {
if err != nil {
return NewError(ErrorDebugging, err)
}
ber.PrintBytes(file, "")
ber.PrintBytes(os.Stdout, file, "")
packet := ber.DecodePacket(file)
addLDAPDescriptions(packet)
ber.PrintPacket(packet)
Expand All @@ -332,7 +333,7 @@ func getLDAPResultCode(packet *ber.Packet) (code LDAPResultCode, description str
if len(packet.Children) >= 2 {
response := packet.Children[1]
if response.ClassType == ber.ClassApplication && response.TagType == ber.TypeConstructed && len(response.Children) == 3 {
return LDAPResultCode(response.Children[0].Value.(uint64)), response.Children[2].Value.(string)
return LDAPResultCode(response.Children[0].Value.(int64)), response.Children[2].Value.(string)
}
}

Expand Down
2 changes: 1 addition & 1 deletion modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"errors"
"log"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import (
"fmt"
"strings"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

const (
Expand Down
30 changes: 15 additions & 15 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"sync"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

type Binder interface {
Expand Down Expand Up @@ -157,11 +157,7 @@ func (server *Server) ListenAndServeTLS(listenString string, certFile string, ke
if err != nil {
return err
}
err = server.Serve(ln)
if err != nil {
return err
}
return nil
return server.Serve(ln)
}

func (server *Server) SetStats(enable bool) {
Expand All @@ -185,11 +181,7 @@ func (server *Server) ListenAndServe(listenString string) error {
if err != nil {
return err
}
err = server.Serve(ln)
if err != nil {
return err
}
return nil
return server.Serve(ln)
}

func (server *Server) Serve(ln net.Listener) error {
Expand All @@ -215,12 +207,19 @@ listener:
go server.handleConnection(c)
case <-server.Quit:
ln.Close()
close(server.Quit)
break listener
}
}
return nil
}

//Close closes the underlying net.Listener, and waits for confirmation
func (server *Server) Close() {
server.Quit <- true
<-server.Quit
}

//
func (server *Server) handleConnection(conn net.Conn) {
boundDN := "" // "" == anonymous
Expand All @@ -229,7 +228,7 @@ handler:
for {
// read incoming LDAP packet
packet, err := ber.ReadPacket(conn)
if err == io.EOF { // Client closed connection
if err == io.EOF || err == io.ErrUnexpectedEOF { // Client closed connection
break
} else if err != nil {
log.Printf("handleConnection ber.ReadPacket ERROR: %s", err.Error())
Expand All @@ -242,11 +241,12 @@ handler:
break
}
// check the message ID and ClassType
messageID, ok := packet.Children[0].Value.(uint64)
messageID64, ok := packet.Children[0].Value.(int64)
if !ok {
log.Print("malformed messageID")
break
}
messageID := uint64(messageID64)
req := packet.Children[1]
if req.ClassType != ber.ClassApplication {
log.Print("req.ClassType != ber.ClassApplication")
Expand Down Expand Up @@ -380,7 +380,7 @@ func routeFunc(dn string, funcNames []string) string {
dnMatch := "," + strings.ToLower(dn)
var weight int
for _, fn := range funcNames {
if strings.HasSuffix(dnMatch, "," + fn) {
if strings.HasSuffix(dnMatch, ","+fn) {
// empty string as 0, no-comma string 1 , etc
if fn == "" {
weight = 0
Expand All @@ -400,7 +400,7 @@ func routeFunc(dn string, funcNames []string) string {
func encodeLDAPResponse(messageID uint64, responseType uint8, ldapResultCode LDAPResultCode, message string) *ber.Packet {
responsePacket := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Response")
responsePacket.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "Message ID"))
reponse := ber.Encode(ber.ClassApplication, ber.TypeConstructed, responseType, nil, ApplicationMap[responseType])
reponse := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ber.Tag(responseType), nil, ApplicationMap[ber.Tag(responseType)])
reponse.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(ldapResultCode), "resultCode: "))
reponse.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "", "matchedDN: "))
reponse.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, message, "errorMessage: "))
Expand Down
5 changes: 3 additions & 2 deletions server_bind.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ldap

import (
"github.com/nmcclain/asn1-ber"
"log"
"net"

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

func HandleBindRequest(req *ber.Packet, fns map[string]Binder, conn net.Conn) (resultCode LDAPResultCode) {
Expand All @@ -14,7 +15,7 @@ func HandleBindRequest(req *ber.Packet, fns map[string]Binder, conn net.Conn) (r
}()

// we only support ldapv3
ldapVersion, ok := req.Children[0].Value.(uint64)
ldapVersion, ok := req.Children[0].Value.(int64)
if !ok {
return LDAPResultProtocolError
}
Expand Down
4 changes: 2 additions & 2 deletions server_modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"net"

"github.com/nmcclain/asn1-ber"
ber "github.com/go-asn1-ber/asn1-ber"
)

func HandleAddRequest(req *ber.Packet, boundDN string, fns map[string]Adder, conn net.Conn) (resultCode LDAPResultCode) {
Expand Down Expand Up @@ -96,7 +96,7 @@ func HandleModifyRequest(req *ber.Packet, boundDN string, fns map[string]Modifie
}
attr.AttrVals = append(attr.AttrVals, v)
}
op, ok := change.Children[0].Value.(uint64)
op, ok := change.Children[0].Value.(int64)
if !ok {
return LDAPResultProtocolError
}
Expand Down
Loading