coredns-ldap/handler.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

package ldap
import (
"context"
"github.com/coredns/coredns/plugin"
2020-06-09 03:01:53 +00:00
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
// ServeDNS implements the plugin.Handler interface.
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{}
state := request.Request{W: w, Req: r}
2020-06-09 03:01:53 +00:00
zone := plugin.Zones(l.Zones.Names).Matches(state.Name())
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-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
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-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
return dns.RcodeSuccess, nil
}
// Name implements the Handler interface.
func (l *Ldap) Name() string { return "ldap" }