2015-05-06 01:54:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2015-05-08 07:28:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
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"
|
2015-05-06 01:54:27 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2016-03-21 17:44:08 +00:00
|
|
|
"strings"
|
2015-05-06 01:54:27 +00:00
|
|
|
)
|
|
|
|
|
2015-07-01 00:45:20 +00:00
|
|
|
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return Backend().Setup(conf)
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Backend() *framework.Backend {
|
|
|
|
var b backend
|
|
|
|
b.Backend = &framework.Backend{
|
|
|
|
Help: backendHelp,
|
|
|
|
|
|
|
|
PathsSpecial: &logical.Paths{
|
2015-07-27 18:24:12 +00:00
|
|
|
Root: append([]string{
|
2015-05-06 01:54:27 +00:00
|
|
|
"config",
|
2015-05-06 23:48:59 +00:00
|
|
|
"groups/*",
|
2015-07-14 22:46:15 +00:00
|
|
|
"users/*",
|
2015-05-06 01:54:27 +00:00
|
|
|
},
|
2015-07-31 00:16:53 +00:00
|
|
|
mfa.MFARootPaths()...,
|
2015-07-27 18:24:12 +00:00
|
|
|
),
|
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
|
|
|
|
2015-05-08 07:28:26 +00:00
|
|
|
AuthRenew: b.pathLoginRenew,
|
2015-05-06 01:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-27 15:17:54 +00:00
|
|
|
bindDN, err := getBindDN(cfg, c, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.Bind(bindDN, password); err != nil {
|
|
|
|
return nil, logical.ErrorResponse(fmt.Sprintf("LDAP bind failed: %v", err)), nil
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:17:54 +00:00
|
|
|
userDN, err := getUserDN(cfg, c, bindDN)
|
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 15:17:54 +00:00
|
|
|
ldapGroups, err := getLdapGroups(cfg, c, userDN, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(err.Error()), nil
|
2016-04-27 12:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ldapResponse := &logical.Response{
|
|
|
|
Data: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
if len(ldapGroups) == 0 {
|
|
|
|
errString := fmt.Sprintf(
|
|
|
|
"no LDAP groups found in userDN '%s' or groupDN '%s';only policies from locally-defined groups available",
|
|
|
|
cfg.UserDN,
|
|
|
|
cfg.GroupDN)
|
|
|
|
ldapResponse.AddWarning(errString)
|
|
|
|
}
|
|
|
|
|
|
|
|
var allGroups []string
|
|
|
|
// Import the custom added groups from ldap backend
|
|
|
|
user, err := b.User(req.Storage, username)
|
|
|
|
if err == nil && user != nil {
|
|
|
|
allGroups = append(allGroups, user.Groups...)
|
|
|
|
}
|
|
|
|
// add the LDAP groups
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(policies) == 0 {
|
|
|
|
errStr := "user is not a member of any authorized group"
|
|
|
|
if len(ldapResponse.Warnings()) > 0 {
|
|
|
|
errStr = fmt.Sprintf("%s; additionally, %s", errStr, ldapResponse.Warnings()[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
ldapResponse.Data["error"] = errStr
|
|
|
|
return nil, ldapResponse, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return policies, ldapResponse, nil
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:17:54 +00:00
|
|
|
func getBindDN(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 != "") {
|
2016-04-27 12:00:26 +00:00
|
|
|
if err := c.Bind(cfg.BindDN, cfg.BindPassword); 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-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-02-19 18:16:52 +00:00
|
|
|
Filter: fmt.Sprintf("(%s=%s)", cfg.UserAttr, ldap.EscapeFilter(username)),
|
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-04-27 15:17:54 +00:00
|
|
|
func 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-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-04-27 15:17:54 +00:00
|
|
|
Filter: fmt.Sprintf("(userPrincipalName=%s)", ldap.EscapeFilter(bindDN)),
|
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-04-27 15:17:54 +00:00
|
|
|
func 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)
|
|
|
|
// Fetch the optional memberOf property values on the user object
|
|
|
|
// This is the most common method used in Active Directory setup to retrieve the groups
|
|
|
|
result, err := c.Search(&ldap.SearchRequest{
|
2016-04-27 15:17:54 +00:00
|
|
|
BaseDN: userDN,
|
|
|
|
Scope: 0, // base scope to fetch only the userDN
|
|
|
|
Filter: "(cn=*)", // bogus filter, required to fetch the CN from userDN
|
2016-04-27 12:00:26 +00:00
|
|
|
Attributes: []string{
|
|
|
|
"memberOf",
|
|
|
|
},
|
|
|
|
})
|
2016-04-27 15:17:54 +00:00
|
|
|
// this check remains in case something happens with the ldap query or connection
|
2016-04-27 12:00:26 +00:00
|
|
|
if err != nil {
|
2016-04-27 15:17:54 +00:00
|
|
|
return nil, fmt.Errorf("LDAP fetch of distinguishedName=%s failed: %v", userDN, err)
|
2016-04-02 17:11:36 +00:00
|
|
|
}
|
2016-04-27 15:17:54 +00:00
|
|
|
// if there are more than one entry, we consider the results irrelevant and ignore them
|
|
|
|
if len(result.Entries) == 1 {
|
|
|
|
for _, attr := range result.Entries[0].Attributes {
|
|
|
|
// Find the groups the user is member of from the 'memberOf' attribute extracting the CN
|
|
|
|
if attr.Name == "memberOf" {
|
|
|
|
for _, value := range attr.Values {
|
|
|
|
memberOfDN, err := ldap.ParseDN(value)
|
|
|
|
if err != nil || len(memberOfDN.RDNs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-07-14 22:46:15 +00:00
|
|
|
|
2016-04-27 15:17:54 +00:00
|
|
|
for _, rdn := range memberOfDN.RDNs {
|
|
|
|
for _, rdnTypeAndValue := range rdn.Attributes {
|
|
|
|
if strings.EqualFold(rdnTypeAndValue.Type, "CN") {
|
|
|
|
ldapMap[rdnTypeAndValue.Value] = true
|
|
|
|
}
|
2016-03-21 14:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 22:37:46 +00:00
|
|
|
}
|
2015-07-14 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 14:55:38 +00:00
|
|
|
// Find groups by searching in groupDN for any of the memberUid, member or uniqueMember attributes
|
2016-03-21 17:44:08 +00:00
|
|
|
// and retrieving the CN in the DN result
|
2016-04-02 17:11:36 +00:00
|
|
|
if cfg.GroupDN != "" {
|
2016-04-27 12:00:26 +00:00
|
|
|
result, err := c.Search(&ldap.SearchRequest{
|
2016-04-02 17:11:36 +00:00
|
|
|
BaseDN: cfg.GroupDN,
|
|
|
|
Scope: 2, // subtree
|
2016-04-27 15:17:54 +00:00
|
|
|
Filter: fmt.Sprintf("(|(memberUid=%s)(member=%s)(uniqueMember=%s))", ldap.EscapeFilter(username), ldap.EscapeFilter(userDN), ldap.EscapeFilter(userDN)),
|
2016-04-02 17:11:36 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2016-04-27 15:17:54 +00:00
|
|
|
return nil, fmt.Errorf("LDAP search failed: %v", err)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2016-04-02 17:11:36 +00:00
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
for _, e := range result.Entries {
|
2016-04-02 17:11:36 +00:00
|
|
|
dn, err := ldap.ParseDN(e.DN)
|
2016-04-27 12:00:26 +00:00
|
|
|
if err != nil || len(dn.RDNs) == 0 {
|
2016-04-02 17:11:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-04-26 10:16:42 +00:00
|
|
|
for _, rdn := range dn.RDNs {
|
|
|
|
for _, rdnTypeAndValue := range rdn.Attributes {
|
|
|
|
if strings.EqualFold(rdnTypeAndValue.Type, "CN" ) {
|
2016-04-27 12:00:26 +00:00
|
|
|
ldapMap[rdnTypeAndValue.Value] = true
|
2016-04-26 10:16:42 +00:00
|
|
|
}
|
2016-03-21 17:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 07:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 12:00:26 +00:00
|
|
|
ldapGroups := make([]string, len(ldapMap))
|
|
|
|
for key, _ := range ldapMap {
|
|
|
|
ldapGroups = append(ldapGroups, key)
|
2015-05-08 07:28:26 +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".
|
|
|
|
`
|