Skip to content

Commit

Permalink
feat: implement having rootDN for dialURL
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhhyip committed May 31, 2022
1 parent eb13da1 commit cf793d4
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 2 deletions.
8 changes: 8 additions & 0 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ type AddRequest struct {
Controls []Control
}

func (req *AddRequest) appendBaseDN(dn string) appendDnRequest {
return &AddRequest{
DN: appendDN(req.DN, dn),
Attributes: req.Attributes,
Controls: req.Controls,
}
}

func (req *AddRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
Expand Down
6 changes: 6 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Conn struct {
wgClose sync.WaitGroup
outstandingRequests uint
messageMutex sync.Mutex
rootDN string
}

var _ Client = &Conn{}
Expand Down Expand Up @@ -278,7 +279,12 @@ func DialURL(addr string, opts ...DialOpt) (*Conn, error) {
return nil, NewError(ErrorNetwork, err)
}

rootDN := ""
if u.Host == "" {
rootDN = u.Path[1:]
}
conn := NewConn(c, u.Scheme == "ldaps")
conn.rootDN = rootDN
conn.Start()
return conn, nil
}
Expand Down
7 changes: 7 additions & 0 deletions del.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type DelRequest struct {
Controls []Control
}

func (req *DelRequest) appendBaseDN(dn string) appendDnRequest {
return &DelRequest{
DN: appendDN(req.DN, dn),
Controls: req.Controls,
}
}

func (req *DelRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationDelRequest, req.DN, "Del Request")
pkt.Data.Write([]byte(req.DN))
Expand Down
12 changes: 12 additions & 0 deletions dn.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,15 @@ func (r *RelativeDN) hasAllAttributesFold(attrs []*AttributeTypeAndValue) bool {
func (a *AttributeTypeAndValue) EqualFold(other *AttributeTypeAndValue) bool {
return strings.EqualFold(a.Type, other.Type) && strings.EqualFold(a.Value, other.Value)
}

func appendDN(baseDN, rootDN string) string {
if rootDN != "" {
var baseDnBuilder strings.Builder
if baseDN != "" {
baseDnBuilder.WriteByte(',')
}
baseDnBuilder.WriteString(rootDN)
return baseDnBuilder.String()
}
return baseDN
}
12 changes: 11 additions & 1 deletion moddn.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewModifyDNRequest(dn string, rdn string, delOld bool, newSup string) *Modi
//
// Refer NewModifyDNRequest for other parameters
func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool,
newSup string, controls []Control) *ModifyDNRequest {
newSup string, controls []Control) *ModifyDNRequest {
return &ModifyDNRequest{
DN: dn,
NewRDN: rdn,
Expand All @@ -50,6 +50,16 @@ func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool,
}
}

func (req *ModifyDNRequest) appendBaseDN(dn string) appendDnRequest {
return &ModifyDNRequest{
DN: appendDN(req.DN, dn),
NewRDN: appendDN(req.NewRDN, dn),
DeleteOldRDN: req.DeleteOldRDN,
NewSuperior: appendDN(req.NewSuperior, dn),
Controls: req.Controls,
}
}

