2022-03-16 22:57:36 -04:00
|
|
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2022-12-09 16:08:33 -05:00
|
|
|
"errors"
|
2022-03-16 22:57:36 -04:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ldap "github.com/go-ldap/ldap/v3"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
|
|
|
"github.com/minio/minio/internal/auth"
|
2024-05-24 19:05:23 -04:00
|
|
|
xldap "github.com/minio/pkg/v3/ldap"
|
2022-03-16 22:57:36 -04:00
|
|
|
)
|
|
|
|
|
2024-04-04 14:36:18 -04:00
|
|
|
// LookupUserDN searches for the full DN and groups of a given short/login
|
|
|
|
// username.
|
2024-05-24 19:05:23 -04:00
|
|
|
func (l *Config) LookupUserDN(username string) (*xldap.DNSearchResult, []string, error) {
|
2022-10-08 01:12:36 -04:00
|
|
|
conn, err := l.LDAP.Connect()
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
2022-10-08 01:12:36 -04:00
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup user DN
|
2024-05-24 19:05:23 -04:00
|
|
|
lookupRes, err := l.LDAP.LookupUsername(conn, username)
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
errRet := fmt.Errorf("Unable to find user DN: %w", err)
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, errRet
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
2024-05-25 09:43:06 -04:00
|
|
|
groups, err := l.LDAP.SearchForUserGroups(conn, username, lookupRes.ActualDN)
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
2024-05-24 19:05:23 -04:00
|
|
|
return lookupRes, groups, nil
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
2024-03-28 02:45:26 -04:00
|
|
|
// GetValidatedDNForUsername checks if the given username exists in the LDAP directory.
|
2022-12-09 16:08:33 -05:00
|
|
|
// The given username could be just the short "login" username or the full DN.
|
2024-03-28 02:45:26 -04:00
|
|
|
//
|
|
|
|
// When the username/DN is found, the full DN returned by the **server** is
|
|
|
|
// returned, otherwise the returned string is empty. The value returned here is
|
|
|
|
// the value sent by the LDAP server and is used in minio as the server performs
|
|
|
|
// LDAP specific normalization (including Unicode normalization).
|
|
|
|
//
|
|
|
|
// If the user is not found, err = nil, otherwise, err != nil.
|
2024-05-24 19:05:23 -04:00
|
|
|
func (l *Config) GetValidatedDNForUsername(username string) (*xldap.DNSearchResult, error) {
|
2022-12-09 16:08:33 -05:00
|
|
|
conn, err := l.LDAP.Connect()
|
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, err
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, err
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the passed in username is a valid DN.
|
2024-04-04 14:36:18 -04:00
|
|
|
if !l.ParsesAsDN(username) {
|
|
|
|
// We consider it as a login username and attempt to check it exists in
|
|
|
|
// the directory.
|
2024-05-24 19:05:23 -04:00
|
|
|
bindDN, err := l.LDAP.LookupUsername(conn, username)
|
2022-12-09 16:08:33 -05:00
|
|
|
if err != nil {
|
2024-04-25 11:50:16 -04:00
|
|
|
if strings.Contains(err.Error(), "User DN not found for") {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, fmt.Errorf("Unable to find user DN: %w", err)
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
return bindDN, nil
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:50:16 -04:00
|
|
|
// Since the username parses as a valid DN, check that it exists and is
|
|
|
|
// under a configured base DN in the LDAP directory.
|
|
|
|
validDN, isUnderBaseDN, err := l.GetValidatedUserDN(conn, username)
|
|
|
|
if err == nil && !isUnderBaseDN {
|
2024-06-22 02:35:35 -04:00
|
|
|
// Not under any configured base DN, so treat as not found.
|
|
|
|
return nil, nil
|
2024-04-25 11:50:16 -04:00
|
|
|
}
|
|
|
|
return validDN, err
|
2024-04-18 11:15:02 -04:00
|
|
|
}
|
2024-04-04 14:36:18 -04:00
|
|
|
|
2024-04-25 11:50:16 -04:00
|
|
|
// GetValidatedUserDN validates the given user DN. Will error out if conn is nil. The returned
|
|
|
|
// boolean is true iff the user DN is found under one of the LDAP user base DNs.
|
2024-05-24 19:05:23 -04:00
|
|
|
func (l *Config) GetValidatedUserDN(conn *ldap.Conn, userDN string) (*xldap.DNSearchResult, bool, error) {
|
|
|
|
return l.GetValidatedDNUnderBaseDN(conn, userDN,
|
|
|
|
l.LDAP.GetUserDNSearchBaseDistNames(), l.LDAP.GetUserDNAttributesList())
|
2024-04-18 11:15:02 -04:00
|
|
|
}
|
2024-04-04 14:36:18 -04:00
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// GetValidatedGroupDN validates the given group DN. If conn is nil, creates a
|
2024-04-25 11:50:16 -04:00
|
|
|
// connection. The returned boolean is true iff the group DN is found under one
|
|
|
|
// of the configured LDAP base DNs.
|
2024-05-24 19:05:23 -04:00
|
|
|
func (l *Config) GetValidatedGroupDN(conn *ldap.Conn, groupDN string) (*xldap.DNSearchResult, bool, error) {
|
2024-04-18 11:15:02 -04:00
|
|
|
if conn == nil {
|
|
|
|
var err error
|
|
|
|
conn, err = l.LDAP.Connect()
|
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, false, err
|
2024-04-18 11:15:02 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
2024-04-04 14:36:18 -04:00
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// Bind to the lookup user account
|
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, false, err
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 19:05:23 -04:00
|
|
|
return l.GetValidatedDNUnderBaseDN(conn, groupDN,
|
|
|
|
l.LDAP.GetGroupSearchBaseDistNames(), nil)
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
2024-05-24 19:05:23 -04:00
|
|
|
// GetValidatedDNUnderBaseDN checks if the given DN exists in the LDAP
|
|
|
|
// directory.
|
|
|
|
//
|
|
|
|
// The `NormDN` value returned here in the search result may not be equal to the
|
|
|
|
// input DN, as LDAP equality is not a simple Golang string equality. However,
|
|
|
|
// we assume the value returned by the LDAP server is canonical. Additionally,
|
|
|
|
// the attribute type names in the DN are lower-cased.
|
2024-04-25 11:50:16 -04:00
|
|
|
//
|
|
|
|
// Return values:
|
|
|
|
//
|
2024-05-24 19:05:23 -04:00
|
|
|
// If the DN is found, the normalized (string) value and any requested
|
|
|
|
// attributes are returned and error is nil.
|
2024-03-28 02:45:26 -04:00
|
|
|
//
|
2024-05-24 19:05:23 -04:00
|
|
|
// If the DN is not found, a nil result and error are returned.
|
2024-04-25 11:50:16 -04:00
|
|
|
//
|
|
|
|
// The returned boolean is true iff the DN is found under one of the LDAP
|
|
|
|
// subtrees listed in `baseDNList`.
|
2024-05-24 19:05:23 -04:00
|
|
|
func (l *Config) GetValidatedDNUnderBaseDN(conn *ldap.Conn, dn string, baseDNList []xldap.BaseDNInfo, attrs []string) (*xldap.DNSearchResult, bool, error) {
|
2024-04-18 11:15:02 -04:00
|
|
|
if len(baseDNList) == 0 {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, false, errors.New("no Base DNs given")
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// Check that DN exists in the LDAP directory.
|
2024-05-24 19:05:23 -04:00
|
|
|
searchRes, err := xldap.LookupDN(conn, dn, attrs)
|
2024-04-04 14:36:18 -04:00
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, false, fmt.Errorf("Error looking up DN %s: %w", dn, err)
|
2024-04-04 14:36:18 -04:00
|
|
|
}
|
2024-05-24 19:05:23 -04:00
|
|
|
if searchRes == nil {
|
|
|
|
return nil, false, nil
|
2024-04-04 14:36:18 -04:00
|
|
|
}
|
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// This will not return an error as the argument is validated to be a DN.
|
2024-05-24 19:05:23 -04:00
|
|
|
pdn, _ := ldap.ParseDN(searchRes.NormDN)
|
2024-04-04 14:36:18 -04:00
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// Check that the DN is under a configured base DN in the LDAP
|
2024-04-04 14:36:18 -04:00
|
|
|
// directory.
|
2024-04-18 11:15:02 -04:00
|
|
|
for _, baseDN := range baseDNList {
|
|
|
|
if baseDN.Parsed.AncestorOf(pdn) {
|
2024-05-24 19:05:23 -04:00
|
|
|
return searchRes, true, nil
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 11:50:16 -04:00
|
|
|
|
|
|
|
// Not under any configured base DN so return false.
|
2024-05-24 19:05:23 -04:00
|
|
|
return searchRes, false, nil
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:57:36 -04:00
|
|
|
// Bind - binds to ldap, searches LDAP and returns the distinguished name of the
|
|
|
|
// user and the list of groups.
|
2024-05-24 19:05:23 -04:00
|
|
|
func (l *Config) Bind(username, password string) (*xldap.DNSearchResult, []string, error) {
|
2022-10-08 01:12:36 -04:00
|
|
|
conn, err := l.LDAP.Connect()
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
2022-10-08 01:12:36 -04:00
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup user DN
|
2024-05-24 19:05:23 -04:00
|
|
|
lookupResult, err := l.LDAP.LookupUsername(conn, username)
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
errRet := fmt.Errorf("Unable to find user DN: %w", err)
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, errRet
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate the user credentials.
|
2024-05-25 09:43:06 -04:00
|
|
|
err = conn.Bind(lookupResult.ActualDN, password)
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
2024-05-25 09:43:06 -04:00
|
|
|
errRet := fmt.Errorf("LDAP auth failed for DN %s: %w", lookupResult.ActualDN, err)
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, errRet
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bind to the lookup user account again to perform group search.
|
2022-10-08 01:12:36 -04:00
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// User groups lookup.
|
2024-05-25 09:43:06 -04:00
|
|
|
groups, err := l.LDAP.SearchForUserGroups(conn, username, lookupResult.ActualDN)
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
2024-05-24 19:05:23 -04:00
|
|
|
return nil, nil, err
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
2024-05-24 19:05:23 -04:00
|
|
|
return lookupResult, groups, nil
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetExpiryDuration - return parsed expiry duration.
|
|
|
|
func (l Config) GetExpiryDuration(dsecs string) (time.Duration, error) {
|
|
|
|
if dsecs == "" {
|
|
|
|
return l.stsExpiryDuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := strconv.Atoi(dsecs)
|
|
|
|
if err != nil {
|
|
|
|
return 0, auth.ErrInvalidDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
dur := time.Duration(d) * time.Second
|
|
|
|
|
|
|
|
if dur < minLDAPExpiry || dur > maxLDAPExpiry {
|
|
|
|
return 0, auth.ErrInvalidDuration
|
|
|
|
}
|
|
|
|
return dur, nil
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:36:18 -04:00
|
|
|
// ParsesAsDN determines if the given string could be a valid DN based on
|
|
|
|
// parsing alone.
|
|
|
|
func (l Config) ParsesAsDN(dn string) bool {
|
|
|
|
_, err := ldap.ParseDN(dn)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:57:36 -04:00
|
|
|
// IsLDAPUserDN determines if the given string could be a user DN from LDAP.
|
|
|
|
func (l Config) IsLDAPUserDN(user string) bool {
|
2024-04-04 14:36:18 -04:00
|
|
|
udn, err := ldap.ParseDN(user)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-05-24 19:05:23 -04:00
|
|
|
for _, baseDN := range l.LDAP.GetUserDNSearchBaseDistNames() {
|
2024-04-04 14:36:18 -04:00
|
|
|
if baseDN.Parsed.AncestorOf(udn) {
|
2022-03-16 22:57:36 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:35:09 -05:00
|
|
|
// IsLDAPGroupDN determines if the given string could be a group DN from LDAP.
|
2024-04-04 14:36:18 -04:00
|
|
|
func (l Config) IsLDAPGroupDN(group string) bool {
|
|
|
|
gdn, err := ldap.ParseDN(group)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-05-24 19:05:23 -04:00
|
|
|
for _, baseDN := range l.LDAP.GetGroupSearchBaseDistNames() {
|
2024-04-04 14:36:18 -04:00
|
|
|
if baseDN.Parsed.AncestorOf(gdn) {
|
2022-11-07 17:35:09 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:57:36 -04:00
|
|
|
// GetNonEligibleUserDistNames - find user accounts (DNs) that are no longer
|
|
|
|
// present in the LDAP server or do not meet filter criteria anymore
|
|
|
|
func (l *Config) GetNonEligibleUserDistNames(userDistNames []string) ([]string, error) {
|
2022-10-08 01:12:36 -04:00
|
|
|
conn, err := l.LDAP.Connect()
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
2022-10-08 01:12:36 -04:00
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2022-03-16 22:57:36 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-25 09:43:06 -04:00
|
|
|
// Evaluate the filter again with generic wildcard instead of specific values
|
2022-10-08 01:12:36 -04:00
|
|
|
filter := strings.ReplaceAll(l.LDAP.UserDNSearchFilter, "%s", "*")
|
2022-03-16 22:57:36 -04:00
|
|
|
|
|
|
|
nonExistentUsers := []string{}
|
|
|
|
for _, dn := range userDistNames {
|
|
|
|
searchRequest := ldap.NewSearchRequest(
|
|
|
|
dn,
|
|
|
|
ldap.ScopeBaseObject, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
|
filter,
|
|
|
|
[]string{}, // only need DN, so pass no attributes here
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
searchResult, err := conn.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
// Object does not exist error?
|
|
|
|
if ldap.IsErrorWithCode(err, 32) {
|
2024-05-25 09:43:06 -04:00
|
|
|
ndn, err := ldap.ParseDN(dn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nonExistentUsers = append(nonExistentUsers, ndn.String())
|
2022-03-16 22:57:36 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(searchResult.Entries) == 0 {
|
|
|
|
// DN was not found - this means this user account is
|
|
|
|
// expired.
|
2024-05-25 09:43:06 -04:00
|
|
|
ndn, err := ldap.ParseDN(dn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nonExistentUsers = append(nonExistentUsers, ndn.String())
|
2022-03-16 22:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nonExistentUsers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupGroupMemberships - for each DN finds the set of LDAP groups they are a
|
|
|
|
// member of.
|
|
|
|
func (l *Config) LookupGroupMemberships(userDistNames []string, userDNToUsernameMap map[string]string) (map[string]set.StringSet, error) {
|
2022-10-08 01:12:36 -04:00
|
|
|
conn, err := l.LDAP.Connect()
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
2022-10-08 01:12:36 -04:00
|
|
|
if err = l.LDAP.LookupBind(conn); err != nil {
|
2022-03-16 22:57:36 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make(map[string]set.StringSet, len(userDistNames))
|
|
|
|
for _, userDistName := range userDistNames {
|
|
|
|
username := userDNToUsernameMap[userDistName]
|
2022-10-08 01:12:36 -04:00
|
|
|
groups, err := l.LDAP.SearchForUserGroups(conn, username, userDistName)
|
2022-03-16 22:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res[userDistName] = set.CreateStringSet(groups...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|