2016-08-19 17:14:17 -07:00
|
|
|
package log
|
2016-03-18 20:57:35 +00:00
|
|
|
|
|
|
|
|
import (
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/response"
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2016-03-19 11:16:08 +00:00
|
|
|
"github.com/miekg/dns"
|
2016-03-18 20:57:35 +00:00
|
|
|
)
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func init() {
|
|
|
|
|
caddy.RegisterPlugin("log", caddy.Plugin{
|
|
|
|
|
ServerType: "dns",
|
|
|
|
|
Action: setup,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
2016-03-18 20:57:35 +00:00
|
|
|
rules, err := logParse(c)
|
|
|
|
|
if err != nil {
|
2017-09-14 09:36:06 +01:00
|
|
|
return plugin.Error("log", err)
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2016-08-20 23:03:36 +01:00
|
|
|
return Logger{Next: next, Rules: rules, ErrorFunc: dnsserver.DefaultErrorFunc}
|
2016-08-19 17:14:17 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return nil
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func logParse(c *caddy.Controller) ([]Rule, error) {
|
|
|
|
|
var rules []Rule
|
2016-03-18 20:57:35 +00:00
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
// Nothing specified; use defaults
|
2016-08-19 17:14:17 -07:00
|
|
|
rules = append(rules, Rule{
|
2017-11-10 15:17:12 +00:00
|
|
|
NameScope: ".",
|
|
|
|
|
Format: DefaultLogFormat,
|
2018-12-10 10:17:15 +00:00
|
|
|
Class: make(map[response.Class]struct{}),
|
2016-03-18 20:57:35 +00:00
|
|
|
})
|
|
|
|
|
} else if len(args) == 1 {
|
2016-08-19 17:14:17 -07:00
|
|
|
rules = append(rules, Rule{
|
2017-11-13 10:23:27 +01:00
|
|
|
NameScope: dns.Fqdn(args[0]),
|
2017-11-10 15:17:12 +00:00
|
|
|
Format: DefaultLogFormat,
|
2018-12-10 10:17:15 +00:00
|
|
|
Class: make(map[response.Class]struct{}),
|
2016-03-18 20:57:35 +00:00
|
|
|
})
|
|
|
|
|
} else {
|
2017-11-13 10:23:27 +01:00
|
|
|
// Name scope, and maybe a format specified
|
2016-08-19 17:14:17 -07:00
|
|
|
format := DefaultLogFormat
|
2016-03-18 20:57:35 +00:00
|
|
|
|
2017-11-13 10:23:27 +01:00
|
|
|
switch args[1] {
|
|
|
|
|
case "{common}":
|
|
|
|
|
format = CommonLogFormat
|
|
|
|
|
case "{combined}":
|
|
|
|
|
format = CombinedLogFormat
|
|
|
|
|
default:
|
|
|
|
|
format = args[1]
|
2017-11-10 15:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
rules = append(rules, Rule{
|
2017-11-10 15:17:12 +00:00
|
|
|
NameScope: dns.Fqdn(args[0]),
|
|
|
|
|
Format: format,
|
2018-12-10 10:17:15 +00:00
|
|
|
Class: make(map[response.Class]struct{}),
|
2016-03-18 20:57:35 +00:00
|
|
|
})
|
|
|
|
|
}
|
2016-10-10 12:09:29 +01:00
|
|
|
|
|
|
|
|
// Class refinements in an extra block.
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
|
switch c.Val() {
|
2018-04-11 09:50:16 +03:00
|
|
|
// class followed by combinations of all, denial, error and success.
|
2016-10-10 12:09:29 +01:00
|
|
|
case "class":
|
|
|
|
|
classes := c.RemainingArgs()
|
|
|
|
|
if len(classes) == 0 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
2018-04-11 09:50:16 +03:00
|
|
|
for _, c := range classes {
|
|
|
|
|
cls, err := response.ClassFromString(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-12-10 10:17:15 +00:00
|
|
|
rules[len(rules)-1].Class[cls] = struct{}{}
|
2016-10-10 12:09:29 +01:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-11 09:50:16 +03:00
|
|
|
if len(rules[len(rules)-1].Class) == 0 {
|
2018-12-10 10:17:15 +00:00
|
|
|
rules[len(rules)-1].Class[response.All] = struct{}{}
|
2018-04-11 09:50:16 +03:00
|
|
|
}
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rules, nil
|
|
|
|
|
}
|