2021-11-05 09:37:30 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-12-02 14:28:58 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-11-05 09:37:30 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-openapi/strfmt"
|
2021-12-02 14:28:58 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-11-05 09:37:30 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-06-02 10:52:20 +00:00
|
|
|
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-12-02 14:28:58 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/bind"
|
2021-12-02 14:28:58 +00:00
|
|
|
ldapConstants "goauthentik.io/internal/outpost/ldap/constants"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/flags"
|
|
|
|
"goauthentik.io/internal/outpost/ldap/search"
|
2021-12-02 14:28:58 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/utils"
|
2021-11-05 09:37:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProviderInstance struct {
|
|
|
|
BaseDN string
|
|
|
|
UserDN string
|
|
|
|
VirtualGroupDN string
|
|
|
|
GroupDN string
|
|
|
|
|
|
|
|
searcher search.Searcher
|
|
|
|
binder bind.Binder
|
|
|
|
|
2023-01-23 19:36:30 +00:00
|
|
|
appSlug string
|
|
|
|
authenticationFlowSlug string
|
|
|
|
invalidationFlowSlug string
|
|
|
|
s *LDAPServer
|
|
|
|
log *log.Entry
|
2021-11-05 09:37:30 +00:00
|
|
|
|
|
|
|
tlsServerName *string
|
|
|
|
cert *tls.Certificate
|
2022-12-28 18:15:29 +00:00
|
|
|
certUUID string
|
2021-11-05 09:37:30 +00:00
|
|
|
outpostName string
|
2021-11-12 23:26:01 +00:00
|
|
|
outpostPk int32
|
2021-11-05 09:37:30 +00:00
|
|
|
searchAllowedGroups []*strfmt.UUID
|
2023-06-02 10:52:20 +00:00
|
|
|
boundUsersMutex *sync.RWMutex
|
2022-05-21 13:48:50 +00:00
|
|
|
boundUsers map[string]*flags.UserFlags
|
2021-11-05 09:37:30 +00:00
|
|
|
|
|
|
|
uidStartNumber int32
|
|
|
|
gidStartNumber int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetAPIClient() *api.APIClient {
|
|
|
|
return pi.s.ac.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetBaseDN() string {
|
|
|
|
return pi.BaseDN
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetBaseGroupDN() string {
|
|
|
|
return pi.GroupDN
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:28:58 +00:00
|
|
|
func (pi *ProviderInstance) GetBaseVirtualGroupDN() string {
|
|
|
|
return pi.VirtualGroupDN
|
|
|
|
}
|
|
|
|
|
2021-11-05 09:37:30 +00:00
|
|
|
func (pi *ProviderInstance) GetBaseUserDN() string {
|
|
|
|
return pi.UserDN
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetOutpostName() string {
|
|
|
|
return pi.outpostName
|
|
|
|
}
|
|
|
|
|
2022-05-21 13:48:50 +00:00
|
|
|
func (pi *ProviderInstance) GetFlags(dn string) *flags.UserFlags {
|
2021-11-05 09:37:30 +00:00
|
|
|
pi.boundUsersMutex.RLock()
|
2022-05-21 13:48:50 +00:00
|
|
|
defer pi.boundUsersMutex.RUnlock()
|
2021-11-05 09:37:30 +00:00
|
|
|
flags, ok := pi.boundUsers[dn]
|
2022-05-21 13:48:50 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return flags
|
2021-11-05 09:37:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 19:36:30 +00:00
|
|
|
func (pi *ProviderInstance) SetFlags(dn string, flag *flags.UserFlags) {
|
2021-11-05 09:37:30 +00:00
|
|
|
pi.boundUsersMutex.Lock()
|
2023-01-23 19:36:30 +00:00
|
|
|
if flag == nil {
|
|
|
|
delete(pi.boundUsers, dn)
|
|
|
|
} else {
|
|
|
|
pi.boundUsers[dn] = flag
|
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
pi.boundUsersMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetAppSlug() string {
|
|
|
|
return pi.appSlug
|
|
|
|
}
|
|
|
|
|
2023-01-23 19:36:30 +00:00
|
|
|
func (pi *ProviderInstance) GetAuthenticationFlowSlug() string {
|
|
|
|
return pi.authenticationFlowSlug
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetInvalidationFlowSlug() string {
|
|
|
|
return pi.invalidationFlowSlug
|
2021-11-05 09:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetSearchAllowedGroups() []*strfmt.UUID {
|
|
|
|
return pi.searchAllowedGroups
|
|
|
|
}
|
2021-12-02 14:28:58 +00:00
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetBaseEntry() *ldap.Entry {
|
|
|
|
return &ldap.Entry{
|
|
|
|
DN: pi.GetBaseDN(),
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "distinguishedName",
|
|
|
|
Values: []string{pi.GetBaseDN()},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "objectClass",
|
|
|
|
Values: []string{ldapConstants.OCTop, ldapConstants.OCDomain},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "supportedLDAPVersion",
|
|
|
|
Values: []string{"3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "namingContexts",
|
|
|
|
Values: []string{
|
|
|
|
pi.GetBaseDN(),
|
|
|
|
pi.GetBaseUserDN(),
|
|
|
|
pi.GetBaseGroupDN(),
|
|
|
|
pi.GetBaseVirtualGroupDN(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "vendorName",
|
|
|
|
Values: []string{"goauthentik.io"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "vendorVersion",
|
2022-01-14 09:45:37 +00:00
|
|
|
Values: []string{fmt.Sprintf("authentik LDAP Outpost Version %s", constants.FullVersion())},
|
2021-12-02 14:28:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pi *ProviderInstance) GetNeededObjects(scope int, baseDN string, filterOC string) (bool, bool) {
|
|
|
|
needUsers := false
|
|
|
|
needGroups := false
|
|
|
|
|
|
|
|
// We only want to load users/groups if we're actually going to be asked
|
|
|
|
// for at least one user or group based on the search's base DN and scope.
|
|
|
|
//
|
|
|
|
// If our requested base DN doesn't match any of the container DNs, then
|
|
|
|
// we're probably loading a user or group. If it does, then make sure our
|
|
|
|
// scope will eventually take us to users or groups.
|
2022-01-25 10:27:27 +00:00
|
|
|
if (strings.EqualFold(baseDN, pi.BaseDN) || utils.HasSuffixNoCase(baseDN, pi.UserDN)) && utils.IncludeObjectClass(filterOC, ldapConstants.GetUserOCs()) {
|
2021-12-02 14:28:58 +00:00
|
|
|
if baseDN != pi.UserDN && baseDN != pi.BaseDN ||
|
2022-01-25 10:27:27 +00:00
|
|
|
strings.EqualFold(baseDN, pi.BaseDN) && scope > 1 ||
|
|
|
|
strings.EqualFold(baseDN, pi.UserDN) && scope > 0 {
|
2021-12-02 14:28:58 +00:00
|
|
|
needUsers = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:27:27 +00:00
|
|
|
if (strings.EqualFold(baseDN, pi.BaseDN) || utils.HasSuffixNoCase(baseDN, pi.GroupDN)) && utils.IncludeObjectClass(filterOC, ldapConstants.GetGroupOCs()) {
|
2021-12-02 14:28:58 +00:00
|
|
|
if baseDN != pi.GroupDN && baseDN != pi.BaseDN ||
|
2022-01-25 10:27:27 +00:00
|
|
|
strings.EqualFold(baseDN, pi.BaseDN) && scope > 1 ||
|
|
|
|
strings.EqualFold(baseDN, pi.GroupDN) && scope > 0 {
|
2021-12-02 14:28:58 +00:00
|
|
|
needGroups = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:27:27 +00:00
|
|
|
if (strings.EqualFold(baseDN, pi.BaseDN) || utils.HasSuffixNoCase(baseDN, pi.VirtualGroupDN)) && utils.IncludeObjectClass(filterOC, ldapConstants.GetVirtualGroupOCs()) {
|
2021-12-02 14:28:58 +00:00
|
|
|
if baseDN != pi.VirtualGroupDN && baseDN != pi.BaseDN ||
|
2022-01-25 10:27:27 +00:00
|
|
|
strings.EqualFold(baseDN, pi.BaseDN) && scope > 1 ||
|
|
|
|
strings.EqualFold(baseDN, pi.VirtualGroupDN) && scope > 0 {
|
2021-12-02 14:28:58 +00:00
|
|
|
needUsers = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return needUsers, needGroups
|
|
|
|
}
|