2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-04-26 09:53:06 +00:00
|
|
|
"errors"
|
2021-04-19 22:30:27 +00:00
|
|
|
"net"
|
|
|
|
|
2021-04-26 09:53:06 +00:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-04-19 22:30:27 +00:00
|
|
|
)
|
|
|
|
|
2021-04-26 09:53:06 +00:00
|
|
|
func (ls *LDAPServer) Search(boundDN string, searchReq ldap.SearchRequest, conn net.Conn) (ldap.ServerSearchResult, error) {
|
2021-04-26 21:25:31 +00:00
|
|
|
ls.log.WithField("boundDN", boundDN).WithField("baseDN", searchReq.BaseDN).Info("search")
|
|
|
|
if searchReq.BaseDN == "" {
|
|
|
|
return ldap.ServerSearchResult{ResultCode: ldap.LDAPResultSuccess}, nil
|
|
|
|
}
|
|
|
|
bd, err := goldap.ParseDN(searchReq.BaseDN)
|
2021-04-19 22:30:27 +00:00
|
|
|
if err != nil {
|
2021-04-26 21:25:31 +00:00
|
|
|
ls.log.WithField("baseDN", searchReq.BaseDN).WithError(err).Info("failed to parse basedn")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.ServerSearchResult{ResultCode: ldap.LDAPResultOperationsError}, errors.New("invalid DN")
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, provider := range ls.providers {
|
|
|
|
providerBase, _ := goldap.ParseDN(provider.BaseDN)
|
|
|
|
if providerBase.AncestorOf(bd) {
|
|
|
|
return provider.Search(boundDN, searchReq, conn)
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-26 21:25:31 +00:00
|
|
|
return ldap.ServerSearchResult{ResultCode: ldap.LDAPResultOperationsError}, errors.New("no provider could handle request")
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|