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-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) {
|
|
|
|
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{
|
|
|
|
Root: []string{
|
|
|
|
"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
|
|
|
},
|
|
|
|
|
|
|
|
Unauthenticated: []string{
|
2015-05-09 19:07:52 +00:00
|
|
|
"login/*",
|
2015-05-06 01:54:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Paths: append([]*framework.Path{
|
|
|
|
pathLogin(&b),
|
|
|
|
pathConfig(&b),
|
2015-05-06 23:48:59 +00:00
|
|
|
pathGroups(&b),
|
2015-07-14 22:46:15 +00:00
|
|
|
pathUsers(&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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to authenticate to the server using the provided credentials
|
2015-07-14 22:37:46 +00:00
|
|
|
binddn := ""
|
|
|
|
if cfg.UPNDomain != "" {
|
|
|
|
binddn = fmt.Sprintf("%s@%s", EscapeLDAPValue(username), cfg.UPNDomain)
|
|
|
|
} else {
|
|
|
|
binddn = fmt.Sprintf("%s=%s,%s", cfg.UserAttr, EscapeLDAPValue(username), cfg.UserDN)
|
|
|
|
}
|
2015-05-08 07:28:26 +00:00
|
|
|
if err = c.Bind(binddn, password); err != nil {
|
|
|
|
return nil, logical.ErrorResponse(fmt.Sprintf("LDAP bind failed: %v", err)), nil
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:37:46 +00:00
|
|
|
userdn := ""
|
|
|
|
if cfg.UPNDomain != "" {
|
|
|
|
// Find the distinguished name for the user if userPrincipalName used for login
|
|
|
|
sresult, err := c.Search(&ldap.SearchRequest{
|
|
|
|
BaseDN: cfg.UserDN,
|
|
|
|
Scope: 2, // subtree
|
|
|
|
Filter: fmt.Sprintf("(userPrincipalName=%s)", binddn),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(fmt.Sprintf("LDAP search failed: %v", err)), nil
|
|
|
|
}
|
|
|
|
for _, e := range sresult.Entries {
|
|
|
|
userdn = e.DN
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
userdn = binddn
|
|
|
|
}
|
|
|
|
|
2015-05-08 07:28:26 +00:00
|
|
|
// Enumerate all groups the user is member of. The search filter should
|
|
|
|
// work with both openldap and MS AD standard schemas.
|
|
|
|
sresult, err := c.Search(&ldap.SearchRequest{
|
|
|
|
BaseDN: cfg.GroupDN,
|
|
|
|
Scope: 2, // subtree
|
2015-07-14 22:37:46 +00:00
|
|
|
Filter: fmt.Sprintf("(|(memberUid=%s)(member=%s)(uniqueMember=%s))", username, userdn, userdn),
|
2015-05-08 07:28:26 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, logical.ErrorResponse(fmt.Sprintf("LDAP search failed: %v", err)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var allgroups []string
|
|
|
|
var policies []string
|
2015-07-14 22:46:15 +00:00
|
|
|
|
|
|
|
user, err := b.User(req.Storage, username)
|
|
|
|
if err == nil && user != nil {
|
2015-07-17 21:40:06 +00:00
|
|
|
allgroups = append(allgroups, user.Groups...)
|
2015-07-14 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 07:28:26 +00:00
|
|
|
for _, e := range sresult.Entries {
|
2015-06-29 21:50:55 +00:00
|
|
|
dn, err := ldap.ParseDN(e.DN)
|
|
|
|
if err != nil || len(dn.RDNs) == 0 || len(dn.RDNs[0].Attributes) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gname := dn.RDNs[0].Attributes[0].Value
|
2015-05-08 07:28:26 +00:00
|
|
|
allgroups = append(allgroups, gname)
|
2015-07-17 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, gname := range allgroups {
|
2015-05-08 07:28:26 +00:00
|
|
|
group, err := b.Group(req.Storage, gname)
|
|
|
|
if err == nil && group != nil {
|
|
|
|
policies = append(policies, group.Policies...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(policies) == 0 {
|
|
|
|
return nil, logical.ErrorResponse("user is not member of any authorized group"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return policies, nil, nil
|
|
|
|
}
|
|
|
|
|
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".
|
|
|
|
`
|