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-11-27 16:59:22 +07:00
|
|
|
"golang.org/x/text/encoding/unicode"
|
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 {
|
2021-01-04 17:28:55 +07:00
|
|
|
configuration schema.LDAPAuthenticationBackendConfiguration
|
|
|
|
tlsConfig *tls.Config
|
2021-08-05 11:30:00 +07:00
|
|
|
dialOpts []ldap.DialOpt
|
2021-11-23 16:45:38 +07:00
|
|
|
log *logrus.Logger
|
2019-12-06 15:15:54 +07:00
|
|
|
connectionFactory LDAPConnectionFactory
|
2021-07-02 06:16:16 +07:00
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
disableResetPassword bool
|
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
// Automatically detected ldap features.
|
2021-07-02 06:16:16 +07:00
|
|
|
supportExtensionPasswdModify bool
|
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.
|
2021-09-17 16:53:59 +07:00
|
|
|
func NewLDAPUserProvider(configuration schema.AuthenticationBackendConfiguration, certPool *x509.CertPool) (provider *LDAPUserProvider) {
|
|
|
|
provider = newLDAPUserProvider(*configuration.LDAP, configuration.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
|
|
|
}
|
|
|
|
|
2021-09-17 16:53:59 +07:00
|
|
|
func newLDAPUserProvider(configuration schema.LDAPAuthenticationBackendConfiguration, disableResetPassword bool, certPool *x509.CertPool, factory LDAPConnectionFactory) (provider *LDAPUserProvider) {
|
2021-01-04 17:28:55 +07:00
|
|
|
if configuration.TLS == nil {
|
|
|
|
configuration.TLS = schema.DefaultLDAPAuthenticationBackendConfiguration.TLS
|
2020-12-03 12:23:52 +07:00
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
tlsConfig := utils.NewTLSConfig(configuration.TLS, tls.VersionTLS12, certPool)
|
2020-12-03 12:23:52 +07:00
|
|
|
|
2021-08-05 11:30:00 +07:00
|
|
|
var dialOpts = []ldap.DialOpt{
|
|
|
|
ldap.DialWithDialer(&net.Dialer{Timeout: configuration.Timeout}),
|
|
|
|
}
|
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 {
|
|
|
|
factory = NewLDAPConnectionFactoryImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
provider = &LDAPUserProvider{
|
2021-09-17 16:53:59 +07:00
|
|
|
configuration: configuration,
|
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
dialOpts: dialOpts,
|
2021-11-23 16:45:38 +07:00
|
|
|
log: logging.Logger(),
|
2021-09-17 16:53:59 +07:00
|
|
|
connectionFactory: factory,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
func (p *LDAPUserProvider) connect(userDN string, password string) (LDAPConnection, error) {
|
2021-08-05 11:30:00 +07:00
|
|
|
conn, err := p.connectionFactory.DialURL(p.configuration.URL, p.dialOpts...)
|
2021-01-04 17:28:55 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-06 15:15:54 +07:00
|
|
|
}
|
|
|
|
|
2020-12-03 12:23:52 +07:00
|
|
|
if p.configuration.StartTLS {
|
2021-01-04 17:28:55 +07:00
|
|
|
if err := conn.StartTLS(p.tlsConfig); err != nil {
|
2020-12-03 12:23:52 +07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
if err := conn.Bind(userDN, password); err != nil {
|
2019-12-06 15:15:54 +07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
return conn, nil
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckUserPassword checks if provided password matches for the given user.
|
2020-03-31 05:36:04 +07:00
|
|
|
func (p *LDAPUserProvider) CheckUserPassword(inputUsername string, password string) (bool, error) {
|
2021-01-04 17:28:55 +07:00
|
|
|
conn, err := p.connect(p.configuration.User, p.configuration.Password)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
defer conn.Close()
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
profile, err := p.getUserProfile(conn, inputUsername)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
userConn, err := p.connect(profile.DN, password)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
2021-12-02 17:28:16 +07:00
|
|
|
return false, fmt.Errorf("authentication failed. Cause: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
defer userConn.Close()
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
func (p *LDAPUserProvider) ldapEscape(inputUsername string) string {
|
|
|
|
inputUsername = ldap.EscapeFilter(inputUsername)
|
|
|
|
for _, c := range specialLDAPRunes {
|
|
|
|
inputUsername = strings.ReplaceAll(inputUsername, string(c), fmt.Sprintf("\\%c", c))
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
return inputUsername
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
type ldapUserProfile struct {
|
2020-06-19 17:50:21 +07:00
|
|
|
DN string
|
|
|
|
Emails []string
|
|
|
|
DisplayName string
|
|
|
|
Username string
|
2020-03-15 14:10:25 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
func (p *LDAPUserProvider) resolveUsersFilter(inputUsername string) (filter string) {
|
|
|
|
filter = p.configuration.UsersFilter
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
if p.usersFilterReplacementInput {
|
|
|
|
// The {input} placeholder is replaced by the users username input.
|
|
|
|
filter = strings.ReplaceAll(filter, ldapPlaceholderInput, p.ldapEscape(inputUsername))
|
|
|
|
}
|
2020-03-31 05:36:04 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
p.log.Tracef("Computed user filter is %s", filter)
|
2021-04-12 08:10:50 +07:00
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
return filter
|
2020-03-31 05:36:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LDAPUserProvider) getUserProfile(conn LDAPConnection, inputUsername string) (*ldapUserProfile, error) {
|
2021-08-05 11:17:07 +07:00
|
|
|
userFilter := p.resolveUsersFilter(inputUsername)
|
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
|
|
|
)
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
sr, err := conn.Search(searchRequest)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
2021-08-05 11:17:07 +07:00
|
|
|
return nil, fmt.Errorf("cannot find user DN of user '%s'. Cause: %w", inputUsername, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
if len(sr.Entries) == 0 {
|
2020-05-05 02:39:25 +07:00
|
|
|
return nil, ErrUserNotFound
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
if len(sr.Entries) > 1 {
|
2021-08-05 11:17:07 +07:00
|
|
|
return nil, fmt.Errorf("multiple users %s found", inputUsername)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
userProfile := ldapUserProfile{
|
|
|
|
DN: sr.Entries[0].DN,
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
for _, attr := range sr.Entries[0].Attributes {
|
2020-06-19 17:50:21 +07:00
|
|
|
if attr.Name == p.configuration.DisplayNameAttribute {
|
|
|
|
userProfile.DisplayName = attr.Values[0]
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
if attr.Name == p.configuration.MailAttribute {
|
|
|
|
userProfile.Emails = attr.Values
|
2020-04-15 19:26:23 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-15 19:26:23 +07:00
|
|
|
if attr.Name == p.configuration.UsernameAttribute {
|
2020-03-15 14:10:25 +07:00
|
|
|
if len(attr.Values) != 1 {
|
2021-08-05 11:17:07 +07:00
|
|
|
return nil, fmt.Errorf("user '%s' cannot have multiple value for attribute '%s'",
|
2020-03-31 05:36:04 +07:00
|
|
|
inputUsername, p.configuration.UsernameAttribute)
|
2020-03-15 14:10:25 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
userProfile.Username = attr.Values[0]
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
if userProfile.DN == "" {
|
2021-08-05 11:17:07 +07:00
|
|
|
return nil, fmt.Errorf("no DN has been found for user %s", inputUsername)
|
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
|
|
|
}
|
|
|
|
|
2021-08-05 11:17:07 +07:00
|
|
|
func (p *LDAPUserProvider) resolveGroupsFilter(inputUsername string, profile *ldapUserProfile) (filter string, err error) { //nolint:unparam
|
|
|
|
filter = p.configuration.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.
|
|
|
|
filter = strings.ReplaceAll(p.configuration.GroupsFilter, ldapPlaceholderInput, p.ldapEscape(inputUsername))
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// GetDetails retrieve the groups a user belongs to.
|
2020-03-31 05:36:04 +07:00
|
|
|
func (p *LDAPUserProvider) GetDetails(inputUsername string) (*UserDetails, error) {
|
2019-04-25 04:52:08 +07:00
|
|
|
conn, err := p.connect(p.configuration.User, p.configuration.Password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
profile, err := p.getUserProfile(conn, inputUsername)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groupsFilter, err := p.resolveGroupsFilter(inputUsername, profile)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
2021-08-05 11:17:07 +07:00
|
|
|
return nil, fmt.Errorf("unable to create group filter for user '%s'. Cause: %w", inputUsername, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Search for the given username.
|
2019-04-25 04:52:08 +07:00
|
|
|
searchGroupRequest := ldap.NewSearchRequest(
|
2021-04-12 08:10:50 +07:00
|
|
|
p.groupsBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
2021-08-05 11:17:07 +07:00
|
|
|
0, 0, false, groupsFilter, p.groupsAttributes, nil,
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
sr, err := conn.Search(searchGroupRequest)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-08-05 11:17:07 +07:00
|
|
|
return nil, fmt.Errorf("unable to retrieve groups of user '%s'. Cause: %w", inputUsername, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
groups := make([]string, 0)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
for _, res := range sr.Entries {
|
2020-02-28 05:21:07 +07:00
|
|
|
if len(res.Attributes) == 0 {
|
2021-11-23 16:45:38 +07:00
|
|
|
p.log.Warningf("No groups retrieved from LDAP for user %s", inputUsername)
|
2020-02-28 05:21:07 +07:00
|
|
|
break
|
|
|
|
}
|
2021-08-05 11:17:07 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Append all values of the document. Normally there should be only one per document.
|
2019-04-25 04:52:08 +07:00
|
|
|
groups = append(groups, res.Attributes[0].Values...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &UserDetails{
|
2020-06-19 17:50:21 +07:00
|
|
|
Username: profile.Username,
|
|
|
|
DisplayName: profile.DisplayName,
|
|
|
|
Emails: profile.Emails,
|
|
|
|
Groups: groups,
|
2019-04-25 04:52:08 +07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePassword update the password of the given user.
|
2020-03-31 05:36:04 +07:00
|
|
|
func (p *LDAPUserProvider) UpdatePassword(inputUsername string, newPassword string) error {
|
2021-01-04 17:28:55 +07:00
|
|
|
conn, err := p.connect(p.configuration.User, p.configuration.Password)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
2021-08-05 11:17:07 +07:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
defer conn.Close()
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
profile, err := p.getUserProfile(conn, inputUsername)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-05 11:17:07 +07:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-07-06 16:13:17 +07:00
|
|
|
switch {
|
|
|
|
case p.supportExtensionPasswdModify:
|
|
|
|
modifyRequest := ldap.NewPasswordModifyRequest(
|
|
|
|
profile.DN,
|
|
|
|
"",
|
|
|
|
newPassword,
|
|
|
|
)
|
|
|
|
|
2022-03-17 11:02:54 +07:00
|
|
|
_, err = conn.PasswordModify(modifyRequest)
|
2021-07-06 16:13:17 +07:00
|
|
|
case p.configuration.Implementation == schema.LDAPImplementationActiveDirectory:
|
|
|
|
modifyRequest := ldap.NewModifyRequest(profile.DN, nil)
|
2020-11-27 16:59:22 +07:00
|
|
|
utf16 := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
|
|
|
|
// 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, _ := utf16.NewEncoder().String(fmt.Sprintf("\"%s\"", newPassword))
|
|
|
|
modifyRequest.Replace("unicodePwd", []string{pwdEncoded})
|
2021-07-06 16:13:17 +07:00
|
|
|
|
|
|
|
err = conn.Modify(modifyRequest)
|
2020-11-27 16:59:22 +07:00
|
|
|
default:
|
2021-07-06 16:13:17 +07:00
|
|
|
modifyRequest := ldap.NewModifyRequest(profile.DN, nil)
|
2020-11-27 16:59:22 +07:00
|
|
|
modifyRequest.Replace("userPassword", []string{newPassword})
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-07-06 16:13:17 +07:00
|
|
|
err = conn.Modify(modifyRequest)
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-05 11:17:07 +07:00
|
|
|
return fmt.Errorf("unable to update password. Cause: %w", err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|