2019-04-25 04:52:08 +07:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2019-12-06 15:15:54 +07:00
|
|
|
"crypto/tls"
|
2019-04-25 04:52:08 +07:00
|
|
|
"fmt"
|
2019-12-06 15:15:54 +07:00
|
|
|
"net/url"
|
2019-04-25 04:52:08 +07:00
|
|
|
"strings"
|
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/logging"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// LDAPUserProvider is a provider using a LDAP or AD as a user database.
|
|
|
|
type LDAPUserProvider struct {
|
|
|
|
configuration schema.LDAPAuthenticationBackendConfiguration
|
2019-12-06 15:15:54 +07:00
|
|
|
|
|
|
|
connectionFactory LDAPConnectionFactory
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:15:54 +07:00
|
|
|
// NewLDAPUserProvider creates a new instance of LDAPUserProvider.
|
|
|
|
func NewLDAPUserProvider(configuration schema.LDAPAuthenticationBackendConfiguration) *LDAPUserProvider {
|
|
|
|
return &LDAPUserProvider{
|
|
|
|
configuration: configuration,
|
|
|
|
connectionFactory: NewLDAPConnectionFactoryImpl(),
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2019-12-06 15:15:54 +07:00
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// NewLDAPUserProviderWithFactory creates a new instance of LDAPUserProvider with existing factory.
|
2019-12-06 15:15:54 +07:00
|
|
|
func NewLDAPUserProviderWithFactory(configuration schema.LDAPAuthenticationBackendConfiguration,
|
|
|
|
connectionFactory LDAPConnectionFactory) *LDAPUserProvider {
|
|
|
|
return &LDAPUserProvider{
|
|
|
|
configuration: configuration,
|
|
|
|
connectionFactory: connectionFactory,
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2019-12-06 15:15:54 +07:00
|
|
|
func (p *LDAPUserProvider) connect(userDN string, password string) (LDAPConnection, error) {
|
|
|
|
var newConnection LDAPConnection
|
|
|
|
|
|
|
|
url, err := url.Parse(p.configuration.URL)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2019-12-06 15:15:54 +07:00
|
|
|
return nil, fmt.Errorf("Unable to parse URL to LDAP: %s", url)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:15:54 +07:00
|
|
|
if url.Scheme == "ldaps" {
|
2020-02-28 05:21:07 +07:00
|
|
|
logging.Logger().Trace("LDAP client starts a TLS session")
|
2019-12-06 15:15:54 +07:00
|
|
|
conn, err := p.connectionFactory.DialTLS("tcp", url.Host, &tls.Config{
|
2020-05-05 02:39:25 +07:00
|
|
|
InsecureSkipVerify: p.configuration.SkipVerify, //nolint:gosec // This is a configurable option, is desirable in some situations and is off by default
|
2019-12-06 15:15:54 +07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newConnection = conn
|
|
|
|
} else {
|
2020-02-28 05:21:07 +07:00
|
|
|
logging.Logger().Trace("LDAP client starts a session over raw TCP")
|
2019-12-06 15:15:54 +07:00
|
|
|
conn, err := p.connectionFactory.Dial("tcp", url.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newConnection = conn
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := newConnection.Bind(userDN, password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newConnection, 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) {
|
2019-04-25 04:52:08 +07:00
|
|
|
adminClient, err := p.connect(p.configuration.User, p.configuration.Password)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer adminClient.Close()
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
profile, err := p.getUserProfile(adminClient, inputUsername)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
conn, err := p.connect(profile.DN, password)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
2020-03-31 05:36:04 +07:00
|
|
|
return false, fmt.Errorf("Authentication of user %s failed. Cause: %s", inputUsername, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// OWASP recommends to escape some special characters.
|
2020-01-21 02:34:53 +07:00
|
|
|
// https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/LDAP_Injection_Prevention_Cheat_Sheet.md
|
2020-03-31 05:36:04 +07:00
|
|
|
const specialLDAPRunes = ",#+<>;\"="
|
2020-01-21 02:34:53 +07:00
|
|
|
|
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-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 {
|
|
|
|
DN string
|
|
|
|
Emails []string
|
|
|
|
Username string
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
func (p *LDAPUserProvider) resolveUsersFilter(userFilter string, inputUsername string) string {
|
|
|
|
inputUsername = p.ldapEscape(inputUsername)
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// We temporarily keep placeholder {0} for backward compatibility.
|
2020-03-31 05:36:04 +07:00
|
|
|
userFilter = strings.ReplaceAll(userFilter, "{0}", inputUsername)
|
|
|
|
|
|
|
|
// The {username} placeholder is equivalent to {0}, it's the new way, a named placeholder.
|
|
|
|
userFilter = strings.ReplaceAll(userFilter, "{input}", inputUsername)
|
|
|
|
|
|
|
|
// {username_attribute} and {mail_attribute} are replaced by the content of the attribute defined
|
|
|
|
// in configuration.
|
|
|
|
userFilter = strings.ReplaceAll(userFilter, "{username_attribute}", p.configuration.UsernameAttribute)
|
|
|
|
userFilter = strings.ReplaceAll(userFilter, "{mail_attribute}", p.configuration.MailAttribute)
|
|
|
|
return userFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LDAPUserProvider) getUserProfile(conn LDAPConnection, inputUsername string) (*ldapUserProfile, error) {
|
|
|
|
userFilter := p.resolveUsersFilter(p.configuration.UsersFilter, inputUsername)
|
|
|
|
logging.Logger().Tracef("Computed user filter is %s", userFilter)
|
|
|
|
|
2019-12-27 18:07:53 +07:00
|
|
|
baseDN := p.configuration.BaseDN
|
2019-12-19 10:21:55 +07:00
|
|
|
if p.configuration.AdditionalUsersDN != "" {
|
|
|
|
baseDN = p.configuration.AdditionalUsersDN + "," + baseDN
|
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
attributes := []string{"dn",
|
|
|
|
p.configuration.MailAttribute,
|
|
|
|
p.configuration.UsernameAttribute}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Search for the given username.
|
2019-04-25 04:52:08 +07:00
|
|
|
searchRequest := ldap.NewSearchRequest(
|
|
|
|
baseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
2020-03-15 14:10:25 +07:00
|
|
|
1, 0, false, userFilter, attributes, 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 {
|
2020-03-31 05:36:04 +07:00
|
|
|
return nil, fmt.Errorf("Cannot find user DN of user %s. Cause: %s", 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 {
|
2020-03-31 05:36:04 +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-03-15 14:10:25 +07:00
|
|
|
for _, attr := range sr.Entries[0].Attributes {
|
|
|
|
if attr.Name == p.configuration.MailAttribute {
|
|
|
|
userProfile.Emails = attr.Values
|
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 {
|
2020-03-31 05:36:04 +07:00
|
|
|
return nil, fmt.Errorf("User %s cannot have multiple value for attribute %s",
|
|
|
|
inputUsername, p.configuration.UsernameAttribute)
|
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 == "" {
|
2020-03-31 05:36:04 +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
|
|
|
}
|
|
|
|
|
2020-04-09 08:05:17 +07:00
|
|
|
func (p *LDAPUserProvider) resolveGroupsFilter(inputUsername string, profile *ldapUserProfile) (string, error) { //nolint:unparam
|
2020-03-31 05:36:04 +07:00
|
|
|
inputUsername = p.ldapEscape(inputUsername)
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// We temporarily keep placeholder {0} for backward compatibility.
|
2020-03-31 05:36:04 +07:00
|
|
|
groupFilter := strings.ReplaceAll(p.configuration.GroupsFilter, "{0}", inputUsername)
|
|
|
|
groupFilter = strings.ReplaceAll(groupFilter, "{input}", inputUsername)
|
|
|
|
if profile != nil {
|
2020-04-21 04:03:38 +07:00
|
|
|
// We temporarily keep placeholder {1} for backward compatibility.
|
2020-03-31 05:36:04 +07:00
|
|
|
groupFilter = strings.ReplaceAll(groupFilter, "{1}", ldap.EscapeFilter(profile.Username))
|
|
|
|
groupFilter = strings.ReplaceAll(groupFilter, "{username}", ldap.EscapeFilter(profile.Username))
|
|
|
|
groupFilter = strings.ReplaceAll(groupFilter, "{dn}", ldap.EscapeFilter(profile.DN))
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-03-31 05:36:04 +07:00
|
|
|
|
|
|
|
return groupFilter, 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 {
|
2020-03-31 05:36:04 +07:00
|
|
|
return nil, fmt.Errorf("Unable to create group filter for user %s. Cause: %s", inputUsername, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-03-31 05:36:04 +07:00
|
|
|
logging.Logger().Tracef("Computed groups filter is %s", groupsFilter)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2019-12-27 18:07:53 +07:00
|
|
|
groupBaseDN := p.configuration.BaseDN
|
2019-12-19 10:21:55 +07:00
|
|
|
if p.configuration.AdditionalGroupsDN != "" {
|
|
|
|
groupBaseDN = p.configuration.AdditionalGroupsDN + "," + groupBaseDN
|
|
|
|
}
|
2019-04-25 04:52:08 +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(
|
|
|
|
groupBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
|
|
|
|
0, 0, false, groupsFilter, []string{p.configuration.GroupNameAttribute}, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
sr, err := conn.Search(searchGroupRequest)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-03-31 05:36:04 +07:00
|
|
|
return nil, fmt.Errorf("Unable to retrieve groups of user %s. Cause: %s", inputUsername, err)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
groups := make([]string, 0)
|
|
|
|
for _, res := range sr.Entries {
|
2020-02-28 05:21:07 +07:00
|
|
|
if len(res.Attributes) == 0 {
|
2020-03-31 05:36:04 +07:00
|
|
|
logging.Logger().Warningf("No groups retrieved from LDAP for user %s", inputUsername)
|
2020-02-28 05:21:07 +07:00
|
|
|
break
|
|
|
|
}
|
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-03-15 14:10:25 +07:00
|
|
|
Username: profile.Username,
|
|
|
|
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 {
|
2019-04-25 04:52:08 +07:00
|
|
|
client, err := p.connect(p.configuration.User, p.configuration.Password)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to update password. Cause: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
profile, err := p.getUserProfile(client, inputUsername)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to update password. Cause: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:10:25 +07:00
|
|
|
modifyRequest := ldap.NewModifyRequest(profile.DN, nil)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
modifyRequest.Replace("userPassword", []string{newPassword})
|
|
|
|
|
|
|
|
err = client.Modify(modifyRequest)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to update password. Cause: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|