func (req *ModifyDNRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyDNRequest, nil, "Modify DN Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
Expand Down
8 changes: 8 additions & 0 deletions modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func (req *ModifyRequest) appendChange(operation uint, attrType string, attrVals
req.Changes = append(req.Changes, Change{operation, PartialAttribute{Type: attrType, Vals: attrVals}})
}

func (req *ModifyRequest) appendBaseDN(dn string) appendDnRequest {
return &ModifyRequest{
DN: appendDN(req.DN, dn),
Changes: req.Changes,
Controls: req.Controls,
}
}

func (req *ModifyRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyRequest, nil, "Modify Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
Expand Down
10 changes: 10 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type request interface {
appendTo(*ber.Packet) error
}

type appendDnRequest interface {
request
appendBaseDN(dn string) appendDnRequest
}

type requestFunc func(*ber.Packet) error

func (f requestFunc) appendTo(p *ber.Packet) error {
Expand All @@ -29,6 +34,11 @@ 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 areq, ok := req.(appendDnRequest); ok {
req = areq.appendBaseDN(l.rootDN)
}

if err := req.appendTo(packet); err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ type SearchRequest struct {
Controls []Control
}

func (req *SearchRequest) appendBaseDN(dn string) appendDnRequest {
return &SearchRequest{
BaseDN: appendDN(req.BaseDN, dn),
Scope: req.Scope,
DerefAliases: req.DerefAliases,
SizeLimit: req.SizeLimit,
TimeLimit: req.TimeLimit,
TypesOnly: req.TypesOnly,
Filter: req.Filter,
Attributes: req.Attributes,
Controls: req.Controls,
}
}

func (req *SearchRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationSearchRequest, nil, "Search Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.BaseDN, "Base DN"))
Expand Down
8 changes: 8 additions & 0 deletions v3/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ type AddRequest struct {
Controls []Control
}

func (req *AddRequest) appendBaseDN(dn string) appendDnRequest {
return &AddRequest{
DN: appendDN(req.DN, dn),
Attributes: req.Attributes,
Controls: req.Controls,
}
}

func (req *AddRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
Expand Down
6 changes: 6 additions & 0 deletions v3/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Conn struct {
wgClose sync.WaitGroup
outstandingRequests uint
messageMutex sync.Mutex
rootDN string
}

var _ Client = &Conn{}
Expand Down Expand Up @@ -278,7 +279,12 @@ func DialURL(addr string, opts ...DialOpt) (*Conn, error) {
return nil, NewError(ErrorNetwork, err)
}

rootDN := ""
if u.Host == "" {
rootDN = u.Path[1:]
}
conn := NewConn(c, u.Scheme == "ldaps")
conn.rootDN = rootDN
conn.Start()
return conn, nil
}
Expand Down
7 changes: 7 additions & 0 deletions v3/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type DelRequest struct {
Controls []Control
}

func (req *DelRequest) appendBaseDN(dn string) appendDnRequest {
return &DelRequest{
DN: appendDN(req.DN, dn),
Controls: req.Controls,
}
}

func (req *DelRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypePrimitive, ApplicationDelRequest, req.DN, "Del Request")
pkt.Data.Write([]byte(req.DN))
Expand Down
12 changes: 12 additions & 0 deletions v3/dn.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,15 @@ func (r *RelativeDN) hasAllAttributesFold(attrs []*AttributeTypeAndValue) bool {
func (a *AttributeTypeAndValue) EqualFold(other *AttributeTypeAndValue) bool {
return strings.EqualFold(a.Type, other.Type) && strings.EqualFold(a.Value, other.Value)
}

func appendDN(baseDN, rootDN string) string {
if rootDN != "" {
var baseDnBuilder strings.Builder
if baseDN != "" {
baseDnBuilder.WriteByte(',')
}
baseDnBuilder.WriteString(rootDN)
return baseDnBuilder.String()
}
return baseDN
}
12 changes: 11 additions & 1 deletion v3/moddn.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewModifyDNRequest(dn string, rdn string, delOld bool, newSup string) *Modi
//
// Refer NewModifyDNRequest for other parameters
func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool,
newSup string, controls []Control) *ModifyDNRequest {
newSup string, controls []Control) *ModifyDNRequest {
return &ModifyDNRequest{
DN: dn,
NewRDN: rdn,
Expand All @@ -50,6 +50,16 @@ func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool,
}
}

func (req *ModifyDNRequest) appendBaseDN(dn string) appendDnRequest {
return &ModifyDNRequest{
DN: appendDN(req.DN, dn),
NewRDN: appendDN(req.NewRDN, dn),
DeleteOldRDN: req.DeleteOldRDN,
NewSuperior: appendDN(req.NewSuperior, dn),
Controls: req.Controls,
}
}

func (req *ModifyDNRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyDNRequest, nil, "Modify DN Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
Expand Down
8 changes: 8 additions & 0 deletions v3/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func (req *ModifyRequest) appendChange(operation uint, attrType string, attrVals
req.Changes = append(req.Changes, Change{operation, PartialAttribute{Type: attrType, Vals: attrVals}})
}

func (req *ModifyRequest) appendBaseDN(dn string) appendDnRequest {
return &ModifyRequest{
DN: appendDN(req.DN, dn),
Changes: req.Changes,
Controls: req.Controls,
}
}

func (req *ModifyRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationModifyRequest, nil, "Modify Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
Expand Down
9 changes: 9 additions & 0 deletions v3/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type request interface {
appendTo(*ber.Packet) error
}

type appendDnRequest interface {
request
appendBaseDN(dn string) appendDnRequest
}

type requestFunc func(*ber.Packet) error

func (f requestFunc) appendTo(p *ber.Packet) error {
Expand All @@ -29,6 +34,10 @@ 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 areq, ok := req.(appendDnRequest); ok {
req = areq.appendBaseDN(l.rootDN)
}
if err := req.appendTo(packet); err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions v3/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ type SearchRequest struct {
Controls []Control
}

func (req *SearchRequest) appendBaseDN(dn string) appendDnRequest {
return &SearchRequest{
BaseDN: appendDN(req.BaseDN, dn),
Scope: req.Scope,
DerefAliases: req.DerefAliases,
SizeLimit: req.SizeLimit,
TimeLimit: req.TimeLimit,
TypesOnly: req.TypesOnly,
Filter: req.Filter,
Attributes: req.Attributes,
Controls: req.Controls,
}
}

func (req *SearchRequest) appendTo(envelope *ber.Packet) error {
pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationSearchRequest, nil, "Search Request")
pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.BaseDN, "Base DN"))
Expand Down

0 comments on commit cf793d4

Please sign in to comment.