2020-06-03 16:25:04 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
2020-06-09 03:01:53 +00:00
|
|
|
"github.com/coredns/coredns/plugin/file"
|
2020-06-03 16:25:04 +00:00
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
2020-06-10 04:00:48 +00:00
|
|
|
func (l *Ldap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2020-06-09 03:01:53 +00:00
|
|
|
// opt := plugin.Options{}
|
2020-06-03 16:25:04 +00:00
|
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
|
2020-06-09 03:01:53 +00:00
|
|
|
zone := plugin.Zones(l.Zones.Names).Matches(state.Name())
|
2020-06-03 16:25:04 +00:00
|
|
|
if zone == "" {
|
|
|
|
return plugin.NextOrFailure(l.Name(), l.Next, ctx, w, r)
|
|
|
|
}
|
2020-06-10 07:41:44 +00:00
|
|
|
|
2020-06-09 03:01:53 +00:00
|
|
|
Zone, ok := l.Zones.Z[zone]
|
|
|
|
if !ok || Zone == nil {
|
|
|
|
return dns.RcodeServerFailure, nil
|
2020-06-03 16:25:04 +00:00
|
|
|
}
|
2020-06-10 07:41:44 +00:00
|
|
|
|
2020-06-09 03:01:53 +00:00
|
|
|
var result file.Result
|
2020-06-10 07:41:44 +00:00
|
|
|
|
2020-06-03 16:25:04 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
|
|
|
m.Authoritative = true
|
2020-06-10 07:41:44 +00:00
|
|
|
|
2020-06-09 03:01:53 +00:00
|
|
|
l.zMu.RLock()
|
|
|
|
m.Answer, m.Ns, m.Extra, result = Zone.Lookup(ctx, state, state.Name())
|
|
|
|
l.zMu.RUnlock()
|
|
|
|
|
|
|
|
if len(m.Answer) == 0 && result != file.NoData && l.Fall.Through(state.Name()) {
|
|
|
|
return plugin.NextOrFailure(l.Name(), l.Next, ctx, w, r)
|
|
|
|
}
|
2020-06-03 16:25:04 +00:00
|
|
|
|
2020-06-09 03:01:53 +00:00
|
|
|
switch result {
|
|
|
|
case file.Success:
|
|
|
|
case file.NoData:
|
|
|
|
case file.NameError:
|
|
|
|
m.Rcode = dns.RcodeNameError
|
|
|
|
case file.Delegation:
|
|
|
|
m.Authoritative = false
|
|
|
|
case file.ServerFailure:
|
|
|
|
return dns.RcodeServerFailure, nil
|
|
|
|
}
|
2020-06-10 08:03:59 +00:00
|
|
|
|
|
|
|
if err := w.WriteMsg(m); err != nil {
|
|
|
|
return dns.RcodeServerFailure, nil
|
|
|
|
}
|
2020-06-10 07:41:44 +00:00
|
|
|
|
2020-06-03 16:25:04 +00:00
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements the Handler interface.
|
2020-06-10 04:00:48 +00:00
|
|
|
func (l *Ldap) Name() string { return "ldap" }
|