2015-05-06 01:54:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2016-05-09 00:21:44 +00:00
|
|
|
"bytes"
|
2015-05-08 07:28:26 +00:00
|
|
|
"fmt"
|
2016-05-09 00:21:44 +00:00
|
|
|
"text/template"
|
2016-05-31 23:42:54 +00:00
|
|
|
|
2015-07-01 00:36:12 +00:00
|
|
|
"github.com/go-ldap/ldap"
|
2015-07-27 18:24:12 +00:00
|
|
|
"github.com/hashicorp/vault/helper/mfa"
|
2016-10-14 21:20:50 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2015-05-06 01:54:27 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-07-01 00:45:20 +00:00
|
|
|
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
b := Backend()
|
|
|
|
if err := b.Setup(conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 19:53:02 +00:00
|
|
|
func Backend() *backend {
|
2015-05-06 01:54:27 +00:00
|
|
|
var b backend
|
|
|
|
b.Backend = &framework.Backend{
|
|
|
|
Help: backendHelp,
|
|
|
|
|
|
|
|
PathsSpecial: &logical.Paths{
|
2016-05-31 23:42:54 +00:00
|
|
|
Root: mfa.MFARootPaths(),
|
2015-05-06 01:54:27 +00:00
|
|
|
|
|
|
|
Unauthenticated: []string{
|
2015-05-09 19:07:52 +00:00
|
|
|
"login/*",
|
2015-05-06 01:54:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Paths: append([]*framework.Path{
|
|
|
|
pathConfig(&b),
|
2015-05-06 23:48:59 +00:00
|
|
|
pathGroups(&b),
|
2016-05-14 23:56:49 +00:00
|
|
|
pathGroupsList(&b),
|
2015-07-14 22:46:15 +00:00
|
|
|
pathUsers(&b),
|
2016-05-14 23:56:49 +00:00
|
|
|
pathUsersList(&b),
|
2015-07-27 18:24:12 +00:00
|
|
|
},
|
|
|
|
mfa.MFAPaths(b.Backend, pathLogin(&b))...,
|
|
|
|
),
|
2015-05-06 01:54:27 +00:00
|
|
|
|
2017-07-28 18:04:46 +00:00
|
|
|
AuthRenew: b.pathLoginRenew,
|
|
|
|
BackendType: logical.TypeCredential,
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 19:53:02 +00:00
|
|
|
return &b
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type backend struct {
|
|
|
|
*framework.Backend
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
func EscapeLDAPValue(input string) string {
|
|
|
|
// RFC4514 forbids un-escaped:
|
|
|
|
// - leading space or hash
|
|
|
|
// - trailing space
|
|
|
|
// - special characters '"', '+', ',', ';', '<', '>', '\\'
|
|
|
|
// - null
|
|
|
|
for i := 0; i < len(input); i++ {
|
|
|
|
escaped := false
|
|
|
|
if input[i] == '\\' {
|
|
|
|
i++
|
|
|
|
escaped = true
|
|
|
|
}
|
|
|
|
switch input[i] {
|
|
|
|
case '"', '+', ',', ';', '<', '>', '\\':
|
|
|
|
if !escaped {
|
|
|
|
input = input[0:i] + "\\" + input[i:]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if escaped {
|
|
|
|
input = input[0:i] + "\\" + input[i:]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if input[0] == ' ' || input[0] == '#' {
|
|
|
|
input = "\\" + input
|
|
|
|
}
|
|
|
|
if input[len(input)-1] == ' ' {
|
|
|
|
input = input[0:len(input)-1] + "\\ "
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2015-05-08 07:28:26 +00:00
|
|
|
func (b *backend) Login(req *logical.Request, username string, password string) ([]string, *logical.Response, error) {
|
|
|
|
|
|
|
|
cfg, err := b.Config(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if cfg == nil {
|
|
|
|
return nil, logical.ErrorResponse("ldap backend not configured"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := cfg.DialLDAP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
|
|
|
}
|
2016-03-29 13:59:28 +00:00
|
|
|
if c == nil {
|
|
|
|
return nil, logical.ErrorResponse("invalid connection returned from LDAP dial"), nil
|
|
|
|
}
|
2016-03-21 14:55:38 +00:00
|
|
|
|
2016-11-28 17:31:36 +00:00
|
|
|
// Clean connection
|
|
|
|
defer c.Close()
|
|
|
|
|
2017-04-18 19:52:05 +00:00
|
|
|
userBindDN, err := b.getUserBindDN(cfg, c, username)
|
2016-04-27 15:17:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
2017-04-18 19:52:05 +00:00
|
|
|
b.Logger().Debug("auth/ldap: User BindDN fetched", "username", username, "binddn", userBindDN)
|
2016-08-19 20:45:17 +00:00
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
|
2016-12-01 18:11:40 +00:00
|
|
|
if cfg.DenyNullBind && len(password) == 0 {
|
|
|
|
return nil, logical.ErrorResponse("password cannot be of zero length when passwordless binds are being denied"), nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
// Try to bind as the login user. This is where the actual authentication takes place.
|
2017-09-07 12:46:43 +00:00
|
|
|
if len(password) > 0 {
|
|
|
|
err = c.Bind(userBindDN, password)
|
|
|
|
} else {
|
|
|
|
err = c.UnauthenticatedBind(userBindDN)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-27 12:00:26 +00:00
|
|
|
return nil, logical.ErrorResponse(fmt.Sprintf("LDAP bind failed: %v", err)), nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 19:52:05 +00:00
|
|
|
// We re-bind to the BindDN if it's defined because we assume
|
|
|
|
// the BindDN should be the one to search, not the user logging in.
|
|
|
|
if cfg.BindDN != "" && cfg.BindPassword != "" {
|
|
|
|
if err := c.Bind(cfg.BindDN, cfg.BindPassword); err != nil {
|
|
|
|
return nil, logical.ErrorResponse(fmt.Sprintf("Encountered an error while attempting to re-bind with the BindDN User: %s", err.Error())), nil
|
|
|
|
}
|
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: Re-Bound to original BindDN")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userDN, err := b.getUserDN(cfg, c, userBindDN)
|
2016-04-27 15:17:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
ldapGroups, err := b.getLdapGroups(cfg, c, userDN, username)
|
2016-04-27 15:17:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: Groups fetched from server", "num_server_groups", len(ldapGroups), "server_groups", ldapGroups)
|
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
|
|
|
|
ldapResponse := &logical.Response{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
if len(ldapGroups) == 0 {
|
|
|
|
errString := fmt.Sprintf(
|
2016-05-09 00:21:44 +00:00
|
|
|
"no LDAP groups found in groupDN '%s'; only policies from locally-defined groups available",
|
2016-04-27 12:00:26 +00:00
|
|
|
cfg.GroupDN)
|
|
|
|
ldapResponse.AddWarning(errString)
|
|
|
|
}
|
|
|
|
|
|
|
|
var allGroups []string
|
|
|
|
// Import the custom added groups from ldap backend
|
|
|
|
user, err := b.User(req.Storage, username)
|
2016-05-09 00:21:44 +00:00
|
|
|
if err == nil && user != nil && user.Groups != nil {
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: adding local groups", "num_local_groups", len(user.Groups), "local_groups", user.Groups)
|
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
allGroups = append(allGroups, user.Groups...)
|
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
// Merge local and LDAP groups
|
2016-04-27 12:00:26 +00:00
|
|
|
allGroups = append(allGroups, ldapGroups...)
|
|
|
|
|
|
|
|
// Retrieve policies
|
|
|
|
var policies []string
|
|
|
|
for _, groupName := range allGroups {
|
|
|
|
group, err := b.Group(req.Storage, groupName)
|
|
|
|
if err == nil && group != nil {
|
|
|
|
policies = append(policies, group.Policies...)
|
|
|
|
}
|
|
|
|
}
|
2017-04-04 15:54:18 +00:00
|
|
|
if user != nil && user.Policies != nil {
|
2016-11-29 13:35:49 +00:00
|
|
|
policies = append(policies, user.Policies...)
|
|
|
|
}
|
2016-10-14 21:20:50 +00:00
|
|
|
// Policies from each group may overlap
|
2017-04-04 15:54:18 +00:00
|
|
|
policies = strutil.RemoveDuplicates(policies, true)
|
2016-10-14 21:20:50 +00:00
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
if len(policies) == 0 {
|
|
|
|
errStr := "user is not a member of any authorized group"
|
2017-06-05 14:52:43 +00:00
|
|
|
if len(ldapResponse.Warnings) > 0 {
|
|
|
|
errStr = fmt.Sprintf("%s; additionally, %s", errStr, ldapResponse.Warnings[0])
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ldapResponse.Data["error"] = errStr
|
|
|
|
return nil, ldapResponse, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return policies, ldapResponse, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
/*
|
|
|
|
* Parses a distinguished name and returns the CN portion.
|
|
|
|
* Given a non-conforming string (such as an already-extracted CN),
|
|
|
|
* it will be returned as-is.
|
|
|
|
*/
|
|
|
|
func (b *backend) getCN(dn string) string {
|
|
|
|
parsedDN, err := ldap.ParseDN(dn)
|
|
|
|
if err != nil || len(parsedDN.RDNs) == 0 {
|
|
|
|
// It was already a CN, return as-is
|
|
|
|
return dn
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rdn := range parsedDN.RDNs {
|
|
|
|
for _, rdnAttr := range rdn.Attributes {
|
|
|
|
if rdnAttr.Type == "CN" {
|
|
|
|
return rdnAttr.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default, return self
|
|
|
|
return dn
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Discover and return the bind string for the user attempting to authenticate.
|
|
|
|
* This is handled in one of several ways:
|
|
|
|
*
|
|
|
|
* 1. If DiscoverDN is set, the user object will be searched for using userdn (base search path)
|
|
|
|
* and userattr (the attribute that maps to the provided username).
|
|
|
|
* The bind will either be anonymous or use binddn and bindpassword if they were provided.
|
|
|
|
* 2. If upndomain is set, the user dn is constructed as 'username@upndomain'. See https://msdn.microsoft.com/en-us/library/cc223499.aspx
|
|
|
|
*
|
|
|
|
*/
|
2017-04-18 19:52:05 +00:00
|
|
|
func (b *backend) getUserBindDN(cfg *ConfigEntry, c *ldap.Conn, username string) (string, error) {
|
2016-04-27 12:00:26 +00:00
|
|
|
bindDN := ""
|
2016-01-27 16:06:27 +00:00
|
|
|
if cfg.DiscoverDN || (cfg.BindDN != "" && cfg.BindPassword != "") {
|
2017-09-07 12:46:43 +00:00
|
|
|
var err error
|
|
|
|
if cfg.BindPassword != "" {
|
|
|
|
err = c.Bind(cfg.BindDN, cfg.BindPassword)
|
|
|
|
} else {
|
|
|
|
err = c.UnauthenticatedBind(cfg.BindDN)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-27 15:17:54 +00:00
|
|
|
return bindDN, fmt.Errorf("LDAP bind (service) failed: %v", err)
|
2016-01-26 14:56:41 +00:00
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
|
|
|
|
filter := fmt.Sprintf("(%s=%s)", cfg.UserAttr, ldap.EscapeFilter(username))
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: Discovering user", "userdn", cfg.UserDN, "filter", filter)
|
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
result, err := c.Search(&ldap.SearchRequest{
|
2016-01-26 14:56:41 +00:00
|
|
|
BaseDN: cfg.UserDN,
|
|
|
|
Scope: 2, // subtree
|
2016-07-25 13:08:29 +00:00
|
|
|
Filter: filter,
|
2016-01-26 14:56:41 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2016-04-27 15:17:54 +00:00
|
|
|
return bindDN, fmt.Errorf("LDAP search for binddn failed: %v", err)
|
2016-01-26 14:56:41 +00:00
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
if len(result.Entries) != 1 {
|
2016-04-27 15:17:54 +00:00
|
|
|
return bindDN, fmt.Errorf("LDAP search for binddn 0 or not unique")
|
2016-01-26 14:56:41 +00:00
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
bindDN = result.Entries[0].DN
|
2015-07-14 22:37:46 +00:00
|
|
|
} else {
|
2016-01-26 14:56:41 +00:00
|
|
|
if cfg.UPNDomain != "" {
|
2016-04-27 12:00:26 +00:00
|
|
|
bindDN = fmt.Sprintf("%s@%s", EscapeLDAPValue(username), cfg.UPNDomain)
|
2016-01-26 14:56:41 +00:00
|
|
|
} else {
|
2016-04-27 12:00:26 +00:00
|
|
|
bindDN = fmt.Sprintf("%s=%s,%s", cfg.UserAttr, EscapeLDAPValue(username), cfg.UserDN)
|
2016-01-26 14:56:41 +00:00
|
|
|
}
|
2015-07-14 22:37:46 +00:00
|
|
|
}
|
2015-05-08 07:28:26 +00:00
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
return bindDN, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
/*
|
|
|
|
* Returns the DN of the object representing the authenticated user.
|
|
|
|
*/
|
|
|
|
func (b *backend) getUserDN(cfg *ConfigEntry, c *ldap.Conn, bindDN string) (string, error) {
|
2016-04-27 12:00:26 +00:00
|
|
|
userDN := ""
|
2015-07-14 22:37:46 +00:00
|
|
|
if cfg.UPNDomain != "" {
|
|
|
|
// Find the distinguished name for the user if userPrincipalName used for login
|
2016-05-09 00:21:44 +00:00
|
|
|
filter := fmt.Sprintf("(userPrincipalName=%s)", ldap.EscapeFilter(bindDN))
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: Searching UPN", "userdn", cfg.UserDN, "filter", filter)
|
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
result, err := c.Search(&ldap.SearchRequest{
|
2015-07-14 22:37:46 +00:00
|
|
|
BaseDN: cfg.UserDN,
|
|
|
|
Scope: 2, // subtree
|
2016-05-09 00:21:44 +00:00
|
|
|
Filter: filter,
|
2015-07-14 22:37:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2016-04-27 15:17:54 +00:00
|
|
|
return userDN, fmt.Errorf("LDAP search failed for detecting user: %v", err)
|
2015-07-14 22:37:46 +00:00
|
|
|
}
|
2016-04-27 12:00:26 +00:00
|
|
|
for _, e := range result.Entries {
|
|
|
|
userDN = e.DN
|
2015-07-14 22:37:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-27 15:17:54 +00:00
|
|
|
userDN = bindDN
|
2015-07-14 22:37:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
return userDN, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
/*
|
|
|
|
* getLdapGroups queries LDAP and returns a slice describing the set of groups the authenticated user is a member of.
|
|
|
|
*
|
|
|
|
* The search query is constructed according to cfg.GroupFilter, and run in context of cfg.GroupDN.
|
|
|
|
* Groups will be resolved from the query results by following the attribute defined in cfg.GroupAttr.
|
|
|
|
*
|
|
|
|
* cfg.GroupFilter is a go template and is compiled with the following context: [UserDN, Username]
|
|
|
|
* UserDN - The DN of the authenticated user
|
|
|
|
* Username - The Username of the authenticated user
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* cfg.GroupFilter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:={{.UserDN}}))"
|
|
|
|
* cfg.GroupDN = "OU=Groups,DC=myorg,DC=com"
|
|
|
|
* cfg.GroupAttr = "cn"
|
|
|
|
*
|
|
|
|
* NOTE - If cfg.GroupFilter is empty, no query is performed and an empty result slice is returned.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
func (b *backend) getLdapGroups(cfg *ConfigEntry, c *ldap.Conn, userDN string, username string) ([]string, error) {
|
2016-04-27 12:00:26 +00:00
|
|
|
// retrieve the groups in a string/bool map as a structure to avoid duplicates inside
|
|
|
|
ldapMap := make(map[string]bool)
|
2016-05-09 00:21:44 +00:00
|
|
|
|
|
|
|
if cfg.GroupFilter == "" {
|
2016-08-19 20:45:17 +00:00
|
|
|
b.Logger().Warn("auth/ldap: GroupFilter is empty, will not query server")
|
2016-05-09 00:21:44 +00:00
|
|
|
return make([]string, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.GroupDN == "" {
|
2016-08-19 20:45:17 +00:00
|
|
|
b.Logger().Warn("auth/ldap: GroupDN is empty, will not query server")
|
2016-05-09 00:21:44 +00:00
|
|
|
return make([]string, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If groupfilter was defined, resolve it as a Go template and use the query for
|
|
|
|
// returning the user's groups
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: Compiling group filter", "group_filter", cfg.GroupFilter)
|
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
|
|
|
|
// Parse the configuration as a template.
|
|
|
|
// Example template "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:={{.UserDN}}))"
|
|
|
|
t, err := template.New("queryTemplate").Parse(cfg.GroupFilter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("LDAP search failed due to template compilation error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build context to pass to template - we will be exposing UserDn and Username.
|
|
|
|
context := struct {
|
|
|
|
UserDN string
|
|
|
|
Username string
|
|
|
|
}{
|
|
|
|
ldap.EscapeFilter(userDN),
|
|
|
|
ldap.EscapeFilter(username),
|
|
|
|
}
|
|
|
|
|
|
|
|
var renderedQuery bytes.Buffer
|
|
|
|
t.Execute(&renderedQuery, context)
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
if b.Logger().IsDebug() {
|
|
|
|
b.Logger().Debug("auth/ldap: Searching", "groupdn", cfg.GroupDN, "rendered_query", renderedQuery.String())
|
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
result, err := c.Search(&ldap.SearchRequest{
|
2016-05-09 00:21:44 +00:00
|
|
|
BaseDN: cfg.GroupDN,
|
|
|
|
Scope: 2, // subtree
|
|
|
|
Filter: renderedQuery.String(),
|
2016-04-27 12:00:26 +00:00
|
|
|
Attributes: []string{
|
2016-05-09 00:21:44 +00:00
|
|
|
cfg.GroupAttr,
|
2016-04-27 12:00:26 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-05-09 00:21:44 +00:00
|
|
|
return nil, fmt.Errorf("LDAP search failed: %v", err)
|
2015-07-14 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
for _, e := range result.Entries {
|
|
|
|
dn, err := ldap.ParseDN(e.DN)
|
|
|
|
if err != nil || len(dn.RDNs) == 0 {
|
|
|
|
continue
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2016-04-02 17:11:36 +00:00
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
// Enumerate attributes of each result, parse out CN and add as group
|
|
|
|
values := e.GetAttributeValues(cfg.GroupAttr)
|
|
|
|
if len(values) > 0 {
|
|
|
|
for _, val := range values {
|
|
|
|
groupCN := b.getCN(val)
|
|
|
|
ldapMap[groupCN] = true
|
2016-03-21 17:44:08 +00:00
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
} else {
|
|
|
|
// If groupattr didn't resolve, use self (enumerating group objects)
|
|
|
|
groupCN := b.getCN(e.DN)
|
|
|
|
ldapMap[groupCN] = true
|
2015-05-08 07:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 00:21:44 +00:00
|
|
|
ldapGroups := make([]string, 0, len(ldapMap))
|
2016-04-27 12:00:26 +00:00
|
|
|
for key, _ := range ldapMap {
|
|
|
|
ldapGroups = append(ldapGroups, key)
|
2015-05-08 07:28:26 +00:00
|
|
|
}
|
2016-05-09 00:21:44 +00:00
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
return ldapGroups, nil
|
2015-05-08 07:28:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 01:54:27 +00:00
|
|
|
const backendHelp = `
|
|
|
|
The "ldap" credential provider allows authentication querying
|
|
|
|
a LDAP server, checking username and password, and associating groups
|
|
|
|
to set of policies.
|
|
|
|
|
|
|
|
Configuration of the server is done through the "config" and "groups"
|
|
|
|
endpoints by a user with root access. Authentication is then done
|
|
|
|
by suppying the two fields for "login".
|
|
|
|
`
|