Skip to content

Commit

Permalink
make serve function public (#4)
Browse files Browse the repository at this point in the history
* make serve function public

* make Dn public

* make attrs public
  • Loading branch information
dirkm authored and nmcclain committed Jul 3, 2019
1 parent 6e14e82 commit ee4388c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
32 changes: 16 additions & 16 deletions modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,57 +49,57 @@ var LDAPModifyAttributeMap = map[uint64]string{
}

type PartialAttribute struct {
attrType string
attrVals []string
AttrType string
AttrVals []string
}

func (p *PartialAttribute) encode() *ber.Packet {
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "PartialAttribute")
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, p.attrType, "Type"))
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, p.AttrType, "Type"))
set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
for _, value := range p.attrVals {
for _, value := range p.AttrVals {
set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
}
seq.AppendChild(set)
return seq
}

type ModifyRequest struct {
dn string
addAttributes []PartialAttribute
deleteAttributes []PartialAttribute
replaceAttributes []PartialAttribute
Dn string
AddAttributes []PartialAttribute
DeleteAttributes []PartialAttribute
ReplaceAttributes []PartialAttribute
}

func (m *ModifyRequest) Add(attrType string, attrVals []string) {
m.addAttributes = append(m.addAttributes, PartialAttribute{attrType: attrType, attrVals: attrVals})
m.AddAttributes = append(m.AddAttributes, PartialAttribute{AttrType: attrType, AttrVals: attrVals})
}

func (m *ModifyRequest) Delete(attrType string, attrVals []string) {
m.deleteAttributes = append(m.deleteAttributes, PartialAttribute{attrType: attrType, attrVals: attrVals})
m.DeleteAttributes = append(m.DeleteAttributes, PartialAttribute{AttrType: attrType, AttrVals: attrVals})
}

func (m *ModifyRequest) Replace(attrType string, attrVals []string) {
m.replaceAttributes = append(m.replaceAttributes, PartialAttribute{attrType: attrType, attrVals: attrVals})
m.ReplaceAttributes = append(m.ReplaceAttributes, PartialAttribute{AttrType: attrType, AttrVals: attrVals})
}

func (m ModifyRequest) encode() *ber.Packet {
request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyRequest, nil, "Modify Request")
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, m.dn, "DN"))
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, m.Dn, "DN"))
changes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Changes")
for _, attribute := range m.addAttributes {
for _, attribute := range m.AddAttributes {
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(AddAttribute), "Operation"))
change.AppendChild(attribute.encode())
changes.AppendChild(change)
}
for _, attribute := range m.deleteAttributes {
for _, attribute := range m.DeleteAttributes {
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(DeleteAttribute), "Operation"))
change.AppendChild(attribute.encode())
changes.AppendChild(change)
}
for _, attribute := range m.replaceAttributes {
for _, attribute := range m.ReplaceAttributes {
change := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Change")
change.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagEnumerated, uint64(ReplaceAttribute), "Operation"))
change.AppendChild(attribute.encode())
Expand All @@ -113,7 +113,7 @@ func NewModifyRequest(
dn string,
) *ModifyRequest {
return &ModifyRequest{
dn: dn,
Dn: dn,
}
}

Expand Down
9 changes: 5 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package ldap

import (
"crypto/tls"
"github.com/nmcclain/asn1-ber"
"io"
"log"
"net"
"strings"
"sync"

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

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

func (server *Server) serve(ln net.Listener) error {
func (server *Server) Serve(ln net.Listener) error {
newConn := make(chan net.Conn)
go func() {
for {
Expand Down
15 changes: 8 additions & 7 deletions server_modify.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"

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

func HandleAddRequest(req *ber.Packet, boundDN string, fns map[string]Adder, conn net.Conn) (resultCode LDAPResultCode) {
Expand Down Expand Up @@ -71,7 +72,7 @@ func HandleModifyRequest(req *ber.Packet, boundDN string, fns map[string]Modifie
}
var ok bool
modReq := ModifyRequest{}
modReq.dn, ok = req.Children[0].Value.(string)
modReq.Dn, ok = req.Children[0].Value.(string)
if !ok {
return LDAPResultProtocolError
}
Expand All @@ -84,7 +85,7 @@ func HandleModifyRequest(req *ber.Packet, boundDN string, fns map[string]Modifie
if len(attrs) != 2 {
return LDAPResultProtocolError
}
attr.attrType, ok = attrs[0].Value.(string)
attr.AttrType, ok = attrs[0].Value.(string)
if !ok {
return LDAPResultProtocolError
}
Expand All @@ -93,7 +94,7 @@ func HandleModifyRequest(req *ber.Packet, boundDN string, fns map[string]Modifie
if !ok {
return LDAPResultProtocolError
}
attr.attrVals = append(attr.attrVals, v)
attr.AttrVals = append(attr.AttrVals, v)
}
op, ok := change.Children[0].Value.(uint64)
if !ok {
Expand All @@ -104,11 +105,11 @@ func HandleModifyRequest(req *ber.Packet, boundDN string, fns map[string]Modifie
log.Printf("Unrecognized Modify attribute %d", op)
return LDAPResultProtocolError
case AddAttribute:
modReq.Add(attr.attrType, attr.attrVals)
modReq.Add(attr.AttrType, attr.AttrVals)
case DeleteAttribute:
modReq.Delete(attr.attrType, attr.attrVals)
modReq.Delete(attr.AttrType, attr.AttrVals)
case ReplaceAttribute:
modReq.Replace(attr.attrType, attr.attrVals)
modReq.Replace(attr.AttrType, attr.AttrVals)
}
}
fnNames := []string{}
Expand Down
6 changes: 3 additions & 3 deletions server_modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ func (h modifyTestHandler) Delete(boundDN, deleteDN string, conn net.Conn) (LDAP
}
func (h modifyTestHandler) Modify(boundDN string, req ModifyRequest, conn net.Conn) (LDAPResultCode, error) {
// only succeed on expected contents of modify.ldif:
if req.dn == "cn=testy,dc=example,dc=com" && len(req.addAttributes) == 1 &&
len(req.deleteAttributes) == 3 && len(req.replaceAttributes) == 2 &&
req.deleteAttributes[2].attrType == "details" && len(req.deleteAttributes[2].attrVals) == 0 {
if req.Dn == "cn=testy,dc=example,dc=com" && len(req.AddAttributes) == 1 &&
len(req.DeleteAttributes) == 3 && len(req.ReplaceAttributes) == 2 &&
req.DeleteAttributes[2].AttrType == "details" && len(req.DeleteAttributes[2].AttrVals) == 0 {
return LDAPResultSuccess, nil
}
return LDAPResultInsufficientAccessRights, nil
Expand Down
2 changes: 1 addition & 1 deletion server_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestSearchFiltering(t *testing.T) {
"-b", serverBaseDN, "-D", "cn=testy,"+serverBaseDN, "-w", "iLike2test", i.filterStr)
out, _ := cmd.CombinedOutput()
if !strings.Contains(string(out), "numResponses: "+i.numResponses) {
t.Errorf("ldapsearch failed - expected numResponses==%d: %v", i.numResponses, string(out))
t.Errorf("ldapsearch failed - expected numResponses==%s: %v", i.numResponses, string(out))
}
done <- true
}()
Expand Down

0 comments on commit ee4388c

Please sign in to comment.