2019-04-25 04:52:08 +07:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2019-12-06 15:15:54 +07:00
|
|
|
"crypto/tls"
|
2021-01-04 17:28:55 +07:00
|
|
|
"crypto/x509"
|
2019-04-25 04:52:08 +07:00
|
|
|
"fmt"
|
2021-08-05 11:30:00 +07:00
|
|
|
"net"
|
2019-04-25 04:52:08 +07:00
|
|
|
"strings"
|
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2021-04-12 08:10:50 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07: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-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
// LDAPUserProvider is a UserProvider that connects to LDAP servers like ActiveDirectory, OpenLDAP, OpenDJ, FreeIPA, etc.
|
2019-04-25 04:52:08 +07:00
|
|
|
type LDAPUserProvider struct {
|
2022-05-02 08:51:38 +07:00
|
|
|
config schema.LDAPAuthenticationBackendConfiguration
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
dialOpts []ldap.DialOpt
|
|
|
|
log *logrus.Logger
|
2022-05-10 11:38:36 +07:00
|
|
|
factory LDAPClientFactory
|
2021-07-02 06:16:16 +07:00
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
disableResetPassword bool
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
// Automatically detected LDAP features.
|
|
|
|
features LDAPSupportedFeatures
|
2021-08-05 11:17:07 +07: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-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:15:54 +07:00
|
|
|
// NewLDAPUserProvider creates a new instance of LDAPUserProvider.
|
2022-05-02 08:51:38 +07:00
|
|
|
func NewLDAPUserProvider(config schema.AuthenticationBackendConfiguration, certPool *x509.CertPool) (provider *LDAPUserProvider) {
|
|
|
|
provider = newLDAPUserProvider(*config.LDAP, config.DisableResetPassword, certPool, nil)
|
2021-07-02 06:16:16 +07:00
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
return provider
|
2021-07-02 06:16:16 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func newLDAPUserProvider(config schema.LDAPAuthenticationBackendConfiguration, disableResetPassword bool, certPool *x509.CertPool, factory LDAPClientFactory) (provider *LDAPUserProvider) {
|
2022-05-02 08:51:38 +07:00
|
|
|
if config.TLS == nil {
|
|
|
|
config.TLS = schema.DefaultLDAPAuthenticationBackendConfiguration.TLS
|
2020-12-03 12:23:52 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
tlsConfig := utils.NewTLSConfig(config.TLS, tls.VersionTLS12, certPool)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-08-05 11:30:00 +07:00
|
|
|
var dialOpts = []ldap.DialOpt{
|
2022-05-02 08:51:38 +07:00
|
|
|
ldap.DialWithDialer(&net.Dialer{Timeout: config.Timeout}),
|
2021-08-05 11:30:00 +07:00
|
|
|
}
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
if tlsConfig != nil {
|
2021-08-05 11:30:00 +07:00
|
|
|
dialOpts = append(dialOpts, ldap.DialWithTLSConfig(tlsConfig))
|
2020-12-03 12:23:52 +07:00
|
|
|
}
|
|
|
|
|
2021-07-02 06:16:16 +07:00
|
|
|
if factory == nil {
|
2022-05-10 11:38:36 +07:00
|
|
|
factory = NewProductionLDAPClientFactory()
|
2021-07-02 06:16:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
provider = &LDAPUserProvider{
|
2022-05-02 08:51:38 +07:00
|
|
|
config: config,
|
2021-09-17 16:53:59 +07:00
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
dialOpts: dialOpts,
|
2021-11-23 16:45:38 +07:00
|
|
|
log: logging.Logger(),
|
2022-05-02 08:51:38 +07:00
|
|
|
factory: factory,
|
2021-09-17 16:53:59 +07:00
|
|
|
disableResetPassword: disableResetPassword,
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
provider.parseDynamicUsersConfiguration()
|
|
|
|
provider.parseDynamicGroupsConfiguration()
|
2021-01-04 17:28:55 +07:00
|
|
|
|
|
|
|
return provider
|
2019-12-06 15:15:54 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
// CheckUserPassword checks if provided password matches for the given user.
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) CheckUserPassword(username string, password string) (valid bool, err error) {
|
2022-05-02 08:51:38 +07:00
|
|
|
var (
|
2022-05-10 11:38:36 +07:00
|
|
|
client, clientUser LDAPClient
|
|
|
|
profile *ldapUserProfile
|
2022-05-02 08:51:38 +07:00
|
|
|
)
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if client, err = p.connect(); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return false, err
|
2019-12-06 15:15:54 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer client.Close()
|
2022-05-02 08:51:38 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if profile, err = p.getUserProfile(client, username); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if clientUser, err = p.connectCustom(p.config.URL, profile.DN, password, p.config.StartTLS, p.dialOpts...); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return false, fmt.Errorf("authentication failed. Cause: %w", err)
|
2020-12-03 12:23:52 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer clientUser.Close()
|
2022-05-02 08:51:38 +07:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDetails retrieve the groups a user belongs to.
|
|
|
|
func (p *LDAPUserProvider) GetDetails(username string) (details *UserDetails, err error) {
|
|
|
|
var (
|
2022-05-10 11:38:36 +07:00
|
|
|
client LDAPClient
|
2022-05-02 08:51:38 +07:00
|
|
|
profile *ldapUserProfile
|
|
|
|
)
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if client, err = p.connect(); err != nil {
|
2019-12-06 15:15:54 +07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer client.Close()
|
2022-05-02 08:51:38 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if profile, err = p.getUserProfile(client, username); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if searchResult, err = p.search(client, searchRequest); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
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-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
// UpdatePassword update the password of the given user.
|
|
|
|
func (p *LDAPUserProvider) UpdatePassword(username, password string) (err error) {
|
|
|
|
var (
|
2022-05-10 11:38:36 +07:00
|
|
|
client LDAPClient
|
2022-05-02 08:51:38 +07:00
|
|
|
profile *ldapUserProfile
|
|
|
|
)
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if client, err = p.connect(); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2022-05-02 08:51:38 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer client.Close()
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if profile, err = p.getUserProfile(client, username); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var controls []ldap.Control
|
|
|
|
|
|
|
|
switch {
|
2022-05-10 11:38:36 +07:00
|
|
|
case p.features.ControlTypes.MsftPwdPolHints:
|
|
|
|
controls = append(controls, &controlMsftServerPolicyHints{ldapOIDControlMsftServerPolicyHints})
|
|
|
|
case p.features.ControlTypes.MsftPwdPolHintsDeprecated:
|
|
|
|
controls = append(controls, &controlMsftServerPolicyHints{ldapOIDControlMsftServerPolicyHintsDeprecated})
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.features.Extensions.PwdModifyExOp:
|
2022-05-02 08:51:38 +07:00
|
|
|
pwdModifyRequest := ldap.NewPasswordModifyRequest(
|
|
|
|
profile.DN,
|
|
|
|
"",
|
|
|
|
password,
|
|
|
|
)
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
err = p.pwdModify(client, pwdModifyRequest)
|
2022-05-02 08:51:38 +07:00
|
|
|
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})
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
err = p.modify(client, modifyRequest)
|
2022-05-02 08:51:38 +07:00
|
|
|
default:
|
|
|
|
modifyRequest := ldap.NewModifyRequest(profile.DN, controls)
|
|
|
|
modifyRequest.Replace(ldapAttributeUserPassword, []string{password})
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
err = p.modify(client, modifyRequest)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) connect() (client LDAPClient, err error) {
|
2022-05-02 08:51:38 +07:00
|
|
|
return p.connectCustom(p.config.URL, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) connectCustom(url, userDN, password string, startTLS bool, opts ...ldap.DialOpt) (client LDAPClient, err error) {
|
|
|
|
if client, err = p.factory.DialURL(url, opts...); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
return nil, fmt.Errorf("dial failed with error: %w", err)
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
if startTLS {
|
2022-05-10 11:38:36 +07:00
|
|
|
if err = client.StartTLS(p.tlsConfig); err != nil {
|
|
|
|
client.Close()
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
return nil, fmt.Errorf("starttls failed with error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if err = client.Bind(userDN, password); err != nil {
|
|
|
|
client.Close()
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
return nil, fmt.Errorf("bind failed with error: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
return client, nil
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) search(client LDAPClient, searchRequest *ldap.SearchRequest) (searchResult *ldap.SearchResult, err error) {
|
|
|
|
if searchResult, err = client.Search(searchRequest); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
if referral, ok := p.getReferral(err); ok {
|
2022-05-10 11:38:36 +07:00
|
|
|
if searchResult == nil {
|
|
|
|
searchResult = &ldap.SearchResult{
|
|
|
|
Referrals: []string{referral},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
searchResult.Referrals = append(searchResult.Referrals, referral)
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.config.PermitReferrals || len(searchResult.Referrals) == 0 {
|
2022-05-10 11:38:36 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
return searchResult, nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if err = p.searchReferrals(searchRequest, searchResult); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-02 08:51:38 +07:00
|
|
|
|
|
|
|
return searchResult, nil
|
2020-03-15 14:10:25 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
func (p *LDAPUserProvider) searchReferral(referral string, searchRequest *ldap.SearchRequest, searchResult *ldap.SearchResult) (err error) {
|
|
|
|
var (
|
2022-05-10 11:38:36 +07:00
|
|
|
client LDAPClient
|
2022-05-02 08:51:38 +07:00
|
|
|
result *ldap.SearchResult
|
|
|
|
)
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if client, err = p.connectCustom(referral, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...); err != nil {
|
|
|
|
return fmt.Errorf("error occurred connecting to referred LDAP server '%s': %w", referral, err)
|
2021-08-05 11:17:07 +07:00
|
|
|
}
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer client.Close()
|
2021-04-12 08:10:50 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if result, err = client.Search(searchRequest); err != nil {
|
|
|
|
return fmt.Errorf("error occurred performing search on referred LDAP server '%s': %w", referral, err)
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) searchReferrals(searchRequest *ldap.SearchRequest, searchResult *ldap.SearchResult) (err error) {
|
2022-05-02 08:51:38 +07:00
|
|
|
for i := 0; i < len(searchResult.Referrals); i++ {
|
2022-05-10 11:38:36 +07:00
|
|
|
if err = p.searchReferral(searchResult.Referrals[i], searchRequest, searchResult); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
2022-05-10 11:38:36 +07:00
|
|
|
|
|
|
|
return nil
|
2020-03-31 05:36:04 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) getUserProfile(client LDAPClient, username string) (profile *ldapUserProfile, err error) {
|
|
|
|
userFilter := p.resolveUsersFilter(username)
|
2020-03-15 14:10:25 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Search for the given username.
|
2019-04-25 04:52:08 +07:00
|
|
|
searchRequest := ldap.NewSearchRequest(
|
2021-04-12 08:10:50 +07:00
|
|
|
p.usersBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
2021-08-05 11:17:07 +07:00
|
|
|
1, 0, false, userFilter, p.usersAttributes, nil,
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
var searchResult *ldap.SearchResult
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if searchResult, err = p.search(client, searchRequest); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot find user DN of user '%s'. Cause: %w", username, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
if len(searchResult.Entries) == 0 {
|
2020-05-05 02:39:25 +07:00
|
|
|
return nil, ErrUserNotFound
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
if len(searchResult.Entries) > 1 {
|
2022-05-10 11:38:36 +07:00
|
|
|
return nil, fmt.Errorf("there were %d users found when searching for '%s' but there should only be 1", len(searchResult.Entries), username)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
userProfile := ldapUserProfile{
|
2022-05-02 08:51:38 +07:00
|
|
|
DN: searchResult.Entries[0].DN,
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
for _, attr := range searchResult.Entries[0].Attributes {
|
2022-05-10 11:38:36 +07:00
|
|
|
switch attr.Name {
|
|
|
|
case p.config.DisplayNameAttribute:
|
2020-06-19 17:50:21 +07:00
|
|
|
userProfile.DisplayName = attr.Values[0]
|
2022-05-10 11:38:36 +07:00
|
|
|
case p.config.MailAttribute:
|
2020-03-15 14:10:25 +07:00
|
|
|
userProfile.Emails = attr.Values
|
2022-05-10 11:38:36 +07:00
|
|
|
case p.config.UsernameAttribute:
|
|
|
|
attrs := len(attr.Values)
|
|
|
|
|
|
|
|
switch attrs {
|
|
|
|
case 1:
|
|
|
|
userProfile.Username = attr.Values[0]
|
|
|
|
case 0:
|
|
|
|
return nil, fmt.Errorf("user '%s' must have value for attribute '%s'",
|
|
|
|
username, p.config.UsernameAttribute)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("user '%s' has %d values for for attribute '%s' but the attribute must be a single value attribute",
|
|
|
|
username, attrs, p.config.UsernameAttribute)
|
2020-03-15 14:10:25 +07:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if userProfile.Username == "" {
|
|
|
|
return nil, fmt.Errorf("user '%s' must have value for attribute '%s'",
|
|
|
|
username, p.config.UsernameAttribute)
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
if userProfile.DN == "" {
|
2022-05-10 11:38:36 +07:00
|
|
|
return nil, fmt.Errorf("user '%s' must have a distinguished name but the result returned an empty distinguished name", username)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
return &userProfile, nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) resolveUsersFilter(username string) (filter string) {
|
2022-05-02 08:51:38 +07:00
|
|
|
filter = p.config.UsersFilter
|
|
|
|
|
|
|
|
if p.usersFilterReplacementInput {
|
|
|
|
// The {input} placeholder is replaced by the username input.
|
2022-05-10 11:38:36 +07:00
|
|
|
filter = strings.ReplaceAll(filter, ldapPlaceholderInput, ldapEscape(username))
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
p.log.Tracef("Detected user filter is %s", filter)
|
|
|
|
|
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) resolveGroupsFilter(username string, profile *ldapUserProfile) (filter string, err error) { //nolint:unparam
|
2022-05-02 08:51:38 +07:00
|
|
|
filter = p.config.GroupsFilter
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
if p.groupsFilterReplacementInput {
|
|
|
|
// The {input} placeholder is replaced by the users username input.
|
2022-05-10 11:38:36 +07:00
|
|
|
filter = strings.ReplaceAll(p.config.GroupsFilter, ldapPlaceholderInput, ldapEscape(username))
|
2021-08-05 11:17:07 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
if profile != nil {
|
2021-08-05 11:17:07 +07: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-25 04:52:08 +07:00
|
|
|
}
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
p.log.Tracef("Computed groups filter is %s", filter)
|
2021-04-12 08:10:50 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
return filter, nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) modify(client LDAPClient, modifyRequest *ldap.ModifyRequest) (err error) {
|
|
|
|
if err = client.Modify(modifyRequest); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
var (
|
|
|
|
referral string
|
|
|
|
ok bool
|
|
|
|
)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
if referral, ok = p.getReferral(err); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
p.log.Debugf("Attempting Modify on referred URL %s", referral)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
var (
|
2022-05-10 11:38:36 +07:00
|
|
|
clientRef LDAPClient
|
|
|
|
errRef error
|
2022-05-02 08:51:38 +07:00
|
|
|
)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if clientRef, errRef = p.connectCustom(referral, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...); errRef != nil {
|
|
|
|
return fmt.Errorf("error occurred connecting to referred LDAP server '%s': %+v. Original Error: %w", referral, errRef, err)
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer clientRef.Close()
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if errRef = clientRef.Modify(modifyRequest); errRef != nil {
|
|
|
|
return fmt.Errorf("error occurred performing modify on referred LDAP server '%s': %+v. Original Error: %w", referral, errRef, err)
|
2020-02-28 05:21:07 +07:00
|
|
|
}
|
2022-05-10 11:38:36 +07:00
|
|
|
|
|
|
|
return nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
return nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
func (p *LDAPUserProvider) pwdModify(client LDAPClient, pwdModifyRequest *ldap.PasswordModifyRequest) (err error) {
|
|
|
|
if _, err = client.PasswordModify(pwdModifyRequest); err != nil {
|
2022-05-02 08:51:38 +07:00
|
|
|
var (
|
|
|
|
referral string
|
|
|
|
ok bool
|
|
|
|
)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
if referral, ok = p.getReferral(err); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
p.log.Debugf("Attempting PwdModify ExOp (1.3.6.1.4.1.4203.1.11.1) on referred URL %s", referral)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
var (
|
2022-05-10 11:38:36 +07:00
|
|
|
clientRef LDAPClient
|
|
|
|
errRef error
|
2021-07-06 16:13:17 +07:00
|
|
|
)
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if clientRef, errRef = p.connectCustom(referral, p.config.User, p.config.Password, p.config.StartTLS, p.dialOpts...); errRef != nil {
|
|
|
|
return fmt.Errorf("error occurred connecting to referred LDAP server '%s': %+v. Original Error: %w", referral, errRef, err)
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
defer clientRef.Close()
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
if _, errRef = clientRef.PasswordModify(pwdModifyRequest); errRef != nil {
|
|
|
|
return fmt.Errorf("error occurred performing password modify on referred LDAP server '%s': %+v. Original Error: %w", referral, errRef, err)
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
2022-05-10 11:38:36 +07:00
|
|
|
|
|
|
|
return nil
|
2021-07-06 16:13:17 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-05-10 11:38:36 +07:00
|
|
|
return nil
|
2022-05-02 08:51:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LDAPUserProvider) getReferral(err error) (referral string, ok bool) {
|
|
|
|
if !p.config.PermitReferrals {
|
|
|
|
return "", false
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2022-05-02 08:51:38 +07:00
|
|
|
return ldapGetReferral(err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|