2019-04-24 21:52:08 +00:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2019-12-06 08:15:54 +00:00
|
|
|
"crypto/tls"
|
2021-01-04 10:28:55 +00:00
|
|
|
"crypto/x509"
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
2021-08-05 04:30:00 +00:00
|
|
|
"net"
|
2019-04-24 21:52:08 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2021-04-12 01:10:50 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
// LDAPUserProvider is a UserProvider that connects to LDAP servers like ActiveDirectory, OpenLDAP, OpenDJ, FreeIPA, etc.
|
2019-04-24 21:52:08 +00:00
|
|
|
type LDAPUserProvider struct {
|
2022-05-02 01:51:38 +00:00
|
|
|
config schema.LDAPAuthenticationBackendConfiguration
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
dialOpts []ldap.DialOpt
|
|
|
|
log *logrus.Logger
|
|
|
|
factory LDAPConnectionFactory
|
2021-07-01 23:16:16 +00:00
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
disableResetPassword bool
|
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
// Automatically detected ldap features.
|
2021-07-01 23:16:16 +00:00
|
|
|
supportExtensionPasswdModify bool
|
2021-08-05 04:17:07 +00:00
|
|
|
|
|
|
|
// Dynamically generated users values.
|
|
|
|
usersBaseDN string
|
|
|
|
usersAttributes []string
|
|
|
|
usersFilterReplacementInput bool
|
|
|
|
|
|
|
|
// Dynamically generated groups values.
|
|
|
|
groupsBaseDN string
|
|
|
|
groupsAttributes []string
|
|
|
|
groupsFilterReplacementInput bool
|
|
|
|
groupsFilterReplacementUsername bool
|
|
|
|
groupsFilterReplacementDN bool
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 08:15:54 +00:00
|
|
|
// NewLDAPUserProvider creates a new instance of LDAPUserProvider.
|
2022-05-02 01:51:38 +00:00
|
|
|
func NewLDAPUserProvider(config schema.AuthenticationBackendConfiguration, certPool *x509.CertPool) (provider *LDAPUserProvider) {
|
|
|
|
provider = newLDAPUserProvider(*config.LDAP, config.DisableResetPassword, certPool, nil)
|
2021-07-01 23:16:16 +00:00
|
|
|
|
2021-09-17 09:53:59 +00:00
|
|
|
return provider
|
2021-07-01 23:16:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func newLDAPUserProvider(config schema.LDAPAuthenticationBackendConfiguration, disableResetPassword bool, certPool *x509.CertPool, factory LDAPConnectionFactory) (provider *LDAPUserProvider) {
|
|
|
|
if config.TLS == nil {
|
|
|
|
config.TLS = schema.DefaultLDAPAuthenticationBackendConfiguration.TLS
|
2020-12-03 05:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
tlsConfig := utils.NewTLSConfig(config.TLS, tls.VersionTLS12, certPool)
|
2020-12-03 05:23:52 +00:00
|
|
|
|
2021-08-05 04:30:00 +00:00
|
|
|
var dialOpts = []ldap.DialOpt{
|
2022-05-02 01:51:38 +00:00
|
|
|
ldap.DialWithDialer(&net.Dialer{Timeout: config.Timeout}),
|
2021-08-05 04:30:00 +00:00
|
|
|
}
|
2020-12-03 05:23:52 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
if tlsConfig != nil {
|
2021-08-05 04:30:00 +00:00
|
|
|
dialOpts = append(dialOpts, ldap.DialWithTLSConfig(tlsConfig))
|
2020-12-03 05:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 23:16:16 +00:00
|
|
|
if factory == nil {
|
2022-05-02 01:51:38 +00:00
|
|
|
factory = NewProductionLDAPConnectionFactory()
|
2021-07-01 23:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
provider = &LDAPUserProvider{
|
2022-05-02 01:51:38 +00:00
|
|
|
config: config,
|
2021-09-17 09:53:59 +00:00
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
dialOpts: dialOpts,
|
2021-11-23 09:45:38 +00:00
|
|
|
log: logging.Logger(),
|
2022-05-02 01:51:38 +00:00
|
|
|
factory: factory,
|
2021-09-17 09:53:59 +00:00
|
|
|
disableResetPassword: disableResetPassword,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2021-01-04 10:28:55 +00:00
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
provider.parseDynamicUsersConfiguration()
|
|
|
|
provider.parseDynamicGroupsConfiguration()
|
2021-01-04 10:28:55 +00:00
|
|
|
|
|
|
|
return provider
|
2019-12-06 08:15:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
// CheckUserPassword checks if provided password matches for the given user.
|
|
|
|
func (p *LDAPUserProvider) CheckUserPassword(inputUsername string, password string) (valid bool, err error) {
|
|
|
|
var (
|
|
|
|
conn, connUser LDAPConnection
|
|
|
|
profile *ldapUserProfile
|
|
|
|
)
|
|
|
|
|
|
|
|
if conn, err = p.connect(); err != nil {
|
|
|
|
return false, err
|
2019-12-06 08:15:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
if profile, err = p.getUserProfile(conn, inputUsername); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if connUser, err = p.connectCustom(p.config.URL, profile.DN, password, p.config.StartTLS, p.dialOpts...); err != nil {
|
|
|
|
return false, fmt.Errorf("authentication failed. Cause: %w", err)
|
2020-12-03 05:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
defer connUser.Close()
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDetails retrieve the groups a user belongs to.
|
|
|
|
func (p *LDAPUserProvider) GetDetails(username string) (details *UserDetails, err error) {
|
|
|
|
var (
|
|
|
|
conn LDAPConnection
|
|
|
|
profile *ldapUserProfile
|
|
|
|
)
|
|
|
|
|
|
|
|
if conn, err = p.connect(); err != nil {
|
2019-12-06 08:15:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
if profile, err = p.getUserProfile(conn, username); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
filter string
|
|
|
|
searchRequest *ldap.SearchRequest
|
|
|
|
searchResult *ldap.SearchResult
|
|
|
|
)
|
|
|
|
|
|
|
|
if filter, err = p.resolveGroupsFilter(username, profile); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create group filter for user '%s'. Cause: %w", username, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the users groups.
|
|
|
|
searchRequest = ldap.NewSearchRequest(
|
|
|
|
p.groupsBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
|
|
|
0, 0, false, filter, p.groupsAttributes, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
if searchResult, err = p.search(conn, searchRequest); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to retrieve groups of user '%s'. Cause: %w", username, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := make([]string, 0)
|
|
|
|
|
|
|
|
for _, res := range searchResult.Entries {
|
|
|
|
if len(res.Attributes) == 0 {
|
|
|
|
p.log.Warningf("No groups retrieved from LDAP for user %s", username)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append all values of the document. Normally there should be only one per document.
|
|
|
|
groups = append(groups, res.Attributes[0].Values...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &UserDetails{
|
|
|
|
Username: profile.Username,
|
|
|
|
DisplayName: profile.DisplayName,
|
|
|
|
Emails: profile.Emails,
|
|
|
|
Groups: groups,
|
|
|
|
}, nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
// UpdatePassword update the password of the given user.
|
|
|
|
func (p *LDAPUserProvider) UpdatePassword(username, password string) (err error) {
|
|
|
|
var (
|
|
|
|
conn LDAPConnection
|
|
|
|
profile *ldapUserProfile
|
|
|
|
)
|
|
|
|
|
|
|
|
if conn, err = p.connect(); err != nil {
|
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2022-05-02 01:51:38 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
defer conn.Close()
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if profile, err = p.getUserProfile(conn, username); err != nil {
|
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var controls []ldap.Control
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.supportExtensionPasswdModify:
|
|
|
|
pwdModifyRequest := ldap.NewPasswordModifyRequest(
|
|
|
|
profile.DN,
|
|
|
|
"",
|
|
|
|
password,
|
|
|
|
)
|
|
|
|
|
|
|
|
err = p.pwdModify(conn, pwdModifyRequest)
|
|
|
|
case p.config.Implementation == schema.LDAPImplementationActiveDirectory:
|
|
|
|
modifyRequest := ldap.NewModifyRequest(profile.DN, controls)
|
|
|
|
// The password needs to be enclosed in quotes
|
|
|
|
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/6e803168-f140-4d23-b2d3-c3a8ab5917d2
|
|
|
|
pwdEncoded, _ := utf16LittleEndian.NewEncoder().String(fmt.Sprintf("\"%s\"", password))
|
|
|
|
modifyRequest.Replace(ldapAttributeUnicodePwd, []string{pwdEncoded})
|
|
|
|
|
|
|
|
err = p.modify(conn, modifyRequest)
|
|
|
|
default:
|
|
|
|
modifyRequest := ldap.NewModifyRequest(profile.DN, controls)
|
|
|
|
modifyRequest.Replace(ldapAttributeUserPassword, []string{password})
|
|
|
|
|
|
|
|
err = p.modify(conn, modifyRequest)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-02 01:51:38 +00:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LDAPUserProvider) connect() (LDAPConnection, error) {
|
|
|
|
return p.connectCustom(p.config.URL, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) connectCustom(url, userDN, password string, startTLS bool, opts ...ldap.DialOpt) (conn LDAPConnection, err error) {
|
|
|
|
if conn, err = p.factory.DialURL(url, opts...); err != nil {
|
|
|
|
return nil, fmt.Errorf("dial failed with error: %w", err)
|
2020-01-20 19:34:53 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if startTLS {
|
|
|
|
if err = conn.StartTLS(p.tlsConfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("starttls failed with error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = conn.Bind(userDN, password); err != nil {
|
|
|
|
return nil, fmt.Errorf("bind failed with error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
2020-01-20 19:34:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) search(conn LDAPConnection, searchRequest *ldap.SearchRequest) (searchResult *ldap.SearchResult, err error) {
|
|
|
|
searchResult, err = conn.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
if referral, ok := p.getReferral(err); ok {
|
|
|
|
if errReferral := p.searchReferral(referral, searchRequest, searchResult); errReferral != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.config.PermitReferrals || len(searchResult.Referrals) == 0 {
|
|
|
|
return searchResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.searchReferrals(searchRequest, searchResult)
|
|
|
|
|
|
|
|
return searchResult, nil
|
2020-03-15 07:10:25 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) searchReferral(referral string, searchRequest *ldap.SearchRequest, searchResult *ldap.SearchResult) (err error) {
|
|
|
|
var (
|
|
|
|
conn LDAPConnection
|
|
|
|
result *ldap.SearchResult
|
|
|
|
)
|
2020-03-30 22:36:04 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if conn, err = p.connectCustom(referral, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...); err != nil {
|
|
|
|
p.log.Errorf("Failed to connect during referred search request (referred to %s): %v", referral, err)
|
|
|
|
|
|
|
|
return err
|
2021-08-05 04:17:07 +00:00
|
|
|
}
|
2020-03-30 22:36:04 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
defer conn.Close()
|
2021-04-12 01:10:50 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if result, err = conn.Search(searchRequest); err != nil {
|
|
|
|
p.log.Errorf("Failed to perform search operation during referred search request (referred to %s): %v", referral, err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Entries) == 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(result.Entries); i++ {
|
|
|
|
if !ldapEntriesContainsEntry(result.Entries[i], searchResult.Entries) {
|
|
|
|
searchResult.Entries = append(searchResult.Entries, result.Entries[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LDAPUserProvider) searchReferrals(searchRequest *ldap.SearchRequest, searchResult *ldap.SearchResult) {
|
|
|
|
for i := 0; i < len(searchResult.Referrals); i++ {
|
|
|
|
_ = p.searchReferral(searchResult.Referrals[i], searchRequest, searchResult)
|
|
|
|
}
|
2020-03-30 22:36:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) getUserProfile(conn LDAPConnection, inputUsername string) (profile *ldapUserProfile, err error) {
|
2021-08-05 04:17:07 +00:00
|
|
|
userFilter := p.resolveUsersFilter(inputUsername)
|
2020-03-15 07:10:25 +00:00
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Search for the given username.
|
2019-04-24 21:52:08 +00:00
|
|
|
searchRequest := ldap.NewSearchRequest(
|
2021-04-12 01:10:50 +00:00
|
|
|
p.usersBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
2021-08-05 04:17:07 +00:00
|
|
|
1, 0, false, userFilter, p.usersAttributes, nil,
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
var searchResult *ldap.SearchResult
|
|
|
|
|
|
|
|
if searchResult, err = p.search(conn, searchRequest); err != nil {
|
2021-08-05 04:17:07 +00:00
|
|
|
return nil, fmt.Errorf("cannot find user DN of user '%s'. Cause: %w", inputUsername, err)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if len(searchResult.Entries) == 0 {
|
2020-05-04 19:39:25 +00:00
|
|
|
return nil, ErrUserNotFound
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if len(searchResult.Entries) > 1 {
|
2021-08-05 04:17:07 +00:00
|
|
|
return nil, fmt.Errorf("multiple users %s found", inputUsername)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 07:10:25 +00:00
|
|
|
userProfile := ldapUserProfile{
|
2022-05-02 01:51:38 +00:00
|
|
|
DN: searchResult.Entries[0].DN,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
for _, attr := range searchResult.Entries[0].Attributes {
|
|
|
|
if attr.Name == p.config.DisplayNameAttribute {
|
2020-06-19 10:50:21 +00:00
|
|
|
userProfile.DisplayName = attr.Values[0]
|
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if attr.Name == p.config.MailAttribute {
|
2020-03-15 07:10:25 +00:00
|
|
|
userProfile.Emails = attr.Values
|
2020-04-15 12:26:23 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if attr.Name == p.config.UsernameAttribute {
|
2020-03-15 07:10:25 +00:00
|
|
|
if len(attr.Values) != 1 {
|
2021-08-05 04:17:07 +00:00
|
|
|
return nil, fmt.Errorf("user '%s' cannot have multiple value for attribute '%s'",
|
2022-05-02 01:51:38 +00:00
|
|
|
inputUsername, p.config.UsernameAttribute)
|
2020-03-15 07:10:25 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-15 07:10:25 +00:00
|
|
|
userProfile.Username = attr.Values[0]
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 07:10:25 +00:00
|
|
|
if userProfile.DN == "" {
|
2021-08-05 04:17:07 +00:00
|
|
|
return nil, fmt.Errorf("no DN has been found for user %s", inputUsername)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 07:10:25 +00:00
|
|
|
return &userProfile, nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) resolveUsersFilter(inputUsername string) (filter string) {
|
|
|
|
filter = p.config.UsersFilter
|
|
|
|
|
|
|
|
if p.usersFilterReplacementInput {
|
|
|
|
// The {input} placeholder is replaced by the username input.
|
|
|
|
filter = strings.ReplaceAll(filter, ldapPlaceholderInput, ldapEscape(inputUsername))
|
|
|
|
}
|
|
|
|
|
|
|
|
p.log.Tracef("Detected user filter is %s", filter)
|
|
|
|
|
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
func (p *LDAPUserProvider) resolveGroupsFilter(inputUsername string, profile *ldapUserProfile) (filter string, err error) { //nolint:unparam
|
2022-05-02 01:51:38 +00:00
|
|
|
filter = p.config.GroupsFilter
|
2020-03-30 22:36:04 +00:00
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
if p.groupsFilterReplacementInput {
|
|
|
|
// The {input} placeholder is replaced by the users username input.
|
2022-05-02 01:51:38 +00:00
|
|
|
filter = strings.ReplaceAll(p.config.GroupsFilter, ldapPlaceholderInput, ldapEscape(inputUsername))
|
2021-08-05 04:17:07 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-30 22:36:04 +00:00
|
|
|
if profile != nil {
|
2021-08-05 04:17:07 +00:00
|
|
|
if p.groupsFilterReplacementUsername {
|
|
|
|
filter = strings.ReplaceAll(filter, ldapPlaceholderUsername, ldap.EscapeFilter(profile.Username))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.groupsFilterReplacementDN {
|
|
|
|
filter = strings.ReplaceAll(filter, ldapPlaceholderDistinguishedName, ldap.EscapeFilter(profile.DN))
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-03-30 22:36:04 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
p.log.Tracef("Computed groups filter is %s", filter)
|
2021-04-12 01:10:50 +00:00
|
|
|
|
2021-08-05 04:17:07 +00:00
|
|
|
return filter, nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) modify(conn LDAPConnection, modifyRequest *ldap.ModifyRequest) (err error) {
|
|
|
|
if err = conn.Modify(modifyRequest); err != nil {
|
|
|
|
var (
|
|
|
|
referral string
|
|
|
|
ok bool
|
|
|
|
)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if referral, ok = p.getReferral(err); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-30 22:36:04 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
p.log.Debugf("Attempting Modify on referred URL %s", referral)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
var (
|
|
|
|
connReferral LDAPConnection
|
|
|
|
errReferral error
|
|
|
|
)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if connReferral, errReferral = p.connectCustom(referral, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...); errReferral != nil {
|
|
|
|
p.log.Errorf("Failed to connect during referred modify request (referred to %s): %v", referral, errReferral)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
defer connReferral.Close()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if errReferral = connReferral.Modify(modifyRequest); errReferral != nil {
|
|
|
|
p.log.Errorf("Failed to perform modify operation during referred modify request (referred to %s): %v", referral, errReferral)
|
2020-02-27 22:21:07 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
return err
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
func (p *LDAPUserProvider) pwdModify(conn LDAPConnection, pwdModifyRequest *ldap.PasswordModifyRequest) (err error) {
|
|
|
|
if _, err = conn.PasswordModify(pwdModifyRequest); err != nil {
|
|
|
|
var (
|
|
|
|
referral string
|
|
|
|
ok bool
|
|
|
|
)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if referral, ok = p.getReferral(err); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
p.log.Debugf("Attempting PwdModify ExOp (1.3.6.1.4.1.4203.1.11.1) on referred URL %s", referral)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
var (
|
|
|
|
connReferral LDAPConnection
|
|
|
|
errReferral error
|
2021-07-06 09:13:17 +00:00
|
|
|
)
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if connReferral, errReferral = p.connectCustom(referral, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...); errReferral != nil {
|
|
|
|
p.log.Errorf("Failed to connect during referred password modify request (referred to %s): %v", referral, errReferral)
|
2021-07-06 09:13:17 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer connReferral.Close()
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
if _, errReferral = connReferral.PasswordModify(pwdModifyRequest); errReferral != nil {
|
|
|
|
p.log.Errorf("Failed to perform modify operation during referred modify request (referred to %s): %v", referral, errReferral)
|
|
|
|
}
|
2021-07-06 09:13:17 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LDAPUserProvider) getReferral(err error) (referral string, ok bool) {
|
|
|
|
if !p.config.PermitReferrals {
|
|
|
|
return "", false
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 01:51:38 +00:00
|
|
|
return ldapGetReferral(err)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|