2020-06-01 07:48:07 +00:00
|
|
|
package ldap
|
2017-07-29 08:17:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-04 17:41:04 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2017-07-29 08:17:24 +00:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
2017-09-15 20:29:47 +00:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2018-04-01 11:56:41 +00:00
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
2020-06-03 17:47:21 +00:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/upstream"
|
2017-07-29 08:17:24 +00:00
|
|
|
|
2019-07-10 07:39:00 +00:00
|
|
|
"github.com/caddyserver/caddy"
|
2017-07-29 08:17:24 +00:00
|
|
|
)
|
|
|
|
|
2020-06-03 17:47:21 +00:00
|
|
|
const pluginName = "ldap"
|
|
|
|
|
|
|
|
// Define log to be a logger with the plugin name in it.
|
|
|
|
var log = clog.NewWithPlugin(pluginName)
|
|
|
|
|
2019-09-24 06:16:55 +00:00
|
|
|
// init registers this plugin.
|
2020-06-03 17:47:21 +00:00
|
|
|
func init() { plugin.Register(pluginName, setup) }
|
2017-07-29 08:17:24 +00:00
|
|
|
|
2020-06-01 07:48:07 +00:00
|
|
|
// setup is the function that gets called when the config parser see the token "ldap". Setup is responsible
|
|
|
|
// for parsing any extra options the ldap plugin may have. The first token this function sees is "ldap".
|
2017-07-29 08:17:24 +00:00
|
|
|
func setup(c *caddy.Controller) error {
|
2020-06-03 17:47:21 +00:00
|
|
|
|
|
|
|
// parse corefile config
|
|
|
|
l, err := ldapParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return plugin.Error(pluginName, err)
|
|
|
|
}
|
|
|
|
|
2020-06-08 04:12:18 +00:00
|
|
|
err = l.InitClient()
|
2020-06-03 17:47:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return plugin.Error(pluginName, err)
|
2017-07-29 08:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 17:47:21 +00:00
|
|
|
// add prometheus metrics on startup
|
2018-04-01 11:56:41 +00:00
|
|
|
c.OnStartup(func() error {
|
2020-06-03 17:47:21 +00:00
|
|
|
// add plugin-global metric once
|
|
|
|
once.Do(func() {
|
|
|
|
metrics.MustRegister(c, requestCount)
|
|
|
|
})
|
2018-04-01 11:56:41 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
|
2017-09-15 20:29:47 +00:00
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2020-06-03 17:47:21 +00:00
|
|
|
l.Next = next
|
|
|
|
return l
|
2017-07-29 08:17:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-03 17:47:21 +00:00
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
func ldapParse(c *caddy.Controller) (*Ldap, error) {
|
|
|
|
var (
|
|
|
|
ldap *Ldap
|
2020-06-04 17:41:04 +00:00
|
|
|
err error
|
2020-06-03 17:47:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for c.Next() {
|
|
|
|
if i > 0 {
|
|
|
|
return nil, plugin.ErrOnce
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
|
2020-06-08 04:12:18 +00:00
|
|
|
ldap, err = ParseStanza(c)
|
2020-06-03 17:47:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return ldap, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ldap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseStanza parses a ldap stanza
|
|
|
|
func ParseStanza(c *caddy.Controller) (*Ldap, error) {
|
|
|
|
ldap := New([]string{""})
|
|
|
|
zones := c.RemainingArgs()
|
|
|
|
|
|
|
|
if len(zones) != 0 {
|
|
|
|
ldap.Zones = zones
|
|
|
|
for i := 0; i < len(ldap.Zones); i++ {
|
|
|
|
ldap.Zones[i] = plugin.Host(ldap.Zones[i]).Normalize()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ldap.Zones = make([]string, len(c.ServerBlockKeys))
|
|
|
|
for i := 0; i < len(c.ServerBlockKeys); i++ {
|
|
|
|
ldap.Zones[i] = plugin.Host(c.ServerBlockKeys[i]).Normalize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap.Upstream = upstream.New()
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
// RFC 4516 URL
|
2020-06-08 04:12:18 +00:00
|
|
|
case "ldap_url":
|
|
|
|
c.NextArg()
|
|
|
|
ldap.ldapURL = c.Val()
|
|
|
|
continue
|
|
|
|
case "paging_limit":
|
|
|
|
c.NextArg()
|
|
|
|
pagingLimit, err := strconv.Atoi(c.Val())
|
|
|
|
if err != nil {
|
2020-06-03 17:47:21 +00:00
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
2020-06-08 04:12:18 +00:00
|
|
|
ldap.pagingLimit = pagingLimit
|
2020-06-03 17:47:21 +00:00
|
|
|
continue
|
2020-06-08 04:12:18 +00:00
|
|
|
case "search_request":
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "base_dn":
|
|
|
|
c.NextArg() // ou=ae-dir
|
|
|
|
ldap.searchRequest.BaseDN = c.Val()
|
|
|
|
case "filter":
|
|
|
|
c.NextArg() // (objectClass=aeNwDevice)
|
|
|
|
ldap.searchRequest.Filter = c.Val()
|
|
|
|
case "attributes":
|
|
|
|
ldap.searchRequest.Attributes = c.RemainingArgs() // aeFqdn ipHostNumber
|
2020-06-03 17:47:21 +00:00
|
|
|
default:
|
2020-06-08 04:12:18 +00:00
|
|
|
return nil, c.Errf("unknown search request property '%s'", c.Val())
|
2020-06-03 17:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 04:12:18 +00:00
|
|
|
continue
|
|
|
|
case "username":
|
|
|
|
c.NextArg()
|
|
|
|
ldap.username = c.Val()
|
|
|
|
case "password":
|
|
|
|
c.NextArg()
|
|
|
|
ldap.password = c.Val()
|
|
|
|
case "sasl":
|
|
|
|
c.NextArg()
|
|
|
|
ldap.sasl = true
|
2020-06-03 17:47:21 +00:00
|
|
|
case "fallthrough":
|
|
|
|
ldap.Fall.SetZonesFromArgs(c.RemainingArgs())
|
|
|
|
default:
|
|
|
|
return nil, c.Errf("unknown property '%s'", c.Val())
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 04:12:18 +00:00
|
|
|
// validate non-default ldap values ...
|
|
|
|
if ldap.ldapURL == "" || &ldap.ldapURL == nil {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
if ldap.searchRequest.BaseDN == "" {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
if ldap.searchRequest.Filter == "" {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
if len(ldap.searchRequest.Attributes) != 2 {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
// if only one of password and username set
|
|
|
|
if (&ldap.username == nil) != (&ldap.password == nil) {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
// if both username/password and sasl are set
|
|
|
|
if &ldap.username != nil && &ldap.sasl != nil {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
// if neither username/password nor sasl are set
|
|
|
|
if &ldap.username == nil && &ldap.sasl == nil {
|
|
|
|
return nil, c.ArgErr()
|
2020-06-03 17:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ldap, nil
|
2020-06-04 17:41:04 +00:00
|
|
|
}
|