2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-07-22 17:17:34 +00:00
|
|
|
"context"
|
2021-04-26 09:53:06 +00:00
|
|
|
"errors"
|
2021-04-19 22:30:27 +00:00
|
|
|
"net"
|
2021-07-19 11:41:29 +00:00
|
|
|
"strings"
|
2021-04-19 22:30:27 +00:00
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-04-26 09:53:06 +00:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
2021-07-19 11:41:29 +00:00
|
|
|
"github.com/google/uuid"
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-07-19 11:41:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-04-19 22:30:27 +00:00
|
|
|
)
|
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
type SearchRequest struct {
|
|
|
|
ldap.SearchRequest
|
|
|
|
BindDN string
|
|
|
|
id string
|
|
|
|
conn net.Conn
|
|
|
|
log *log.Entry
|
2021-07-22 17:17:34 +00:00
|
|
|
ctx context.Context
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 16:49:15 +00:00
|
|
|
func (ls *LDAPServer) Search(bindDN string, searchReq ldap.SearchRequest, conn net.Conn) (ldap.ServerSearchResult, error) {
|
2021-07-22 17:17:34 +00:00
|
|
|
span := sentry.StartSpan(context.TODO(), "authentik.providers.ldap.search", sentry.TransactionName("authentik.providers.ldap.search"))
|
2021-07-22 18:38:30 +00:00
|
|
|
span.SetTag("user.username", bindDN)
|
|
|
|
span.SetTag("ak_filter", searchReq.Filter)
|
|
|
|
span.SetTag("ak_base_dn", searchReq.BaseDN)
|
2021-07-22 17:17:34 +00:00
|
|
|
|
|
|
|
defer span.Finish()
|
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
bindDN = strings.ToLower(bindDN)
|
|
|
|
rid := uuid.New().String()
|
|
|
|
req := SearchRequest{
|
|
|
|
SearchRequest: searchReq,
|
|
|
|
BindDN: bindDN,
|
|
|
|
conn: conn,
|
|
|
|
log: ls.log.WithField("bindDN", bindDN).WithField("requestId", rid).WithField("client", conn.RemoteAddr().String()).WithField("filter", searchReq.Filter).WithField("baseDN", searchReq.BaseDN),
|
|
|
|
id: rid,
|
2021-07-22 17:17:34 +00:00
|
|
|
ctx: span.Context(),
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
|
|
|
req.log.Info("Search request")
|
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.WithError(err.(error)).Error("recover in serach request")
|
|
|
|
sentry.CaptureException(err.(error))
|
|
|
|
}()
|
|
|
|
|
2021-04-26 21:25:31 +00:00
|
|
|
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-07-19 11:41:29 +00:00
|
|
|
req.log.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) {
|
2021-07-19 11:41:29 +00:00
|
|
|
return provider.Search(req)
|
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
|
|
|
}
|