2016-07-26 01:30:28 +05:30
|
|
|
// Package ldap implements strategies for authenticating using the LDAP protocol.
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2017-03-09 00:03:19 +05:30
|
|
|
"context"
|
2016-10-21 05:47:43 +05:30
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
2016-07-26 01:30:28 +05:30
|
|
|
"fmt"
|
2016-10-21 05:47:43 +05:30
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
"gopkg.in/ldap.v2"
|
|
|
|
|
2017-07-26 02:15:17 +05:30
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-03 12:14:44 +05:30
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
// Config holds the configuration parameters for the LDAP connector. The LDAP
|
|
|
|
// connectors require executing two queries, the first to find the user based on
|
|
|
|
// the username and password given to the connector. The second to use the user
|
|
|
|
// entry to search for groups.
|
|
|
|
//
|
|
|
|
// An example config:
|
|
|
|
//
|
|
|
|
// type: ldap
|
|
|
|
// config:
|
|
|
|
// host: ldap.example.com:636
|
|
|
|
// # The following field is required if using port 389.
|
|
|
|
// # insecureNoSSL: true
|
|
|
|
// rootCA: /etc/dex/ldap.ca
|
|
|
|
// bindDN: uid=seviceaccount,cn=users,dc=example,dc=com
|
|
|
|
// bindPW: password
|
|
|
|
// userSearch:
|
|
|
|
// # Would translate to the query "(&(objectClass=person)(uid=<username>))"
|
|
|
|
// baseDN: cn=users,dc=example,dc=com
|
|
|
|
// filter: "(objectClass=person)"
|
|
|
|
// username: uid
|
|
|
|
// idAttr: uid
|
|
|
|
// emailAttr: mail
|
|
|
|
// nameAttr: name
|
|
|
|
// groupSearch:
|
|
|
|
// # Would translate to the query "(&(objectClass=group)(member=<user uid>))"
|
|
|
|
// baseDN: cn=groups,dc=example,dc=com
|
|
|
|
// filter: "(objectClass=group)"
|
|
|
|
// userAttr: uid
|
2016-11-19 02:46:50 +05:30
|
|
|
// # Use if full DN is needed and not available as any other attribute
|
|
|
|
// # Will only work if "DN" attribute does not exist in the record
|
|
|
|
// # userAttr: DN
|
2016-10-21 05:47:43 +05:30
|
|
|
// groupAttr: member
|
|
|
|
// nameAttr: name
|
|
|
|
//
|
2016-07-26 01:30:28 +05:30
|
|
|
type Config struct {
|
2016-10-21 05:47:43 +05:30
|
|
|
// The host and optional port of the LDAP server. If port isn't supplied, it will be
|
|
|
|
// guessed based on the TLS configuration. 389 or 636.
|
2016-11-04 03:02:23 +05:30
|
|
|
Host string `json:"host"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Required if LDAP host does not use TLS.
|
2016-11-04 03:02:23 +05:30
|
|
|
InsecureNoSSL bool `json:"insecureNoSSL"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
// Don't verify the CA.
|
|
|
|
InsecureSkipVerify bool `json:"insecureSkipVerify"`
|
|
|
|
|
2017-04-13 02:43:34 +05:30
|
|
|
// Connect to the insecure port then issue a StartTLS command to negotiate a
|
|
|
|
// secure connection. If unsupplied secure connections will use the LDAPS
|
|
|
|
// protocol.
|
|
|
|
StartTLS bool `json:"startTLS"`
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
// Path to a trusted root certificate file.
|
2016-11-04 03:02:23 +05:30
|
|
|
RootCA string `json:"rootCA"`
|
2018-09-22 09:45:11 +05:30
|
|
|
// Path to a client cert file generated by rootCA.
|
|
|
|
ClientCert string `json:"clientCert"`
|
|
|
|
// Path to a client private key file generated by rootCA.
|
|
|
|
ClientKey string `json:"clientKey"`
|
2016-11-04 04:58:23 +05:30
|
|
|
// Base64 encoded PEM data containing root CAs.
|
|
|
|
RootCAData []byte `json:"rootCAData"`
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
// BindDN and BindPW for an application service account. The connector uses these
|
|
|
|
// credentials to search for users and groups.
|
2016-11-04 03:02:23 +05:30
|
|
|
BindDN string `json:"bindDN"`
|
|
|
|
BindPW string `json:"bindPW"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
2017-11-07 14:58:21 +05:30
|
|
|
// UsernamePrompt allows users to override the username attribute (displayed
|
|
|
|
// in the username/password prompt). If unset, the handler will use
|
|
|
|
// "Username".
|
|
|
|
UsernamePrompt string `json:"usernamePrompt"`
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
// User entry search configuration.
|
|
|
|
UserSearch struct {
|
2018-01-14 18:04:45 +05:30
|
|
|
// BaseDN to start the search from. For example "cn=users,dc=example,dc=com"
|
2016-11-04 03:02:23 +05:30
|
|
|
BaseDN string `json:"baseDN"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Optional filter to apply when searching the directory. For example "(objectClass=person)"
|
2016-11-04 03:02:23 +05:30
|
|
|
Filter string `json:"filter"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Attribute to match against the inputted username. This will be translated and combined
|
|
|
|
// with the other filter as "(<attr>=<username>)".
|
2016-11-04 03:02:23 +05:30
|
|
|
Username string `json:"username"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Can either be:
|
|
|
|
// * "sub" - search the whole sub tree
|
|
|
|
// * "one" - only search one level
|
2016-11-04 03:02:23 +05:30
|
|
|
Scope string `json:"scope"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// A mapping of attributes on the user entry to claims.
|
2016-11-04 03:02:23 +05:30
|
|
|
IDAttr string `json:"idAttr"` // Defaults to "uid"
|
|
|
|
EmailAttr string `json:"emailAttr"` // Defaults to "mail"
|
|
|
|
NameAttr string `json:"nameAttr"` // No default.
|
|
|
|
} `json:"userSearch"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Group search configuration.
|
|
|
|
GroupSearch struct {
|
2018-01-14 18:04:45 +05:30
|
|
|
// BaseDN to start the search from. For example "cn=groups,dc=example,dc=com"
|
2016-11-04 03:02:23 +05:30
|
|
|
BaseDN string `json:"baseDN"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Optional filter to apply when searching the directory. For example "(objectClass=posixGroup)"
|
2016-11-04 03:02:23 +05:30
|
|
|
Filter string `json:"filter"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
2016-11-04 03:02:23 +05:30
|
|
|
Scope string `json:"scope"` // Defaults to "sub"
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// These two fields are use to match a user to a group.
|
|
|
|
//
|
|
|
|
// It adds an additional requirement to the filter that an attribute in the group
|
|
|
|
// match the user's attribute value. For example that the "members" attribute of
|
|
|
|
// a group matches the "uid" of the user. The exact filter being added is:
|
|
|
|
//
|
|
|
|
// (<groupAttr>=<userAttr value>)
|
|
|
|
//
|
2016-11-04 03:02:23 +05:30
|
|
|
UserAttr string `json:"userAttr"`
|
|
|
|
GroupAttr string `json:"groupAttr"`
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// The attribute of the group that represents its name.
|
2016-11-04 03:02:23 +05:30
|
|
|
NameAttr string `json:"nameAttr"`
|
|
|
|
} `json:"groupSearch"`
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
|
2017-08-11 22:47:30 +05:30
|
|
|
func scopeString(i int) string {
|
|
|
|
switch i {
|
|
|
|
case ldap.ScopeBaseObject:
|
|
|
|
return "base"
|
|
|
|
case ldap.ScopeSingleLevel:
|
|
|
|
return "one"
|
|
|
|
case ldap.ScopeWholeSubtree:
|
|
|
|
return "sub"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
func parseScope(s string) (int, bool) {
|
|
|
|
// NOTE(ericchiang): ScopeBaseObject doesn't really make sense for us because we
|
|
|
|
// never know the user's or group's DN.
|
|
|
|
switch s {
|
|
|
|
case "", "sub":
|
|
|
|
return ldap.ScopeWholeSubtree, true
|
|
|
|
case "one":
|
|
|
|
return ldap.ScopeSingleLevel, true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
// Open returns an authentication strategy using LDAP.
|
2017-10-21 20:24:54 +05:30
|
|
|
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
|
2016-11-23 05:05:46 +05:30
|
|
|
conn, err := c.OpenConnector(logger)
|
2016-11-04 04:58:23 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return connector.Connector(conn), nil
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
type refreshData struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Entry ldap.Entry `json:"entry"`
|
|
|
|
}
|
|
|
|
|
2016-11-04 04:58:23 +05:30
|
|
|
// OpenConnector is the same as Open but returns a type with all implemented connector interfaces.
|
2016-11-23 05:05:46 +05:30
|
|
|
func (c *Config) OpenConnector(logger logrus.FieldLogger) (interface {
|
2016-11-04 04:58:23 +05:30
|
|
|
connector.Connector
|
|
|
|
connector.PasswordConnector
|
2016-11-19 03:10:41 +05:30
|
|
|
connector.RefreshConnector
|
2016-11-04 04:58:23 +05:30
|
|
|
}, error) {
|
2017-04-11 03:32:40 +05:30
|
|
|
return c.openConnector(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) openConnector(logger logrus.FieldLogger) (*ldapConnector, error) {
|
2016-11-04 04:58:23 +05:30
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
requiredFields := []struct {
|
|
|
|
name string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{"host", c.Host},
|
|
|
|
{"userSearch.baseDN", c.UserSearch.BaseDN},
|
|
|
|
{"userSearch.username", c.UserSearch.Username},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range requiredFields {
|
|
|
|
if field.val == "" {
|
|
|
|
return nil, fmt.Errorf("ldap: missing required field %q", field.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
host string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if host, _, err = net.SplitHostPort(c.Host); err != nil {
|
|
|
|
host = c.Host
|
|
|
|
if c.InsecureNoSSL {
|
|
|
|
c.Host = c.Host + ":389"
|
|
|
|
} else {
|
|
|
|
c.Host = c.Host + ":636"
|
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
tlsConfig := &tls.Config{ServerName: host, InsecureSkipVerify: c.InsecureSkipVerify}
|
2016-11-04 04:58:23 +05:30
|
|
|
if c.RootCA != "" || len(c.RootCAData) != 0 {
|
|
|
|
data := c.RootCAData
|
|
|
|
if len(data) == 0 {
|
|
|
|
var err error
|
|
|
|
if data, err = ioutil.ReadFile(c.RootCA); err != nil {
|
|
|
|
return nil, fmt.Errorf("ldap: read ca file: %v", err)
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
rootCAs := x509.NewCertPool()
|
|
|
|
if !rootCAs.AppendCertsFromPEM(data) {
|
|
|
|
return nil, fmt.Errorf("ldap: no certs found in ca file")
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = rootCAs
|
|
|
|
}
|
2018-09-22 09:45:11 +05:30
|
|
|
|
|
|
|
if c.ClientKey != "" && c.ClientCert != "" {
|
|
|
|
cert, err := tls.LoadX509KeyPair(c.ClientCert, c.ClientKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ldap: load client cert failed: %v", err)
|
|
|
|
}
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
userSearchScope, ok := parseScope(c.UserSearch.Scope)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("userSearch.Scope unknown value %q", c.UserSearch.Scope)
|
|
|
|
}
|
|
|
|
groupSearchScope, ok := parseScope(c.GroupSearch.Scope)
|
|
|
|
if !ok {
|
2017-04-14 09:00:05 +05:30
|
|
|
return nil, fmt.Errorf("groupSearch.Scope unknown value %q", c.GroupSearch.Scope)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2016-11-23 05:05:46 +05:30
|
|
|
return &ldapConnector{*c, userSearchScope, groupSearchScope, tlsConfig, logger}, nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type ldapConnector struct {
|
|
|
|
Config
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
userSearchScope int
|
|
|
|
groupSearchScope int
|
|
|
|
|
|
|
|
tlsConfig *tls.Config
|
2016-11-23 05:05:46 +05:30
|
|
|
|
|
|
|
logger logrus.FieldLogger
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
var (
|
|
|
|
_ connector.PasswordConnector = (*ldapConnector)(nil)
|
|
|
|
_ connector.RefreshConnector = (*ldapConnector)(nil)
|
|
|
|
)
|
2016-08-03 09:44:24 +05:30
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
// do initializes a connection to the LDAP directory and passes it to the
|
|
|
|
// provided function. It then performs appropriate teardown or reuse before
|
|
|
|
// returning.
|
2016-11-19 03:10:41 +05:30
|
|
|
func (c *ldapConnector) do(ctx context.Context, f func(c *ldap.Conn) error) error {
|
|
|
|
// TODO(ericchiang): support context here
|
2016-10-21 05:47:43 +05:30
|
|
|
var (
|
|
|
|
conn *ldap.Conn
|
|
|
|
err error
|
|
|
|
)
|
2017-04-13 02:43:34 +05:30
|
|
|
switch {
|
|
|
|
case c.InsecureNoSSL:
|
|
|
|
conn, err = ldap.Dial("tcp", c.Host)
|
|
|
|
case c.StartTLS:
|
2016-10-21 05:47:43 +05:30
|
|
|
conn, err = ldap.Dial("tcp", c.Host)
|
2017-04-13 02:43:34 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
if err := conn.StartTLS(c.tlsConfig); err != nil {
|
|
|
|
return fmt.Errorf("start TLS failed: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
2016-10-21 05:47:43 +05:30
|
|
|
conn, err = ldap.DialTLS("tcp", c.Host, c.tlsConfig)
|
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
// If bindDN and bindPW are empty this will default to an anonymous bind.
|
|
|
|
if err := conn.Bind(c.BindDN, c.BindPW); err != nil {
|
2018-11-13 14:10:40 +05:30
|
|
|
if c.BindDN == "" && c.BindPW == "" {
|
|
|
|
return fmt.Errorf("ldap: initial anonymous bind failed: %v", err)
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
return fmt.Errorf("ldap: initial bind for user %q failed: %v", c.BindDN, err)
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
return f(conn)
|
|
|
|
}
|
|
|
|
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
func getAttrs(e ldap.Entry, name string) []string {
|
2016-10-21 05:47:43 +05:30
|
|
|
for _, a := range e.Attributes {
|
|
|
|
if a.Name != name {
|
|
|
|
continue
|
|
|
|
}
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
return a.Values
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
2016-11-19 02:46:50 +05:30
|
|
|
if name == "DN" {
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
return []string{e.DN}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAttr(e ldap.Entry, name string) string {
|
|
|
|
if a := getAttrs(e, name); len(a) > 0 {
|
|
|
|
return a[0]
|
2016-11-19 02:46:50 +05:30
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
func (c *ldapConnector) identityFromEntry(user ldap.Entry) (ident connector.Identity, err error) {
|
|
|
|
// If we're missing any attributes, such as email or ID, we want to report
|
|
|
|
// an error rather than continuing.
|
|
|
|
missing := []string{}
|
|
|
|
|
|
|
|
// Fill the identity struct using the attributes from the user entry.
|
|
|
|
if ident.UserID = getAttr(user, c.UserSearch.IDAttr); ident.UserID == "" {
|
|
|
|
missing = append(missing, c.UserSearch.IDAttr)
|
|
|
|
}
|
|
|
|
if ident.Email = getAttr(user, c.UserSearch.EmailAttr); ident.Email == "" {
|
|
|
|
missing = append(missing, c.UserSearch.EmailAttr)
|
|
|
|
}
|
2016-12-10 02:52:19 +05:30
|
|
|
// TODO(ericchiang): Let this value be set from an attribute.
|
|
|
|
ident.EmailVerified = true
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
if c.UserSearch.NameAttr != "" {
|
|
|
|
if ident.Username = getAttr(user, c.UserSearch.NameAttr); ident.Username == "" {
|
|
|
|
missing = append(missing, c.UserSearch.NameAttr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missing) != 0 {
|
|
|
|
err := fmt.Errorf("ldap: entry %q missing following required attribute(s): %q", user.DN, missing)
|
|
|
|
return connector.Identity{}, err
|
|
|
|
}
|
|
|
|
return ident, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ldapConnector) userEntry(conn *ldap.Conn, username string) (user ldap.Entry, found bool, err error) {
|
2016-10-21 05:47:43 +05:30
|
|
|
|
2016-11-19 04:46:09 +05:30
|
|
|
filter := fmt.Sprintf("(%s=%s)", c.UserSearch.Username, ldap.EscapeFilter(username))
|
2016-10-21 05:47:43 +05:30
|
|
|
if c.UserSearch.Filter != "" {
|
|
|
|
filter = fmt.Sprintf("(&%s%s)", c.UserSearch.Filter, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial search.
|
|
|
|
req := &ldap.SearchRequest{
|
|
|
|
BaseDN: c.UserSearch.BaseDN,
|
|
|
|
Filter: filter,
|
|
|
|
Scope: c.userSearchScope,
|
|
|
|
// We only need to search for these specific requests.
|
|
|
|
Attributes: []string{
|
|
|
|
c.UserSearch.IDAttr,
|
|
|
|
c.UserSearch.EmailAttr,
|
|
|
|
c.GroupSearch.UserAttr,
|
|
|
|
// TODO(ericchiang): what if this contains duplicate values?
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.UserSearch.NameAttr != "" {
|
|
|
|
req.Attributes = append(req.Attributes, c.UserSearch.NameAttr)
|
|
|
|
}
|
2017-08-11 22:47:30 +05:30
|
|
|
|
|
|
|
c.logger.Infof("performing ldap search %s %s %s",
|
|
|
|
req.BaseDN, scopeString(req.Scope), req.Filter)
|
2016-11-19 03:10:41 +05:30
|
|
|
resp, err := conn.Search(req)
|
|
|
|
if err != nil {
|
|
|
|
return ldap.Entry{}, false, fmt.Errorf("ldap: search with filter %q failed: %v", req.Filter, err)
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
switch n := len(resp.Entries); n {
|
|
|
|
case 0:
|
2016-12-13 04:24:01 +05:30
|
|
|
c.logger.Errorf("ldap: no results returned for filter: %q", filter)
|
2016-11-19 03:10:41 +05:30
|
|
|
return ldap.Entry{}, false, nil
|
|
|
|
case 1:
|
2017-08-11 22:47:30 +05:30
|
|
|
user = *resp.Entries[0]
|
|
|
|
c.logger.Infof("username %q mapped to entry %s", username, user.DN)
|
|
|
|
return user, true, nil
|
2016-11-19 03:10:41 +05:30
|
|
|
default:
|
|
|
|
return ldap.Entry{}, false, fmt.Errorf("ldap: filter returned multiple (%d) results: %q", n, filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ldapConnector) Login(ctx context.Context, s connector.Scopes, username, password string) (ident connector.Identity, validPass bool, err error) {
|
2017-05-05 04:09:08 +05:30
|
|
|
// make this check to avoid unauthenticated bind to the LDAP server.
|
2017-04-29 03:27:10 +05:30
|
|
|
if password == "" {
|
|
|
|
return connector.Identity{}, false, nil
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
var (
|
|
|
|
// We want to return a different error if the user's password is incorrect vs
|
|
|
|
// if there was an error.
|
|
|
|
incorrectPass = false
|
|
|
|
user ldap.Entry
|
|
|
|
)
|
|
|
|
|
|
|
|
err = c.do(ctx, func(conn *ldap.Conn) error {
|
|
|
|
entry, found, err := c.userEntry(conn, username)
|
2016-10-21 05:47:43 +05:30
|
|
|
if err != nil {
|
2016-11-19 03:10:41 +05:30
|
|
|
return err
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
2016-11-19 03:10:41 +05:30
|
|
|
if !found {
|
2016-11-02 02:33:22 +05:30
|
|
|
incorrectPass = true
|
|
|
|
return nil
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
2016-11-19 03:10:41 +05:30
|
|
|
user = entry
|
2016-10-21 05:47:43 +05:30
|
|
|
|
|
|
|
// Try to authenticate as the distinguished name.
|
|
|
|
if err := conn.Bind(user.DN, password); err != nil {
|
|
|
|
// Detect a bad password through the LDAP error code.
|
|
|
|
if ldapErr, ok := err.(*ldap.Error); ok {
|
2018-09-04 16:07:39 +05:30
|
|
|
switch ldapErr.ResultCode {
|
|
|
|
case ldap.LDAPResultInvalidCredentials:
|
2016-12-13 04:24:01 +05:30
|
|
|
c.logger.Errorf("ldap: invalid password for user %q", user.DN)
|
2016-10-21 05:47:43 +05:30
|
|
|
incorrectPass = true
|
|
|
|
return nil
|
2018-09-04 16:07:39 +05:30
|
|
|
case ldap.LDAPResultConstraintViolation:
|
|
|
|
c.logger.Errorf("ldap: constraint violation for user %q: %s", user.DN, ldapErr.Error())
|
|
|
|
incorrectPass = true
|
|
|
|
return nil
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
2018-09-04 16:07:39 +05:30
|
|
|
} // will also catch all ldap.Error without a case statement above
|
2016-10-21 05:47:43 +05:30
|
|
|
return fmt.Errorf("ldap: failed to bind as dn %q: %v", user.DN, err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-07-26 01:30:28 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2016-08-03 09:44:24 +05:30
|
|
|
return connector.Identity{}, false, err
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2016-11-02 02:33:22 +05:30
|
|
|
if incorrectPass {
|
|
|
|
return connector.Identity{}, false, nil
|
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
if ident, err = c.identityFromEntry(user); err != nil {
|
|
|
|
return connector.Identity{}, false, err
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
if s.Groups {
|
|
|
|
groups, err := c.groups(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
return connector.Identity{}, false, fmt.Errorf("ldap: failed to query groups: %v", err)
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
2016-11-19 03:10:41 +05:30
|
|
|
ident.Groups = groups
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
if s.OfflineAccess {
|
|
|
|
refresh := refreshData{
|
|
|
|
Username: username,
|
|
|
|
Entry: user,
|
|
|
|
}
|
|
|
|
// Encode entry for follow up requests such as the groups query and
|
|
|
|
// refresh attempts.
|
|
|
|
if ident.ConnectorData, err = json.Marshal(refresh); err != nil {
|
|
|
|
return connector.Identity{}, false, fmt.Errorf("ldap: marshal entry: %v", err)
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
|
2016-11-02 02:33:22 +05:30
|
|
|
return ident, true, nil
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
func (c *ldapConnector) Refresh(ctx context.Context, s connector.Scopes, ident connector.Identity) (connector.Identity, error) {
|
|
|
|
var data refreshData
|
|
|
|
if err := json.Unmarshal(ident.ConnectorData, &data); err != nil {
|
2018-11-13 14:10:40 +05:30
|
|
|
return ident, fmt.Errorf("ldap: failed to unmarshal internal data: %v", err)
|
2016-11-19 03:10:41 +05:30
|
|
|
}
|
|
|
|
|
2016-10-21 05:47:43 +05:30
|
|
|
var user ldap.Entry
|
2016-11-19 03:10:41 +05:30
|
|
|
err := c.do(ctx, func(conn *ldap.Conn) error {
|
|
|
|
entry, found, err := c.userEntry(conn, data.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("ldap: user not found %q", data.Username)
|
|
|
|
}
|
|
|
|
user = entry
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ident, err
|
|
|
|
}
|
|
|
|
if user.DN != data.Entry.DN {
|
|
|
|
return ident, fmt.Errorf("ldap: refresh for username %q expected DN %q got %q", data.Username, data.Entry.DN, user.DN)
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
newIdent, err := c.identityFromEntry(user)
|
|
|
|
if err != nil {
|
|
|
|
return ident, err
|
|
|
|
}
|
|
|
|
newIdent.ConnectorData = ident.ConnectorData
|
|
|
|
|
|
|
|
if s.Groups {
|
|
|
|
groups, err := c.groups(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
return connector.Identity{}, fmt.Errorf("ldap: failed to query groups: %v", err)
|
|
|
|
}
|
|
|
|
newIdent.Groups = groups
|
|
|
|
}
|
|
|
|
return newIdent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ldapConnector) groups(ctx context.Context, user ldap.Entry) ([]string, error) {
|
2016-12-28 00:37:03 +05:30
|
|
|
if c.GroupSearch.BaseDN == "" {
|
|
|
|
c.logger.Debugf("No groups returned for %q because no groups baseDN has been configured.", getAttr(user, c.UserSearch.NameAttr))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
var groups []*ldap.Entry
|
|
|
|
for _, attr := range getAttrs(user, c.GroupSearch.UserAttr) {
|
|
|
|
filter := fmt.Sprintf("(%s=%s)", c.GroupSearch.GroupAttr, ldap.EscapeFilter(attr))
|
|
|
|
if c.GroupSearch.Filter != "" {
|
|
|
|
filter = fmt.Sprintf("(&%s%s)", c.GroupSearch.Filter, filter)
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
req := &ldap.SearchRequest{
|
|
|
|
BaseDN: c.GroupSearch.BaseDN,
|
|
|
|
Filter: filter,
|
|
|
|
Scope: c.groupSearchScope,
|
|
|
|
Attributes: []string{c.GroupSearch.NameAttr},
|
|
|
|
}
|
2016-10-21 05:47:43 +05:30
|
|
|
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
gotGroups := false
|
|
|
|
if err := c.do(ctx, func(conn *ldap.Conn) error {
|
2017-08-11 22:47:30 +05:30
|
|
|
c.logger.Infof("performing ldap search %s %s %s",
|
|
|
|
req.BaseDN, scopeString(req.Scope), req.Filter)
|
connector/ldap: fix case where groups are listed on the user entity
Support schemas that determine membership by having fields on the
user entity, instead of listing users on a groups entity. E.g. the
following schema is now supported when it wasn't previously:
cn=eric,cn=user,dn=exapmle,dn=com
objectClass=myPerson
cn: eric
uid: eric
email: eric@example.com
memberOf: foo
memberOf: bar
cn=foo,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: foo
cn=bar,cn=group,dn=exapmle,dn=com
objectClass=myGroup
cn: bar
2017-04-11 22:18:48 +05:30
|
|
|
resp, err := conn.Search(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ldap: search failed: %v", err)
|
|
|
|
}
|
|
|
|
gotGroups = len(resp.Entries) != 0
|
|
|
|
groups = append(groups, resp.Entries...)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !gotGroups {
|
|
|
|
// TODO(ericchiang): Is this going to spam the logs?
|
|
|
|
c.logger.Errorf("ldap: groups search with filter %q returned no groups", filter)
|
2016-10-21 05:47:43 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var groupNames []string
|
|
|
|
for _, group := range groups {
|
|
|
|
name := getAttr(*group, c.GroupSearch.NameAttr)
|
|
|
|
if name == "" {
|
|
|
|
// Be obnoxious about missing missing attributes. If the group entry is
|
|
|
|
// missing its name attribute, that indicates a misconfiguration.
|
|
|
|
//
|
|
|
|
// In the future we can add configuration options to just log these errors.
|
|
|
|
return nil, fmt.Errorf("ldap: group entity %q missing required attribute %q",
|
|
|
|
group.DN, c.GroupSearch.NameAttr)
|
|
|
|
}
|
|
|
|
|
|
|
|
groupNames = append(groupNames, name)
|
|
|
|
}
|
|
|
|
return groupNames, nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2017-11-07 14:58:21 +05:30
|
|
|
|
|
|
|
func (c *ldapConnector) Prompt() string {
|
|
|
|
return c.UsernamePrompt
|
|
|
|
}
|