2015-05-07 17:59:38 +00:00
package ldap
import (
"errors"
"fmt"
2015-11-18 15:36:57 +00:00
"sort"
2015-05-07 17:59:38 +00:00
"strings"
2019-11-08 16:24:23 +00:00
ber "github.com/go-asn1-ber/asn1-ber"
2015-05-07 17:59:38 +00:00
)
2016-07-23 00:11:47 +00:00
// scope choices
2015-05-07 17:59:38 +00:00
const (
ScopeBaseObject = 0
ScopeSingleLevel = 1
ScopeWholeSubtree = 2
)
2016-07-23 00:11:47 +00:00
// ScopeMap contains human readable descriptions of scope choices
2015-05-07 17:59:38 +00:00
var ScopeMap = map [ int ] string {
ScopeBaseObject : "Base Object" ,
ScopeSingleLevel : "Single Level" ,
ScopeWholeSubtree : "Whole Subtree" ,
}
2016-07-23 00:11:47 +00:00
// derefAliases
2015-05-07 17:59:38 +00:00
const (
NeverDerefAliases = 0
DerefInSearching = 1
DerefFindingBaseObj = 2
DerefAlways = 3
)
2016-07-23 00:11:47 +00:00
// DerefMap contains human readable descriptions of derefAliases choices
2015-05-07 17:59:38 +00:00
var DerefMap = map [ int ] string {
NeverDerefAliases : "NeverDerefAliases" ,
DerefInSearching : "DerefInSearching" ,
DerefFindingBaseObj : "DerefFindingBaseObj" ,
DerefAlways : "DerefAlways" ,
}
2015-11-18 15:36:57 +00:00
// NewEntry returns an Entry object with the specified distinguished name and attribute key-value pairs.
// The map of attributes is accessed in alphabetical order of the keys in order to ensure that, for the
// same input map of attributes, the output entry will contain the same order of attributes
func NewEntry ( dn string , attributes map [ string ] [ ] string ) * Entry {
var attributeNames [ ] string
for attributeName := range attributes {
attributeNames = append ( attributeNames , attributeName )
}
sort . Strings ( attributeNames )
var encodedAttributes [ ] * EntryAttribute
for _ , attributeName := range attributeNames {
encodedAttributes = append ( encodedAttributes , NewEntryAttribute ( attributeName , attributes [ attributeName ] ) )
}
return & Entry {
DN : dn ,
Attributes : encodedAttributes ,
}
}
2016-07-23 00:11:47 +00:00
// Entry represents a single search result entry
2015-05-07 17:59:38 +00:00
type Entry struct {
2016-07-23 00:11:47 +00:00
// DN is the distinguished name of the entry
DN string
// Attributes are the returned attributes for the entry
2015-05-07 17:59:38 +00:00
Attributes [ ] * EntryAttribute
}
2016-07-23 00:11:47 +00:00
// GetAttributeValues returns the values for the named attribute, or an empty list
2015-05-07 17:59:38 +00:00
func ( e * Entry ) GetAttributeValues ( attribute string ) [ ] string {
for _ , attr := range e . Attributes {
if attr . Name == attribute {
return attr . Values
}
}
return [ ] string { }
}
2016-07-23 00:11:47 +00:00
// GetRawAttributeValues returns the byte values for the named attribute, or an empty list
2015-06-29 21:50:55 +00:00
func ( e * Entry ) GetRawAttributeValues ( attribute string ) [ ] [ ] byte {
for _ , attr := range e . Attributes {
if attr . Name == attribute {
return attr . ByteValues
}
}
return [ ] [ ] byte { }
}
2016-07-23 00:11:47 +00:00
// GetAttributeValue returns the first value for the named attribute, or ""
2015-05-07 17:59:38 +00:00
func ( e * Entry ) GetAttributeValue ( attribute string ) string {
values := e . GetAttributeValues ( attribute )
if len ( values ) == 0 {
return ""
}
return values [ 0 ]
}
2016-07-23 00:11:47 +00:00
// GetRawAttributeValue returns the first value for the named attribute, or an empty slice
2015-06-29 21:50:55 +00:00
func ( e * Entry ) GetRawAttributeValue ( attribute string ) [ ] byte {
values := e . GetRawAttributeValues ( attribute )
if len ( values ) == 0 {
return [ ] byte { }
}
return values [ 0 ]
}
2016-07-23 00:11:47 +00:00
// Print outputs a human-readable description
2015-05-07 17:59:38 +00:00
func ( e * Entry ) Print ( ) {
fmt . Printf ( "DN: %s\n" , e . DN )
for _ , attr := range e . Attributes {
attr . Print ( )
}
}
2016-07-23 00:11:47 +00:00
// PrettyPrint outputs a human-readable description indenting
2015-05-07 17:59:38 +00:00
func ( e * Entry ) PrettyPrint ( indent int ) {
fmt . Printf ( "%sDN: %s\n" , strings . Repeat ( " " , indent ) , e . DN )
for _ , attr := range e . Attributes {
attr . PrettyPrint ( indent + 2 )
}
}
2015-11-18 15:36:57 +00:00
// NewEntryAttribute returns a new EntryAttribute with the desired key-value pair
func NewEntryAttribute ( name string , values [ ] string ) * EntryAttribute {
var bytes [ ] [ ] byte
for _ , value := range values {
bytes = append ( bytes , [ ] byte ( value ) )
}
return & EntryAttribute {
Name : name ,
Values : values ,
ByteValues : bytes ,
}
}
2016-07-23 00:11:47 +00:00
// EntryAttribute holds a single attribute
2015-05-07 17:59:38 +00:00
type EntryAttribute struct {
2016-07-23 00:11:47 +00:00
// Name is the name of the attribute
Name string
// Values contain the string values of the attribute
Values [ ] string
// ByteValues contain the raw values of the attribute
2015-06-29 21:50:55 +00:00
ByteValues [ ] [ ] byte
2015-05-07 17:59:38 +00:00
}
2016-07-23 00:11:47 +00:00
// Print outputs a human-readable description
2015-05-07 17:59:38 +00:00
func ( e * EntryAttribute ) Print ( ) {
fmt . Printf ( "%s: %s\n" , e . Name , e . Values )
}
2016-07-23 00:11:47 +00:00
// PrettyPrint outputs a human-readable description with indenting
2015-05-07 17:59:38 +00:00
func ( e * EntryAttribute ) PrettyPrint ( indent int ) {
fmt . Printf ( "%s%s: %s\n" , strings . Repeat ( " " , indent ) , e . Name , e . Values )
}
2016-07-23 00:11:47 +00:00
// SearchResult holds the server's response to a search request
2015-05-07 17:59:38 +00:00
type SearchResult struct {
2016-07-23 00:11:47 +00:00
// Entries are the returned entries
Entries [ ] * Entry
// Referrals are the returned referrals
2015-05-07 17:59:38 +00:00
Referrals [ ] string
2016-07-23 00:11:47 +00:00
// Controls are the returned controls
Controls [ ] Control
2015-05-07 17:59:38 +00:00
}
2016-07-23 00:11:47 +00:00
// Print outputs a human-readable description
2015-05-07 17:59:38 +00:00
func ( s * SearchResult ) Print ( ) {
for _ , entry := range s . Entries {
entry . Print ( )
}
}
2016-07-23 00:11:47 +00:00
// PrettyPrint outputs a human-readable description with indenting
2015-05-07 17:59:38 +00:00
func ( s * SearchResult ) PrettyPrint ( indent int ) {
for _ , entry := range s . Entries {
entry . PrettyPrint ( indent )
}
}
2016-07-23 00:11:47 +00:00
// SearchRequest represents a search request to send to the server
2015-05-07 17:59:38 +00:00
type SearchRequest struct {
BaseDN string
Scope int
DerefAliases int
SizeLimit int
TimeLimit int
TypesOnly bool
Filter string
Attributes [ ] string
Controls [ ] Control
}
2019-11-08 16:24:23 +00:00
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" ) )
pkt . AppendChild ( ber . NewInteger ( ber . ClassUniversal , ber . TypePrimitive , ber . TagEnumerated , uint64 ( req . Scope ) , "Scope" ) )
pkt . AppendChild ( ber . NewInteger ( ber . ClassUniversal , ber . TypePrimitive , ber . TagEnumerated , uint64 ( req . DerefAliases ) , "Deref Aliases" ) )
pkt . AppendChild ( ber . NewInteger ( ber . ClassUniversal , ber . TypePrimitive , ber . TagInteger , uint64 ( req . SizeLimit ) , "Size Limit" ) )
pkt . AppendChild ( ber . NewInteger ( ber . ClassUniversal , ber . TypePrimitive , ber . TagInteger , uint64 ( req . TimeLimit ) , "Time Limit" ) )
pkt . AppendChild ( ber . NewBoolean ( ber . ClassUniversal , ber . TypePrimitive , ber . TagBoolean , req . TypesOnly , "Types Only" ) )
2015-05-07 17:59:38 +00:00
// compile and encode filter
2019-11-08 16:24:23 +00:00
filterPacket , err := CompileFilter ( req . Filter )
2015-05-07 17:59:38 +00:00
if err != nil {
2019-11-08 16:24:23 +00:00
return err
2015-05-07 17:59:38 +00:00
}
2019-11-08 16:24:23 +00:00
pkt . AppendChild ( filterPacket )
2015-05-07 17:59:38 +00:00
// encode attributes
attributesPacket := ber . Encode ( ber . ClassUniversal , ber . TypeConstructed , ber . TagSequence , nil , "Attributes" )
2019-11-08 16:24:23 +00:00
for _ , attribute := range req . Attributes {
2015-05-07 17:59:38 +00:00
attributesPacket . AppendChild ( ber . NewString ( ber . ClassUniversal , ber . TypePrimitive , ber . TagOctetString , attribute , "Attribute" ) )
}
2019-11-08 16:24:23 +00:00
pkt . AppendChild ( attributesPacket )
envelope . AppendChild ( pkt )
if len ( req . Controls ) > 0 {
envelope . AppendChild ( encodeControls ( req . Controls ) )
}
return nil
2015-05-07 17:59:38 +00:00
}
2016-07-23 00:11:47 +00:00
// NewSearchRequest creates a new search request
2015-05-07 17:59:38 +00:00
func NewSearchRequest (
BaseDN string ,
Scope , DerefAliases , SizeLimit , TimeLimit int ,
TypesOnly bool ,
Filter string ,
Attributes [ ] string ,
Controls [ ] Control ,
) * SearchRequest {
return & SearchRequest {
BaseDN : BaseDN ,
Scope : Scope ,
DerefAliases : DerefAliases ,
SizeLimit : SizeLimit ,
TimeLimit : TimeLimit ,
TypesOnly : TypesOnly ,
Filter : Filter ,
Attributes : Attributes ,
Controls : Controls ,
}
}
2016-02-18 19:19:02 +00:00
// SearchWithPaging accepts a search request and desired page size in order to execute LDAP queries to fulfill the
// search request. All paged LDAP query responses will be buffered and the final result will be returned atomically.
// The following four cases are possible given the arguments:
// - given SearchRequest missing a control of type ControlTypePaging: we will add one with the desired paging size
// - given SearchRequest contains a control of type ControlTypePaging that isn't actually a ControlPaging: fail without issuing any queries
// - given SearchRequest contains a control of type ControlTypePaging with pagingSize equal to the size requested: no change to the search request
// - given SearchRequest contains a control of type ControlTypePaging with pagingSize not equal to the size requested: fail without issuing any queries
// A requested pagingSize of 0 is interpreted as no limit by LDAP servers.
2015-05-07 17:59:38 +00:00
func ( l * Conn ) SearchWithPaging ( searchRequest * SearchRequest , pagingSize uint32 ) ( * SearchResult , error ) {
2016-02-18 19:19:02 +00:00
var pagingControl * ControlPaging
control := FindControl ( searchRequest . Controls , ControlTypePaging )
if control == nil {
pagingControl = NewControlPaging ( pagingSize )
searchRequest . Controls = append ( searchRequest . Controls , pagingControl )
} else {
castControl , ok := control . ( * ControlPaging )
if ! ok {
2018-06-15 17:13:57 +00:00
return nil , fmt . Errorf ( "expected paging control to be of type *ControlPaging, got %v" , control )
2016-02-18 19:19:02 +00:00
}
if castControl . PagingSize != pagingSize {
2018-06-15 17:13:57 +00:00
return nil , fmt . Errorf ( "paging size given in search request (%d) conflicts with size given in search call (%d)" , castControl . PagingSize , pagingSize )
2016-02-18 19:19:02 +00:00
}
pagingControl = castControl
2015-05-07 17:59:38 +00:00
}
searchResult := new ( SearchResult )
for {
result , err := l . Search ( searchRequest )
l . Debug . Printf ( "Looking for Paging Control..." )
if err != nil {
return searchResult , err
}
if result == nil {
return searchResult , NewError ( ErrorNetwork , errors . New ( "ldap: packet not received" ) )
}
for _ , entry := range result . Entries {
searchResult . Entries = append ( searchResult . Entries , entry )
}
for _ , referral := range result . Referrals {
searchResult . Referrals = append ( searchResult . Referrals , referral )
}
for _ , control := range result . Controls {
searchResult . Controls = append ( searchResult . Controls , control )
}
l . Debug . Printf ( "Looking for Paging Control..." )
pagingResult := FindControl ( result . Controls , ControlTypePaging )
if pagingResult == nil {
pagingControl = nil
l . Debug . Printf ( "Could not find paging control. Breaking..." )
break
}
cookie := pagingResult . ( * ControlPaging ) . Cookie
if len ( cookie ) == 0 {
pagingControl = nil
l . Debug . Printf ( "Could not find cookie. Breaking..." )
break
}
pagingControl . SetCookie ( cookie )
}
if pagingControl != nil {
l . Debug . Printf ( "Abandoning Paging..." )
pagingControl . PagingSize = 0
l . Search ( searchRequest )
}
return searchResult , nil
}
2016-07-23 00:11:47 +00:00
// Search performs the given search request
2015-05-07 17:59:38 +00:00
func ( l * Conn ) Search ( searchRequest * SearchRequest ) ( * SearchResult , error ) {
2019-11-08 16:24:23 +00:00
msgCtx , err := l . doRequest ( searchRequest )
2015-05-07 17:59:38 +00:00
if err != nil {
return nil , err
}
2016-06-30 18:19:03 +00:00
defer l . finishMessage ( msgCtx )
2015-05-07 17:59:38 +00:00
result := & SearchResult {
Entries : make ( [ ] * Entry , 0 ) ,
Referrals : make ( [ ] string , 0 ) ,
Controls : make ( [ ] Control , 0 ) }
2019-11-08 16:24:23 +00:00
for {
packet , err := l . readPacket ( msgCtx )
2016-04-26 00:18:04 +00:00
if err != nil {
return nil , err
2015-05-07 17:59:38 +00:00
}
switch packet . Children [ 1 ] . Tag {
case 4 :
entry := new ( Entry )
entry . DN = packet . Children [ 1 ] . Children [ 0 ] . Value . ( string )
for _ , child := range packet . Children [ 1 ] . Children [ 1 ] . Children {
attr := new ( EntryAttribute )
attr . Name = child . Children [ 0 ] . Value . ( string )
for _ , value := range child . Children [ 1 ] . Children {
attr . Values = append ( attr . Values , value . Value . ( string ) )
2015-06-29 21:50:55 +00:00
attr . ByteValues = append ( attr . ByteValues , value . ByteValue )
2015-05-07 17:59:38 +00:00
}
entry . Attributes = append ( entry . Attributes , attr )
}
result . Entries = append ( result . Entries , entry )
case 5 :
2019-01-31 22:07:25 +00:00
err := GetLDAPError ( packet )
if err != nil {
return nil , err
2015-05-07 17:59:38 +00:00
}
if len ( packet . Children ) == 3 {
for _ , child := range packet . Children [ 2 ] . Children {
2018-06-15 17:13:57 +00:00
decodedChild , err := DecodeControl ( child )
if err != nil {
return nil , fmt . Errorf ( "failed to decode child control: %s" , err )
}
result . Controls = append ( result . Controls , decodedChild )
2015-05-07 17:59:38 +00:00
}
}
2019-11-08 16:24:23 +00:00
return result , nil
2015-05-07 17:59:38 +00:00
case 19 :
result . Referrals = append ( result . Referrals , packet . Children [ 1 ] . Children [ 0 ] . Value . ( string ) )
}
}
}