diff --git a/search.go b/search.go index 35fc2497..33655496 100644 --- a/search.go +++ b/search.go @@ -372,15 +372,37 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { } switch packet.Children[1].Tag { - case 4: + case ApplicationSearchResultEntry: entry := &Entry{ DN: packet.Children[1].Children[0].Value.(string), Attributes: unpackAttributes(packet.Children[1].Children[1].Children), } result.Entries = append(result.Entries, entry) - case 5: + case ApplicationSearchResultDone: err := GetLDAPError(packet) if err != nil { + if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 { + var ( + referral string + ok bool + ) + + for _, child := range packet.Children[1].Children { + if child.Tag == ber.TagBitString && len(child.Children) >= 1 { + referral, ok = child.Children[0].Value.(string) + + if ok { + break + } + } + } + + if ok { + result.Referrals = append(result.Referrals, referral) + continue + } + } + return result, err } if len(packet.Children) == 3 { @@ -393,8 +415,10 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { } } return result, nil - case 19: - result.Referrals = append(result.Referrals, packet.Children[1].Children[0].Value.(string)) + case ApplicationSearchResultReference: + if len(packet.Children) >= 2 && len(packet.Children[1].Children) >= 1 { + result.Referrals = append(result.Referrals, packet.Children[1].Children[0].Value.(string)) + } } } } diff --git a/v3/search.go b/v3/search.go index 35fc2497..33655496 100644 --- a/v3/search.go +++ b/v3/search.go @@ -372,15 +372,37 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { } switch packet.Children[1].Tag { - case 4: + case ApplicationSearchResultEntry: entry := &Entry{ DN: packet.Children[1].Children[0].Value.(string), Attributes: unpackAttributes(packet.Children[1].Children[1].Children), } result.Entries = append(result.Entries, entry) - case 5: + case ApplicationSearchResultDone: err := GetLDAPError(packet) if err != nil { + if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 { + var ( + referral string + ok bool + ) + + for _, child := range packet.Children[1].Children { + if child.Tag == ber.TagBitString && len(child.Children) >= 1 { + referral, ok = child.Children[0].Value.(string) + + if ok { + break + } + } + } + + if ok { + result.Referrals = append(result.Referrals, referral) + continue + } + } + return result, err } if len(packet.Children) == 3 { @@ -393,8 +415,10 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) { } } return result, nil - case 19: - result.Referrals = append(result.Referrals, packet.Children[1].Children[0].Value.(string)) + case ApplicationSearchResultReference: + if len(packet.Children) >= 2 && len(packet.Children[1].Children) >= 1 { + result.Referrals = append(result.Referrals, packet.Children[1].Children[0].Value.(string)) + } } } }