| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package log | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/replacer" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"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 { | 
					
						
							| 
									
										
										
										
											2019-03-25 03:36:46 +00:00
										 |  |  | 		return Logger{Next: next, Rules: rules, repl: replacer.New()} | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		length := len(rules) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		switch len(args) { | 
					
						
							|  |  |  | 		case 0: | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			// 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
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		case 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
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			// Name scopes, and maybe a format specified | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			format := DefaultLogFormat | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 			if strings.Contains(args[len(args)-1], "{") { | 
					
						
							|  |  |  | 				switch args[len(args)-1] { | 
					
						
							|  |  |  | 				case "{common}": | 
					
						
							|  |  |  | 					format = CommonLogFormat | 
					
						
							|  |  |  | 				case "{combined}": | 
					
						
							|  |  |  | 					format = CombinedLogFormat | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					format = args[len(args)-1] | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				args = args[:len(args)-1] | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 			for _, str := range args { | 
					
						
							|  |  |  | 				rules = append(rules, Rule{ | 
					
						
							|  |  |  | 					NameScope: dns.Fqdn(str), | 
					
						
							|  |  |  | 					Format:    format, | 
					
						
							|  |  |  | 					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. | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		classes := make(map[response.Class]struct{}) | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		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": | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 				classesArgs := c.RemainingArgs() | 
					
						
							|  |  |  | 				if len(classesArgs) == 0 { | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 					return nil, c.ArgErr() | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 				for _, c := range classesArgs { | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 					cls, err := response.ClassFromString(c) | 
					
						
							|  |  |  | 					if err != nil { | 
					
						
							|  |  |  | 						return nil, err | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 					classes[cls] = struct{}{} | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				return nil, c.ArgErr() | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-01-08 15:40:50 +08:00
										 |  |  | 		if len(classes) == 0 { | 
					
						
							|  |  |  | 			classes[response.All] = struct{}{} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for i := len(rules) - 1; i >= length; i -= 1 { | 
					
						
							|  |  |  | 			rules[i].Class = classes | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return rules, nil | 
					
						
							|  |  |  | } |